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 final SAXParserFactory factory;
39 private final TreeLogger logger;
40 private final ResourceOracle resourceOracle;
41
42 public W3cDomHelper(TreeLogger logger, ResourceOracle resourceOracle) {
43 this.logger = logger;
44 this.resourceOracle = resourceOracle;
45 factory = SAXParserFactory.newInstance();
46 factory.setNamespaceAware(true);
47 }
48
49
50
51
52 public Document documentFor(String string, String resourcePath)
53 throws SAXParseException {
54 try {
55
56 factory.setFeature("http://xml.org/sax/features/xmlns-uris", true);
57 factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
58
59 if (resourcePath != null) {
60 int pos = resourcePath.lastIndexOf('/');
61 resourcePath = (pos < 0) ? "" : resourcePath.substring(0, pos + 1);
62 }
63 W3cDocumentBuilder handler = new W3cDocumentBuilder(logger, resourcePath,
64 resourceOracle);
65 SAXParser parser = factory.newSAXParser();
66 InputSource input = new InputSource(new StringReader(string));
67 input.setSystemId(resourcePath);
68 parser.parse(input, handler);
69 return handler.getDocument();
70 } catch (SAXParseException e) {
71
72 throw e;
73 } catch (SAXException e) {
74 throw new RuntimeException(e);
75 } catch (IOException e) {
76 throw new RuntimeException(e);
77 } catch (ParserConfigurationException e) {
78 throw new RuntimeException(e);
79 }
80 }
81 }