Coverage Report - org.kuali.rice.edl.impl.EDLXmlUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
EDLXmlUtils
0%
0/84
0%
0/34
2.75
EDLXmlUtils$1
0%
0/6
N/A
2.75
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 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  
  * Contains a bunch of dom utility methods.
 39  
  *
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
                 // well folks, there is not much we can do if we get a ParserConfigurationException
 61  
                 // so might as well isolate the evilness here, and just balk if this occurs
 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  
      * Returns a valid DocumentBuilder
 75  
      * @return a valid DocumentBuilder
 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  
      * Returns, and creates if absent, a child element
 152  
      * @param parent the parent element
 153  
      * @param name the name of the child element to create and/or return
 154  
      * @return reference to the child element
 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  
      * Returns a node child with the specified tag name of the specified parent node,
 171  
      * or null if no such child node is found.
 172  
      * @param parent the parent node
 173  
      * @param name the name of the child node
 174  
      * @return child node if found, null otherwise
 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  
             // we must test against NodeName, not just LocalName
 181  
             // LocalName seems to be null - I am guessing this is because
 182  
             // the DocumentBuilderFactory is not "namespace aware"
 183  
             // although I would have expected LocalName to default to
 184  
             // NodeName
 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  
      * Returns the text value of a child element with the given name, of the given parent element,
 196  
      * or null if the child does not exist or does not have a child text node
 197  
      * @param parent parent element
 198  
      * @param name name of child element
 199  
      * @return the text value of a child element with the given name, of the given parent element,
 200  
      * or null if the child does not exist or does not have a child text node
 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  
      * Adds the specified errors and messages to the &lt;documentState&gt; element of the
 218  
      * given EDL doc
 219  
      * @param doc the EDL doc
 220  
      * @param errors the list of error Strings
 221  
      * @param messages the list of message Strings
 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