Search in sources :

Example 31 with EntityResolver

use of org.xml.sax.EntityResolver in project metagenomicsTools by afodor.

the class GenbankXMLParser method main.

/**
 * Author:  anthony.fodor@gmail.com
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version,
 * provided that any use properly credits the author.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details at http://www.gnu.org * *
 */
public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    dbf.setValidating(false);
    db.setEntityResolver(new EntityResolver() {

        public InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId) throws SAXException, java.io.IOException {
            return new InputSource(new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
        }
    });
    Document document = db.parse(ConfigReader.getGenbankCacheDir() + File.separator + "D37875.xml");
    Element element = document.getDocumentElement();
    // printNodeAndChildren(element, 0);
    Holder h = new Holder();
    getFirstGBSeq_Definition(element, h);
    System.out.println(h.returnVal);
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) EntityResolver(org.xml.sax.EntityResolver) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 32 with EntityResolver

use of org.xml.sax.EntityResolver in project Mycat_plus by coderczp.

the class ConfigUtil method getDocument.

public static Document getDocument(final InputStream dtd, InputStream xml) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) {
            return new InputSource(dtd);
        }
    });
    builder.setErrorHandler(new ErrorHandler() {

        @Override
        public void warning(SAXParseException e) {
        }

        @Override
        public void error(SAXParseException e) throws SAXException {
            throw e;
        }

        @Override
        public void fatalError(SAXParseException e) throws SAXException {
            throw e;
        }
    });
    return builder.parse(xml);
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) EntityResolver(org.xml.sax.EntityResolver) SAXException(org.xml.sax.SAXException)

Example 33 with EntityResolver

use of org.xml.sax.EntityResolver in project motech by motech.

the class Log4JBundleLoader method loadBundle.

public void loadBundle(Bundle bundle) throws BundleConfigurationLoadingException, IOException {
    String symbolicName = bundle.getSymbolicName();
    LOGGER.debug("Looking for log4j config in {}", symbolicName);
    URL log4jUrl = bundle.getResource(log4JConf);
    if (log4jUrl != null) {
        LOGGER.debug("Log4j config found in {}, loading", symbolicName);
        InputStream log4jStream = null;
        try {
            URLConnection conn = log4jUrl.openConnection();
            log4jStream = conn.getInputStream();
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            db.setEntityResolver(new EntityResolver() {

                public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                    return new InputSource(new StringReader(""));
                }
            });
            Document log4jDoc = db.parse(log4jStream);
            if (loggers != null && checkLogXmlConfiguration(log4jDoc)) {
                PropertyConfigurator.configure(loggerProperties);
            } else {
                DOMConfigurator.configure(log4jDoc.getDocumentElement());
            }
            logService.reconfigure();
            LOGGER.debug("Added log4j configuration for [" + bundle.getLocation() + "]");
        } catch (ParserConfigurationException | SAXException e) {
            throw new BundleConfigurationLoadingException("Error while loading log4j configuration from " + bundle, e);
        } finally {
            IOUtils.closeQuietly(log4jStream);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) URL(java.net.URL) URLConnection(java.net.URLConnection) BundleConfigurationLoadingException(org.motechproject.osgi.web.exception.BundleConfigurationLoadingException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 34 with EntityResolver

use of org.xml.sax.EntityResolver in project validator by validator.

the class DmozDriver method main.

/**
 */
public static void main(String[] args) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    XMLReader parser = factory.newSAXParser().getXMLReader();
    DmozHandler domzHandler = new DmozHandler();
    parser.setContentHandler(domzHandler);
    parser.setFeature("http://xml.org/sax/features/string-interning", true);
    parser.setEntityResolver(new EntityResolver() {

        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            InputSource is = new InputSource(new ByteArrayInputStream(new byte[0]));
            is.setPublicId(publicId);
            is.setSystemId(systemId);
            return is;
        }
    });
    parser.setErrorHandler(new ErrorHandler() {

        public void error(SAXParseException exception) throws SAXException {
        }

        public void fatalError(SAXParseException exception) throws SAXException {
        }

        public void warning(SAXParseException exception) throws SAXException {
        }
    });
    InputSource is = new InputSource(new GZIPInputStream(new FileInputStream(args[0])));
    parser.parse(is);
    Writer out = new OutputStreamWriter(new FileOutputStream(args[1]), "utf-8");
    for (Map.Entry<String, String> entry : domzHandler.getTheMap().entrySet()) {
        out.write(entry.getKey());
        out.write('\t');
        out.write(entry.getValue());
        out.write('\n');
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParseException(org.xml.sax.SAXParseException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Map(java.util.Map) XMLReader(org.xml.sax.XMLReader) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 35 with EntityResolver

use of org.xml.sax.EntityResolver in project maven-plugins by apache.

the class XmlAppendingTransformer method processResource.

public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException {
    Document r;
    try {
        SAXBuilder builder = new SAXBuilder(false);
        builder.setExpandEntities(false);
        if (ignoreDtd) {
            builder.setEntityResolver(new EntityResolver() {

                public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                    return new InputSource(new StringReader(""));
                }
            });
        }
        r = builder.build(is);
    } catch (JDOMException e) {
        throw new RuntimeException("Error processing resource " + resource + ": " + e.getMessage(), e);
    }
    if (doc == null) {
        doc = r;
    } else {
        Element root = r.getRootElement();
        for (@SuppressWarnings("unchecked") Iterator<Attribute> itr = root.getAttributes().iterator(); itr.hasNext(); ) {
            Attribute a = itr.next();
            itr.remove();
            Element mergedEl = doc.getRootElement();
            Attribute mergedAtt = mergedEl.getAttribute(a.getName(), a.getNamespace());
            if (mergedAtt == null) {
                mergedEl.setAttribute(a);
            }
        }
        for (@SuppressWarnings("unchecked") Iterator<Content> itr = root.getChildren().iterator(); itr.hasNext(); ) {
            Content n = itr.next();
            itr.remove();
            doc.getRootElement().addContent(n);
        }
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) InputSource(org.xml.sax.InputSource) Attribute(org.jdom.Attribute) Element(org.jdom.Element) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) SAXException(org.xml.sax.SAXException) Content(org.jdom.Content) StringReader(java.io.StringReader)

Aggregations

EntityResolver (org.xml.sax.EntityResolver)85 InputSource (org.xml.sax.InputSource)41 SAXException (org.xml.sax.SAXException)37 IOException (java.io.IOException)32 DefaultHandler (org.xml.sax.helpers.DefaultHandler)25 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)23 DocumentBuilder (javax.xml.parsers.DocumentBuilder)22 Test (org.junit.jupiter.api.Test)22 Document (org.w3c.dom.Document)18 StringReader (java.io.StringReader)15 Field (java.lang.reflect.Field)14 SAXParseException (org.xml.sax.SAXParseException)14 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)11 NodeList (org.w3c.dom.NodeList)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 InputStream (java.io.InputStream)10 ArrayList (java.util.ArrayList)9 Node (org.w3c.dom.Node)9 ErrorHandler (org.xml.sax.ErrorHandler)9 Element (org.w3c.dom.Element)8