001: /*
002: * <copyright>
003: *
004: * Copyright 2002-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.util;
028:
029: import java.io.PrintWriter;
030:
031: /**
032: * A simple XML formatter.
033: * <p>
034: * For an example use, see the {@link ComponentViewServlet}.
035: */
036: public final class XMLWriter {
037:
038: private final PrintWriter out;
039:
040: private boolean pending;
041:
042: private int indentCount = 0;
043: private String indentString = " ";
044:
045: public XMLWriter(PrintWriter out) {
046: this .out = out;
047: }
048:
049: public void header() {
050: out.println("<?xml version='1.0'?>");
051: }
052:
053: public void comment(String s) {
054: if (pending) {
055: out.println(">");
056: pending = false;
057: }
058: out.println(indent() + "<!-- " + encode(s) + " -->");
059: }
060:
061: public void begin(String tag) {
062: if (pending) {
063: out.println(">");
064: }
065: out.print(indent() + "<" + tag);
066: pending = true;
067: moreIndent();
068: }
069:
070: public void attr(String name, int i) {
071: attr(name, Integer.toString(i));
072: }
073:
074: public void attr(String name, String value) {
075: if (!pending) {
076: throw new RuntimeException("Unable to write attribute("
077: + name + ", " + value + ") outside of <tag>!");
078: }
079: out.print(" " + name + "=\'" + encode(value) + "\'");
080: }
081:
082: public void end(String tag) {
083: lessIndent();
084: if (pending) {
085: out.println("/>");
086: pending = false;
087: } else {
088: out.println(indent() + "</" + tag + ">");
089: }
090: }
091:
092: public void value(String tag, long l) {
093: value(tag, Long.toString(l));
094: }
095:
096: public void value(String tag, String content) {
097: if (content == null) {
098: begin(tag);
099: end(tag);
100: return;
101: }
102: if (pending) {
103: out.println(">");
104: pending = false;
105: }
106: out.println(indent() + "<" + tag + ">" + encode(content) + "</"
107: + tag + ">");
108: }
109:
110: // encoding this string be XML-safe, by replacing all:
111: // "<" becomes "<"
112: // ">" becomes ">"
113: private String encode(String s) {
114: if (s != null) {
115: if (s.indexOf('>') >= 0) {
116: s = s.replaceAll("<", "<");
117: }
118: if (s.indexOf('<') >= 0) {
119: s = s.replaceAll(">", ">");
120: }
121: }
122: return s;
123: }
124:
125: private String indent() {
126: if (indentCount <= 0) {
127: return "";
128: }
129: if (indentCount > indentString.length()) {
130: indentString += " ";
131: }
132: return indentString.substring(0, indentCount);
133: }
134:
135: private void moreIndent() {
136: indentCount += 2;
137: }
138:
139: private void lessIndent() {
140: indentCount -= 2;
141: }
142: }
|