1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.google.gwt.uibinder.rebind;
17
18 import com.google.gwt.core.ext.TreeLogger;
19 import com.google.gwt.dev.resource.ResourceOracle;
20
21 import org.w3c.dom.Document;
22 import org.xml.sax.InputSource;
23 import org.xml.sax.SAXException;
24 import org.xml.sax.SAXParseException;
25
26 import java.io.IOException;
27 import java.io.StringReader;
28
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.parsers.SAXParser;
31 import javax.xml.parsers.SAXParserFactory;
32
33
34
35
36
37 public class W3cDomHelper {
38 private static final String LOAD_EXTERNAL_DTD =
39 "http://apache.org/xml/features/nonvalidating/load-external-dtd";
40
41 private final SAXParserFactory factory;
42 private final TreeLogger logger;
43 private final ResourceOracle resourceOracle;
44
45 public W3cDomHelper(TreeLogger logger, ResourceOracle resourceOracle) {
46 this.logger = logger;
47 this.resourceOracle = resourceOracle;
48 factory = SAXParserFactory.newInstance();
49 try {
50 factory.setFeature(LOAD_EXTERNAL_DTD, true);
51 } catch (ParserConfigurationException e) {
52 throw new RuntimeException(e);
53 } catch (SAXException e) {
54
55 }
56 factory.setNamespaceAware(true);
57 }
58
59
60
61
62 public Document documentFor(String string, String resourcePath)
63 throws SAXParseException {
64 try {
65
66 factory.setFeature("http://xml.org/sax/features/xmlns-uris", true);
67 factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
68
69 if (resourcePath != null) {
70 int pos = resourcePath.lastIndexOf('/');
71 resourcePath = (pos < 0) ? "" : resourcePath.substring(0, pos + 1);
72 }
73 W3cDocumentBuilder handler = new W3cDocumentBuilder(logger, resourcePath,
74 resourceOracle);
75 SAXParser parser = factory.newSAXParser();
76 InputSource input = new InputSource(new StringReader(string));
77 input.setSystemId(resourcePath);
78 parser.parse(input, handler);
79 return handler.getDocument();
80 } catch (SAXParseException e) {
81
82 throw e;
83 } catch (SAXException e) {
84 throw new RuntimeException(e);
85 } catch (IOException e) {
86 throw new RuntimeException(e);
87 } catch (ParserConfigurationException e) {
88 throw new RuntimeException(e);
89 }
90 }
91 }