Example usage for org.xml.sax EntityResolver EntityResolver

List of usage examples for org.xml.sax EntityResolver EntityResolver

Introduction

In this page you can find the example usage for org.xml.sax EntityResolver EntityResolver.

Prototype

EntityResolver

Source Link

Usage

From source file:Main.java

public static Document read(String filename) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);// w w  w .ja v  a2s.  c  o  m
    DocumentBuilder builder = null;
    Document document = null;
    try {
        builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                return new InputSource(new StringReader(""));
            }
        });
        document = builder.parse(new File(filename));
        document.getDocumentElement().normalize();
    } catch (Exception e) {
    }
    return document;
}

From source file:Main.java

public static Document loadFromStream(InputStream inputStream) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);//from   w  ww  .  j a v  a 2s.c  om
    factory.setNamespaceAware(false);

    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            //System.out.println("Ignoring " + publicId + ", " + systemId);
            return new InputSource(new StringReader(""));
        }
    });

    //System.out.println(StringUtil.toString(inputStream));

    Document doc = builder.parse(inputStream);
    return doc;
}

From source file:Main.java

public static Document loadDocument(InputStream is)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    //skip DTD validation
    builder.setEntityResolver(new EntityResolver() {

        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            return new InputSource(new StringReader(""));
        }// ww w  . ja v  a 2  s  .c om
    });

    return builder.parse(is);
}

From source file:Main.java

private static void validateDTD0(String xmlFile, final String dtdPath)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setValidating(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    if (dtdPath != null && !dtdPath.isEmpty()) {
        documentBuilder.setEntityResolver(new EntityResolver() {

            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {

                return new InputSource(new FileInputStream(dtdPath));
            }/*from  ww w. j a va 2 s. c om*/
        });
    }
    documentBuilder.setErrorHandler(new ErrorHandler() {

        public void warning(SAXParseException exception) throws SAXException {

        }

        public void fatalError(SAXParseException exception) throws SAXException {

            throw exception;
        }

        public void error(SAXParseException exception) throws SAXException {

            throw exception;
        }
    });

    documentBuilder.parse(new FileInputStream(xmlFile));

}

From source file:Main.java

/** Read an XML file into a DOM tree
 * /*  ww  w .ja va  2 s .co m*/
 * @param file the file to read
 * @return a new XML document
 * @throws SAXException if an XML parse error occurs
 * @throws IOException if a file I/O error occurs
 */
public static Document read(File file) throws SAXException, IOException {
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();

        // Workaround for missing external DTD.  When the parser looks up
        // an external reference, we provide an empty document back.  While
        // not correct, we don't expect SVG files loaded by this program
        // to depend on externally defined entities; nor do we require
        // validation.
        //
        // http://stackoverflow.com/questions/2640825/how-to-parse-a-xhtml-ignoring-the-doctype-declaration-using-dom-parser
        builder.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                return new InputSource(new StringReader(""));
            }
        });

        return builder.parse(file);
    } catch (ParserConfigurationException e) {
        // This exception is never thrown, treat as fatal if it is
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            InputStream source = !systemId.startsWith("file:") ? null
                    : getClass().getResourceAsStream(
                            "/net/sf/logsupport/" + new File(URI.create(systemId)).getName());

            return source == null ? new InputSource(new StringReader("")) : new InputSource(source);
        }/*from   ww w  .  j  ava 2s. c  o m*/
    });
    return builder;
}

From source file:Main.java

public static String findInXml(String xml, String xpath) {
    try {//from  ww  w. j  a  v  a  2  s.c  o m
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new EntityResolver() {
            @Override
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                //                    if (systemId.contains("foo.dtd")) {
                return new InputSource(new StringReader(""));
                //                    } else {
                //                        return null;
                //                    }
            }
        });
        Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPathExpression expr = xPathfactory.newXPath().compile(xpath);
        return (String) expr.evaluate(doc, XPathConstants.STRING);
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}

From source file:Main.java

public static Document createDocument(InputSource inputSource)
        throws SAXException, ParserConfigurationException, IOException {
    Document doc = null;/*from w  w w .  j av a 2  s  .  c  o  m*/
    DocumentBuilderFactory factory = createDocumentBuilderFactory();
    DocumentBuilder builder = createDocumentBuilder(factory);
    builder.setEntityResolver(new EntityResolver() {
        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            return new InputSource(new StringReader(""));
        }
    });
    doc = builder.parse(inputSource);
    return doc;
}

From source file:Main.java

/**
 * Parses a document from the given string
 * //from   w w w .j a v  a 2s.  co m
 * @param template The string to parse
 * @return The parsed {@link Document}
 */
public static Document getTemplateDocument(String template) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;
    dbf.setValidating(false);
    try {
        db = dbf.newDocumentBuilder();
        db.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicID, String systemID) throws SAXException {
                return new InputSource(new StringReader(""));
            }
        });
        Document doc = db.parse(new ByteArrayInputStream(template.getBytes("utf8")));
        return doc;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Document getXmlDocFromString(String xml) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/* w w w. j a  va  2s.  c  o m*/
    dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    DocumentBuilder builder = dbf.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {
        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            return new InputSource(new StringReader(""));
        }
    });
    return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
}