1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.edl; |
18 | |
|
19 | |
import java.util.Date; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import javax.xml.parsers.DocumentBuilder; |
25 | |
import javax.xml.parsers.DocumentBuilderFactory; |
26 | |
import javax.xml.parsers.ParserConfigurationException; |
27 | |
|
28 | |
import org.apache.log4j.Logger; |
29 | |
import org.kuali.rice.kew.edl.components.MatchingParam; |
30 | |
import org.kuali.rice.kew.exception.WorkflowRuntimeException; |
31 | |
import org.w3c.dom.Document; |
32 | |
import org.w3c.dom.Element; |
33 | |
import org.w3c.dom.Node; |
34 | |
import org.w3c.dom.NodeList; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class EDLXmlUtils { |
44 | |
|
45 | 0 | private static final Logger LOG = Logger.getLogger(EDLXmlUtils.class); |
46 | |
|
47 | |
public static final String EDL_E = "edl"; |
48 | |
public static final String EDLCONTENT_E = "edlContent"; |
49 | |
public static final String DATA_E = "data"; |
50 | |
public static final String TYPE_E = "type"; |
51 | |
public static final String VALIDATION_E = "validation"; |
52 | |
public static final String VERSION_E = "version"; |
53 | |
public static final String DOCID_E = "docId"; |
54 | |
|
55 | 0 | private static ThreadLocal DOCUMENT_BUILDER = new ThreadLocal() { |
56 | |
protected Object initialValue() { |
57 | |
try { |
58 | 0 | return DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
59 | 0 | } catch (ParserConfigurationException pce) { |
60 | |
|
61 | |
|
62 | 0 | String message = "Error obtaining document builder"; |
63 | 0 | LOG.error(message, pce); |
64 | 0 | return new RuntimeException(message, pce); |
65 | |
} |
66 | |
} |
67 | |
}; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public static DocumentBuilder getDocumentBuilder() { |
74 | 0 | return (DocumentBuilder) DOCUMENT_BUILDER.get(); |
75 | |
} |
76 | |
|
77 | |
|
78 | |
public static Element createFieldDataElement(Element parentVersionElement, MatchingParam matchingParam) { |
79 | 0 | Element fieldData = createChildElement(parentVersionElement, "field"); |
80 | 0 | fieldData.setAttribute("name", matchingParam.getParamName()); |
81 | 0 | if (matchingParam.getError().booleanValue()) { |
82 | 0 | fieldData.setAttribute("invalid", "true"); |
83 | 0 | Element errorMessage = getOrCreateChildElement(fieldData, "errorMessage", true); |
84 | 0 | placeTextInElement(errorMessage, matchingParam.getErrorMessage()); |
85 | |
} |
86 | 0 | Element fieldValue = getOrCreateChildElement(fieldData, "value", true); |
87 | 0 | placeTextInElement(fieldValue, matchingParam.getParamValue()); |
88 | 0 | return fieldData; |
89 | |
} |
90 | |
|
91 | |
public static Element createChildElement(Element parentElement, String elementName) { |
92 | 0 | Element child = parentElement.getOwnerDocument().createElement(elementName); |
93 | 0 | parentElement.appendChild(child); |
94 | 0 | return child; |
95 | |
} |
96 | |
|
97 | |
public static Element getDocumentStateElement(Document dom) { |
98 | 0 | return EDLXmlUtils.getOrCreateChildElement(dom.getDocumentElement(), "documentState", true); |
99 | |
} |
100 | |
|
101 | |
|
102 | |
public static void addGlobalErrorMessage(Document dom, String errorMessage) { |
103 | 0 | Element documentState = getDocumentStateElement(dom); |
104 | 0 | createTextElementOnParent(documentState, "error", errorMessage); |
105 | 0 | } |
106 | |
|
107 | |
private static void placeTextInElement(Element element, String text) { |
108 | 0 | if (element.getOwnerDocument() == null) { |
109 | 0 | throw new WorkflowRuntimeException("The element must have an owner document in order to add text"); |
110 | |
} |
111 | 0 | element.appendChild(element.getOwnerDocument().createTextNode(text)); |
112 | 0 | } |
113 | |
|
114 | |
public static Element createTextElementOnParent(Element parent, String childElementName, String text) { |
115 | 0 | if (text == null) { |
116 | 0 | throw new WorkflowRuntimeException("The text placed in an Element cannot be null"); |
117 | |
} |
118 | 0 | Element childElement = parent.getOwnerDocument().createElement(childElementName); |
119 | 0 | parent.appendChild(childElement); |
120 | 0 | childElement.appendChild(parent.getOwnerDocument().createTextNode(text)); |
121 | 0 | return childElement; |
122 | |
} |
123 | |
|
124 | |
public static Element getVersionFromData(Element dataElement, Integer versionCount) { |
125 | 0 | if (dataElement == null) { |
126 | 0 | throw new WorkflowRuntimeException("Attempting to put version element inside null data Element"); |
127 | |
} |
128 | 0 | if (!dataElement.getTagName().equals(DATA_E)) { |
129 | 0 | throw new WorkflowRuntimeException("Attempting to put version element inside a parent that is not a data element " + dataElement.getTagName()); |
130 | |
} |
131 | 0 | Element version = createChildElement(dataElement, VERSION_E); |
132 | 0 | version.setAttribute("current", "true"); |
133 | 0 | version.setAttribute("date", new Date().toString()); |
134 | 0 | version.setAttribute("version", versionCount.toString()); |
135 | 0 | return version; |
136 | |
} |
137 | |
|
138 | |
public static Element getDataFromEDLDocument(Element edlContent, boolean create) { |
139 | 0 | return getOrCreateChildElement(edlContent, DATA_E, create); |
140 | |
} |
141 | |
|
142 | |
public static Element getEDLContent(Document displayDoc, boolean create) { |
143 | 0 | return getOrCreateChildElement(displayDoc.getDocumentElement(), EDLCONTENT_E, create); |
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public static Element getOrCreateChildElement(Element parent, String name, boolean create) { |
153 | 0 | if (parent == null) { |
154 | 0 | throw new WorkflowRuntimeException("Passed in null parent element attempting to create child element '" + name + "'"); |
155 | |
} |
156 | 0 | Element child = getChildElement(parent, name); |
157 | 0 | if (child == null && create) { |
158 | 0 | LOG.debug("Creating child element '" + name + "' of parent: " + parent); |
159 | 0 | child = parent.getOwnerDocument().createElement(name); |
160 | 0 | parent.appendChild(child); |
161 | |
} |
162 | 0 | return child; |
163 | |
} |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public static Element getChildElement(Node parent, String name) { |
173 | 0 | NodeList childList = parent.getChildNodes(); |
174 | 0 | for (int i = 0; i < childList.getLength(); i++) { |
175 | 0 | Node node = childList.item(i); |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | 0 | if (node.getNodeType() == Node.ELEMENT_NODE |
182 | |
&& (name.equals(node.getLocalName()) |
183 | |
|| name.equals(node.getNodeName()))) { |
184 | 0 | return (Element) node; |
185 | |
} |
186 | |
} |
187 | 0 | return null; |
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
public static String getChildElementTextValue(Node parent, String name) { |
199 | 0 | Element child = getChildElement(parent, name); |
200 | 0 | if (child == null) { |
201 | 0 | return null; |
202 | |
} |
203 | 0 | Node textNode = child.getFirstChild(); |
204 | 0 | if (textNode == null) { |
205 | 0 | return null; |
206 | |
} |
207 | 0 | return textNode.getNodeValue(); |
208 | |
} |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
public static void addErrorsAndMessagesToDocument(Document doc, List errors, List messages, Map<String, String> fieldErrors) { |
220 | 0 | Node documentState = EDLXmlUtils.getDocumentStateElement(doc); |
221 | 0 | Iterator it = errors.iterator(); |
222 | 0 | while (it.hasNext()) { |
223 | 0 | Element error = doc.createElement("error"); |
224 | 0 | error.appendChild(doc.createTextNode(it.next().toString())); |
225 | 0 | documentState.appendChild(error); |
226 | 0 | } |
227 | 0 | it = messages.iterator(); |
228 | 0 | while (it.hasNext()) { |
229 | 0 | Element error = doc.createElement("message"); |
230 | 0 | error.appendChild(doc.createTextNode(it.next().toString())); |
231 | 0 | documentState.appendChild(error); |
232 | 0 | } |
233 | 0 | for (String errorKey : fieldErrors.keySet()) { |
234 | 0 | Element error = doc.createElement("fieldError"); |
235 | 0 | error.setAttribute("key", errorKey); |
236 | 0 | error.appendChild(doc.createTextNode(fieldErrors.get(errorKey))); |
237 | 0 | documentState.appendChild(error); |
238 | 0 | } |
239 | 0 | } |
240 | |
|
241 | |
} |
242 | |
|
243 | |
|