Coverage Report - org.kuali.rice.kew.xml.StyleXmlParser
 
Classes in this File Line Coverage Branch Coverage Complexity
StyleXmlParser
0%
0/53
0%
0/22
3.625
StyleXmlParser$1
0%
0/6
N/A
3.625
 
 1  
 /*
 2  
  * Copyright 2007-2009 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.kew.xml;
 17  
 
 18  
 import java.io.InputStream;
 19  
 
 20  
 import javax.xml.parsers.DocumentBuilder;
 21  
 import javax.xml.parsers.DocumentBuilderFactory;
 22  
 import javax.xml.parsers.ParserConfigurationException;
 23  
 import javax.xml.transform.TransformerException;
 24  
 import javax.xml.xpath.XPath;
 25  
 import javax.xml.xpath.XPathConstants;
 26  
 import javax.xml.xpath.XPathExpressionException;
 27  
 import javax.xml.xpath.XPathFactory;
 28  
 
 29  
 import org.apache.log4j.Logger;
 30  
 import org.kuali.rice.kew.edl.bo.EDocLiteStyle;
 31  
 import org.kuali.rice.kew.edl.service.StyleService;
 32  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
 33  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
 34  
 import org.kuali.rice.kew.util.KEWConstants;
 35  
 import org.kuali.rice.kew.util.XmlHelper;
 36  
 import org.w3c.dom.Document;
 37  
 import org.w3c.dom.Element;
 38  
 import org.w3c.dom.Node;
 39  
 import org.w3c.dom.NodeList;
 40  
 
 41  
 
 42  
 /**
 43  
  * Parser for Style content type, managed by StyleService
 44  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 45  
  */
 46  0
 public class StyleXmlParser {
 47  0
         private static final Logger LOG = Logger.getLogger(StyleXmlParser.class);
 48  
 
 49  0
     private static ThreadLocal DOCUMENT_BUILDER = new ThreadLocal() {
 50  
         protected Object initialValue() {
 51  
             try {
 52  0
                 return DocumentBuilderFactory.newInstance().newDocumentBuilder();
 53  0
             } catch (ParserConfigurationException pce) {
 54  
                 // well folks, there is not much we can do if we get a ParserConfigurationException
 55  
                 // so might as well isolate the evilness here, and just balk if this occurs
 56  0
                 String message = "Error obtaining document builder";
 57  0
                 LOG.error(message, pce);
 58  0
                 return new RuntimeException(message, pce);
 59  
             }
 60  
         }
 61  
     };
 62  
 
 63  
     /**
 64  
      * Returns a valid DocumentBuilder
 65  
      * @return a valid DocumentBuilder
 66  
      */
 67  
     private static DocumentBuilder getDocumentBuilder() {
 68  0
         return (DocumentBuilder) DOCUMENT_BUILDER.get();
 69  
     }
 70  
 
 71  
     public static void loadXml(StyleService styleService, InputStream inputStream, String principalId) {
 72  0
         DocumentBuilder db = getDocumentBuilder();
 73  0
         XPath xpath = XPathFactory.newInstance().newXPath();
 74  
         Document doc;
 75  
         // parse and save EDocLiteDefinition, EDocLiteStyle, or EDocLiteAssociation xml from to-be-determined XML format
 76  
         //try {
 77  
         try {
 78  0
             doc = db.parse(inputStream);
 79  0
         } catch (Exception e) {
 80  0
             throw generateException("Error parsing Style XML file", e);
 81  0
         }
 82  
             /*try {
 83  
                 LOG.info(XmlHelper.writeNode(doc.getFirstChild(), true));
 84  
             } catch (TransformerException e) {
 85  
                 LOG.warn("Error displaying document");
 86  
             }*/
 87  
 
 88  
             NodeList styles;
 89  
             try {
 90  0
                 styles = (NodeList) xpath.evaluate("//" + XmlConstants.STYLE_STYLES, doc.getFirstChild(), XPathConstants.NODESET);
 91  0
             } catch (XPathExpressionException e) {
 92  0
                 throw generateException("Error evaluating XPath expression", e);
 93  0
             }
 94  
 
 95  0
             LOG.info("Styles: " + styles);
 96  0
             for (int i = 0; i < styles.getLength(); i++) {
 97  0
                 Node edl = styles.item(i);
 98  0
                 NodeList children = edl.getChildNodes();
 99  0
                 for (int j = 0; j < children.getLength(); j++) {
 100  0
                     Node node = children.item(j);
 101  0
                     if (node.getNodeType() == Node.ELEMENT_NODE) {
 102  0
                         Element e = (Element) node;
 103  0
                         if (XmlConstants.STYLE_STYLE.equals(node.getNodeName())) {
 104  0
                             LOG.debug("Digesting style: " + e.getAttribute("name"));
 105  0
                             EDocLiteStyle style = parseStyle(e);
 106  0
                             styleService.saveStyle(style);
 107  
                         }
 108  
                     }
 109  
                 }
 110  
             }
 111  
         //} catch (Exception e) {
 112  
         //    throw generateException("Error parsing EDocLite XML file", e);
 113  
         //}
 114  0
     }
 115  
 
 116  
     private static WorkflowServiceErrorException generateException(String error, Throwable cause) {
 117  0
         WorkflowServiceErrorException wsee = new WorkflowServiceErrorException(error, new WorkflowServiceErrorImpl(error, KEWConstants.XML_FILE_PARSE_ERROR));
 118  0
         if (cause != null) {
 119  0
             wsee.initCause(cause);
 120  
         }
 121  0
         return wsee;
 122  
     }
 123  
 
 124  
     /**
 125  
      * Parses an EDocLiteStyle
 126  
      *
 127  
      * @param e
 128  
      *            element to parse
 129  
      * @return an EDocLiteStyle
 130  
      */
 131  
     private static EDocLiteStyle parseStyle(Element e) {
 132  0
         String name = e.getAttribute("name");
 133  0
         if (name == null || name.length() == 0) {
 134  0
             throw generateMissingAttribException(XmlConstants.STYLE_STYLE, "name");
 135  
         }
 136  0
         EDocLiteStyle style = new EDocLiteStyle();
 137  0
         style.setName(name);
 138  0
         Element stylesheet = null;
 139  0
         NodeList children = e.getChildNodes();
 140  0
         for (int i = 0; i < children.getLength(); i++) {
 141  0
             Node child = children.item(i);
 142  
             /*
 143  
              * LOG.debug("NodeName: " + child.getNodeName()); LOG.debug("LocalName: " + child.getLocalName()); LOG.debug("Prefix: " + child.getPrefix()); LOG.debug("NS URI: " + child.getNamespaceURI());
 144  
              */
 145  0
             if (child.getNodeType() == Node.ELEMENT_NODE && "xsl:stylesheet".equals(child.getNodeName())) {
 146  0
                 stylesheet = (Element) child;
 147  0
                 break;
 148  
             }
 149  
         }
 150  0
         if (stylesheet == null) {
 151  0
             throw generateMissingChildException(XmlConstants.STYLE_STYLE, "xsl:stylesheet");
 152  
         }
 153  
         try {
 154  0
             style.setXmlContent(XmlHelper.writeNode(stylesheet, true));
 155  0
         } catch (TransformerException te) {
 156  0
             throw generateSerializationException(XmlConstants.STYLE_STYLE, te);
 157  0
         }
 158  0
         return style;
 159  
     }
 160  
 
 161  
     private static WorkflowServiceErrorException generateMissingAttribException(String element, String attrib) {
 162  0
         return generateException("Style '" + element + "' element must contain a '" + attrib + "' attribute", null);
 163  
     }
 164  
 
 165  
     private static WorkflowServiceErrorException generateMissingChildException(String element, String child) {
 166  0
         return generateException("Style '" + element + "' element must contain a '" + child + "' child element", null);
 167  
     }
 168  
 
 169  
     private static WorkflowServiceErrorException generateSerializationException(String element, TransformerException cause) {
 170  0
         return generateException("Error serializing Style '" + element + "' element", cause);
 171  
     }
 172  
 }