Coverage Report - org.kuali.rice.kew.routeheader.StandardDocumentContent
 
Classes in this File Line Coverage Branch Coverage Complexity
StandardDocumentContent
0%
0/55
0%
0/26
2.75
 
 1  
 /*
 2  
  * Copyright 2005-2008 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.kew.routeheader;
 18  
 
 19  
 import org.kuali.rice.core.util.xml.XmlException;
 20  
 import org.kuali.rice.kew.engine.RouteContext;
 21  
 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
 22  
 import org.kuali.rice.kew.util.KEWConstants;
 23  
 import org.w3c.dom.Document;
 24  
 import org.w3c.dom.Element;
 25  
 import org.w3c.dom.Node;
 26  
 import org.w3c.dom.NodeList;
 27  
 import org.xml.sax.InputSource;
 28  
 import org.xml.sax.SAXException;
 29  
 
 30  
 import javax.xml.parsers.DocumentBuilder;
 31  
 import javax.xml.parsers.DocumentBuilderFactory;
 32  
 import javax.xml.parsers.ParserConfigurationException;
 33  
 import java.io.*;
 34  
 
 35  
 
 36  
 /**
 37  
  * Standard implementation of {@link DocumentContent} which nows hows to parse a
 38  
  * String that it's constructed with into content with the application,
 39  
  * attribute, and searchable content sections.
 40  
  *
 41  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 42  
  */
 43  
 public class StandardDocumentContent implements DocumentContent, Serializable {
 44  
 
 45  
         private static final long serialVersionUID = -3189330007364191220L;
 46  
         
 47  
         private static final String LEGACY_FLEXDOC_ELEMENT = "flexdoc";
 48  
 
 49  
         private String docContent;
 50  
 
 51  
         private transient Document document;
 52  
 
 53  
         private transient Element applicationContent;
 54  
 
 55  
         private transient Element attributeContent;
 56  
 
 57  
         private transient Element searchableContent;
 58  
 
 59  
         private RouteContext routeContext;
 60  
 
 61  
         public StandardDocumentContent(String docContent) throws XmlException {
 62  0
                 this(docContent, null);
 63  0
         }
 64  
 
 65  0
         public StandardDocumentContent(String docContent, RouteContext routeContext) throws XmlException {
 66  0
                 this.routeContext = routeContext;
 67  0
                 initialize(docContent, routeContext);
 68  0
         }
 69  
 
 70  
         private void initialize(String docContent, RouteContext routeContext) throws XmlException {
 71  0
                 if (org.apache.commons.lang.StringUtils.isEmpty(docContent)) {
 72  0
                         this.docContent = "";
 73  0
                         this.document = null;
 74  
                 } else {
 75  
                         try {
 76  0
                                 this.docContent = docContent;
 77  0
                                 this.document = parseDocContent(docContent);
 78  0
                                 extractElements(this.document);
 79  0
                         } catch (IOException e) {
 80  0
                                 throw new XmlException("I/O Error when attempting to parse document content.", e);
 81  0
                         } catch (SAXException e) {
 82  0
                                 throw new XmlException("XML parse error when attempting to parse document content.", e);
 83  0
                         } catch (ParserConfigurationException e) {
 84  0
                                 throw new XmlException("XML parser configuration error when attempting to parse document content.", e);
 85  0
                         }
 86  
                 }
 87  0
         }
 88  
 
 89  
         private Document parseDocContent(String docContent) throws IOException, SAXException, ParserConfigurationException {
 90  0
                 DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 91  0
                 return documentBuilder.parse(new InputSource(new BufferedReader(new StringReader(docContent))));
 92  
         }
 93  
 
 94  
         private void extractElements(Document document) {
 95  
                 // this handles backward compatibility in document content
 96  0
                 if (!document.getDocumentElement().getNodeName().equals(KEWConstants.DOCUMENT_CONTENT_ELEMENT)) {
 97  
                         // if the root element is the flexdoc element (pre Workflow 2.0)
 98  
                         // then designate that as attribute content
 99  0
                         if (document.getDocumentElement().getNodeName().equals(LEGACY_FLEXDOC_ELEMENT)) {
 100  0
                                 attributeContent = document.getDocumentElement();
 101  
                         } else {
 102  0
                                 applicationContent = document.getDocumentElement();
 103  
                         }
 104  
                 } else {
 105  0
                         NodeList nodes = document.getDocumentElement().getChildNodes();
 106  0
                         for (int index = 0; index < nodes.getLength(); index++) {
 107  0
                                 Node node = nodes.item(index);
 108  0
                                 if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(KEWConstants.APPLICATION_CONTENT_ELEMENT)) {
 109  0
                                         int numChildElements = 0;
 110  0
                                         for (int childIndex = 0; childIndex < node.getChildNodes().getLength(); childIndex++) {
 111  0
                                                 Node child = (Node) node.getChildNodes().item(childIndex);
 112  0
                                                 if (child.getNodeType() == Node.ELEMENT_NODE) {
 113  0
                                                         applicationContent = (Element) child;
 114  0
                                                         numChildElements++;
 115  
                                                 }
 116  
                                         }
 117  
                                         // TODO can we have application content without a root node?
 118  0
                                         if (numChildElements > 1) {
 119  0
                                                 applicationContent = (Element) node;
 120  
                                         }
 121  0
                                 } else if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(KEWConstants.ATTRIBUTE_CONTENT_ELEMENT)) {
 122  0
                                         attributeContent = (Element) node;
 123  0
                                 } else if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(KEWConstants.SEARCHABLE_CONTENT_ELEMENT)) {
 124  0
                                         searchableContent = (Element) node;
 125  
                                 }
 126  
                         }
 127  
                 }
 128  0
         }
 129  
 
 130  
         public Element getApplicationContent() {
 131  0
                 return applicationContent;
 132  
         }
 133  
 
 134  
         public Element getAttributeContent() {
 135  0
                 return attributeContent;
 136  
         }
 137  
 
 138  
         public String getDocContent() {
 139  0
                 return docContent;
 140  
         }
 141  
 
 142  
         public Document getDocument() {
 143  0
                 return document;
 144  
         }
 145  
 
 146  
         public Element getSearchableContent() {
 147  0
                 return searchableContent;
 148  
         }
 149  
 
 150  
         public RouteContext getRouteContext() {
 151  0
                 return this.routeContext;
 152  
         }
 153  
 
 154  
         private void readObject(ObjectInputStream ais) throws IOException, ClassNotFoundException {
 155  0
                 ais.defaultReadObject();
 156  
                 try {
 157  0
                         initialize(this.docContent, this.routeContext);
 158  0
                 } catch (Exception e) {
 159  0
                         throw new WorkflowRuntimeException(e);
 160  0
                 }
 161  0
         }
 162  
 
 163  
 }