/
SaxWriter.java
195 lines (161 loc) · 6.54 KB
/
SaxWriter.java
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright 2006 Helsinki Institute for Information Technology
*
* This file is a part of Fuego middleware. Fuego middleware is free software; you can redistribute
* it and/or modify it under the terms of the MIT license, included as the file MIT-LICENSE in the
* Fuego middleware source distribution. If you did not receive the MIT license with the
* distribution, write to the Fuego Core project at fuego-core-users@hoslab.cs.helsinki.fi.
*/
package fuegocore.util.xas;
import java.util.Map;
import java.util.HashMap;
import java.io.OutputStream;
import java.io.IOException;
import fc.util.log.Log;
import org.xml.sax.ContentHandler;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlSerializer;
/**
* A wrapper class to write with SAX events with XmlPull. This class wraps a
* <code>XmlSerializer</code> and implements the SAX {@link ContentHandler} interface, which is
* useful for output. The intent is to allow legacy SAX applications to use an XmlPull-based
* serializer without altering their own code. New classes are recommended to use the
* {@link EventSequence} interface and its implementing classes to handle XML.
*/
public class SaxWriter implements ContentHandler {
protected XmlSerializer target;
private Map prefixes = new HashMap();
public SaxWriter(XmlSerializer target) {
this(target, null);
}
public SaxWriter(XmlSerializer target, OutputStream os) {
Log.debug("Constructor: target=" + target + ", os=" + os);
this.target = target;
if (os != null) {
try {
target.setOutput(os, "ISO-8859-1");
} catch (IOException ex) {
/*
* It is nonsensical for this method to throw exceptions, since this should only
* prepare for output, not actually output anything.
*/
}
}
}
public void setDocumentLocator(Locator locator) {
}
public void startDocument() throws SAXException {
Log.debug("startDocument()");
if (target != null) {
try {
target.startDocument("ISO-8859-1", null);
} catch (IOException ex) {
throw new SAXException(ex);
}
}
}
public void endDocument() throws SAXException {
Log.debug("endDocument()");
if (target != null) {
try {
target.endDocument();
target.flush();
} catch (IOException ex) {
throw new SAXException(ex);
}
}
}
public void startPrefixMapping(String prefix, String uri) throws SAXException {
Log.debug("startPrefixMapping(" + prefix + ", " + uri + ")");
prefixes.put(prefix, uri);
if (target != null) {
try {
target.setPrefix(prefix, uri);
} catch (IOException ex) {
throw new SAXException(ex);
}
}
}
public void endPrefixMapping(String prefix) throws SAXException {
Log.debug("endPrefixMapping(" + prefix + ")");
prefixes.remove(prefix);
}
public void startElement(String namespaceURI, String localName, String qName,
Attributes attributes) throws SAXException {
Log.debug("startElement(" + namespaceURI + ", " + localName + ", " + qName + ")");
if (target != null) {
try {
if (localName == null || localName.equals("")) {
int index = qName.indexOf(':');
if (index >= 0) {
String uri = (String) prefixes.get(qName.substring(0, index));
if (uri != null) {
namespaceURI = uri;
}
}
localName = qName.substring(index + 1);
}
target.startTag(namespaceURI, localName);
if (attributes != null) {
int length = attributes.getLength();
for (int i = 0; i < length; i++) {
String localAttName = attributes.getLocalName(i);
String attUri = attributes.getURI(i);
String attQName = attributes.getQName(i);
if (localAttName == null || localAttName.length() == 0) {
int index = attQName.indexOf(':');
if (index >= 0) {
String uri = (String) prefixes.get(attQName.substring(0, index));
if (uri != null) {
attUri = uri;
}
}
localAttName = attQName.substring(index + 1);
}
target.attribute(attUri, localAttName, attributes.getValue(i));
}
}
} catch (IOException ex) {
throw new SAXException(ex);
}
}
}
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
Log.debug("endElement(" + namespaceURI + ", " + localName + ", " + qName + ")");
if (target != null) {
try {
if (localName == null || localName.equals("")) {
int index = qName.indexOf(':');
if (index >= 0) {
String uri = (String) prefixes.get(qName.substring(0, index));
if (uri != null) {
namespaceURI = uri;
}
}
localName = qName.substring(index + 1);
}
target.endTag(namespaceURI, localName);
} catch (IOException ex) {
throw new SAXException(ex);
}
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
Log.debug("characters(" + new String(ch, start, length) + ")");
if (target != null) {
try {
target.text(ch, start, length);
} catch (IOException ex) {
throw new SAXException(ex);
}
}
}
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
}
public void processingInstruction(String target, String data) throws SAXException {
}
public void skippedEntity(String name) throws SAXException {
}
}