KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xsl > IdentityStylesheet


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xsl;
30
31 import com.caucho.xml.QElement;
32 import com.caucho.xml.XMLWriter;
33
34 import org.w3c.dom.Node JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36
37 import javax.xml.transform.TransformerException JavaDoc;
38 import java.io.IOException JavaDoc;
39
40 /**
41  * A compiled XSL stylesheet. Stylesheets use 'transform' to transform
42  * an XML tree to an XML Document.
43  *
44  * <p>The resulting document can be printed, or it can be added to another
45  * XML tree.
46  */

47 public class IdentityStylesheet extends StylesheetImpl {
48   /**
49    * Transforms the XML node to a new XML document based on this stylesheet.
50    *
51    * <p>Since Documents are DocumentFragments, calling functions can insert
52    * the contents using appendChild.
53    *
54    * @param xml source xml to convert
55    * @param out source xml to convert
56    * @return the converted document
57    */

58   public void transform(Node JavaDoc xml,
59                         XMLWriter writer,
60                         TransformerImpl transformer)
61     throws SAXException JavaDoc, IOException JavaDoc, TransformerException JavaDoc
62   {
63     XslWriter out = new XslWriter(null, this, transformer);
64     out.init(writer);
65     
66     applyNode(out, xml);
67
68     out.close();
69   }
70   
71   protected void applyNode(XslWriter out, Node JavaDoc node)
72     throws SAXException JavaDoc, IOException JavaDoc, TransformerException JavaDoc
73   {
74     if (node == null)
75       return;
76
77     switch (node.getNodeType()) {
78     case Node.DOCUMENT_NODE:
79     case Node.DOCUMENT_FRAGMENT_NODE:
80       for (Node JavaDoc child = node.getFirstChild();
81            child != null;
82            child = child.getNextSibling()) {
83         applyNode(out, child);
84       }
85       break;
86       
87     case Node.ELEMENT_NODE:
88       out.pushCopy(node);
89       for (Node JavaDoc child = ((QElement) node).getFirstAttribute();
90            child != null;
91            child = child.getNextSibling()) {
92         applyNode(out, child);
93       }
94       for (Node JavaDoc child = node.getFirstChild();
95            child != null;
96            child = child.getNextSibling()) {
97         applyNode(out, child);
98       }
99       out.popCopy(node);
100       break;
101
102     case Node.TEXT_NODE:
103     case Node.CDATA_SECTION_NODE:
104       String JavaDoc value = node.getNodeValue();
105       out.print(value);
106       return;
107       
108     case Node.ATTRIBUTE_NODE:
109     case Node.ENTITY_REFERENCE_NODE:
110       out.pushCopy(node);
111       out.popCopy(node);
112       break;
113     }
114   }
115
116   public OutputFormat getOutputFormat()
117   {
118     return new OutputFormat();
119   }
120 }
121     
122
Popular Tags