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