1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
44 | |
|
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 | |
|
55 | |
|
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 | |
|
65 | |
|
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 | |
|
76 | |
|
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 | |
|
83 | |
|
84 | |
|
85 | |
|
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 | |
|
112 | |
|
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 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
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 | |
|
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 | |
} |