1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44:
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54:
55: import ;
56: import ;
57: import ;
58: import ;
59:
60:
66: public class GnomeDocumentBuilder
67: extends DocumentBuilder
68: implements DOMImplementation
69: {
70:
71: static
72: {
73: XMLJ.init();
74: }
75:
76:
77:
78: private boolean validate;
79: private boolean coalesce;
80: private boolean expandEntities;
81: private EntityResolver entityResolver;
82: private ErrorHandler errorHandler;
83: private boolean seenFatalError;
84:
85:
88: public GnomeDocumentBuilder()
89: {
90: this(true, false, false);
91: }
92:
93:
99: public GnomeDocumentBuilder(boolean validate,
100: boolean coalesce,
101: boolean expandEntities)
102: {
103: this.validate = validate;
104: this.coalesce = coalesce;
105: this.expandEntities = expandEntities;
106: }
107:
108: public DOMImplementation getDOMImplementation()
109: {
110: return this;
111: }
112:
113: public boolean isNamespaceAware()
114: {
115: return true;
116: }
117:
118: public boolean isValidating()
119: {
120: return validate;
121: }
122:
123: public Document newDocument()
124: {
125: return createDocument(null, null, null);
126: }
127:
128: public Document parse(InputSource input)
129: throws SAXException, IOException
130: {
131: NamedInputStream in = XMLJ.getInputStream(input);
132: byte[] detectBuffer = in.getDetectBuffer();
133: String publicId = input.getPublicId();
134: String systemId = input.getSystemId();
135: String base = XMLJ.getBaseURI(systemId);
136:
137: if (detectBuffer == null)
138: {
139: throw new SAXParseException("No document element", publicId,
140: systemId, 0, 0);
141: }
142: seenFatalError = false;
143: return parseStream(in,
144: detectBuffer,
145: publicId,
146: systemId,
147: base,
148: validate,
149: coalesce,
150: expandEntities,
151: true,
152: errorHandler != null);
153: }
154:
155: private native Document parseStream(InputStream in,
156: byte[] detectBuffer,
157: String publicId,
158: String systemId,
159: String base,
160: boolean validate,
161: boolean coalesce,
162: boolean expandEntities,
163: boolean entityResolver,
164: boolean errorHandler);
165:
166: public void setEntityResolver(EntityResolver resolver)
167: {
168: entityResolver = resolver;
169: }
170:
171: public void setErrorHandler(ErrorHandler handler)
172: {
173: errorHandler = handler;
174: }
175:
176:
177:
178: public boolean hasFeature(String name, String version)
179: {
180: if (name.length() == 0)
181: {
182: return false;
183: }
184: name = name.toLowerCase();
185: if (name.charAt(0) == '+')
186: {
187: name = name.substring(1);
188: }
189:
190: if ("xml".equals(name) || "core".equals(name))
191: {
192: return (version == null ||
193: "".equals(version) ||
194: "1.0".equals(version) ||
195: "2.0".equals(version) ||
196: "3.0".equals(version));
197:
198: }
199: else if ("ls".equals(name) || "ls-async".equals(name))
200: {
201:
202:
207: return false;
208: }
209: else if ("traversal".equals(name))
210: {
211: return (version == null ||
212: "".equals(version) ||
213: "2.0".equals(version));
214: }
215: else if ("xpath".equals(name))
216: {
217: return (version == null ||
218: "".equals(version) ||
219: "3.0".equals(version));
220: }
221: return false;
222: }
223:
224:
225:
226: public Object getFeature(String feature, String version)
227: {
228: if (hasFeature(feature, version))
229: {
230: return this;
231: }
232: return null;
233: }
234:
235: public native Document createDocument(String namespaceURI,
236: String qualifiedName,
237: DocumentType doctype);
238:
239: public DocumentType createDocumentType(String qualifiedName,
240: String publicId,
241: String systemId)
242: {
243: return new StandaloneDocumentType(qualifiedName, publicId, systemId);
244: }
245:
246:
247:
248: private void setDocumentLocator(Object ctx, Object loc)
249: {
250:
251: }
252:
253: private InputStream resolveEntity(String publicId, String systemId,
254: String base)
255: throws SAXException, IOException
256: {
257: String url = XMLJ.getAbsoluteURI(base, systemId);
258: InputStream in = null;
259: if (entityResolver != null)
260: {
261: InputSource source = entityResolver.resolveEntity(publicId, url);
262: if (source != null)
263: {
264: in = XMLJ.getInputStream(source);
265: }
266: }
267: if (in == null)
268: {
269: in = XMLJ.getInputStream(new URL(url));
270: }
271: return in;
272: }
273:
274: private void warning(String message,
275: int lineNumber,
276: int columnNumber,
277: String publicId,
278: String systemId)
279: throws SAXException
280: {
281: if (!seenFatalError && errorHandler != null)
282: {
283: Locator l = new StandaloneLocator(lineNumber,
284: columnNumber,
285: publicId,
286: systemId);
287: errorHandler.warning(new SAXParseException(message, l));
288: }
289: }
290:
291: private void error(String message,
292: int lineNumber,
293: int columnNumber,
294: String publicId,
295: String systemId)
296: throws SAXException
297: {
298: if (!seenFatalError && errorHandler != null)
299: {
300: Locator l = new StandaloneLocator(lineNumber,
301: columnNumber,
302: publicId,
303: systemId);
304: errorHandler.error(new SAXParseException(message, l));
305: }
306: }
307:
308: private void fatalError(String message,
309: int lineNumber,
310: int columnNumber,
311: String publicId,
312: String systemId)
313: throws SAXException
314: {
315: if (!seenFatalError && errorHandler != null)
316: {
317: seenFatalError = true;
318: Locator l = new StandaloneLocator(lineNumber,
319: columnNumber,
320: publicId,
321: systemId);
322: errorHandler.fatalError(new SAXParseException(message, l));
323: }
324: }
325:
326: }