summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/Unparser.java
blob: b6ea4fc138a065de92d096c5009a8ef88db228bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package org.madore.damlengine;

import java.io.Writer;
import java.io.IOException;
import org.w3c.dom.*;

public final class Unparser {

    private Document doc;
    private Node cursor;
    private enum Dir { PUSHING, POPPING };
    private Dir dir;
    private Writer out;

    public Unparser(Document doc, Writer out) {
	this.doc = doc;
	this.out = out;
    }

    protected void enter() {
	Node chld = cursor.getFirstChild();
	if ( chld == null ) {
	    dir = Dir.POPPING;
	} else {
	    cursor = chld;
	    dir = Dir.PUSHING;
	}
    }

    protected void skip() {
	Node nsib = cursor.getNextSibling();
	if ( nsib == null ) {
	    cursor = cursor.getParentNode();
	    dir = Dir.POPPING;
	} else {
	    cursor = nsib;
	    dir = Dir.PUSHING;
	}
    }

    public static String quote(String s) {
	StringBuilder buf = new StringBuilder(s.length()+64);
	int ptr0 = 0;  int ptr;
	for ( ptr=0 ; ptr<s.length() ; ptr++ ) {
	    char ch = s.charAt(ptr);
	    String qch = null;
	    switch ( ch ) {
	    case '&': qch = "&amp;"; break;
	    case '<': qch = "&lt;"; break;
	    case '>': qch = "&gt;"; break;
	    case '"': qch = "&quot;"; break;
	    }
	    if ( qch != null ) {
		buf.append(s, ptr0, ptr);
		buf.append(qch);
	    }
	}
	buf.append(s, ptr0, ptr);
	return buf.toString();
    }

    protected void unparseOne()
	throws IOException {
	// System.err.println("unparseOne(): cursor.nodeName="+cursor.getNodeName());
	switch ( dir ) {
	case PUSHING:
	    if ( cursor.getNodeType() == Node.ELEMENT_NODE ) {
		Element elt = (Element)cursor;
		String qname = elt.getTagName();
		out.write("<"+qname);
		if ( elt.hasAttributes() ) {
		    NamedNodeMap attrs = elt.getAttributes();
		    Node n2;
		    for ( int i=0 ; (n2=attrs.item(i)) != null ; i++ ) {
			Attr attr = (Attr)n2;
			out.write(" "+attr.getName()+"=\""+quote(attr.getValue())+"\"");

		    }
		}
		if ( ! elt.hasChildNodes() && ( qname.equals("br") || qname.equals("hr")
						|| qname.equals("img")
						|| qname.equals("area")
						|| qname.equals("base")
						|| qname.equals("basefont")
						|| qname.equals("col")
						|| qname.equals("frame")
						|| qname.equals("input")
						|| qname.equals("isindex")
						|| qname.equals("link")
						|| qname.equals("meta")
						|| qname.equals("param") ) ) {
		    out.write(" />");
		    skip();
		} else {
		    out.write(">");
		    enter();
		}
	    } else if ( cursor.getNodeType() == Node.DOCUMENT_NODE ) {
		enter();
	    } else if ( cursor.getNodeType() == Node.TEXT_NODE ) {
		out.write(quote(((Text)cursor).getData()));
		skip();
	    } else if ( cursor.getNodeType() == Node.COMMENT_NODE ) {
		// FIXME: comments could conceivably contain the "--" string
		out.write("<!--"+((Comment)cursor).getData()+"-->");
		skip();
	    } else if ( cursor.getNodeType() == Node.CDATA_SECTION_NODE ) {
		// FIXME: cdata section could conceivably contain the "]]>" string
		out.write("<![CDATA["+((CDATASection)cursor).getData()+"]]>");
		skip();
	    } else {
		skip();
	    }
	    break;
	case POPPING:
	    if ( cursor.getNodeType() == Node.ELEMENT_NODE ) {
		Element elt = (Element)cursor;
		out.write("</"+elt.getTagName()+">");
	    }
	    skip();
	    break;
	}
    }

    public void unparse()
	throws IOException {
	out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
	DocumentType doctype = doc.getDoctype();
	if ( doctype != null ) {
	    String externalId = "";
	    if ( doctype.getPublicId() != null ) {
		externalId = " PUBLIC \""+doctype.getPublicId()+"\" \""
		    +doctype.getSystemId()+"\"";
	    } else if ( doctype.getSystemId() != null ) {
		externalId = " SYSTEM \""+doctype.getSystemId()+"\"";
	    }
	    out.write("<!DOCTYPE "+doctype.getName()+externalId+">\n");
	}
	cursor = doc.getDocumentElement();
	dir = Dir.PUSHING;
	while ( cursor != null ) {
	    unparseOne();
	}
	out.write("\n");
	out.flush();
    }

}