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