001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.context;
035:
036: import org.w3c.dom.Document;
037: import org.w3c.dom.Node;
038:
039: import javax.xml.transform.OutputKeys;
040: import javax.xml.transform.Result;
041: import javax.xml.transform.Transformer;
042: import javax.xml.transform.TransformerConfigurationException;
043: import javax.xml.transform.TransformerException;
044: import javax.xml.transform.TransformerFactory;
045: import javax.xml.transform.dom.DOMSource;
046: import javax.xml.transform.stream.StreamResult;
047: import javax.xml.transform.stream.StreamSource;
048: import java.io.IOException;
049: import java.io.Writer;
050: import java.util.Properties;
051:
052: public class JAXPSerializer implements DOMSerializer {
053: private static final TransformerFactory transformerFactory = TransformerFactory
054: .newInstance();
055: private static Transformer PrettyPrintingTransformer;
056: private static Transformer NormalPrintingTransformer;
057:
058: static {
059: try {
060: PrettyPrintingTransformer = transformerFactory
061: .newTransformer(new StreamSource(
062: JAXPSerializer.class
063: .getResourceAsStream("pretty-printing.xslt")));
064: NormalPrintingTransformer = transformerFactory
065: .newTransformer(new StreamSource(
066: JAXPSerializer.class
067: .getResourceAsStream("normal-printing.xslt")));
068: } catch (TransformerConfigurationException e) {
069: throw new RuntimeException(e);
070: }
071: }
072:
073: private Result result;
074: private Properties properties = new Properties();
075: private Transformer transformer = NormalPrintingTransformer;
076:
077: public JAXPSerializer(Writer writer) {
078: this .result = new StreamResult(writer);
079: }
080:
081: public JAXPSerializer(Writer writer, String publicId,
082: String systemId) {
083: this .result = new StreamResult(writer);
084: properties.setProperty(OutputKeys.DOCTYPE_PUBLIC, publicId);
085: properties.setProperty(OutputKeys.DOCTYPE_SYSTEM, systemId);
086: properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
087: }
088:
089: public void serialize(Document document) throws IOException {
090: try {
091: transformer.setOutputProperties(properties);
092: transformer.transform(new DOMSource(document), result);
093: } catch (TransformerException e) {
094: throw new IOException(e.getMessage());
095: }
096: }
097:
098: public void serialize(Node node) throws IOException {
099: try {
100: transformer.setOutputProperties(properties);
101: transformer.transform(new DOMSource(node), result);
102: } catch (TransformerException e) {
103: throw new IOException(e.getMessage());
104: }
105: }
106:
107: public void printPretty() {
108: transformer = PrettyPrintingTransformer;
109: }
110:
111: public void outputAsHTML() {
112: properties.setProperty(OutputKeys.METHOD, "html");
113: }
114:
115: public void outputAsXML() {
116: properties.setProperty(OutputKeys.METHOD, "xml");
117: }
118:
119: public void ommitXMLDeclaration() {
120: properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
121: }
122: }
|