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 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 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
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 | |
|
96 | 0 | if (!document.getDocumentElement().getNodeName().equals(KEWConstants.DOCUMENT_CONTENT_ELEMENT)) { |
97 | |
|
98 | |
|
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 | |
|
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 | |
} |