| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.edl.impl; |
| 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.edl.impl.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 final 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 | 0 | private EDLXmlUtils() { |
| 70 | 0 | throw new UnsupportedOperationException("do not call"); |
| 71 | |
} |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
public static DocumentBuilder getDocumentBuilder() { |
| 78 | 0 | return (DocumentBuilder) DOCUMENT_BUILDER.get(); |
| 79 | |
} |
| 80 | |
|
| 81 | |
|
| 82 | |
public static Element createFieldDataElement(Element parentVersionElement, MatchingParam matchingParam) { |
| 83 | 0 | Element fieldData = createChildElement(parentVersionElement, "field"); |
| 84 | 0 | fieldData.setAttribute("name", matchingParam.getParamName()); |
| 85 | 0 | if (matchingParam.getError().booleanValue()) { |
| 86 | 0 | fieldData.setAttribute("invalid", "true"); |
| 87 | 0 | Element errorMessage = getOrCreateChildElement(fieldData, "errorMessage", true); |
| 88 | 0 | placeTextInElement(errorMessage, matchingParam.getErrorMessage()); |
| 89 | |
} |
| 90 | 0 | Element fieldValue = getOrCreateChildElement(fieldData, "value", true); |
| 91 | 0 | placeTextInElement(fieldValue, matchingParam.getParamValue()); |
| 92 | 0 | return fieldData; |
| 93 | |
} |
| 94 | |
|
| 95 | |
public static Element createChildElement(Element parentElement, String elementName) { |
| 96 | 0 | Element child = parentElement.getOwnerDocument().createElement(elementName); |
| 97 | 0 | parentElement.appendChild(child); |
| 98 | 0 | return child; |
| 99 | |
} |
| 100 | |
|
| 101 | |
public static Element getDocumentStateElement(Document dom) { |
| 102 | 0 | return EDLXmlUtils.getOrCreateChildElement(dom.getDocumentElement(), "documentState", true); |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
public static void addGlobalErrorMessage(Document dom, String errorMessage) { |
| 107 | 0 | Element documentState = getDocumentStateElement(dom); |
| 108 | 0 | createTextElementOnParent(documentState, "error", errorMessage); |
| 109 | 0 | } |
| 110 | |
|
| 111 | |
private static void placeTextInElement(Element element, String text) { |
| 112 | 0 | if (element.getOwnerDocument() == null) { |
| 113 | 0 | throw new WorkflowRuntimeException("The element must have an owner document in order to add text"); |
| 114 | |
} |
| 115 | 0 | element.appendChild(element.getOwnerDocument().createTextNode(text)); |
| 116 | 0 | } |
| 117 | |
|
| 118 | |
public static Element createTextElementOnParent(Element parent, String childElementName, String text) { |
| 119 | 0 | if (text == null) { |
| 120 | 0 | throw new WorkflowRuntimeException("The text placed in an Element cannot be null"); |
| 121 | |
} |
| 122 | 0 | Element childElement = parent.getOwnerDocument().createElement(childElementName); |
| 123 | 0 | parent.appendChild(childElement); |
| 124 | 0 | childElement.appendChild(parent.getOwnerDocument().createTextNode(text)); |
| 125 | 0 | return childElement; |
| 126 | |
} |
| 127 | |
|
| 128 | |
public static Element getVersionFromData(Element dataElement, Integer versionCount) { |
| 129 | 0 | if (dataElement == null) { |
| 130 | 0 | throw new WorkflowRuntimeException("Attempting to put version element inside null data Element"); |
| 131 | |
} |
| 132 | 0 | if (!dataElement.getTagName().equals(DATA_E)) { |
| 133 | 0 | throw new WorkflowRuntimeException("Attempting to put version element inside a parent that is not a data element " + dataElement.getTagName()); |
| 134 | |
} |
| 135 | 0 | Element version = createChildElement(dataElement, VERSION_E); |
| 136 | 0 | version.setAttribute("current", "true"); |
| 137 | 0 | version.setAttribute("date", new Date().toString()); |
| 138 | 0 | version.setAttribute("version", versionCount.toString()); |
| 139 | 0 | return version; |
| 140 | |
} |
| 141 | |
|
| 142 | |
public static Element getDataFromEDLDocument(Element edlContent, boolean create) { |
| 143 | 0 | return getOrCreateChildElement(edlContent, DATA_E, create); |
| 144 | |
} |
| 145 | |
|
| 146 | |
public static Element getEDLContent(Document displayDoc, boolean create) { |
| 147 | 0 | return getOrCreateChildElement(displayDoc.getDocumentElement(), EDLCONTENT_E, create); |
| 148 | |
} |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
public static Element getOrCreateChildElement(Element parent, String name, boolean create) { |
| 157 | 0 | if (parent == null) { |
| 158 | 0 | throw new WorkflowRuntimeException("Passed in null parent element attempting to create child element '" + name + "'"); |
| 159 | |
} |
| 160 | 0 | Element child = getChildElement(parent, name); |
| 161 | 0 | if (child == null && create) { |
| 162 | 0 | LOG.debug("Creating child element '" + name + "' of parent: " + parent); |
| 163 | 0 | child = parent.getOwnerDocument().createElement(name); |
| 164 | 0 | parent.appendChild(child); |
| 165 | |
} |
| 166 | 0 | return child; |
| 167 | |
} |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
public static Element getChildElement(Node parent, String name) { |
| 177 | 0 | NodeList childList = parent.getChildNodes(); |
| 178 | 0 | for (int i = 0; i < childList.getLength(); i++) { |
| 179 | 0 | Node node = childList.item(i); |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | 0 | if (node.getNodeType() == Node.ELEMENT_NODE |
| 186 | |
&& (name.equals(node.getLocalName()) |
| 187 | |
|| name.equals(node.getNodeName()))) { |
| 188 | 0 | return (Element) node; |
| 189 | |
} |
| 190 | |
} |
| 191 | 0 | return null; |
| 192 | |
} |
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
public static String getChildElementTextValue(Node parent, String name) { |
| 203 | 0 | Element child = getChildElement(parent, name); |
| 204 | 0 | if (child == null) { |
| 205 | 0 | return null; |
| 206 | |
} |
| 207 | 0 | Node textNode = child.getFirstChild(); |
| 208 | 0 | if (textNode == null) { |
| 209 | 0 | return null; |
| 210 | |
} |
| 211 | 0 | return textNode.getNodeValue(); |
| 212 | |
} |
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
public static void addErrorsAndMessagesToDocument(Document doc, List errors, List messages, Map<String, String> fieldErrors) { |
| 224 | 0 | Node documentState = EDLXmlUtils.getDocumentStateElement(doc); |
| 225 | 0 | Iterator it = errors.iterator(); |
| 226 | 0 | while (it.hasNext()) { |
| 227 | 0 | Element error = doc.createElement("error"); |
| 228 | 0 | error.appendChild(doc.createTextNode(it.next().toString())); |
| 229 | 0 | documentState.appendChild(error); |
| 230 | 0 | } |
| 231 | 0 | it = messages.iterator(); |
| 232 | 0 | while (it.hasNext()) { |
| 233 | 0 | Element error = doc.createElement("message"); |
| 234 | 0 | error.appendChild(doc.createTextNode(it.next().toString())); |
| 235 | 0 | documentState.appendChild(error); |
| 236 | 0 | } |
| 237 | 0 | for (String errorKey : fieldErrors.keySet()) { |
| 238 | 0 | Element error = doc.createElement("fieldError"); |
| 239 | 0 | error.setAttribute("key", errorKey); |
| 240 | 0 | error.appendChild(doc.createTextNode(fieldErrors.get(errorKey))); |
| 241 | 0 | documentState.appendChild(error); |
| 242 | 0 | } |
| 243 | 0 | } |
| 244 | |
|
| 245 | |
} |
| 246 | |
|
| 247 | |
|