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.instance.builder;
|
11
|
|
|
12
|
|
import java.net.URL;
|
13
|
|
import java.util.Iterator;
|
14
|
|
import java.util.Map;
|
15
|
|
|
16
|
|
import org.jbind.message.IMessage;
|
17
|
|
import org.jbind.util.other.StrBuffer;
|
18
|
|
import org.jbind.xml.base.ILocation;
|
19
|
|
import org.jbind.xml.base.InputSourceLocation;
|
20
|
|
import org.jbind.xml.core.bridge.IDataImplFactory;
|
21
|
|
import org.jbind.xml.core.bridge.IDocumentImpl;
|
22
|
|
import org.jbind.xml.core.bridge.IElementImpl;
|
23
|
|
import org.jbind.xml.core.bridge.IImplContainer;
|
24
|
|
import org.jbind.xml.core.data.IDataContext;
|
25
|
|
import org.jbind.xml.msg.IConstraintViolations;
|
26
|
|
import org.jbind.xml.msg.XmlException;
|
27
|
|
import org.jbind.xml.msg.XmlMessages;
|
28
|
|
import org.jbind.xml.parser.AttributeData;
|
29
|
|
import org.jbind.xml.parser.IContentHandler;
|
30
|
|
import org.jbind.xml.parser.IErrorHandler;
|
31
|
|
import org.jbind.xml.parser.INamespaceContext;
|
32
|
|
import org.jbind.xml.parser.InputSourceParser;
|
33
|
|
import org.jbind.xml.parser.QualifiedName;
|
34
|
|
import org.xml.sax.EntityResolver;
|
35
|
|
import org.xml.sax.InputSource;
|
36
|
|
|
37
|
|
/**
|
38
|
|
* Builds implementation documents.
|
39
|
|
*/
|
40
|
|
public class ImplBuilder implements IImplBuilder, IContentHandler, IErrorHandler {
|
41
|
|
|
42
|
|
private IImplContainer myCurrentParent = null;
|
43
|
|
|
44
|
|
private IDocumentImpl myDocument = null;
|
45
|
|
|
46
|
|
private EntityResolver myEntityResolver = null;
|
47
|
|
private IDataImplFactory myImplFactory = null;
|
48
|
|
|
49
|
|
private StrBuffer myCharacterBuffer = new StrBuffer();
|
50
|
|
private ILocation myCharacterLocation = null;
|
51
|
|
private Map myCharacterPrefixMappings = null;
|
52
|
|
|
53
|
|
private IDataBuilder myDataBuilder = null;
|
54
|
|
private IConstraintViolations myViolations = null;
|
55
|
|
private IDataValidator myDataValidator = null;
|
56
|
|
private IDataContext myDataContext = null;
|
57
|
|
|
58
|
|
private int myExceptionElements = 0;
|
59
|
|
|
60
|
|
private InputSourceParser myParser = new InputSourceParser();
|
61
|
|
|
62
|
222
|
public ImplBuilder(EntityResolver anEntityResolver, IDataImplFactory anImplFactory, IDataBuilder aDataBuilder, IDataValidator aDataValidator, IDataContext aDataContext) {
|
63
|
222
|
myEntityResolver = anEntityResolver;
|
64
|
222
|
myImplFactory = anImplFactory;
|
65
|
222
|
myDataBuilder = aDataBuilder;
|
66
|
222
|
myDataValidator = aDataValidator;
|
67
|
222
|
myDataContext = (null != aDataContext) ? aDataContext : new DataContext(false);
|
68
|
|
}
|
69
|
|
|
70
|
223
|
private void prepare() {
|
71
|
223
|
myViolations = XmlMessages.constraintViolations();
|
72
|
223
|
myExceptionElements = 0;
|
73
|
223
|
myDataBuilder.initialize();
|
74
|
|
}
|
75
|
|
|
76
|
2
|
public IDocumentImpl buildDocument(URL aUrl) throws XmlException {
|
77
|
2
|
return buildDocument(new InputSource(aUrl.toString()));
|
78
|
|
}
|
79
|
|
|
80
|
223
|
public synchronized IDocumentImpl buildDocument(InputSource anInputSource) throws XmlException {
|
81
|
223
|
prepare();
|
82
|
223
|
myParser.parse(anInputSource, this, this, myEntityResolver);
|
83
|
223
|
if (!myViolations.isEmpty()) {
|
84
|
146
|
throw new XmlException(myViolations);
|
85
|
|
}
|
86
|
77
|
return myDocument;
|
87
|
|
}
|
88
|
|
|
89
|
223
|
public void startDocument(InputSource anInputSource, INamespaceContext aNamespaceContext) {
|
90
|
223
|
myDocument = myImplFactory.createDocumentImpl();
|
91
|
223
|
myCurrentParent = myDocument;
|
92
|
|
}
|
93
|
|
|
94
|
223
|
public void endDocument(InputSource anInputSource, INamespaceContext aNamespaceContext) {
|
95
|
223
|
addTextChild();
|
96
|
|
// validate the newly created data.
|
97
|
223
|
if (myViolations.isEmpty()) {
|
98
|
93
|
myDataValidator.validate(myDocument.getRootData(), myDataContext, myViolations);
|
99
|
|
}
|
100
|
|
}
|
101
|
|
|
102
|
2747
|
public void startElement(QualifiedName aQName, Map anAttributes, int aLine, int aColumn, InputSource anInputSource, INamespaceContext aNamespaceContext) throws Exception {
|
103
|
2747
|
try {
|
104
|
|
|
105
|
2747
|
addTextChild();
|
106
|
|
|
107
|
2747
|
ILocation location = new InputSourceLocation(anInputSource, aLine, aColumn);
|
108
|
2747
|
IElementImpl element = myCurrentParent.addElement(aQName.getNamespace(), aQName.toString(), aNamespaceContext.getCurrentMapping(), location);
|
109
|
2747
|
myCurrentParent = element;
|
110
|
|
|
111
|
2747
|
for (Iterator i = anAttributes.values().iterator(); i.hasNext(); ) {
|
112
|
2042
|
AttributeData ad = (AttributeData)i.next();
|
113
|
2042
|
String qName = ad.qn.toString();
|
114
|
2042
|
String namespace = ad.qn.getNamespace();
|
115
|
2042
|
String value = ad.value;
|
116
|
2042
|
element.addAttribute(namespace, qName, value, false, location);
|
117
|
|
}
|
118
|
|
|
119
|
2747
|
if (myExceptionElements == 0) {
|
120
|
2739
|
try {
|
121
|
2739
|
myDataBuilder.startElement(element, myViolations);
|
122
|
|
} catch (XmlException e) {
|
123
|
35
|
myViolations.add(e.getXmlMessage());
|
124
|
35
|
myExceptionElements = 1;
|
125
|
|
}
|
126
|
|
|
127
|
|
} else {
|
128
|
8
|
myExceptionElements++;
|
129
|
|
|
130
|
|
}
|
131
|
|
|
132
|
|
} catch (RuntimeException e) {
|
133
|
0
|
e.printStackTrace();
|
134
|
0
|
throw e;
|
135
|
|
}
|
136
|
|
|
137
|
|
}
|
138
|
|
|
139
|
2747
|
public void endElement(QualifiedName aQName, Map anAttributes, int aLine, int aColumn, InputSource anInputSource, INamespaceContext aNamespaceContext) throws Exception {
|
140
|
2747
|
try {
|
141
|
2747
|
addTextChild();
|
142
|
|
|
143
|
2747
|
IElementImpl element = (IElementImpl)myCurrentParent;
|
144
|
2747
|
myCurrentParent = (IImplContainer)element.getParentImpl();
|
145
|
|
|
146
|
2747
|
if (myExceptionElements == 0) {
|
147
|
2704
|
try {
|
148
|
2704
|
ILocation location = new InputSourceLocation(anInputSource, aLine, aColumn);
|
149
|
2704
|
myDataBuilder.endElement(element, location, myViolations);
|
150
|
|
} catch (XmlException e) {
|
151
|
78
|
myViolations.add(e.getXmlMessage());
|
152
|
|
}
|
153
|
|
} else {
|
154
|
43
|
myExceptionElements--;
|
155
|
|
}
|
156
|
|
|
157
|
|
} catch (RuntimeException e) {
|
158
|
0
|
e.printStackTrace();
|
159
|
0
|
throw e;
|
160
|
|
}
|
161
|
|
}
|
162
|
|
|
163
|
5717
|
private void addTextChild() {
|
164
|
5717
|
if (myCharacterBuffer.length() > 0) {
|
165
|
3665
|
myDataBuilder.signalText(myCurrentParent, myCharacterBuffer.toString(), myCharacterLocation, myViolations);
|
166
|
3665
|
myCharacterBuffer.setLength(0);
|
167
|
|
}
|
168
|
5717
|
myCharacterPrefixMappings = null;
|
169
|
|
}
|
170
|
|
|
171
|
3733
|
public void text(String aString, int aLine, int aColumn, InputSource anInputSource, INamespaceContext aNamespaceContext) throws Exception {
|
172
|
3733
|
if (myCharacterBuffer.length() == 0) {
|
173
|
3665
|
myCharacterLocation = new InputSourceLocation(anInputSource, aLine, aColumn);
|
174
|
3665
|
myCharacterPrefixMappings = aNamespaceContext.getCurrentMapping();
|
175
|
|
}
|
176
|
3733
|
myCharacterBuffer.append(aString);
|
177
|
|
}
|
178
|
|
|
179
|
1
|
public void startXInclude(String aParseMethod, URL aUrl, String aFragementId) {}
|
180
|
1
|
public void endXInclude() {}
|
181
|
|
|
182
|
0
|
public void fatalError(IMessage aMessage) {
|
183
|
0
|
myViolations.add(aMessage);
|
184
|
|
}
|
185
|
|
|
186
|
0
|
public boolean error(IMessage aMessage) {
|
187
|
0
|
myViolations.add(aMessage);
|
188
|
0
|
return false;
|
189
|
|
}
|
190
|
|
|
191
|
0
|
public boolean warning(IMessage aMessage) {
|
192
|
0
|
myViolations.add(aMessage);
|
193
|
0
|
return false;
|
194
|
|
}
|
195
|
|
|
196
|
0
|
public void exception(Exception anException, InputSource anInputSource) {
|
197
|
0
|
anException.printStackTrace();
|
198
|
0
|
myViolations.add(XmlMessages.wrappedException(anException, new InputSourceLocation(anInputSource)));
|
199
|
|
}
|
200
|
|
}
|
201
|
|
|