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