Search in sources :

Example 46 with InputSource

use of org.xml.sax.InputSource in project liquibase by liquibase.

the class LiquibaseSchemaResolverTest method shouldReturnNullWhenNoResourceAsStreamFound.

@Test
public void shouldReturnNullWhenNoResourceAsStreamFound() {
    when(resourceAccessorXsdStreamResolver.getResourceAsStream(XSD_FILE)).thenReturn(null);
    InputSource inputSource = liquibaseSchemaResolver.resolve(liquibaseParser);
    assertThat(inputSource).isNull();
}
Also used : InputSource(org.xml.sax.InputSource) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 47 with InputSource

use of org.xml.sax.InputSource in project liquibase by liquibase.

the class LiquibaseSchemaResolverTest method shouldReturnNullWhenExceptionOccurs.

@Test
public void shouldReturnNullWhenExceptionOccurs() {
    when(resourceAccessorXsdStreamResolver.getResourceAsStream(XSD_FILE)).thenThrow(new RuntimeException());
    InputSource inputSource = liquibaseSchemaResolver.resolve(liquibaseParser);
    assertThat(inputSource).isNull();
}
Also used : InputSource(org.xml.sax.InputSource) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 48 with InputSource

use of org.xml.sax.InputSource in project android-async-http by loopj.

the class SaxAsyncHttpResponseHandler method getResponseData.

/**
     * Deconstructs response into given content handler
     *
     * @param entity returned HttpEntity
     * @return deconstructed response
     * @throws java.io.IOException if there is problem assembling SAX response from stream
     * @see cz.msebera.android.httpclient.HttpEntity
     */
@Override
protected byte[] getResponseData(HttpEntity entity) throws IOException {
    if (entity != null) {
        InputStream instream = entity.getContent();
        InputStreamReader inputStreamReader = null;
        if (instream != null) {
            try {
                SAXParserFactory sfactory = SAXParserFactory.newInstance();
                SAXParser sparser = sfactory.newSAXParser();
                XMLReader rssReader = sparser.getXMLReader();
                rssReader.setContentHandler(handler);
                inputStreamReader = new InputStreamReader(instream, getCharset());
                rssReader.parse(new InputSource(inputStreamReader));
            } catch (SAXException e) {
                AsyncHttpClient.log.e(LOG_TAG, "getResponseData exception", e);
            } catch (ParserConfigurationException e) {
                AsyncHttpClient.log.e(LOG_TAG, "getResponseData exception", e);
            } finally {
                AsyncHttpClient.silentCloseInputStream(instream);
                if (inputStreamReader != null) {
                    try {
                        inputStreamReader.close();
                    } catch (IOException e) {
                    /*ignore*/
                    }
                }
            }
        }
    }
    return null;
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 49 with InputSource

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

the class XAdESSignatureProperties method createChildFromXmlFragmentOrText.

protected Element createChildFromXmlFragmentOrText(Document doc, Input input, String localElementName, String errorMessage, String elementOrText) throws IOException, ParserConfigurationException, XmlSignatureException {
    String ending = localElementName + ">";
    Element child;
    if (elementOrText.startsWith("<") && elementOrText.endsWith(ending)) {
        try {
            // assume xml
            InputSource source = new InputSource(new StringReader(elementOrText));
            source.setEncoding("UTF-8");
            Document parsedDoc = XmlSignatureHelper.newDocumentBuilder(Boolean.TRUE).parse(source);
            replacePrefixes(parsedDoc, input);
            child = (Element) doc.adoptNode(parsedDoc.getDocumentElement());
            // check for correct namespace
            String ns = findNamespace(input.getMessage());
            if (!ns.equals(child.getNamespaceURI())) {
                throw new XmlSignatureException(String.format("The XAdES confguration is invalid. The root element '%s' of the provided XML fragment '%s' has the invalid namespace '%s'. The correct namespace is '%s'.", child.getLocalName(), elementOrText, child.getNamespaceURI(), ns));
            }
        } catch (SAXException e) {
            throw new XmlSignatureException(String.format(errorMessage, elementOrText, localElementName, namespace), e);
        }
    } else {
        child = createElement(localElementName, doc, input);
        child.setTextContent(elementOrText);
    }
    return child;
}
Also used : InputSource(org.xml.sax.InputSource) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 50 with InputSource

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

the class XmlRpcDataFormat method unmarshalResponse.

protected Object unmarshalResponse(Exchange exchange, InputStream stream) throws Exception {
    InputSource isource = new InputSource(stream);
    XMLReader xr = newXMLReader();
    XmlRpcResponseParser xp;
    try {
        xp = new XmlRpcResponseParser(xmlRpcStreamRequestConfig, typeFactory);
        xr.setContentHandler(xp);
        xr.parse(isource);
    } catch (SAXException e) {
        throw new XmlRpcClientException("Failed to parse server's response: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new XmlRpcClientException("Failed to read server's response: " + e.getMessage(), e);
    }
    if (xp.isSuccess()) {
        return xp.getResult();
    }
    Throwable t = xp.getErrorCause();
    if (t == null) {
        throw new XmlRpcException(xp.getErrorCode(), xp.getErrorMessage());
    }
    if (t instanceof XmlRpcException) {
        throw (XmlRpcException) t;
    }
    if (t instanceof RuntimeException) {
        throw (RuntimeException) t;
    }
    throw new XmlRpcException(xp.getErrorCode(), xp.getErrorMessage(), t);
}
Also used : InputSource(org.xml.sax.InputSource) XmlRpcClientException(org.apache.xmlrpc.client.XmlRpcClientException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XmlRpcResponseParser(org.apache.xmlrpc.parser.XmlRpcResponseParser) SAXException(org.xml.sax.SAXException)

Aggregations

InputSource (org.xml.sax.InputSource)1124 StringReader (java.io.StringReader)401 IOException (java.io.IOException)304 Document (org.w3c.dom.Document)282 SAXException (org.xml.sax.SAXException)281 DocumentBuilder (javax.xml.parsers.DocumentBuilder)262 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)213 XMLReader (org.xml.sax.XMLReader)194 Test (org.junit.Test)160 InputStream (java.io.InputStream)158 NodeList (org.w3c.dom.NodeList)145 Element (org.w3c.dom.Element)144 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)143 ByteArrayInputStream (java.io.ByteArrayInputStream)103 SAXParser (javax.xml.parsers.SAXParser)103 SAXSource (javax.xml.transform.sax.SAXSource)95 SAXParserFactory (javax.xml.parsers.SAXParserFactory)90 File (java.io.File)82 Node (org.w3c.dom.Node)82 URL (java.net.URL)65