001: /*
002: * Copyright 2005-2006 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.routeheader;
018:
019: import java.io.StringWriter;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import javax.xml.parsers.DocumentBuilder;
024: import javax.xml.parsers.DocumentBuilderFactory;
025: import javax.xml.transform.OutputKeys;
026: import javax.xml.transform.Result;
027: import javax.xml.transform.Source;
028: import javax.xml.transform.Transformer;
029: import javax.xml.transform.TransformerConfigurationException;
030: import javax.xml.transform.TransformerException;
031: import javax.xml.transform.TransformerFactory;
032: import javax.xml.transform.dom.DOMSource;
033: import javax.xml.transform.stream.StreamResult;
034:
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Element;
037:
038: import edu.iu.uis.eden.EdenConstants;
039: import edu.iu.uis.eden.engine.RouteContext;
040: import edu.iu.uis.eden.exception.InvalidXmlException;
041: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
042:
043: /**
044: * Allows the construction of DocumentContent from fragments of XML.
045: *
046: * @author ewestfal
047: */
048: public class PartialAttributeContent implements DocumentContent {
049:
050: private static final long serialVersionUID = -7710201192800150123L;
051:
052: private Document document;
053: private Element attributeContent;
054: private RouteContext routeContext;
055:
056: public PartialAttributeContent(List attributeContents)
057: throws InvalidXmlException {
058: this (attributeContents, null);
059: }
060:
061: public PartialAttributeContent(List attributeContents,
062: RouteContext routeContext) throws InvalidXmlException {
063: try {
064: this .routeContext = routeContext;
065: DocumentBuilder documentBuilder = DocumentBuilderFactory
066: .newInstance().newDocumentBuilder();
067: this .document = documentBuilder.newDocument();
068: Element rootElement = document
069: .createElement(EdenConstants.DOCUMENT_CONTENT_ELEMENT);
070: this .attributeContent = document
071: .createElement(EdenConstants.ATTRIBUTE_CONTENT_ELEMENT);
072: rootElement.appendChild(attributeContent);
073: for (Iterator iterator = attributeContents.iterator(); iterator
074: .hasNext();) {
075: Element element = (Element) iterator.next();
076: element = (Element) document.importNode(element, true);
077: attributeContent.appendChild(element);
078: }
079: document.appendChild(rootElement);
080: } catch (Exception e) {
081: throw new InvalidXmlException(e);
082: }
083: }
084:
085: public Document getDocument() {
086: return document;
087: }
088:
089: public Element getApplicationContent() {
090: return null;
091: }
092:
093: public Element getAttributeContent() {
094: return attributeContent;
095: }
096:
097: public Element getSearchableContent() {
098: return null;
099: }
100:
101: public String getDocContent() {
102: try {
103: Source source = new DOMSource(document);
104: StringWriter writer = new StringWriter();
105: Result result = new StreamResult(writer);
106: Transformer transformer = TransformerFactory.newInstance()
107: .newTransformer();
108: transformer.setOutputProperty(
109: OutputKeys.OMIT_XML_DECLARATION, "yes");
110: transformer.transform(source, result);
111: return writer.toString();
112: } catch (TransformerConfigurationException e) {
113: throw new WorkflowRuntimeException(
114: "Error configuring transformer to write doc content.",
115: e);
116: } catch (TransformerException e) {
117: throw new WorkflowRuntimeException(
118: "Error transforming DOM into doc content.", e);
119: }
120: }
121:
122: public RouteContext getRouteContext() {
123: return this.routeContext;
124: }
125:
126: }
|