001: /*
002: * Copyright (c) 2005 Jeff Tassin. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.store.xml.writer;
031:
032: import java.io.IOException;
033: import java.io.Writer;
034: import java.util.Collection;
035: import java.util.Iterator;
036:
037: import com.jeta.forms.store.jml.dom.JMLNode;
038: import com.jeta.forms.store.jml.dom.TextJMLNode;
039: import com.jeta.forms.store.xml.XMLUtils;
040:
041: /**
042: * A helper class that writes an JMlNode and all of its children to an XML file.
043: *
044: * @author Jeff Tassin
045: */
046: public class XMLWriter {
047: private Writer m_writer;
048:
049: /** the number of spaces to indent between nested tags */
050: private static final int DEFAULT_INDENT = 1;
051:
052: private int m_indent_pos = -1;
053:
054: /**
055: * ctor
056: */
057: public XMLWriter() {
058:
059: }
060:
061: /**
062: * Writes the JMLNode to the writer associated with this object.
063: *
064: * @param node
065: * the node to convert to XML
066: * @param writer
067: * A writer where the XML will be output to.
068: * @throws IOException
069: */
070: public void write(Writer writer, JMLNode node) throws IOException {
071: m_writer = writer;
072: m_writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
073: write(node);
074: }
075:
076: /**
077: * Writes the JMLNode to the writer associated with this object.
078: *
079: * @param node
080: * @throws IOException
081: */
082: private void write(JMLNode node) throws IOException {
083: assert (node != null);
084:
085: indent();
086:
087: if (node instanceof TextJMLNode) {
088: TextJMLNode txtnode = (TextJMLNode) node;
089: if (txtnode.getTextValue() != null)
090: m_writer.write(XMLUtils.escape(txtnode.getTextValue()));
091:
092: } else {
093: m_writer.write('\n');
094:
095: for (int index = 0; index < m_indent_pos; index++)
096: m_writer.write(' ');
097:
098: m_writer.write('<');
099: m_writer.write(node.getNodeName());
100: m_writer.write(' ');
101: writeAttributes(node);
102: if (node.getChildCount() == 0) {
103: m_writer.write('/');
104: m_writer.write(">");
105: } else {
106:
107: m_writer.write(">");
108: for (int index = 0; index < node.getChildCount(); index++) {
109: JMLNode childnode = node.getNode(index);
110: write(childnode);
111: }
112:
113: if (node.getChildCount() > 0
114: && !(node.getNode(node.getChildCount() - 1) instanceof TextJMLNode)) {
115: m_writer.write('\n');
116: for (int index = 0; index < m_indent_pos; index++)
117: m_writer.write(' ');
118: }
119:
120: m_writer.write("</");
121: m_writer.write(node.getNodeName());
122: m_writer.write('>');
123: }
124:
125: }
126: unindent();
127: }
128:
129: private void writeAttributes(JMLNode node) throws IOException {
130: Collection anames = node.getAttributeNames();
131: Iterator iter = anames.iterator();
132: while (iter.hasNext()) {
133: String name = (String) iter.next();
134: String value = node.getAttribute(name);
135: m_writer.write(name);
136: m_writer.write("=\"");
137: m_writer.write(XMLUtils.escape(value));
138: m_writer.write("\"");
139: if (iter.hasNext())
140: m_writer.write(' ');
141: }
142: }
143:
144: private void indent() {
145: m_indent_pos++;
146:
147: }
148:
149: private void unindent() {
150: m_indent_pos--;
151: }
152: }
|