001: package org.ontoware.aifbcommons.xml;
002:
003: import java.io.IOException;
004: import java.io.Writer;
005: import java.util.Iterator;
006: import java.util.Stack;
007:
008: public class XmlOutWriter implements XmlOut {
009:
010: private Writer w;
011:
012: private boolean firstLine = true;
013:
014: private boolean inElement = false;
015:
016: private boolean inProcessingInstruction = false;
017:
018: private Stack<String> openThings = new Stack<String>();
019:
020: public XmlOutWriter(Writer w) throws IOException {
021: this .w = w;
022: w.write(XmlOut.XML_DECLARATION);
023: }
024:
025: public String getOpentags() {
026: String s = "";
027: Iterator<String> it = openThings.iterator();
028: while (it.hasNext()) {
029: s += "/" + it.next();
030: }
031: return s;
032: }
033:
034: public void open(String elementName) throws IOException {
035: openThings.push(elementName);
036: if (inElement) {
037: w.write(">");
038: inElement = false;
039: }
040: if (firstLine) {
041: firstLine = false;
042: } else {
043: w.write("\n");
044: }
045: w.write("<" + elementName);
046: inElement = true;
047: }
048:
049: public void attribute(String name, String value) throws IOException {
050:
051: if (!(inElement | inProcessingInstruction))
052: throw new IllegalStateException(
053: "Atributes are only allowed in elements. We are here "
054: + getOpentags() + " so cannot add " + name
055: + "=" + value);
056:
057: w.write(" ");
058: w.write(name);
059: w.write("=\"");
060: w.write(XMLUtils.xmlencode(value));
061: w.write("\"");
062: }
063:
064: public void content(String rawContent) throws IOException {
065: if (inElement) {
066: w.write(">");
067: inElement = false;
068: }
069: w.write(XMLUtils.xmlencode(rawContent));
070: }
071:
072: public void close(String elementName) throws IOException {
073: String open = openThings.pop();
074: assert open.equals(elementName) : "trying to close '"
075: + elementName + "' but last opened was '" + open + "'";
076:
077: if (inElement) {
078: w.write(">");
079: inElement = false;
080: }
081: w.write("</" + elementName + ">");
082: }
083:
084: public void comment(String comment) throws IOException {
085: if (inElement) {
086: w.write(">");
087: inElement = false;
088: }
089: w.write("\n<!-- " + comment + " -->");
090: }
091:
092: public void write(String s) throws IOException {
093: w.write(s);
094: }
095:
096: public void openProcessingInstruction(String processingInstruction)
097: throws IOException {
098: inProcessingInstruction = true;
099: if (firstLine) {
100: firstLine = false;
101: } else {
102: w.write("\n");
103: }
104: w.write("<?" + processingInstruction);
105: }
106:
107: public void closeProcessingInstruction() throws IOException {
108: inProcessingInstruction = false;
109: w.write("?>");
110: }
111:
112: public void doctype(String doctype, String publicID, String url)
113: throws IOException {
114: if (firstLine) {
115: firstLine = false;
116: } else {
117: w.write("\n");
118: }
119: w.write("<!DOCTYPE " + doctype + " PUBLIC " + publicID + " "
120: + url + ">");
121: }
122:
123: public void flush() throws IOException {
124: w.flush();
125: }
126:
127: public void close() throws IOException {
128: w.flush();
129: }
130: }
|