001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2004, TOPP - www.openplans.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.validation.xml;
018:
019: import java.io.IOException;
020: import java.io.Writer;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.logging.Logger;
024:
025: /**
026: * WriterUtils purpose.
027: *
028: * <p>
029: * Used to provide assitance writing xml to a Writer.
030: * </p>
031: *
032: * <p></p>
033: *
034: * @author dzwiers, Refractions Research, Inc.
035: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/main/java/org/geotools/validation/xml/WriterUtils.java $
036: * @version $Id: WriterUtils.java 27862 2007-11-12 19:51:19Z desruisseaux $
037: */
038: class WriterUtils {
039: /** Used internally to create log information to detect errors. */
040: private static final Logger LOGGER = org.geotools.util.logging.Logging
041: .getLogger("org.vfny.geoserver.global");
042:
043: /** The output writer. */
044: protected Writer writer;
045:
046: /**
047: * WriterUtils constructor.
048: *
049: * <p>
050: * Should never be called.
051: * </p>
052: */
053: protected WriterUtils() {
054: }
055:
056: /**
057: * WriterUtils constructor.
058: *
059: * <p>
060: * Stores the specified writer to use for output.
061: * </p>
062: *
063: * @param writer the writer which will be used for outputing the xml.
064: */
065: public WriterUtils(Writer writer) {
066: //LOGGER.finest("In constructor WriterHelper");
067: this .writer = writer;
068: }
069:
070: /**
071: * write purpose.
072: *
073: * <p>
074: * Writes the String specified to the stored output writer.
075: * </p>
076: *
077: * @param s The String to write.
078: *
079: * @throws IOException When an IO exception occurs.
080: */
081: public void write(String s) throws IOException {
082: writer.write(s);
083: writer.flush();
084: }
085:
086: /**
087: * writeln purpose.
088: *
089: * <p>
090: * Writes the String specified to the stored output writer.
091: * </p>
092: *
093: * @param s The String to write.
094: *
095: * @throws IOException When an IO exception occurs.
096: */
097: public void writeln(String s) throws IOException {
098: writer.write(s + "\n");
099: writer.flush();
100: }
101:
102: /**
103: * openTag purpose.
104: *
105: * <p>
106: * Writes an open xml tag with the name specified to the stored output
107: * writer.
108: * </p>
109: *
110: * @param tagName The tag name to write.
111: *
112: * @throws IOException When an IO exception occurs.
113: */
114: public void openTag(String tagName) throws IOException {
115: writeln("<" + tagName + ">");
116: }
117:
118: /**
119: * openTag purpose.
120: *
121: * <p>
122: * Writes an open xml tag with the name and attributes specified to the
123: * stored output writer.
124: * </p>
125: *
126: * @param tagName The tag name to write.
127: * @param attributes The tag attributes to write.
128: *
129: * @throws IOException When an IO exception occurs.
130: */
131: public void openTag(String tagName, Map attributes)
132: throws IOException {
133: write("<" + tagName + " ");
134:
135: Iterator i = attributes.keySet().iterator();
136:
137: while (i.hasNext()) {
138: String s = (String) i.next();
139: write(s + " = " + "\"" + (attributes.get(s)).toString()
140: + "\" ");
141: }
142:
143: writeln(">");
144: }
145:
146: /**
147: * closeTag purpose.
148: *
149: * <p>
150: * Writes an close xml tag with the name specified to the stored output
151: * writer.
152: * </p>
153: *
154: * @param tagName The tag name to write.
155: *
156: * @throws IOException When an IO exception occurs.
157: */
158: public void closeTag(String tagName) throws IOException {
159: writeln("</" + tagName + ">");
160: }
161:
162: /**
163: * textTag purpose.
164: *
165: * <p>
166: * Writes a text xml tag with the name and text specified to the stored
167: * output writer.
168: * </p>
169: *
170: * @param tagName The tag name to write.
171: * @param data The text data to write.
172: *
173: * @throws IOException When an IO exception occurs.
174: */
175: public void textTag(String tagName, String data) throws IOException {
176: writeln("<" + tagName + ">" + data + "</" + tagName + ">");
177: }
178:
179: /**
180: * valueTag purpose.
181: *
182: * <p>
183: * Writes an xml tag with the name and value specified to the stored output
184: * writer.
185: * </p>
186: *
187: * @param tagName The tag name to write.
188: * @param value The text data to write.
189: *
190: * @throws IOException When an IO exception occurs.
191: */
192: public void valueTag(String tagName, String value)
193: throws IOException {
194: writeln("<" + tagName + " value = \"" + value + "\" />");
195: }
196:
197: /**
198: * attrTag purpose.
199: *
200: * <p>
201: * Writes an xml tag with the name and attributes specified to the stored
202: * output writer.
203: * </p>
204: *
205: * @param tagName The tag name to write.
206: * @param attributes The tag attributes to write.
207: *
208: * @throws IOException When an IO exception occurs.
209: */
210: public void attrTag(String tagName, Map attributes)
211: throws IOException {
212: write("<" + tagName + " ");
213:
214: Iterator i = attributes.keySet().iterator();
215:
216: while (i.hasNext()) {
217: String s = (String) i.next();
218: write(s + " = " + "\"" + (attributes.get(s)).toString()
219: + "\" ");
220: }
221:
222: write(" />");
223: }
224:
225: /**
226: * textTag purpose.
227: *
228: * <p>
229: * Writes an xml tag with the name, text and attributes specified to the
230: * stored output writer.
231: * </p>
232: *
233: * @param tagName The tag name to write.
234: * @param attributes The tag attributes to write.
235: * @param data The tag text to write.
236: *
237: * @throws IOException When an IO exception occurs.
238: */
239: public void textTag(String tagName, Map attributes, String data)
240: throws IOException {
241: write("<" + tagName + " ");
242:
243: Iterator i = attributes.keySet().iterator();
244:
245: while (i.hasNext()) {
246: String s = (String) i.next();
247: write(s + " = " + "\"" + (attributes.get(s)).toString()
248: + "\" ");
249: }
250:
251: write(">" + data + "</" + tagName + ">");
252: }
253:
254: /**
255: * comment purpose.
256: *
257: * <p>
258: * Writes an xml comment with the text specified to the stored output
259: * writer.
260: * </p>
261: *
262: * @param comment The comment text to write.
263: *
264: * @throws IOException When an IO exception occurs.
265: */
266: public void comment(String comment) throws IOException {
267: writeln("<!--");
268: writeln(comment);
269: writeln("-->");
270: }
271: }
|