1
|
|
/*
|
2
|
|
* JBind
|
3
|
|
*
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
5
|
|
*
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
8
|
|
* 'Apache Software License V 1.1'.
|
9
|
|
*/
|
10
|
|
package org.jbind.xml.core.bridge;
|
11
|
|
|
12
|
|
import java.util.ArrayList;
|
13
|
|
import java.util.Collection;
|
14
|
|
import java.util.Collections;
|
15
|
|
import java.util.HashMap;
|
16
|
|
import java.util.Iterator;
|
17
|
|
import java.util.List;
|
18
|
|
import java.util.Map;
|
19
|
|
|
20
|
|
import org.jbind.util.collection.UnsynchronizedStack;
|
21
|
|
import org.jbind.xml.base.QName;
|
22
|
|
import org.jbind.xml.core.data.IImpl;
|
23
|
|
import org.jbind.xml.msg.XmlException;
|
24
|
|
import org.jbind.xml.msg.XmlMessages;
|
25
|
|
import org.xml.sax.Attributes;
|
26
|
|
import org.xml.sax.ContentHandler;
|
27
|
|
import org.xml.sax.SAXException;
|
28
|
|
|
29
|
|
public class SaxVisitor implements IDataImplVisitor {
|
30
|
|
|
31
|
|
private ContentHandler myContentHandler = null;
|
32
|
|
|
33
|
|
private UnsynchronizedStack myMappings = null;
|
34
|
|
private UnsynchronizedStack myNewNamespaces = null;
|
35
|
|
private Map myTopLevelAttributes = null;
|
36
|
|
|
37
|
0
|
public SaxVisitor() {}
|
38
|
|
|
39
|
0
|
private String getQName(String aNamespace, String aLocalName) {
|
40
|
0
|
String res = null;
|
41
|
0
|
if ((null != aNamespace) && !"".equals(aNamespace)) {
|
42
|
0
|
Map currentMapping = (Map)myMappings.peek();
|
43
|
0
|
String prefix = (String)currentMapping.get(aNamespace);
|
44
|
0
|
if (null == prefix) {
|
45
|
0
|
int i = 0;
|
46
|
0
|
do {
|
47
|
0
|
String p = "p" + i++;
|
48
|
0
|
if (!currentMapping.values().contains(p)) {
|
49
|
0
|
prefix = p;
|
50
|
0
|
Map m = new HashMap(currentMapping);
|
51
|
0
|
m.put(aNamespace, p);
|
52
|
0
|
myMappings.pop();
|
53
|
0
|
myMappings.push(m);
|
54
|
0
|
((List)myNewNamespaces.peek()).add(aNamespace);
|
55
|
|
}
|
56
|
0
|
} while (prefix == null);
|
57
|
|
}
|
58
|
0
|
res = prefix + ":" + aLocalName;
|
59
|
|
} else {
|
60
|
0
|
res = aLocalName;
|
61
|
|
}
|
62
|
0
|
return res;
|
63
|
|
}
|
64
|
|
|
65
|
0
|
public void visitElementImplStart(IElementImpl anImpl) throws XmlException {
|
66
|
0
|
try {
|
67
|
0
|
myMappings.push(myMappings.peek());
|
68
|
0
|
ArrayList newNamespaces = new ArrayList();
|
69
|
0
|
myNewNamespaces.push(newNamespaces);
|
70
|
|
|
71
|
0
|
String namespace = anImpl.getNamespace();
|
72
|
0
|
String localName = anImpl.getPartName();
|
73
|
0
|
String qName = getQName(namespace, localName);
|
74
|
|
|
75
|
0
|
SaxAttrs attrs = new SaxAttrs();
|
76
|
0
|
for (Iterator i = anImpl.iterAttributeImpls(); i.hasNext(); ) {
|
77
|
0
|
IAttributeImpl ai = (IAttributeImpl)i.next();
|
78
|
0
|
String attrNamespace = ai.getNamespace();
|
79
|
0
|
String attrLocalName = ai.getPartName();
|
80
|
0
|
String attrQName = getQName(attrNamespace, attrLocalName);
|
81
|
0
|
SaxAttr a = new SaxAttr(attrNamespace, attrLocalName, attrQName, ai.getTextContent());
|
82
|
0
|
attrs.add(a);
|
83
|
|
}
|
84
|
|
|
85
|
0
|
if (null != myTopLevelAttributes) {
|
86
|
0
|
for (Iterator i = myTopLevelAttributes.entrySet().iterator();
|
87
|
0
|
i.hasNext(); ) {
|
88
|
0
|
Map.Entry me = (Map.Entry)i.next();
|
89
|
0
|
String attrQName = (String)me.getKey();
|
90
|
0
|
QName qn = QName.create(attrQName, anImpl.getPrefixToNamespaceMapping());
|
91
|
0
|
if (null == qn) {
|
92
|
0
|
throw new XmlException(XmlMessages.invalidQName(attrQName, null));
|
93
|
|
}
|
94
|
0
|
SaxAttr a = new SaxAttr(qn.getNamespace(), qn.getLocalPart(), attrQName, (String)me.getValue());
|
95
|
0
|
attrs.add(a);
|
96
|
|
}
|
97
|
0
|
myTopLevelAttributes = null;
|
98
|
|
}
|
99
|
|
|
100
|
0
|
startPrefixMappings(newNamespaces, (Map)myMappings.peek());
|
101
|
|
|
102
|
0
|
myContentHandler.startElement(namespace, localName, qName, attrs);
|
103
|
|
} catch (SAXException e) {
|
104
|
0
|
throw new XmlException(e, XmlMessages.wrappedException(e, anImpl));
|
105
|
|
}
|
106
|
|
}
|
107
|
|
|
108
|
0
|
public void visitElementImplEnd(IElementImpl anImpl) throws XmlException {
|
109
|
0
|
try {
|
110
|
0
|
String namespace = anImpl.getNamespace();
|
111
|
0
|
String localName = anImpl.getPartName();
|
112
|
0
|
String qName = getQName(namespace, localName);
|
113
|
0
|
myContentHandler.endElement(namespace, localName, qName);
|
114
|
0
|
Map m = (Map)myMappings.pop();
|
115
|
0
|
List newNamespaces = (List)myNewNamespaces.pop();
|
116
|
0
|
endPrefixMappings(newNamespaces, m);
|
117
|
|
} catch (SAXException e) {
|
118
|
0
|
throw new XmlException(e, XmlMessages.wrappedException(e, anImpl));
|
119
|
|
}
|
120
|
|
}
|
121
|
|
|
122
|
0
|
public void visitTextImpl(ITextImpl anImpl) throws XmlException {
|
123
|
0
|
try {
|
124
|
0
|
char[] chars = anImpl.getTextContent().toCharArray();
|
125
|
0
|
myContentHandler.characters(chars, 0, chars.length);
|
126
|
|
} catch (SAXException e) {
|
127
|
0
|
throw new XmlException(e, XmlMessages.wrappedException(e, anImpl));
|
128
|
|
}
|
129
|
|
}
|
130
|
|
|
131
|
0
|
public synchronized void outputSaxEvents(IImpl anImpl, ContentHandler aHandler, Map aNamespaceToPrefixMapping, Map aTopLevelAttributes) throws XmlException {
|
132
|
0
|
try {
|
133
|
0
|
myContentHandler = aHandler;
|
134
|
0
|
myTopLevelAttributes = aTopLevelAttributes;
|
135
|
0
|
myContentHandler.startDocument();
|
136
|
0
|
Map namespaces = (null != aNamespaceToPrefixMapping) ? aNamespaceToPrefixMapping : Collections.EMPTY_MAP;
|
137
|
0
|
startPrefixMappings(namespaces.keySet(), namespaces);
|
138
|
0
|
myMappings = new UnsynchronizedStack();
|
139
|
0
|
myMappings.push(namespaces);
|
140
|
0
|
myNewNamespaces = new UnsynchronizedStack();
|
141
|
0
|
anImpl.accept(this);
|
142
|
0
|
endPrefixMappings(namespaces.keySet(), namespaces);
|
143
|
0
|
myContentHandler.endDocument();
|
144
|
|
} catch (SAXException e) {
|
145
|
0
|
throw new XmlException(e, XmlMessages.wrappedException(e, anImpl));
|
146
|
|
}
|
147
|
|
}
|
148
|
|
|
149
|
0
|
private void startPrefixMappings(Collection aNamespaces, Map aMappings) throws SAXException {
|
150
|
0
|
for (Iterator i = aNamespaces.iterator(); i.hasNext(); ) {
|
151
|
0
|
String namespace = (String)i.next();
|
152
|
0
|
myContentHandler.startPrefixMapping((String)aMappings.get(namespace), namespace);
|
153
|
|
}
|
154
|
|
}
|
155
|
|
|
156
|
0
|
private void endPrefixMappings(Collection aNamespaces, Map aMappings) throws SAXException {
|
157
|
0
|
for (Iterator i = aNamespaces.iterator(); i.hasNext(); ) {
|
158
|
0
|
String namespace = (String)i.next();
|
159
|
0
|
myContentHandler.endPrefixMapping((String)aMappings.get(namespace));
|
160
|
|
}
|
161
|
|
}
|
162
|
|
|
163
|
|
private static class SaxAttrs implements Attributes {
|
164
|
|
|
165
|
|
private List myAttrs = new ArrayList();
|
166
|
|
|
167
|
0
|
public int getIndex(String aQName) {
|
168
|
0
|
for (int i = 0; i < myAttrs.size(); i++) {
|
169
|
0
|
if (aQName.equals(((SaxAttr)myAttrs.get(i)).qName)) {
|
170
|
0
|
return i;
|
171
|
|
}
|
172
|
|
}
|
173
|
0
|
return -1;
|
174
|
|
}
|
175
|
|
|
176
|
0
|
public int getIndex(String aNamespace, String aLocalName) {
|
177
|
0
|
for (int i = 0; i < myAttrs.size(); i++) {
|
178
|
0
|
if (aNamespace.equals(((SaxAttr)myAttrs.get(i)).namespace) && aLocalName.equals(((SaxAttr)myAttrs.get(i)).localName)) {
|
179
|
0
|
return i;
|
180
|
|
}
|
181
|
|
}
|
182
|
0
|
return -1;
|
183
|
|
}
|
184
|
|
|
185
|
0
|
public int getLength() {
|
186
|
0
|
return myAttrs.size();
|
187
|
|
}
|
188
|
|
|
189
|
0
|
public String getLocalName(int param) {
|
190
|
0
|
return ((param < 0) || (param >= myAttrs.size())) ? null : ((SaxAttr)myAttrs.get(param)).localName;
|
191
|
|
}
|
192
|
|
|
193
|
0
|
public String getQName(int param) {
|
194
|
0
|
return ((param < 0) || (param >= myAttrs.size())) ? null : ((SaxAttr)myAttrs.get(param)).qName;
|
195
|
|
}
|
196
|
|
|
197
|
0
|
public String getValue(int param) {
|
198
|
0
|
return ((param < 0) || (param >= myAttrs.size())) ? null : ((SaxAttr)myAttrs.get(param)).value;
|
199
|
|
}
|
200
|
0
|
public String getValue(String str) {
|
201
|
0
|
return getValue(getIndex(str));
|
202
|
|
}
|
203
|
0
|
public String getValue(String str, String str1) {
|
204
|
0
|
return getValue(getIndex(str, str1));
|
205
|
|
}
|
206
|
|
|
207
|
0
|
public String getURI(int param) {
|
208
|
0
|
return ((param < 0) || (param >= myAttrs.size())) ? null : ((SaxAttr)myAttrs.get(param)).namespace;
|
209
|
|
}
|
210
|
|
|
211
|
0
|
public String getType(int param) {
|
212
|
0
|
return ((param < 0) || (param >= myAttrs.size())) ? null : "CDATA";
|
213
|
|
}
|
214
|
0
|
public String getType(String str) {
|
215
|
0
|
return getType(getIndex(str));
|
216
|
|
}
|
217
|
0
|
public String getType(String str, String str1) {
|
218
|
0
|
return getType(getIndex(str, str1));
|
219
|
|
}
|
220
|
|
|
221
|
0
|
public void add(SaxAttr anAttr) {
|
222
|
0
|
myAttrs.add(anAttr);
|
223
|
|
}
|
224
|
|
}
|
225
|
|
|
226
|
|
private static class SaxAttr {
|
227
|
|
|
228
|
|
String namespace;
|
229
|
|
String localName;
|
230
|
|
String qName;
|
231
|
|
String value;
|
232
|
0
|
SaxAttr(String aNamespace, String aName, String aQName, String aValue) {
|
233
|
0
|
namespace = aNamespace;
|
234
|
0
|
localName = aName;
|
235
|
0
|
qName = aQName;
|
236
|
0
|
value = aValue;
|
237
|
|
}
|
238
|
|
}
|
239
|
|
|
240
|
|
}
|
241
|
|
|