001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xsl;
030:
031: import com.caucho.xml.QElement;
032: import com.caucho.xml.XMLWriter;
033: import com.caucho.xml.saaj.SOAPElementImpl;
034:
035: import org.w3c.dom.Element;
036: import org.w3c.dom.NamedNodeMap;
037: import org.w3c.dom.Node;
038: import org.xml.sax.SAXException;
039:
040: import javax.xml.transform.TransformerException;
041: import java.io.IOException;
042:
043: /**
044: * A compiled XSL stylesheet. Stylesheets use 'transform' to transform
045: * an XML tree to an XML Document.
046: *
047: * <p>The resulting document can be printed, or it can be added to another
048: * XML tree.
049: */
050: public class IdentityStylesheet extends StylesheetImpl {
051: /**
052: * Transforms the XML node to a new XML document based on this stylesheet.
053: *
054: * <p>Since Documents are DocumentFragments, calling functions can insert
055: * the contents using appendChild.
056: *
057: * @param xml source xml to convert
058: * @param out source xml to convert
059: * @return the converted document
060: */
061: public void transform(Node xml, XMLWriter writer,
062: TransformerImpl transformer) throws SAXException,
063: IOException, TransformerException {
064: XslWriter out = new XslWriter(null, this , transformer);
065: out.init(writer);
066:
067: applyNode(out, xml);
068:
069: out.close();
070: }
071:
072: protected void applyNode(XslWriter out, Node node)
073: throws SAXException, IOException, TransformerException {
074: if (node == null)
075: return;
076:
077: switch (node.getNodeType()) {
078: case Node.DOCUMENT_NODE:
079: case Node.DOCUMENT_FRAGMENT_NODE:
080: for (Node child = node.getFirstChild(); child != null; child = child
081: .getNextSibling()) {
082: applyNode(out, child);
083: }
084: break;
085:
086: case Node.ELEMENT_NODE:
087: out.pushCopy(node);
088:
089: if (node instanceof QElement) {
090: for (Node child = ((QElement) node).getFirstAttribute(); child != null; child = child
091: .getNextSibling()) {
092: applyNode(out, child);
093: }
094: } else if (node instanceof SOAPElementImpl) {
095: for (Node child = ((SOAPElementImpl) node)
096: .getFirstAttribute(); child != null; child = child
097: .getNextSibling()) {
098: applyNode(out, child);
099: }
100: } else {
101: NamedNodeMap attributes = ((Element) node)
102: .getAttributes();
103:
104: for (int i = 0; i < attributes.getLength(); i++) {
105: Node child = attributes.item(i);
106: applyNode(out, child);
107: }
108: }
109:
110: for (Node child = node.getFirstChild(); child != null; child = child
111: .getNextSibling()) {
112: applyNode(out, child);
113: }
114: out.popCopy(node);
115: break;
116:
117: case Node.TEXT_NODE:
118: case Node.CDATA_SECTION_NODE:
119: String value = node.getNodeValue();
120: out.print(value);
121: return;
122:
123: case Node.ATTRIBUTE_NODE:
124: case Node.ENTITY_REFERENCE_NODE:
125: out.pushCopy(node);
126: out.popCopy(node);
127: break;
128: }
129: }
130:
131: public OutputFormat getOutputFormat() {
132: return new OutputFormat();
133: }
134: }
|