KickJava   Java API By Example, From Geeks To Geeks.

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


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.java.LineMap;
32 import com.caucho.vfs.IOExceptionWrapper;
33 import com.caucho.vfs.WriteStream;
34 import com.caucho.xml.CauchoNode;
35 import com.caucho.xml.XmlPrinter;
36
37 import org.w3c.dom.Node JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39
40 import javax.xml.transform.OutputKeys JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.util.Properties JavaDoc;
44
45 /**
46  * Transforms a document to a result string.
47  */

48 public class StringTransformerImpl extends TransformerImpl {
49   protected LineMap _lineMap;
50   
51   StringTransformerImpl(StylesheetImpl stylesheet)
52   {
53     super(stylesheet);
54   }
55
56   public Object JavaDoc getProperty(String JavaDoc name)
57   {
58     if (name.equals(LINE_MAP))
59       return _lineMap;
60     else
61       return super.getProperty(name);
62   }
63   
64   public String JavaDoc transform(InputStream JavaDoc source)
65     throws SAXException JavaDoc, IOException JavaDoc
66   {
67     return transform(parseDocument(source, null));
68   }
69   
70   public String JavaDoc transform(String JavaDoc systemId)
71     throws SAXException JavaDoc, IOException JavaDoc
72   {
73     return transform(parseDocument(systemId));
74   }
75   
76   public String JavaDoc transformString(String JavaDoc source)
77     throws SAXException JavaDoc, IOException JavaDoc
78   {
79     return transform(parseStringDocument(source, null));
80   }
81
82   /**
83    * Transform a node, producing an output string.
84    *
85    * @param node the input node to transform
86    *
87    * @return the resulting string.
88    */

89   public String JavaDoc transform(Node node)
90     throws SAXException JavaDoc, IOException JavaDoc
91   {
92     _lineMap = null;
93     Properties JavaDoc output = _stylesheet.getOutputProperties();
94
95     com.caucho.vfs.StringWriter sw = new com.caucho.vfs.StringWriter();
96     WriteStream ws = sw.openWrite();
97     
98     XmlPrinter out = new XmlPrinter(ws);
99
100     out.setMethod((String JavaDoc) output.get(OutputKeys.METHOD));
101     out.setEncoding("UTF-8");
102     out.setMimeType((String JavaDoc) output.get(OutputKeys.MEDIA_TYPE));
103     String JavaDoc omit = (String JavaDoc) output.get(OutputKeys.OMIT_XML_DECLARATION);
104
105     if (omit == null || omit.equals("false") || omit.equals("no"))
106       out.setPrintDeclaration(true);
107     out.setStandalone((String JavaDoc) output.get(OutputKeys.STANDALONE));
108     out.setSystemId((String JavaDoc) output.get(OutputKeys.DOCTYPE_SYSTEM));
109     out.setPublicId((String JavaDoc) output.get(OutputKeys.DOCTYPE_PUBLIC));
110     
111     String JavaDoc indent = (String JavaDoc) output.get(OutputKeys.INDENT);
112     if (indent != null)
113       out.setPretty(indent.equals("true"));
114     out.setVersion((String JavaDoc) output.get(OutputKeys.VERSION));
115     if (node instanceof CauchoNode) {
116       String JavaDoc filename = ((CauchoNode) node).getFilename();
117       out.setLineMap(filename != null ? filename : "anonymous.xsl");
118     }
119     else
120       out.setLineMap("anonymous.xsl");
121     
122       String JavaDoc includeContentType = (String JavaDoc) output.get("include-content-type");
123       if (includeContentType != null)
124         out.setIncludeContentType(includeContentType.equals("true") ||
125                                   includeContentType.equals("yes"));
126
127     try {
128       out.startDocument();
129       _stylesheet.transform(node, out, this);
130       out.endDocument();
131       _lineMap = out.getLineMap();
132       ws.close();
133
134       return sw.getString();
135     } catch (Exception JavaDoc e) {
136       throw new IOExceptionWrapper(e);
137     }
138   }
139 }
140
Popular Tags