Coverage Report - org.kuali.rice.edl.impl.xml.EDocLiteXmlParser
 
Classes in this File Line Coverage Branch Coverage Complexity
EDocLiteXmlParser
0%
0/101
0%
0/46
4.9
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.edl.impl.xml;
 17  
 
 18  
 import org.apache.log4j.Logger;
 19  
 import org.kuali.rice.core.api.CoreApiServiceLocator;
 20  
 import org.kuali.rice.core.api.impex.xml.XmlIngestionException;
 21  
 import org.kuali.rice.core.api.style.Style;
 22  
 import org.kuali.rice.core.api.style.StyleService;
 23  
 import org.kuali.rice.core.api.util.xml.XmlException;
 24  
 import org.kuali.rice.core.api.util.xml.XmlJotter;
 25  
 import org.kuali.rice.edl.impl.EDLXmlUtils;
 26  
 import org.kuali.rice.edl.impl.bo.EDocLiteAssociation;
 27  
 import org.kuali.rice.edl.impl.bo.EDocLiteDefinition;
 28  
 import org.kuali.rice.edl.impl.service.EDocLiteService;
 29  
 import org.kuali.rice.edl.impl.service.EdlServiceLocator;
 30  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 31  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 32  
 import org.w3c.dom.Document;
 33  
 import org.w3c.dom.Element;
 34  
 import org.w3c.dom.Node;
 35  
 import org.w3c.dom.NodeList;
 36  
 
 37  
 import javax.xml.parsers.DocumentBuilder;
 38  
 import javax.xml.xpath.XPath;
 39  
 import javax.xml.xpath.XPathConstants;
 40  
 import javax.xml.xpath.XPathExpressionException;
 41  
 import javax.xml.xpath.XPathFactory;
 42  
 import java.io.InputStream;
 43  
 import java.util.ArrayList;
 44  
 import java.util.Collection;
 45  
 import java.util.Iterator;
 46  
 
 47  
 
 48  
 /**
 49  
  * An XML parser which parses EDocLite definitions.
 50  
  *
 51  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 52  
  */
 53  0
 public class EDocLiteXmlParser {
 54  
 
 55  0
         private static final Logger LOG = Logger.getLogger(EDocLiteXmlParser.class);
 56  
 
 57  
     public static void loadXml(InputStream inputStream, String principalId) {
 58  0
         DocumentBuilder db = EDLXmlUtils.getDocumentBuilder();
 59  0
         XPath xpath = XPathFactory.newInstance().newXPath();
 60  
         Document doc;
 61  
         // parse and save EDocLiteDefinition, EDocLiteStyle, or EDocLiteAssociation xml from to-be-determined XML format
 62  
         //try {
 63  
         try {
 64  0
             doc = db.parse(inputStream);
 65  0
         } catch (Exception e) {
 66  0
             throw generateException("Error parsing EDocLite XML file", e);
 67  0
         }
 68  
             /*try {
 69  
                 LOG.info(XmlHelper.writeNode(doc.getFirstChild(), true));
 70  
             } catch (TransformerException e) {
 71  
                 LOG.warn("Error displaying document");
 72  
             }*/
 73  
 
 74  
             NodeList edls;
 75  
             try {
 76  0
                 edls = (NodeList) xpath.evaluate("//edoclite", doc.getFirstChild(), XPathConstants.NODESET);
 77  0
             } catch (XPathExpressionException e) {
 78  0
                 throw generateException("Error evaluating XPath expression", e);
 79  0
             }
 80  
 
 81  0
             for (int i = 0; i < edls.getLength(); i++) {
 82  0
                 Node edl = edls.item(i);
 83  0
                 NodeList children = edl.getChildNodes();
 84  0
                 for (int j = 0; j < children.getLength(); j++) {
 85  0
                     Node node = children.item(j);
 86  
                     /*try {
 87  
                         LOG.info(XmlHelper.writeNode(node, true));
 88  
                     } catch (TransformerException te) {
 89  
                         LOG.warn("Error displaying node");
 90  
                     }*/
 91  0
                     if (node.getNodeType() == Node.ELEMENT_NODE) {
 92  0
                         Element e = (Element) node;
 93  0
                         if ("style".equals(node.getNodeName())) {
 94  0
                             LOG.debug("Digesting EDocLiteStyle: " + e.getAttribute("name"));
 95  0
                             Style style = parseStyle(e);
 96  0
                             getStyleService().saveStyle(style);
 97  0
                         } else if ("edl".equals(node.getNodeName())) {
 98  0
                             LOG.debug("Digesting EDocLiteDefinition: " + e.getAttribute("name"));
 99  0
                             EDocLiteDefinition def = parseEDocLiteDefinition(e);
 100  0
                             getEDLService().saveEDocLiteDefinition(def);
 101  0
                         } else if ("association".equals(node.getNodeName())) {
 102  0
                             LOG.debug("Digesting EDocLiteAssociation: " + e.getAttribute("name"));
 103  0
                             EDocLiteAssociation assoc = parseEDocLiteAssociation(e);
 104  0
                             getEDLService().saveEDocLiteAssociation(assoc);
 105  
                         } else {
 106  
                             // LOG.debug("Unrecognized element '" + node.getNodeName() + "' in EDocLite XML doc");
 107  
                         }
 108  
                     }
 109  
                 }
 110  
             }
 111  
         //} catch (Exception e) {
 112  
         //    throw generateException("Error parsing EDocLite XML file", e);
 113  
         //}
 114  0
     }
 115  
 
 116  
     private static XmlIngestionException generateException(String error, Throwable cause) {
 117  0
             throw new XmlIngestionException(error, cause);
 118  
     }
 119  
 
 120  
     /**
 121  
      * Parses an EDocLiteAssocation
 122  
      *
 123  
      * @param e
 124  
      *            element to parse
 125  
      * @return an EDocLiteAssocation
 126  
      */
 127  
     private static EDocLiteAssociation parseEDocLiteAssociation(Element e) {
 128  0
         String docType = EDLXmlUtils.getChildElementTextValue(e, "docType");
 129  0
         if (docType == null) {
 130  0
             throw generateMissingChildException("association", "docType");
 131  
         }
 132  0
         EDocLiteAssociation assoc = new EDocLiteAssociation();
 133  0
         assoc.setEdlName(docType);
 134  0
         assoc.setDefinition(EDLXmlUtils.getChildElementTextValue(e, "definition"));
 135  0
         assoc.setStyle(EDLXmlUtils.getChildElementTextValue(e, "style"));
 136  0
         assoc.setActiveInd(Boolean.valueOf(EDLXmlUtils.getChildElementTextValue(e, "active")));
 137  0
         return assoc;
 138  
     }
 139  
 
 140  
     /**
 141  
      * Parses an EDocLiteStyle
 142  
      *
 143  
      * @param e
 144  
      *            element to parse
 145  
      * @return an EDocLiteStyle
 146  
      */
 147  
     private static Style parseStyle(Element e) {
 148  0
         String name = e.getAttribute("name");
 149  0
         if (name == null || name.length() == 0) {
 150  0
             throw generateMissingAttribException("style", "name");
 151  
         }
 152  0
         Style.Builder styleBuilder = Style.Builder.create(name);
 153  0
         Element stylesheet = null;
 154  0
         NodeList children = e.getChildNodes();
 155  0
         for (int i = 0; i < children.getLength(); i++) {
 156  0
             Node child = children.item(i);
 157  
             /*
 158  
              * LOG.debug("NodeName: " + child.getNodeName()); LOG.debug("LocalName: " + child.getLocalName()); LOG.debug("Prefix: " + child.getPrefix()); LOG.debug("NS URI: " + child.getNamespaceURI());
 159  
              */
 160  0
             if (child.getNodeType() == Node.ELEMENT_NODE && "xsl:stylesheet".equals(child.getNodeName())) {
 161  0
                 stylesheet = (Element) child;
 162  0
                 break;
 163  
             }
 164  
         }
 165  0
         if (stylesheet == null) {
 166  0
             throw generateMissingChildException("style", "xsl:stylesheet");
 167  
         }
 168  
         try {
 169  0
             styleBuilder.setXmlContent(XmlJotter.jotNode(stylesheet, true));
 170  0
         } catch (XmlException te) {
 171  0
             throw generateSerializationException("style", te);
 172  0
         }
 173  0
         return styleBuilder.build();
 174  
     }
 175  
 
 176  
     /**
 177  
      * Parses an EDocLiteDefinition
 178  
      *
 179  
      * @param e
 180  
      *            element to parse
 181  
      * @return an EDocLiteDefinition
 182  
      */
 183  
     private static EDocLiteDefinition parseEDocLiteDefinition(Element e) {
 184  0
         EDocLiteDefinition def = new EDocLiteDefinition();
 185  0
         String name = e.getAttribute("name");
 186  0
         if (name == null || name.length() == 0) {
 187  0
             throw generateMissingAttribException(EDLXmlUtils.EDL_E, "name");
 188  
         }
 189  0
         def.setName(name);
 190  
 
 191  
         // do some validation to ensure that any attributes referenced actually exist
 192  
         // blow up if there is a problem
 193  
 
 194  0
         XPath xpath = XPathFactory.newInstance().newXPath();
 195  
         NodeList fields;
 196  
         try {
 197  0
             fields = (NodeList) xpath.evaluate("fieldDef", e, XPathConstants.NODESET);
 198  0
         } catch (XPathExpressionException xpee) {
 199  0
             throw new XmlIngestionException("Invalid EDocLiteDefinition", xpee);
 200  0
         }
 201  
 
 202  0
         if (fields != null) {
 203  0
             Collection invalidAttributes = new ArrayList(5);
 204  0
             for (int i = 0; i < fields.getLength(); i++) {
 205  0
                 Node node = (Node) fields.item(i);
 206  
                 // they should all be Element...
 207  0
                 if (node instanceof Element) {
 208  0
                     Element field = (Element) node;
 209  
                     // rely on XML validation to ensure this is present
 210  0
                     String fieldName = field.getAttribute("name");
 211  0
                     String attribute = field.getAttribute("attributeName");
 212  0
                     if (attribute != null && attribute.length() > 0) {
 213  0
                         RuleAttribute ruleAttrib = KEWServiceLocator.getRuleAttributeService().findByName(attribute);
 214  0
                         if (ruleAttrib == null) {
 215  0
                             LOG.error("Invalid attribute referenced in EDocLite definition: " + attribute);
 216  0
                             invalidAttributes.add("Attribute '" + attribute + "' referenced in field '" + fieldName + "' not found");
 217  
                         }
 218  
                     }
 219  
                 }
 220  
             }
 221  0
             if (invalidAttributes.size() > 0) {
 222  0
                 LOG.error("Invalid attributes referenced in EDocLite definition");
 223  0
                 StringBuffer message = new StringBuffer("EDocLite definition contains references to non-existent attributes;\n");
 224  0
                 Iterator it = invalidAttributes.iterator();
 225  0
                 while (it.hasNext()) {
 226  0
                     message.append(it.next());
 227  0
                     message.append("\n");
 228  
                 }
 229  0
                 throw new XmlIngestionException(message.toString());
 230  
             }
 231  
         }
 232  
 
 233  
         try {
 234  0
             def.setXmlContent(XmlJotter.jotNode(e, true));
 235  0
         } catch (XmlException te) {
 236  0
             throw generateSerializationException(EDLXmlUtils.EDL_E, te);
 237  0
         }
 238  0
         return def;
 239  
     }
 240  
 
 241  
     private static XmlIngestionException generateMissingAttribException(String element, String attrib) {
 242  0
         return generateException("EDocLite '" + element + "' element must contain a '" + attrib + "' attribute", null);
 243  
     }
 244  
 
 245  
     private static XmlIngestionException generateMissingChildException(String element, String child) {
 246  0
         return generateException("EDocLite '" + element + "' element must contain a '" + child + "' child element", null);
 247  
     }
 248  
 
 249  
     private static XmlIngestionException generateSerializationException(String element, XmlException cause) {
 250  0
         return generateException("Error serializing EDocLite '" + element + "' element", cause);
 251  
     }
 252  
 
 253  
     private static EDocLiteService getEDLService() {
 254  0
             return EdlServiceLocator.getEDocLiteService();
 255  
     }
 256  
     
 257  
     private static StyleService getStyleService() {
 258  0
             return CoreApiServiceLocator.getStyleService();
 259  
     }
 260  
 }