001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.routemodule;
018:
019: import java.io.BufferedInputStream;
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.xml.parsers.DocumentBuilder;
027: import javax.xml.parsers.DocumentBuilderFactory;
028: import javax.xml.parsers.ParserConfigurationException;
029:
030: import org.w3c.dom.Document;
031: import org.w3c.dom.NamedNodeMap;
032: import org.w3c.dom.Node;
033: import org.w3c.dom.NodeList;
034: import org.xml.sax.SAXException;
035:
036: import edu.iu.uis.eden.exception.ResourceUnavailableException;
037: import edu.iu.uis.eden.exception.WorkflowException;
038: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
039:
040: /**
041: * @author ewestfal
042: */
043: public class TestRouteModuleXMLHelper {
044:
045: /**
046: *
047: */
048: public static String toXML(TestDocContent docContent) {
049: StringBuffer buffer = new StringBuffer();
050: buffer.append("<docContent>");
051: for (Iterator rlIt = docContent.getRouteLevels().iterator(); rlIt
052: .hasNext();) {
053: TestRouteLevel routeLevel = (TestRouteLevel) rlIt.next();
054: buffer.append("<routeLevel priority=\"").append(
055: routeLevel.getPriority()).append("\">");
056: for (Iterator respIt = routeLevel.getResponsibilities()
057: .iterator(); respIt.hasNext();) {
058: TestResponsibility responsibility = (TestResponsibility) respIt
059: .next();
060: buffer.append("<responsibility actionRequested=\"")
061: .append(responsibility.getActionRequested())
062: .append("\" priority=\"").append(
063: responsibility.getPriority()).append(
064: "\">");
065: TestRecipient recipient = responsibility.getRecipient();
066: buffer.append("<recipient type=\"").append(
067: recipient.getType()).append("\" id=\"").append(
068: recipient.getId()).append("\"/>");
069: if (!responsibility.getDelegations().isEmpty()) {
070: buffer.append("<delegations>");
071: }
072: for (Iterator delIt = responsibility.getDelegations()
073: .iterator(); delIt.hasNext();) {
074: TestDelegation delegation = (TestDelegation) delIt
075: .next();
076: buffer.append("<delegation type=\"").append(
077: delegation.getType()).append("\">");
078: TestResponsibility delResp = delegation
079: .getResponsibility();
080: buffer.append("<delegateResponsibility>");
081: TestRecipient delRecipient = delResp.getRecipient();
082: buffer.append("<recipient type=\"").append(
083: delRecipient.getType()).append("\" id=\"")
084: .append(delRecipient.getId())
085: .append("\"/>");
086: buffer.append("</delegateResponsibility>");
087: buffer.append("</delegation>");
088: }
089: if (!responsibility.getDelegations().isEmpty()) {
090: buffer.append("</delegations>");
091: }
092: buffer.append("</responsibility>");
093: }
094: buffer.append("</routeLevel>");
095: }
096: buffer.append("</docContent>");
097: return buffer.toString();
098: }
099:
100: public static TestRouteLevel parseCurrentRouteLevel(
101: DocumentRouteHeaderValue routeHeader)
102: throws ResourceUnavailableException, WorkflowException {
103: try {
104: DocumentBuilder builder = DocumentBuilderFactory
105: .newInstance().newDocumentBuilder();
106: Document document = builder.parse(new BufferedInputStream(
107: new ByteArrayInputStream(routeHeader
108: .getDocContent().getBytes())));
109: NodeList nodes = document
110: .getElementsByTagName(TestRouteModuleConstants.ROUTE_LEVEL_ELEMENT);
111: Node routeLevelNode = null;
112: Integer priority = null;
113: for (int index = 0; index < nodes.getLength(); index++) {
114: Node node = nodes.item(index);
115: NamedNodeMap nodeMap = node.getAttributes();
116: Node priorityNode = nodeMap
117: .getNamedItem(TestRouteModuleConstants.PRIORITY_ATTRIBUTE);
118: try {
119: priority = Integer.valueOf(priorityNode
120: .getNodeValue());
121: } catch (NumberFormatException e) {
122: throw new WorkflowException(
123: "Invalid route level priority '"
124: + priorityNode.getNodeValue() + "'",
125: e);
126: }
127: if (priorityNode != null
128: && routeHeader.getDocRouteLevel().equals(
129: priority)) {
130: routeLevelNode = node;
131: break;
132: }
133: }
134: if (routeLevelNode == null) {
135: return null;
136: }
137: TestRouteLevel routeLevel = new TestRouteLevel();
138: routeLevel.setPriority(priority.intValue());
139: List responsibilityNodes = new ArrayList();
140: NodeList rlNodes = routeLevelNode.getChildNodes();
141: for (int index = 0; index < rlNodes.getLength(); index++) {
142: Node node = rlNodes.item(index);
143: if (node
144: .getNodeName()
145: .equals(
146: TestRouteModuleConstants.RESPONSIBILITY_ELEMENT)) {
147: responsibilityNodes.add(node);
148: }
149: }
150: routeLevel
151: .setResponsibilities(parseResponsibilities(responsibilityNodes));
152: return routeLevel;
153: } catch (ParserConfigurationException e) {
154: throw new ResourceUnavailableException(
155: "Could not configure DOM parser for route level "
156: + routeHeader.getDocRouteLevel());
157: } catch (IOException e) {
158: throw new ResourceUnavailableException(
159: "Could not parse XML doc content for route level "
160: + routeHeader.getDocRouteLevel());
161: } catch (SAXException e) {
162: throw new ResourceUnavailableException(
163: "Could not parse XML doc content for route level "
164: + routeHeader.getDocRouteLevel());
165: }
166: }
167:
168: private static List parseResponsibilities(List responsibilityNodes)
169: throws WorkflowException {
170: List responsibilities = new ArrayList();
171: for (Iterator iterator = responsibilityNodes.iterator(); iterator
172: .hasNext();) {
173: Node node = (Node) iterator.next();
174: responsibilities.add(parseResponsibility(node));
175: }
176: return responsibilities;
177: }
178:
179: private static TestResponsibility parseResponsibility(
180: Node responsibilityNode) throws WorkflowException {
181: TestResponsibility responsibility = new TestResponsibility();
182: NamedNodeMap attributes = responsibilityNode.getAttributes();
183: Node actionRequestedNode = attributes
184: .getNamedItem(TestRouteModuleConstants.ACTION_REQUESTED_ATTRIBUTE);
185: Node priorityNode = attributes
186: .getNamedItem(TestRouteModuleConstants.PRIORITY_ATTRIBUTE);
187: responsibility.setActionRequested(actionRequestedNode
188: .getNodeValue());
189: try {
190: responsibility.setPriority(Integer.parseInt(priorityNode
191: .getNodeValue()));
192: } catch (NumberFormatException e) {
193: throw new WorkflowException(
194: "Invalid responsibility priority '"
195: + priorityNode.getNodeValue() + "'", e);
196: }
197: NodeList childNodes = responsibilityNode.getChildNodes();
198: for (int index = 0; index < childNodes.getLength(); index++) {
199: Node node = childNodes.item(index);
200: if (node.getNodeName().equals(
201: TestRouteModuleConstants.RECIPIENT_ELEMENT)) {
202: responsibility.setRecipient(parseRecipient(node));
203: } else if (node.getNodeName().equals(
204: TestRouteModuleConstants.DELEGATIONS_ELEMENT)) {
205: responsibility.setDelegations(parseDelegations(node));
206: }
207: }
208: return responsibility;
209: }
210:
211: private static TestRecipient parseRecipient(Node recipientNode) {
212: TestRecipient recipient = new TestRecipient();
213: NamedNodeMap attributes = recipientNode.getAttributes();
214: recipient
215: .setType(attributes.getNamedItem(
216: TestRouteModuleConstants.TYPE_ATTRIBUTE)
217: .getNodeValue());
218: recipient.setId(attributes.getNamedItem(
219: TestRouteModuleConstants.ID_ATTRIBUTE).getNodeValue());
220: return recipient;
221: }
222:
223: private static List parseDelegations(Node delegationsNode) {
224: List delegations = new ArrayList();
225: NodeList childNodes = delegationsNode.getChildNodes();
226: for (int index = 0; index < childNodes.getLength(); index++) {
227: Node node = childNodes.item(index);
228: if (node.getNodeName().equals(
229: TestRouteModuleConstants.DELEGATION_ELEMENT)) {
230: delegations.add(parseDelegation(node));
231: }
232: }
233: return delegations;
234: }
235:
236: private static TestDelegation parseDelegation(Node delegationNode) {
237: TestDelegation delegation = new TestDelegation();
238: NamedNodeMap attributes = delegationNode.getAttributes();
239: delegation
240: .setType(attributes.getNamedItem(
241: TestRouteModuleConstants.TYPE_ATTRIBUTE)
242: .getNodeValue());
243: NodeList childNodes = delegationNode.getChildNodes();
244: for (int index = 0; index < childNodes.getLength(); index++) {
245: Node node = childNodes.item(index);
246: if (node
247: .getNodeName()
248: .equals(
249: TestRouteModuleConstants.DELEGATE_RESPONSIBILITY_ELEMENT)) {
250: delegation
251: .setResponsibility(parseDelegateResponsibility(node));
252: }
253: }
254: return delegation;
255: }
256:
257: private static TestResponsibility parseDelegateResponsibility(
258: Node delegateResponsibilityNode) {
259: TestResponsibility responsibility = new TestResponsibility();
260: NodeList childNodes = delegateResponsibilityNode
261: .getChildNodes();
262: for (int index = 0; index < childNodes.getLength(); index++) {
263: Node node = childNodes.item(index);
264: if (node.getNodeName().equals(
265: TestRouteModuleConstants.RECIPIENT_ELEMENT)) {
266: responsibility.setRecipient(parseRecipient(node));
267: }
268: }
269: return responsibility;
270: }
271:
272: }
|