001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: XMLSerializer.java 9457 2006-08-24 12:58:41Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_lib.xml;
025:
026: import java.io.IOException;
027: import java.io.OutputStream;
028: import java.io.OutputStreamWriter;
029: import java.io.Writer;
030:
031: import org.apache.xml.serialize.Method;
032: import org.apache.xml.serialize.OutputFormat;
033: import org.apache.xml.serialize.Serializer;
034: import org.apache.xml.serialize.SerializerFactory;
035: import org.w3c.dom.Document;
036:
037: /**
038: * Serialize a given DOM Document.
039: * Handle namespaces nicely.
040: *
041: * @author Guillaume Sauthier
042: */
043: public class XMLSerializer {
044:
045: /**
046: * Indent size (char)
047: */
048: private static final int DEF_INDENT_SIZE = 4;
049:
050: /**
051: * Maximum Line Width (char)
052: */
053: private static final int DEF_LINE_WIDTH = 80;
054:
055: /**
056: * Maximum Line Width (char)
057: */
058: private static final String DEF_LINE_SEP = "\n";
059:
060: /**
061: * Document to be formated and serialized
062: */
063: private Document doc;
064:
065: /**
066: * XML Format
067: */
068: private OutputFormat format;
069:
070: /**
071: * Serializer Factory
072: */
073: private SerializerFactory factory;
074:
075: /**
076: * Creates a new XMLSerializer object.
077: * @param doc Document to be serialized
078: */
079: public XMLSerializer(Document doc) {
080: this .doc = doc;
081: // define the format for the xml document
082: format = new OutputFormat();
083: setIndent(DEF_INDENT_SIZE);
084: setLineSeparator(DEF_LINE_SEP);
085: setLineWidth(DEF_LINE_WIDTH);
086:
087: // document serialization and writing
088: factory = SerializerFactory.getSerializerFactory(Method.XML);
089: }
090:
091: /**
092: * Set the format line separator character.
093: * @param sep line separator character
094: */
095: public void setLineSeparator(String sep) {
096: format.setLineSeparator(sep);
097: }
098:
099: /**
100: * Set the format line width value.
101: * @param width line width value
102: */
103: public void setLineWidth(int width) {
104: format.setLineWidth(width);
105: }
106:
107: /**
108: * Set the format line indent value.
109: * @param indent line indent value
110: */
111: public void setIndent(int indent) {
112: format.setIndent(indent);
113: }
114:
115: /**
116: * Serialize the encapsulated Document into the OutputStream
117: *
118: * @param os output stream
119: *
120: * @throws IOException When serialization fails
121: */
122: public void serialize(OutputStream os) throws IOException {
123: serialize(new OutputStreamWriter(os));
124: }
125:
126: /**
127: * Serialize the encapsulated Document into the Writer
128: *
129: * @param writer writer
130: *
131: * @throws IOException When serialization fails
132: */
133: public void serialize(Writer writer) throws IOException {
134: Serializer genericSerializer = factory.makeSerializer(writer,
135: format);
136: org.apache.xml.serialize.XMLSerializer domSerializer = (org.apache.xml.serialize.XMLSerializer) genericSerializer
137: .asDOMSerializer();
138:
139: domSerializer.setNamespaces(true);
140:
141: domSerializer.serialize(doc);
142: }
143: }
|