Example usage for org.xml.sax SAXException getCause

List of usage examples for org.xml.sax SAXException getCause

Introduction

In this page you can find the example usage for org.xml.sax SAXException getCause.

Prototype

public Throwable getCause() 

Source Link

Document

Return the cause of the exception

Usage

From source file:org.apache.tika.server.TikaResource.java

@PUT
@Consumes("*/*")/*  w w  w.j a v  a  2 s .  c om*/
@Produces("text/plain")
public StreamingOutput getText(final InputStream is, @Context HttpHeaders httpHeaders,
        @Context final UriInfo info) {
    final AutoDetectParser parser = createParser();
    final Metadata metadata = new Metadata();

    fillMetadata(parser, metadata, httpHeaders);

    logRequest(logger, info, metadata);

    return new StreamingOutput() {
        public void write(OutputStream outputStream) throws IOException, WebApplicationException {
            BodyContentHandler body = new BodyContentHandler(new WriteOutContentHandler(outputStream) {
                @Override
                public void startElement(String uri, String localName, String qName, Attributes attributes)
                        throws SAXException {
                    super.startElement(uri, localName, qName, attributes);

                    if ("img".equals(localName) && attributes.getValue("alt") != null) {
                        String nfo = "[image: " + attributes.getValue("alt") + ']';

                        characters(nfo.toCharArray(), 0, nfo.length());
                    }

                    if ("a".equals(localName) && attributes.getValue("name") != null) {
                        String nfo = "[bookmark: " + attributes.getValue("name") + ']';

                        characters(nfo.toCharArray(), 0, nfo.length());
                    }
                }
            });

            TikaInputStream tis = TikaInputStream.get(is);

            try {
                tis.getFile();

                parser.parse(tis, body, metadata);
            } catch (SAXException e) {
                throw new WebApplicationException(e);
            } catch (TikaException e) {
                if (e.getCause() != null && e.getCause() instanceof WebApplicationException) {
                    throw (WebApplicationException) e.getCause();
                }

                if (e.getCause() != null && e.getCause() instanceof IllegalStateException) {
                    throw new WebApplicationException(Response.status(422).build());
                }

                if (e.getCause() != null && e.getCause() instanceof EncryptedDocumentException) {
                    throw new WebApplicationException(Response.status(422).build());
                }

                if (e.getCause() != null && e.getCause() instanceof OldWordFileFormatException) {
                    throw new WebApplicationException(Response.status(422).build());
                }

                logger.warn(String.format("%s: Text extraction failed", info.getPath()), e);

                throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
            } finally {
                tis.close();
            }
        }
    };
}

From source file:org.drools.compiler.PackageBuilder.java

/**
 * Load a rule package from XML source.//from  w  w w.  j  a v a  2s.  c  o m
 * @param reader
 * @throws DroolsParserException
 * @throws IOException
 */
public void addPackageFromXml(final Reader reader) throws DroolsParserException, IOException {
    final XmlPackageReader xmlReader = new XmlPackageReader();

    try {
        xmlReader.read(reader);
    } catch (final SAXException e) {
        throw new DroolsParserException(e.getCause());
    }

    addPackage(xmlReader.getPackageDescr());
}

From source file:org.ebayopensource.turmeric.tools.codegen.AbstractServiceGeneratorTestCase.java

protected void assertXML(String expectedPath, String actualPath, String[] attNames) {
    XMLUnit.setIgnoreComments(true);//from   w  ww  . j a  va2s . co m
    XMLUnit.setIgnoreWhitespace(true);
    try {
        Diff d = new Diff(readFileAsString(expectedPath), readFileAsString(actualPath));

        if (attNames == null) {
            d.overrideElementQualifier(new ElementNameAndAttributeQualifier());
        } else
            d.overrideElementQualifier(new ElementNameAndAttributeQualifier(attNames));
        DetailedDiff dd = new DetailedDiff(d);
        List l = dd.getAllDifferences();

        for (Iterator i = l.iterator(); i.hasNext();) {
            Difference di = (Difference) i.next();
            System.err.println(di);
        }

        Assert.assertTrue(d.similar());
    } catch (SAXException e) {

        Assert.fail("XML assert failed because of" + e.getMessage() + " and cause " + e.getCause());
    } catch (IOException e) {
        Assert.fail("XML assert failed because of" + e.getMessage() + " and cause " + e.getCause());
    }
}

From source file:org.exist.xquery.modules.exi.EncodeExiFunction.java

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }/*  www  . j  av a2s  .co  m*/
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        EXISerializer exiSerializer = null;
        if (args.length > 1) {
            if (!args[1].isEmpty()) {
                Item xsdItem = args[1].itemAt(0);
                InputStream xsdInputStream = EXIUtils.getInputStream(xsdItem, context);
                exiSerializer = new EXISerializer(baos, xsdInputStream);
            } else {
                exiSerializer = new EXISerializer(baos);
            }
        } else {
            exiSerializer = new EXISerializer(baos);
        }
        Item inputNode = args[0].itemAt(0);
        exiSerializer.startDocument();
        inputNode.toSAX(context.getBroker(), exiSerializer, new Properties());
        exiSerializer.endDocument();
        return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(),
                new ByteArrayInputStream(baos.toByteArray()));
    } catch (IOException ioex) {
        // TODO - test!
        throw new XPathException(this, ErrorCodes.FODC0002, ioex.getMessage());
    } catch (EXIException exie) {
        throw new XPathException(this, new JavaErrorCode(exie.getCause()), exie.getMessage());
    } catch (SAXException saxe) {
        throw new XPathException(this, new JavaErrorCode(saxe.getCause()), saxe.getMessage());
    }
}

From source file:org.jts.gui.importJSIDL.Import.java

public Import() {
    try {//from   ww  w. j  av  a 2  s .  c om
        if (um == null) {
            JAXBContext jc = JAXBContext.newInstance("org.jts.jsidl.binding");
            um = jc.createUnmarshaller();
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(new File(sourceSchema));
            um.setSchema(schema);
        }
    } catch (JAXBException jaxbe) {
        throw new GUIError(jaxbe.getCause());
    } catch (SAXException saxe) {
        throw new GUIError(saxe.getCause());
    }
}

From source file:org.tridas.io.formats.tridasjson.TridasJSONFile.java

public void validate() throws ImpossibleConversionException {
    Schema schema = null;// w ww  .  j a v a2 s  .  c o m

    // Validate output against schema first
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    URL file = IOUtils.getFileInJarURL("schemas/tridas.xsd");
    if (file == null) {
        log.error("Could not find schema file");
    } else {
        try {
            schema = factory.newSchema(file);
        } catch (SAXException e) {
            log.error("Error getting TRiDaS schema for validation, not using.", e);
            throw new ImpossibleConversionException(I18n.getText("fileio.errorGettingSchema"));
        }
    }

    swriter = new StringWriter();
    // Marshaller code goes here...
    JAXBContext jc;
    try {
        jc = JAXBContext.newInstance("org.tridas.schema");
        Marshaller m = jc.createMarshaller();
        m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new TridasNamespacePrefixMapper());
        if (schema != null) {
            m.setSchema(schema);
        }
        m.marshal(getTridasContainer(), swriter);

    } catch (Exception e) {
        log.error("Jaxb error", e);

        String cause = e.getCause().getMessage();
        if (cause != null) {
            throw new ImpossibleConversionException(I18n.getText("fileio.jaxbError") + " " + cause);
        } else {
            throw new ImpossibleConversionException(I18n.getText("fileio.jaxbError"));
        }

    }

}