/******************************************************************************* * Copyright (c) 2011 IRIS/DMC. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * Contributors: * IRIS/DMC- initial API and implementation * * ACKNOWLEDGEMENT * This software was developed as part of a project supported by * Cooperative Agreement Number G10AC00533 from the United States * Geological Survey. Its contents are solely the responsibility of * the authors and the USGS is not responsible for the efficacy, * safety, or suitability of this software. ******************************************************************************/ import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.util.Map; import java.util.Stack; import org.apache.commons.lang.StringEscapeUtils; public class XMLWriter { private BufferedWriter out; private int depth; private final Stack stack = new Stack(); private final static String OPEN_BRACKET = "<"; private final static String CLOSE_BRACKET = ">"; private final static String OPEN_END_BRACKET = ""; public XMLWriter() { out = new BufferedWriter(new OutputStreamWriter(System.out)); } public void startDocument() throws IOException { out.write("\n"); } public void endDocument() throws IOException { // fElementDepth = 0; // fXML11 = false; // fInCDATA = false; if(!stack.isEmpty()){ System.out.println("Stack is not empty..."); } out.newLine(); } private boolean justClosedATag = false; public void startElement(String string) throws IOException { out.newLine(); out.write(Schema.SPACE[depth]); out.write(OPEN_BRACKET); out.write(string); out.write(CLOSE_BRACKET); stack.push(string); depth++; justClosedATag = false; string = null; } public void startElement(String string, Map attributes) throws IOException { out.newLine(); out.write(Schema.SPACE[depth]); out.write(OPEN_BRACKET); out.write(string); if (attributes != null && attributes.size() > 0) { for (String key : attributes.keySet()) { out.write(' '); out.write(key); out.write("=\""); out.write((String)attributes.get(key)); out.write("\""); } } out.write(CLOSE_BRACKET); stack.push(string); depth++; justClosedATag = false; string = null; attributes = null; } public void endElement() throws IOException { if (justClosedATag) { out.newLine(); out.write(Schema.SPACE[depth - 1]); } if(stack.isEmpty()){ System.out.println("Stack is empty........"); throw new IOException("Stack is empty.............."); } String string = stack.pop(); out.write(OPEN_END_BRACKET); out.write(string); out.write(CLOSE_BRACKET); depth--; justClosedATag = true; string = null; } public void value(String string) throws IOException { try{ StringEscapeUtils.escapeXml(out, string); string = null; }catch(Exception e){ e.printStackTrace(); } } public void value(double v) throws IOException{ out.write(v+""); } public void value(int v) throws IOException{ out.write(v+""); } public void close() throws IOException { if (out != null) { out.close(); } } public void flush() throws IOException{ this.out.flush(); } }