Search in sources :

Example 1 with InputSource

use of org.xml.sax.InputSource in project moco by dreamhead.

the class XPathRequestExtractor method doExtract.

@Override
protected Optional<String[]> doExtract(final HttpRequest request) {
    try {
        Optional<InputSource> source = helper.extractAsInputSource(request, extractor);
        if (!source.isPresent()) {
            return absent();
        }
        NodeList list = (NodeList) xPathExpression.evaluate(source.get(), XPathConstants.NODESET);
        if (list.getLength() == 0) {
            return absent();
        }
        return doExtract(list);
    } catch (XPathExpressionException e) {
        return absent();
    }
}
Also used : InputSource(org.xml.sax.InputSource) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList)

Example 2 with InputSource

use of org.xml.sax.InputSource in project jetty.project by eclipse.

the class XmlParser method parse.

/* ------------------------------------------------------------ */
/**
     * Parse InputStream.
     * @param in the input stream of the xml to parse
     * @return the root node of the xml
     * @throws IOException if unable to load the xml
     * @throws SAXException if unable to parse the xml
     */
public synchronized Node parse(InputStream in) throws IOException, SAXException {
    _dtd = null;
    Handler handler = new Handler();
    XMLReader reader = _parser.getXMLReader();
    reader.setContentHandler(handler);
    reader.setErrorHandler(handler);
    reader.setEntityResolver(handler);
    _parser.parse(new InputSource(in), handler);
    if (handler._error != null)
        throw handler._error;
    Node doc = (Node) handler._top.get(0);
    handler.clear();
    return doc;
}
Also used : InputSource(org.xml.sax.InputSource) ContentHandler(org.xml.sax.ContentHandler) DefaultHandler(org.xml.sax.helpers.DefaultHandler) XMLReader(org.xml.sax.XMLReader)

Example 3 with InputSource

use of org.xml.sax.InputSource in project che by eclipse.

the class DialogSettings method load.

/* (non-Javadoc)
     * Method declared on IDialogSettings.
     */
@Override
public void load(Reader r) {
    Document document = null;
    try {
        DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        //		parser.setProcessNamespace(true);
        document = parser.parse(new InputSource(r));
        //Strip out any comments first
        Node root = document.getFirstChild();
        while (root.getNodeType() == Node.COMMENT_NODE) {
            document.removeChild(root);
            root = document.getFirstChild();
        }
        load(document, (Element) root);
    } catch (ParserConfigurationException e) {
    // ignore
    } catch (IOException e) {
    // ignore
    } catch (SAXException e) {
    // ignore
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Node(org.w3c.dom.Node) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 4 with InputSource

use of org.xml.sax.InputSource in project hadoop by apache.

the class TestRMWithCSRFFilter method verifyClusterInfoXML.

public void verifyClusterInfoXML(String xml) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));
    Document dom = db.parse(is);
    NodeList nodes = dom.getElementsByTagName("clusterInfo");
    assertEquals("incorrect number of elements", 1, nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        Element element = (Element) nodes.item(i);
        verifyClusterGeneric(WebServicesTestUtils.getXmlLong(element, "id"), WebServicesTestUtils.getXmlLong(element, "startedOn"), WebServicesTestUtils.getXmlString(element, "state"), WebServicesTestUtils.getXmlString(element, "haState"), WebServicesTestUtils.getXmlString(element, "haZooKeeperConnectionState"), WebServicesTestUtils.getXmlString(element, "hadoopVersionBuiltOn"), WebServicesTestUtils.getXmlString(element, "hadoopBuildVersion"), WebServicesTestUtils.getXmlString(element, "hadoopVersion"), WebServicesTestUtils.getXmlString(element, "resourceManagerVersionBuiltOn"), WebServicesTestUtils.getXmlString(element, "resourceManagerBuildVersion"), WebServicesTestUtils.getXmlString(element, "resourceManagerVersion"));
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 5 with InputSource

use of org.xml.sax.InputSource in project tomcat by apache.

the class SetParentClassLoaderRule method load.

/**
     * Start a new server instance.
     */
public void load() {
    long t1 = System.nanoTime();
    initDirs();
    // Before digester - it may be needed
    initNaming();
    // Create and execute our Digester
    Digester digester = createStartDigester();
    InputSource inputSource = null;
    InputStream inputStream = null;
    File file = null;
    try {
        try {
            file = configFile();
            inputStream = new FileInputStream(file);
            inputSource = new InputSource(file.toURI().toURL().toString());
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("catalina.configFail", file), e);
            }
        }
        if (inputStream == null) {
            try {
                inputStream = getClass().getClassLoader().getResourceAsStream(getConfigFile());
                inputSource = new InputSource(getClass().getClassLoader().getResource(getConfigFile()).toString());
            } catch (Exception e) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("catalina.configFail", getConfigFile()), e);
                }
            }
        }
        // Alternative: don't bother with xml, just create it manually.
        if (inputStream == null) {
            try {
                inputStream = getClass().getClassLoader().getResourceAsStream("server-embed.xml");
                inputSource = new InputSource(getClass().getClassLoader().getResource("server-embed.xml").toString());
            } catch (Exception e) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("catalina.configFail", "server-embed.xml"), e);
                }
            }
        }
        if (inputStream == null || inputSource == null) {
            if (file == null) {
                log.warn(sm.getString("catalina.configFail", getConfigFile() + "] or [server-embed.xml]"));
            } else {
                log.warn(sm.getString("catalina.configFail", file.getAbsolutePath()));
                if (file.exists() && !file.canRead()) {
                    log.warn("Permissions incorrect, read permission is not allowed on the file.");
                }
            }
            return;
        }
        try {
            inputSource.setByteStream(inputStream);
            digester.push(this);
            digester.parse(inputSource);
        } catch (SAXParseException spe) {
            log.warn("Catalina.start using " + getConfigFile() + ": " + spe.getMessage());
            return;
        } catch (Exception e) {
            log.warn("Catalina.start using " + getConfigFile() + ": ", e);
            return;
        }
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            // Ignore
            }
        }
    }
    getServer().setCatalina(this);
    getServer().setCatalinaHome(Bootstrap.getCatalinaHomeFile());
    getServer().setCatalinaBase(Bootstrap.getCatalinaBaseFile());
    // Stream redirection
    initStreams();
    // Start the new server
    try {
        getServer().init();
    } catch (LifecycleException e) {
        if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE")) {
            throw new java.lang.Error(e);
        } else {
            log.error("Catalina.start", e);
        }
    }
    long t2 = System.nanoTime();
    if (log.isInfoEnabled()) {
        log.info("Initialization processed in " + ((t2 - t1) / 1000000) + " ms");
    }
}
Also used : InputSource(org.xml.sax.InputSource) LifecycleException(org.apache.catalina.LifecycleException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SAXParseException(org.xml.sax.SAXParseException) Digester(org.apache.tomcat.util.digester.Digester) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) ConnectException(java.net.ConnectException) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException)

Aggregations

InputSource (org.xml.sax.InputSource)2048 StringReader (java.io.StringReader)746 IOException (java.io.IOException)656 SAXException (org.xml.sax.SAXException)572 Document (org.w3c.dom.Document)537 DocumentBuilder (javax.xml.parsers.DocumentBuilder)486 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)404 InputStream (java.io.InputStream)333 XMLReader (org.xml.sax.XMLReader)328 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)301 NodeList (org.w3c.dom.NodeList)251 Element (org.w3c.dom.Element)232 ByteArrayInputStream (java.io.ByteArrayInputStream)206 Test (org.junit.Test)203 File (java.io.File)177 SAXParser (javax.xml.parsers.SAXParser)174 Node (org.w3c.dom.Node)165 FileInputStream (java.io.FileInputStream)162 SAXParserFactory (javax.xml.parsers.SAXParserFactory)156 SAXSource (javax.xml.transform.sax.SAXSource)153