001: /*
002: ******************************************************************
003: Copyright (c) 200, Jeff Martin, Tim Bacon
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: * Redistributions of source code must retain the above copyright
011: notice, this list of conditions and the following disclaimer.
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the following
014: disclaimer in the documentation and/or other materials provided
015: with the distribution.
016: * Neither the name of the xmlunit.sourceforge.net nor the names
017: of its contributors may be used to endorse or promote products
018: derived from this software without specific prior written
019: permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
024: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
025: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
029: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
030: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
031: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: POSSIBILITY OF SUCH DAMAGE.
033:
034: ******************************************************************
035: */
036:
037: package org.custommonkey.xmlunit;
038:
039: import java.io.IOException;
040: import java.io.InputStreamReader;
041: import java.io.UnsupportedEncodingException;
042: import java.util.Properties;
043:
044: import org.w3c.dom.Node;
045:
046: /**
047: * Simple serialization class that uses a NodeInputStream (and hence
048: * a DOM-to-Stream identity transformation) to perform the work.
049: * This is not an efficient process for serialization, but it is portable
050: * across underlying transform implementations which always comes in handy...
051: * Only used so far for testing hence it's position in the test side of the
052: * source tree.
053: */
054: public class SimpleSerializer {
055: private final int bufferSize;
056: private final Properties outputProperties;
057:
058: /**
059: * Construct a new instance with default buffer size
060: */
061: public SimpleSerializer() {
062: this (1024);
063: }
064:
065: /**
066: * Construct a new instance with specific buffer size (handy for large
067: * documents)
068: */
069: public SimpleSerializer(int bufferSize) {
070: this .bufferSize = bufferSize;
071: outputProperties = new Properties();
072: }
073:
074: /**
075: * Set an output property for the serialied form
076: * @param name
077: * @param value
078: * @see javax.xml.transform.OutputKeys
079: */
080: public void setOutputProperty(String name, String value) {
081: outputProperties.setProperty(name, value);
082: }
083:
084: /**
085: * Serialize a Node to a String with default String encoding
086: * @param domNode
087: * @return full String representation of Node
088: * @throws IOException
089: * @throws UnsupportedEncodingException
090: */
091: public String serialize(Node domNode) throws IOException,
092: UnsupportedEncodingException {
093: return serialize(domNode, null);
094: }
095:
096: /**
097: * Serialize a Node to a String with specific String encoding
098: * @param domNode
099: * @return full String representation of Node
100: * @throws IOException
101: * @throws UnsupportedEncodingException
102: */
103: public String serialize(Node domNode, String encoding)
104: throws IOException, UnsupportedEncodingException {
105: NodeInputStream nodeStream = new NodeInputStream(domNode,
106: outputProperties);
107:
108: InputStreamReader reader;
109: if (encoding == null) {
110: // use default encoding
111: reader = new InputStreamReader(nodeStream);
112: } else {
113: reader = new InputStreamReader(nodeStream, encoding);
114: }
115:
116: StringBuffer buf = new StringBuffer();
117: char[] chars = new char[bufferSize];
118: int len;
119: while ((len = reader.read(chars)) != -1) {
120: buf.append(chars, 0, len);
121: }
122: reader.close();
123:
124: return buf.toString();
125: }
126: }
|