Search in sources :

Example 1 with LengthInputStream

use of org.apache.openejb.util.LengthInputStream in project tomee by apache.

the class ReadDescriptors method isEmpty.

private static boolean isEmpty(final InputStream is, final String rootElement) throws IOException, ParserConfigurationException, SAXException {
    final LengthInputStream in = new LengthInputStream(is);
    final InputSource inputSource = new InputSource(in);
    final SAXParser parser;
    final Thread thread = Thread.currentThread();
    final ClassLoader original = thread.getContextClassLoader();
    thread.setContextClassLoader(Saxs.class.getClassLoader());
    try {
        parser = Saxs.namespaceAwareFactory().newSAXParser();
    } finally {
        thread.setContextClassLoader(original);
    }
    try {
        parser.parse(inputSource, new DefaultHandler() {

            public void startElement(final String uri, final String localName, final String qName, final Attributes att) throws SAXException {
                if (!localName.equals(rootElement)) {
                    throw new SAXException(localName);
                }
            }

            public InputSource resolveEntity(final String publicId, final String systemId) throws IOException, SAXException {
                return new InputSource(new ByteArrayInputStream(new byte[0]));
            }
        });
        return true;
    } catch (final SAXException e) {
        return in.getLength() == 0;
    }
}
Also used : LengthInputStream(org.apache.openejb.util.LengthInputStream) InputSource(org.xml.sax.InputSource) ByteArrayInputStream(java.io.ByteArrayInputStream) Attributes(org.xml.sax.Attributes) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) Saxs(org.apache.openejb.util.Saxs) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException)

Example 2 with LengthInputStream

use of org.apache.openejb.util.LengthInputStream in project tomee by apache.

the class ReadDescriptors method getId.

private static String getId(final InputStream is) {
    final String[] id = { null };
    try {
        final LengthInputStream in = new LengthInputStream(is);
        final InputSource inputSource = new InputSource(in);
        final SAXParser parser = Saxs.namespaceAwareFactory().newSAXParser();
        parser.parse(inputSource, new DefaultHandler() {

            public void startElement(final String uri, final String localName, final String qName, final Attributes att) throws SAXException {
                id[0] = att.getValue("id");
            }

            public InputSource resolveEntity(final String publicId, final String systemId) throws IOException, SAXException {
                return new InputSource(new ByteArrayInputStream(new byte[0]));
            }
        });
    } catch (final Exception e) {
    // no-op
    }
    return id[0];
}
Also used : LengthInputStream(org.apache.openejb.util.LengthInputStream) InputSource(org.xml.sax.InputSource) ByteArrayInputStream(java.io.ByteArrayInputStream) Attributes(org.xml.sax.Attributes) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) OpenEJBException(org.apache.openejb.OpenEJBException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 SAXParser (javax.xml.parsers.SAXParser)2 LengthInputStream (org.apache.openejb.util.LengthInputStream)2 Attributes (org.xml.sax.Attributes)2 InputSource (org.xml.sax.InputSource)2 SAXException (org.xml.sax.SAXException)2 DefaultHandler (org.xml.sax.helpers.DefaultHandler)2 JAXBException (javax.xml.bind.JAXBException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 Saxs (org.apache.openejb.util.Saxs)1