View Javadoc

1   /*
2    * Copyright 2009 Google Inc.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
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   * Simplifies instantiation of the w3c XML parser, in just the style that
35   * UiBinder likes it. Used by both prod and test.
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        // ignore since parser doesn't know about this feature
55      }
56      factory.setNamespaceAware(true);
57    }
58  
59    /**
60     * Creates an XML document model with the given contents.
61     */
62    public Document documentFor(String string, String resourcePath)
63        throws SAXParseException {
64      try {
65        // laaglu
66          factory.setFeature("http://xml.org/sax/features/xmlns-uris", true);
67          factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
68        // laaglu
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        // Let SAXParseExceptions through.
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  }