001: /* Transformer.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Mon May 13 17:31:01 2002, Created by andrewho
010: May 19 2003, Modified by tomyeh to make API more complete
011: and let developer know about Source and Result
012: }}IS_NOTE
013:
014: Copyright (C) 2002 Potix Corporation. All Rights Reserved.
015:
016: {{IS_RIGHT
017: This program is distributed under GPL Version 2.0 in the hope that
018: it will be useful, but WITHOUT ANY WARRANTY.
019: }}IS_RIGHT
020: */
021: package org.zkoss.idom.transform;
022:
023: import java.io.File;
024: import java.io.OutputStream;
025: import java.io.Writer;
026: import java.util.Properties;
027:
028: import javax.xml.transform.TransformerFactory;
029: import javax.xml.transform.Source;
030: import javax.xml.transform.Result;
031: import javax.xml.transform.OutputKeys;
032: import javax.xml.transform.dom.DOMSource;
033: import javax.xml.transform.ErrorListener;
034: import javax.xml.transform.TransformerException;
035: import javax.xml.transform.TransformerConfigurationException;
036: import javax.xml.transform.sax.SAXResult;
037:
038: import org.zkoss.idom.Document;
039: import org.zkoss.idom.Element;
040: import org.zkoss.idom.DocType;
041: import org.zkoss.idom.input.SAXHandler;
042:
043: import org.zkoss.util.logging.Log;
044:
045: /**
046: * Transforms an iDOM Document.
047: *
048: * @author <a href="mailto:andrewho@potix.com">andrewho@potix.com</a>
049: * @author tomyeh
050: */
051: public class Transformer {
052: private static final Log log = Log.lookup(Transformer.class);
053:
054: /** The transformer. */
055: private final javax.xml.transform.Transformer _tfmr;
056: /** Whether to output doc-type. */
057: private boolean _outDocType = true;
058:
059: /**
060: * Transfomer constructor without stylesheet.
061: */
062: public Transformer() throws TransformerConfigurationException {
063: final TransformerFactory tf = TransformerFactory.newInstance();
064: _tfmr = tf.newTransformer();
065: }
066:
067: /** Constructs a transformer with a stylesheet in form of Source.
068: *
069: * <p>Examples:<br>
070: * <dl>
071: * <dt>File file</dt>
072: * <dd>new Transformer(new javax.xml.transform.stream.StreamSource(file));</dd>
073: * <dt>Reader reader</dt>
074: * <dd>new Transformer(new javax.xml.transform.stream.StreamSource(reader));</dd>
075: * <dt>URL url</dt>
076: * <dd>new Transformer(new javax.xml.transform.stream.StreamSource(url.openStream()));</dd>
077: * <dt>iDOM or DOM dom</dt>
078: * <dd>new Transformer(new javax.xml.transform.dom.DOMSource(dom));</dd>
079: * </dl>
080: *
081: * <p>See javax.xml.transform.stream.StreamSource
082: * and javax.xml.transform.dom.DOMSource
083: */
084: public Transformer(Source source)
085: throws TransformerConfigurationException {
086: final TransformerFactory tf = TransformerFactory.newInstance();
087: _tfmr = source != null ? tf.newTransformer(source) : tf
088: .newTransformer();
089: }
090:
091: /** Sets whether to output the doc type.
092: * Default: true.
093: *
094: * <p>Useful only if {@link Document} is used in transform(), e.g.,
095: * {@link #transform(Document, Result)}.
096: * If not, you have to set OutputKeys.DOCTYPE_SYSTEM and
097: * OutputKeys.DOCTYPE_PUBLIC explicitly (thru {@link #getTransformer}).
098: */
099: public final void enableOutputDocType(boolean enable) {
100: _outDocType = enable;
101: }
102:
103: private final Document processDocType(final Document doc) {
104: if (!_outDocType)
105: return doc;
106:
107: final DocType dt = doc.getDocType();
108: if (dt == null)
109: return doc;
110:
111: final String sysid = dt.getSystemId();
112: if (sysid != null && sysid.length() > 0)
113: _tfmr.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, sysid);
114: final String pubid = dt.getPublicId();
115: if (pubid != null && pubid.length() > 0)
116: _tfmr.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pubid);
117: return doc;
118: }
119:
120: /** Returns the JAXP transformer encapsulated by this object.
121: * Then, you can use it to set properties, listener and so on.
122: * <p>Notice: OutputKeys.DOCTYPE_SYSTEM and OutputKeys.DOCTYPE_PUBLIC
123: * are set automatically if outDocType is true when constructing
124: * this object and {@link Document} is used to transform.
125: */
126: public final javax.xml.transform.Transformer getTransformer() {
127: return _tfmr;
128: }
129:
130: /**
131: * Transforms from a source to a result.
132: *
133: * and javax.xml.transform.dom.DOMSource
134: * @param source the source
135: * @param result the result
136: * @see #transform(Document, Result)
137: */
138: public final void transform(Source source, Result result)
139: throws TransformerException {
140: _tfmr.transform(source, result);
141: }
142:
143: /**
144: * Transforms from an iDOM document to a result.
145: *
146: * <p>Examples:<br>
147: * <dl>
148: * <dt>File file</dt>
149: * <dd>transformer.transform(doc, new javax.xml.transform.stream.StreamResult(file));</dd>
150: * <dt>Writer writer</dt>
151: * <dd>transformer.transform(doc, new javax.xml.transform.stream.StreamResult(writer));</dd>
152: * <dt>URL url</dt>
153: * <dd>No simple way yet.</dd>
154: * <dt>iDOM or DOM dom</dt>
155: * <dd>{@link #transform(Source)} and {@link #transform(Document)}.</dd>
156: * <dt>String systemId</dt>
157: * <dd>transformer.transform(doc, new javax.xml.transform.stream.StreamResult(systemId));</dd>
158: * </dl>
159: *
160: * <p>See javax.xml.transform.stream.StreamResult
161: * and javax.xml.transform.dom.DOMResult
162: *
163: * @param doc the source document
164: * @param result the result
165: * @see #transform(Source, Result)
166: * @see #transform(Document)
167: */
168: public final void transform(Document doc, Result result)
169: throws TransformerException {
170: _tfmr.transform(new DOMSource(processDocType(doc)), result);
171: }
172:
173: /**
174: * Transforms from an iDOM element to a result.
175: *
176: * @param elm the source element
177: * @param result the result
178: */
179: public final void transform(Element elm, Result result)
180: throws TransformerException {
181: _tfmr.transform(new DOMSource(elm), result);
182: }
183:
184: /**
185: * Trasforms a source and returns the transformed result as
186: * an iDOM Document.
187: *
188: * @param source the source
189: * @return the transformed result in an iDOM document
190: */
191: public final Document transform(Source source)
192: throws TransformerException {
193: final SAXHandler hdl = new SAXHandler();
194: _tfmr.transform(source, new SAXResult(hdl));
195: return hdl.getDocument();
196: }
197:
198: /**
199: * Trasforms an iDOM document and returns the transformed result as
200: * another iDOM Document.
201: *
202: * @param doc the source document
203: * @return the transformed result in an iDOM document
204: */
205: public final Document transform(Document doc)
206: throws TransformerException {
207: return transform(new DOMSource(processDocType(doc)));
208: }
209:
210: /**
211: * Trasforms an iDOM element and returns the transformed result as
212: * another iDOM Document.
213: *
214: * @param elm the source element
215: * @return the transformed result in an iDOM document
216: */
217: public final Document transform(Element elm)
218: throws TransformerException {
219: return transform(new DOMSource(elm));
220: }
221:
222: /** Get a copy of the output properties for the transformation.
223: */
224: public final Properties getOutputProperties() {
225: return _tfmr.getOutputProperties();
226: }
227:
228: /** Get an output property that is in effect for the transformation.
229: */
230: public final String getOutputProperty(String name) {
231: return _tfmr.getOutputProperty(name);
232: }
233:
234: /** Set an output property that will be in effect for the transformation.
235: */
236: public final void setOutputProperty(String name, String value) {
237: _tfmr.setOutputProperty(name, value);
238: }
239:
240: /** Set the output properties for the transformation.
241: */
242: public final void setOutputProperties(Properties props) {
243: _tfmr.setOutputProperties(props);
244: }
245:
246: /** Get the error event handler in effect for the transformation.
247: */
248: public final ErrorListener getErrorListener() {
249: return _tfmr.getErrorListener();
250: }
251:
252: /** Set the error event listener in effect for the transformation.
253: */
254: public final void setErrorListener(ErrorListener listener) {
255: _tfmr.setErrorListener(listener);
256: }
257: }
|