1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
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 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
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 | |
|
102 | 0 | if (!document.getDocumentElement().getNodeName().equals(KEWConstants.DOCUMENT_CONTENT_ELEMENT)) { |
103 | |
|
104 | |
|
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 | |
|
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 | |
} |