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.core.api.exception.RiceRuntimeException; |
30 | |
import org.kuali.rice.kew.api.document.InvalidDocumentContentException; |
31 | |
import org.kuali.rice.kew.engine.RouteContext; |
32 | |
import org.kuali.rice.kew.util.KEWConstants; |
33 | |
import org.w3c.dom.Document; |
34 | |
import org.w3c.dom.Element; |
35 | |
import org.w3c.dom.Node; |
36 | |
import org.w3c.dom.NodeList; |
37 | |
import org.xml.sax.InputSource; |
38 | |
import org.xml.sax.SAXException; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public class StandardDocumentContent implements DocumentContent, Serializable { |
49 | |
|
50 | |
private static final long serialVersionUID = -3189330007364191220L; |
51 | |
|
52 | |
private static final String LEGACY_FLEXDOC_ELEMENT = "flexdoc"; |
53 | |
|
54 | |
private String docContent; |
55 | |
|
56 | |
private transient Document document; |
57 | |
|
58 | |
private transient Element applicationContent; |
59 | |
|
60 | |
private transient Element attributeContent; |
61 | |
|
62 | |
private transient Element searchableContent; |
63 | |
|
64 | |
private RouteContext routeContext; |
65 | |
|
66 | |
public StandardDocumentContent(String docContent) { |
67 | 1 | this(docContent, null); |
68 | 1 | } |
69 | |
|
70 | 1 | public StandardDocumentContent(String docContent, RouteContext routeContext) { |
71 | 1 | this.routeContext = routeContext; |
72 | 1 | initialize(docContent, routeContext); |
73 | 1 | } |
74 | |
|
75 | |
private void initialize(String docContent, RouteContext routeContext) { |
76 | 1 | if (org.apache.commons.lang.StringUtils.isEmpty(docContent)) { |
77 | 0 | this.docContent = ""; |
78 | 0 | this.document = null; |
79 | |
} else { |
80 | |
try { |
81 | 1 | this.docContent = docContent; |
82 | 1 | this.document = parseDocContent(docContent); |
83 | 1 | extractElements(this.document); |
84 | 0 | } catch (IOException e) { |
85 | 0 | throw new InvalidDocumentContentException("I/O Error when attempting to parse document content.", e); |
86 | 0 | } catch (SAXException e) { |
87 | 0 | throw new InvalidDocumentContentException("XML parse error when attempting to parse document content.", e); |
88 | 0 | } catch (ParserConfigurationException e) { |
89 | 0 | throw new RiceRuntimeException("XML parser configuration error when attempting to parse document content.", e); |
90 | 1 | } |
91 | |
} |
92 | 1 | } |
93 | |
|
94 | |
private Document parseDocContent(String docContent) throws IOException, SAXException, ParserConfigurationException { |
95 | 1 | DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
96 | 1 | return documentBuilder.parse(new InputSource(new BufferedReader(new StringReader(docContent)))); |
97 | |
} |
98 | |
|
99 | |
private void extractElements(Document document) { |
100 | |
|
101 | 1 | if (!document.getDocumentElement().getNodeName().equals(KEWConstants.DOCUMENT_CONTENT_ELEMENT)) { |
102 | |
|
103 | |
|
104 | 0 | if (document.getDocumentElement().getNodeName().equals(LEGACY_FLEXDOC_ELEMENT)) { |
105 | 0 | attributeContent = document.getDocumentElement(); |
106 | |
} else { |
107 | 0 | applicationContent = document.getDocumentElement(); |
108 | |
} |
109 | |
} else { |
110 | 1 | NodeList nodes = document.getDocumentElement().getChildNodes(); |
111 | 3 | for (int index = 0; index < nodes.getLength(); index++) { |
112 | 2 | Node node = nodes.item(index); |
113 | 2 | if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(KEWConstants.APPLICATION_CONTENT_ELEMENT)) { |
114 | 0 | int numChildElements = 0; |
115 | 0 | for (int childIndex = 0; childIndex < node.getChildNodes().getLength(); childIndex++) { |
116 | 0 | Node child = (Node) node.getChildNodes().item(childIndex); |
117 | 0 | if (child.getNodeType() == Node.ELEMENT_NODE) { |
118 | 0 | applicationContent = (Element) child; |
119 | 0 | numChildElements++; |
120 | |
} |
121 | |
} |
122 | |
|
123 | 0 | if (numChildElements > 1) { |
124 | 0 | applicationContent = (Element) node; |
125 | |
} |
126 | 0 | } else if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(KEWConstants.ATTRIBUTE_CONTENT_ELEMENT)) { |
127 | 1 | attributeContent = (Element) node; |
128 | 1 | } else if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(KEWConstants.SEARCHABLE_CONTENT_ELEMENT)) { |
129 | 0 | searchableContent = (Element) node; |
130 | |
} |
131 | |
} |
132 | |
} |
133 | 1 | } |
134 | |
|
135 | |
public Element getApplicationContent() { |
136 | 0 | return applicationContent; |
137 | |
} |
138 | |
|
139 | |
public Element getAttributeContent() { |
140 | 2 | return attributeContent; |
141 | |
} |
142 | |
|
143 | |
public String getDocContent() { |
144 | 0 | return docContent; |
145 | |
} |
146 | |
|
147 | |
public Document getDocument() { |
148 | 0 | return document; |
149 | |
} |
150 | |
|
151 | |
public Element getSearchableContent() { |
152 | 0 | return searchableContent; |
153 | |
} |
154 | |
|
155 | |
public RouteContext getRouteContext() { |
156 | 0 | return this.routeContext; |
157 | |
} |
158 | |
|
159 | |
private void readObject(ObjectInputStream ais) throws IOException, ClassNotFoundException { |
160 | 0 | ais.defaultReadObject(); |
161 | 0 | initialize(this.docContent, this.routeContext); |
162 | 0 | } |
163 | |
|
164 | |
} |