Search in sources :

Example 71 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler 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 72 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler 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)

Example 73 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project tomee by apache.

the class ReadDescriptors method readOpenejbJar.

private void readOpenejbJar(final EjbModule ejbModule) throws OpenEJBException {
    final Source source = getSource(ejbModule.getAltDDs().get("openejb-jar.xml"));
    if (source != null) {
        try {
            // Attempt to parse it first as a v3 descriptor
            final OpenejbJar openejbJar = JaxbOpenejbJar3.unmarshal(OpenejbJar.class, source.get()).postRead();
            ejbModule.setOpenejbJar(openejbJar);
        } catch (final Exception v3ParsingException) {
            // Attempt to parse it second as a v2 descriptor
            final OpenejbJar openejbJar = new OpenejbJar();
            ejbModule.setOpenejbJar(openejbJar);
            try {
                final JAXBElement element = (JAXBElement) JaxbOpenejbJar2.unmarshal(OpenejbJarType.class, source.get());
                final OpenejbJarType o2 = (OpenejbJarType) element.getValue();
                ejbModule.getAltDDs().put("openejb-jar.xml", o2);
                final GeronimoEjbJarType g2 = OpenEjb2Conversion.convertToGeronimoOpenejbXml(o2);
                ejbModule.getAltDDs().put("geronimo-openejb.xml", g2);
            } catch (final Exception v2ParsingException) {
                // Now we have to determine which error to throw; the v3 file exception or the fallback v2 file exception.
                final Exception[] realIssue = { v3ParsingException };
                try {
                    final SAXParserFactory factory = Saxs.namespaceAwareFactory();
                    final SAXParser parser = factory.newSAXParser();
                    parser.parse(source.get(), new DefaultHandler() {

                        public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
                            if (localName.equals("environment")) {
                                realIssue[0] = v2ParsingException;
                                throw new SAXException("Throw exception to stop parsing");
                            }
                            if (uri == null) {
                                return;
                            }
                            if (uri.contains("openejb-jar-2.") || uri.contains("geronimo.apache.org/xml/ns")) {
                                realIssue[0] = v2ParsingException;
                                throw new SAXException("Throw exception to stop parsing");
                            }
                        }
                    });
                } catch (final Exception dontCare) {
                // no-op
                }
                String filePath = "<error: could not be written>";
                try {
                    File tempFile;
                    try {
                        tempFile = File.createTempFile("openejb-jar-", ".xml");
                    } catch (final Throwable e) {
                        final File tmp = new File("tmp");
                        if (!tmp.exists() && !tmp.mkdirs()) {
                            throw new IOException("Failed to create local tmp directory: " + tmp.getAbsolutePath());
                        }
                        tempFile = File.createTempFile("openejb-jar-", ".xml", tmp);
                    }
                    try {
                        IO.copy(source.get(), tempFile);
                    } catch (final IOException e) {
                    // no-op
                    }
                    filePath = tempFile.getAbsolutePath();
                } catch (final IOException e) {
                // no-op
                }
                final Exception e = realIssue[0];
                if (e instanceof SAXException) {
                    throw new OpenEJBException("Cannot parse the openejb-jar.xml. Xml content written to: " + filePath, e);
                } else if (e instanceof JAXBException) {
                    throw new OpenEJBException("Cannot unmarshall the openejb-jar.xml. Xml content written to: " + filePath, e);
                } else if (e instanceof IOException) {
                    throw new OpenEJBException("Cannot read the openejb-jar.xml.", e);
                } else {
                    throw new OpenEJBException("Encountered unknown error parsing the openejb-jar.xml.", e);
                }
            }
        }
    }
    final Source source1 = getSource(ejbModule.getAltDDs().get("geronimo-openejb.xml"));
    if (source1 != null) {
        try {
            GeronimoEjbJarType geronimoEjbJarType = null;
            final Object o = JaxbOpenejbJar2.unmarshal(GeronimoEjbJarType.class, source1.get());
            if (o instanceof GeronimoEjbJarType) {
                geronimoEjbJarType = (GeronimoEjbJarType) o;
            } else if (o instanceof JAXBElement) {
                final JAXBElement element = (JAXBElement) o;
                geronimoEjbJarType = (GeronimoEjbJarType) element.getValue();
            }
            if (geronimoEjbJarType != null) {
                final Object nested = geronimoEjbJarType.getOpenejbJar();
                if (nested != null && nested instanceof OpenejbJar) {
                    final OpenejbJar existingOpenejbJar = ejbModule.getOpenejbJar();
                    if (existingOpenejbJar == null || existingOpenejbJar.getEjbDeploymentCount() <= 0) {
                        final OpenejbJar openejbJar = (OpenejbJar) nested;
                        ejbModule.getAltDDs().put("openejb-jar.xml", openejbJar);
                        ejbModule.setOpenejbJar(openejbJar);
                    }
                }
                ejbModule.getAltDDs().put("geronimo-openejb.xml", geronimoEjbJarType);
            }
        } catch (final Exception e) {
            throw new OpenEJBException("Failed parsing geronimo-openejb.xml", e);
        }
    }
}
Also used : OpenejbJarType(org.apache.openejb.jee.oejb2.OpenejbJarType) OpenEJBException(org.apache.openejb.OpenEJBException) JAXBException(javax.xml.bind.JAXBException) Attributes(org.xml.sax.Attributes) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException) InputSource(org.xml.sax.InputSource) OpenEJBException(org.apache.openejb.OpenEJBException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) GeronimoEjbJarType(org.apache.openejb.jee.oejb2.GeronimoEjbJarType) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) SAXParser(javax.xml.parsers.SAXParser) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 74 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project tomee by apache.

the class Report method main.

private void main() throws Exception {
    // final File file = new File("/Users/dblevins/work/uber/geronimo-tck-public-trunk/jcdi-tck-runner/target/surefire-reports/testng-results.xml");
    final File file = new File("/Users/dblevins/work/all/trunk/openejb/tck/cdi-tomee/target/failsafe-reports/testng-results.xml");
    // final File file = new File("/Users/dblevins/work/uber/testng-results.xml");
    final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    parser.parse(file, new DefaultHandler() {

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            final String name = qName;
            if ("class".equals(name)) {
                classes.add(new TestClass(attributes.getValue("name")));
            }
            if ("test-method".equals(name)) {
                classes.getLast().addStatus(attributes.getValue("status"), attributes.getValue("name"));
            }
        }
    });
    Collections.sort(classes);
    textReport(file);
    passingXml(file);
    failingXml(file);
}
Also used : Attributes(org.xml.sax.Attributes) SAXParser(javax.xml.parsers.SAXParser) File(java.io.File) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException)

Example 75 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project android_frameworks_base by crdroidandroid.

the class ExpatPerformanceTest method runSax.

private void runSax() throws IOException, SAXException {
    long start = System.currentTimeMillis();
    Xml.parse(newInputStream(), Xml.Encoding.UTF_8, new DefaultHandler());
    long elapsed = System.currentTimeMillis() - start;
    Log.i(TAG, "expat SAX: " + elapsed + "ms");
}
Also used : DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Aggregations

DefaultHandler (org.xml.sax.helpers.DefaultHandler)148 InputStream (java.io.InputStream)65 Metadata (org.apache.tika.metadata.Metadata)59 ParseContext (org.apache.tika.parser.ParseContext)52 Test (org.junit.Test)44 Attributes (org.xml.sax.Attributes)41 SAXParser (javax.xml.parsers.SAXParser)40 SAXException (org.xml.sax.SAXException)39 ByteArrayInputStream (java.io.ByteArrayInputStream)32 SAXParserFactory (javax.xml.parsers.SAXParserFactory)29 IOException (java.io.IOException)26 InputSource (org.xml.sax.InputSource)23 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)22 Parser (org.apache.tika.parser.Parser)22 TikaInputStream (org.apache.tika.io.TikaInputStream)20 ContentHandler (org.xml.sax.ContentHandler)20 File (java.io.File)19 AutoDetectParser (org.apache.tika.parser.AutoDetectParser)17 BodyContentHandler (org.apache.tika.sax.BodyContentHandler)16 FileInputStream (java.io.FileInputStream)15