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