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.java.LineMap;
032: import com.caucho.vfs.IOExceptionWrapper;
033: import com.caucho.vfs.WriteStream;
034: import com.caucho.xml.CauchoNode;
035: import com.caucho.xml.XmlPrinter;
036:
037: import org.w3c.dom.Node;
038: import org.xml.sax.SAXException;
039:
040: import javax.xml.transform.OutputKeys;
041: import java.io.IOException;
042: import java.io.InputStream;
043: import java.util.Properties;
044:
045: /**
046: * Transforms a document to a result string.
047: */
048: public class StringTransformerImpl extends TransformerImpl {
049: protected LineMap _lineMap;
050:
051: StringTransformerImpl(StylesheetImpl stylesheet) {
052: super (stylesheet);
053: }
054:
055: public Object getProperty(String name) {
056: if (name.equals(LINE_MAP))
057: return _lineMap;
058: else
059: return super .getProperty(name);
060: }
061:
062: public String transform(InputStream source) throws SAXException,
063: IOException {
064: return transform(parseDocument(source, null));
065: }
066:
067: public String transform(String systemId) throws SAXException,
068: IOException {
069: return transform(parseDocument(systemId));
070: }
071:
072: public String transformString(String source) throws SAXException,
073: IOException {
074: return transform(parseStringDocument(source, null));
075: }
076:
077: /**
078: * Transform a node, producing an output string.
079: *
080: * @param node the input node to transform
081: *
082: * @return the resulting string.
083: */
084: public String transform(Node node) throws SAXException, IOException {
085: _lineMap = null;
086: Properties output = _stylesheet.getOutputProperties();
087:
088: com.caucho.vfs.StringWriter sw = new com.caucho.vfs.StringWriter();
089: WriteStream ws = sw.openWrite();
090:
091: XmlPrinter out = new XmlPrinter(ws);
092:
093: out.setMethod((String) output.get(OutputKeys.METHOD));
094: out.setEncoding("UTF-8");
095: out.setMimeType((String) output.get(OutputKeys.MEDIA_TYPE));
096: String omit = (String) output
097: .get(OutputKeys.OMIT_XML_DECLARATION);
098:
099: if (omit == null || omit.equals("false") || omit.equals("no"))
100: out.setPrintDeclaration(true);
101: out.setStandalone((String) output.get(OutputKeys.STANDALONE));
102: out.setSystemId((String) output.get(OutputKeys.DOCTYPE_SYSTEM));
103: out.setPublicId((String) output.get(OutputKeys.DOCTYPE_PUBLIC));
104:
105: String indent = (String) output.get(OutputKeys.INDENT);
106: if (indent != null)
107: out.setPretty(indent.equals("true"));
108: out.setVersion((String) output.get(OutputKeys.VERSION));
109: if (node instanceof CauchoNode) {
110: String filename = ((CauchoNode) node).getFilename();
111: out.setLineMap(filename != null ? filename
112: : "anonymous.xsl");
113: } else
114: out.setLineMap("anonymous.xsl");
115:
116: String includeContentType = (String) output
117: .get("include-content-type");
118: if (includeContentType != null)
119: out.setIncludeContentType(includeContentType.equals("true")
120: || includeContentType.equals("yes"));
121:
122: try {
123: out.startDocument();
124: _stylesheet.transform(node, out, this );
125: out.endDocument();
126: _lineMap = out.getLineMap();
127: ws.close();
128:
129: return sw.getString();
130: } catch (Exception e) {
131: throw new IOExceptionWrapper(e);
132: }
133: }
134: }
|