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 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     * Creates an XML document model with the given contents.
51     */
52    public Document documentFor(String string, String resourcePath)
53        throws SAXParseException {
54      try {
55        // laaglu
56          factory.setFeature("http://xml.org/sax/features/xmlns-uris", true);
57          factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
58        // laaglu
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        // Let SAXParseExceptions through.
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  }