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.omg.bpmn.miwg.bpmn2_0.comparison.Bpmn20ConformanceChecker.java

public List<Difference> getSignificantDifferences(InputStream expectedBpmnXml, InputStream actualBpmnXml)
        throws IOException, ParserConfigurationException {
    try {//  ww w  .j  a v a2s .  c o  m
        Document expectedBpmnXmlDoc = docBuilder.parse(expectedBpmnXml);
        Document actualBpmnXmlDoc = docBuilder.parse(actualBpmnXml);
        return xmlDiff.areDocumentsEqualReporting(expectedBpmnXmlDoc, actualBpmnXmlDoc);
    } catch (SAXException e) {
        throw new IOException(e.getMessage(), e.getCause());
    }
}

From source file:com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptorParserTest.java

@Test
public void shouldValidatePluginVersion() throws Exception {
    InputStream pluginXml = IOUtils.toInputStream("<go-plugin version=\"10\"></go-plugin>");
    try {//from   ww  w .j a  v  a  2s . co  m
        GoPluginDescriptorParser.parseXML(pluginXml, "/tmp/", new File("/tmp/"), true);
        fail("xsd validation should have failed");
    } catch (SAXException e) {
        assertThat(e.getMessage(), is("XML Schema validation of Plugin Descriptor(plugin.xml) failed"));
        assertThat(e.getCause().getMessage(),
                contains("Value '10' of attribute 'version' of element 'go-plugin' is not valid"));
    }
}

From source file:com.webcohesion.ofx4j.io.BaseOFXReader.java

/**
 * Parse an OFX version 2 stream from the first OFX element (defined by the {@link #getFirstElementStart() first element characters}).
 *
 * @param reader The reader./*from   w w  w.java 2  s.  co m*/
 */
protected void parseV2FromFirstElement(Reader reader) throws IOException, OFXParseException {
    try {
        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setFeature("http://xml.org/sax/features/namespaces", false);
        xmlReader.setContentHandler(new OFXV2ContentHandler(getContentHandler()));
        xmlReader.parse(new InputSource(reader));
    } catch (SAXException e) {
        if (e.getCause() instanceof OFXParseException) {
            throw (OFXParseException) e.getCause();
        }

        throw new OFXParseException(e);
    }
}

From source file:com.myjeeva.poi.ExcelReader.java

private void read(int sheetNumber) throws RuntimeException {
    ReadOnlySharedStringsTable strings;//from  ww  w  . ja  va2s. co  m
    try {
        strings = new ReadOnlySharedStringsTable(this.xlsxPackage);
        XSSFReader xssfReader = new XSSFReader(this.xlsxPackage);
        StylesTable styles = xssfReader.getStylesTable();
        XSSFReader.SheetIterator worksheets = (XSSFReader.SheetIterator) xssfReader.getSheetsData();

        for (int sheetIndex = 0; worksheets.hasNext(); sheetIndex++) {
            InputStream stream = worksheets.next();
            if (null != sheetCallback)
                this.sheetCallback.startSheet(sheetIndex, worksheets.getSheetName());

            if ((READ_ALL == sheetNumber) || (sheetIndex == sheetNumber)) {
                readSheet(styles, strings, stream);
            }
            IOUtils.closeQuietly(stream);

            if (null != sheetCallback)
                this.sheetCallback.endSheet();
        }
    } catch (IOException ioe) {
        LOG.error(ioe.getMessage(), ioe.getCause());
    } catch (SAXException se) {
        LOG.error(se.getMessage(), se.getCause());
    } catch (OpenXML4JException oxe) {
        LOG.error(oxe.getMessage(), oxe.getCause());
    } catch (ParserConfigurationException pce) {
        LOG.error(pce.getMessage(), pce.getCause());
    }
}

From source file:com.myjeeva.poi.ExcelReader.java

private void read(String sheetName) throws RuntimeException {
    ReadOnlySharedStringsTable strings;/*from ww  w . j a v  a 2s.c  o m*/
    try {
        strings = new ReadOnlySharedStringsTable(this.xlsxPackage);
        XSSFReader xssfReader = new XSSFReader(this.xlsxPackage);
        StylesTable styles = xssfReader.getStylesTable();
        XSSFReader.SheetIterator worksheets = (XSSFReader.SheetIterator) xssfReader.getSheetsData();

        for (int sheetIndex = 0; worksheets.hasNext(); sheetIndex++) {
            InputStream stream = worksheets.next();
            if (null != sheetCallback)
                this.sheetCallback.startSheet(sheetIndex, worksheets.getSheetName());

            if (sheetName.equals(worksheets.getSheetName())) {
                readSheet(styles, strings, stream);
            }
            IOUtils.closeQuietly(stream);

            if (null != sheetCallback)
                this.sheetCallback.endSheet();
        }
    } catch (IOException ioe) {
        LOG.error(ioe.getMessage(), ioe.getCause());
    } catch (SAXException se) {
        LOG.error(se.getMessage(), se.getCause());
    } catch (OpenXML4JException oxe) {
        LOG.error(oxe.getMessage(), oxe.getCause());
    } catch (ParserConfigurationException pce) {
        LOG.error(pce.getMessage(), pce.getCause());
    }
}

From source file:de.ii.xtraplatform.ogc.api.gml.parser.GMLSchemaParser.java

public void parse(InputSource is, Map<String, List<String>> elements, boolean lax) {
    //LOGGER.debug("Parsing GML application schema");
    XSOMParser parser = new XSOMParser();

    try {//from  w w w .  j  a v  a  2s .  c om
        parser.setErrorHandler(new GMLSchemaParserErrorHandler());

        parser.setEntityResolver(entityResolver);

        is.setSystemId(baseURI.toString());

        parser.parse(is);

        XSSchemaSet schemas = parser.getResult();

        for (GML.VERSION version : GML.VERSION.values()) {
            XSSchema schema0 = schemas.getSchema(GML.getWord(version, GML.NAMESPACE.URI));
            if (schema0 != null) {
                XSElementDecl a = schema0.getElementDecl(GML.getWord(version, GML.VOCABULARY.ABSTRACT_OBJECT));
                if (a != null) {
                    abstractObjectDecl.add(a);
                }
            }
        }

        XSSchema schema1 = schemas.getSchema("http://www.isotc211.org/2005/gco");
        if (schema1 != null) {
            gcoObjectType = schema1.getElementDecl("AbstractObject").getType();
        }

        for (Map.Entry<String, List<String>> ns : elements.entrySet()) {
            String nsuri = ns.getKey();
            String oldNsUri = null;
            //LOGGER.debug("namespace {}", nsuri);

            XSSchema schema = schemas.getSchema(nsuri);

            // workaround for broken WFSs where FeatureTypes are in different namespaces in Capabilities and Schema
            // in this case we search in the targetNamespace of the Schema
            if (schema == null && lax) {
                LOGGER.info(
                        "Schema for Namespace '{}' not found, searching in targetNamespace schema instead. ",
                        ns.getKey());

                // looks as if the schema for the targetNamespace of the document is always second in the list
                schema = schemas.getSchema(1);
                oldNsUri = nsuri;
                nsuri = schema.getTargetNamespace();
            }

            for (String e : ns.getValue()) {
                XSElementDecl elem = schema.getElementDecl(e);
                if (elem != null && elem.getType().isComplexType()) {
                    //LOGGER.debug(" - element {}, type: {}", elem.getName(), elem.getType().getName());

                    for (GMLSchemaAnalyzer analyzer : analyzers) {
                        if (oldNsUri != null) {
                            analyzer.analyzeNamespaceRewrite(oldNsUri, nsuri, elem.getName());
                        }
                        analyzer.analyzeFeatureType(nsuri, elem.getName());
                        for (XSAttributeUse att : elem.getType().asComplexType().getAttributeUses()) {
                            //LOGGER.debug("   - attribute {}, required: {}, type: {}, ns: {}", att.getDecl().getName(), att.isRequired(), att.getDecl().getType().getName(), att.getDecl().getTargetNamespace());

                            analyzer.analyzeAttribute(att.getDecl().getTargetNamespace(),
                                    att.getDecl().getName(), att.getDecl().getType().getName(),
                                    att.isRequired());
                        }
                    }
                    XSParticle particle = elem.getType().asComplexType().getContentType().asParticle();
                    if (particle != null) {
                        XSTerm term = particle.getTerm();
                        if (term.isModelGroup()) {
                            complexTypes = new HashSet<String>();
                            parseGroup(term.asModelGroup(), 1, false);
                        }
                    }
                }
            }
        }
    } catch (SAXException ex) {

        // File included in schema not found
        if (ex.getCause() != null && ex.getCause().getClass().getName().contains("FileNotFoundException")) {
            LOGGER.error(
                    "The GML application schema provided by the WFS imports schema '{}', but that schema cannot be accessed. A valid GML application schema is required to determine the layers of the proxy service and its characteristics.. Please contact the WFS provider to correct the schema error.",
                    ex.getCause().getMessage());
            throw new SchemaParseException(
                    "The GML application schema provided by the WFS imports schema '{}', but that schema cannot be accessed. A valid GML application schema is required to determine the layers of the proxy service and its characteristics.. Please contact the WFS provider to correct the schema error.",
                    ex.getCause().getMessage());
        }

        String msg = ex.getMessage();
        String msgex = "";
        if (msg != null && !msg.isEmpty()) {
            msg = "Parser details: " + msg;

            msgex = msg.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
        }

        LOGGER.error(
                "The GML application schema provided by the WFS is invalid. A valid GML application schema is required to determine the layers of the proxy service and its characteristics.. Please contact the WFS provider to correct the schema error. {}",
                msg);
        SchemaParseException spe = new SchemaParseException(
                "The GML application schema provided by the WFS is invalid. A valid GML application schema is required to determine the layers of the proxy service and its characteristics.. Please contact the WFS provider to correct the schema error. {}",
                "");

        spe.addDetail(msgex);

        throw spe;
    }
}

From source file:com.nidhinova.tika.server.TikaService.java

/**
 * Serves HTTP PUT Returns metadata formatted as json or plain text content
 * of the file//from   ww w . j a v  a  2s .  c o  m
 * 
 * @param filename
 * @param pathkey
 *            (JNDI lookup key)
 * @param opkey
 *            (can be "text" or "metadata")
 * @param httpHeaders
 * @return
 * @throws Exception
 */

@PUT
@Consumes("*/*")
@Produces({ MediaType.APPLICATION_JSON })
@Path("/{opkey}")
public StreamingOutput getMetadata(final InputStream is, @PathParam("opkey") final String opkey,
        @Context HttpHeaders httpHeaders) throws Exception {
    final Detector detector = createDetector(httpHeaders);
    final AutoDetectParser parser = new AutoDetectParser(detector);
    final ParseContext context = new ParseContext();
    context.set(Parser.class, parser);
    final org.apache.tika.metadata.Metadata metadata = new org.apache.tika.metadata.Metadata();
    setMetadataFromHeader(parser, metadata, httpHeaders);

    return new StreamingOutput() {
        public void write(OutputStream outputStream) throws IOException, WebApplicationException {

            StringWriter textBuffer = new StringWriter();

            ContentHandler handler = null;
            if (opkey.equalsIgnoreCase("metadata")) {
                handler = new DefaultHandler();
            } else if (opkey.equalsIgnoreCase("text") || opkey.equalsIgnoreCase("fulldata")) {
                handler = new BodyContentHandler(textBuffer);
            }
            try {
                parser.parse(new BufferedInputStream(is), handler, metadata, context);
                String contentEncoding = (metadata
                        .get(org.apache.tika.metadata.HttpHeaders.CONTENT_TYPE) == null ? "UTF-8"
                                : metadata.get(org.apache.tika.metadata.HttpHeaders.CONTENT_TYPE));
                Writer outWriter = getOutputWriter(outputStream, contentEncoding);

                //metadata is always gathered
                // munch tika metadata object it to make json
                String jsonMetadata = JSONHelper.metadataToJson(metadata);

                if (opkey.equalsIgnoreCase("metadata")) {
                    outWriter.write("{\"metadata\":" + jsonMetadata + "}");
                } else if (opkey.equalsIgnoreCase("text")) {
                    // write it out
                    outWriter.write("{ \"text\":" + JSONHelper.toJSON(textBuffer.toString()) + " }");
                } else if (opkey.equalsIgnoreCase("fulldata")) {
                    StringBuilder data = new StringBuilder();
                    data.append("{ \"metadata\":" + jsonMetadata).append(", ")
                            .append("\"text\":" + JSONHelper.toJSON(textBuffer.toString()) + " }");
                    outWriter.write(data.toString());
                }
                outWriter.flush();
            } catch (SAXException e) {
                throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
            } 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("Text extraction failed", e);

                throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
            }
        }
    };

}

From source file:com.nidhinova.tika.server.TikaService.java

/**
 * Serves HTTP GET Returns metadata formatted as json or plain text content
 * of the file. File should be locally accessible for Tika Server using
 * pathkey JNDI//from  w  w w  .  j a v a 2  s.  c o m
 * 
 * @param filename
 * @param pathkey
 *            (JNDI lookup key)
 * @param opkey
 *            (can be "text" or "metadata" or "fulldata")
 * @param httpHeaders
 * @return
 * @throws Exception
 */
@GET
@Produces({ MediaType.APPLICATION_JSON })
@Path("/{opkey}/{pathkey}/{resourceid: .*}")
public StreamingOutput getMetadata(@javax.ws.rs.core.Context javax.ws.rs.core.UriInfo uriInfo,
        @PathParam("opkey") final String opkey, @PathParam("pathkey") final String pathkey,
        @PathParam("resourceid") final String resourceId, @Context HttpHeaders httpHeaders) throws Exception {

    // get the resource segment, this may have query params
    // we are ok with it as long as we can get something at that location
    String[] segments = uriInfo.getRequestUri().toASCIIString().split("/" + opkey + "/" + pathkey + "/");
    final String filename = segments[segments.length - 1];
    logger.info("resource :" + segments[segments.length - 1]);

    final Detector detector = createDetector(httpHeaders);
    final AutoDetectParser parser = new AutoDetectParser(detector);
    final ParseContext context = new ParseContext();
    context.set(Parser.class, parser);
    final org.apache.tika.metadata.Metadata metadata = new org.apache.tika.metadata.Metadata();
    setMetadataFromHeader(parser, metadata, httpHeaders);

    URL url = null;
    try {
        if (pathkey != null && resourceId != null) {
            String filepath = getFilePath(pathkey) + filename;
            File file = new File(filepath);
            if (file.isFile()) {
                url = file.toURI().toURL();
            } else {
                url = new URL(filepath);
            }
        }
    } catch (MalformedURLException mex) {
        throw new WebApplicationException(Response.Status.NOT_FOUND);
    }

    final InputStream is = TikaInputStream.get(url, metadata);

    return new StreamingOutput() {
        public void write(OutputStream outputStream) throws IOException, WebApplicationException {

            StringWriter textBuffer = new StringWriter();
            ContentHandler handler = null;
            if (opkey.equalsIgnoreCase("metadata")) {
                handler = new DefaultHandler();
            } else if (opkey.equalsIgnoreCase("text") || opkey.equalsIgnoreCase("fulldata")) {
                handler = new BodyContentHandler(textBuffer);
            }
            try {

                parser.parse(is, handler, metadata, context);

                String contentEncoding = (metadata
                        .get(org.apache.tika.metadata.HttpHeaders.CONTENT_ENCODING) == null ? "UTF-8"
                                : metadata.get(org.apache.tika.metadata.HttpHeaders.CONTENT_ENCODING));

                logger.info("Content encoding: "
                        + metadata.get(org.apache.tika.metadata.HttpHeaders.CONTENT_ENCODING));

                Writer outWriter = getOutputWriter(outputStream, contentEncoding);

                //metadata is always gathered
                // munch tika metadata object it to make json
                String jsonMetadata = JSONHelper.metadataToJson(metadata);

                if (opkey.equalsIgnoreCase("metadata")) {
                    outWriter.write("{\"metadata\":" + jsonMetadata + "}");
                } else if (opkey.equalsIgnoreCase("text")) {
                    // write it out
                    outWriter.write("{ \"text\":" + JSONHelper.toJSON(textBuffer.toString()) + " }");
                } else if (opkey.equalsIgnoreCase("fulldata")) {
                    StringBuilder data = new StringBuilder();
                    data.append("{ \"metadata\":" + jsonMetadata).append(", ")
                            .append("\"text\":" + JSONHelper.toJSON(textBuffer.toString()) + " }");
                    outWriter.write(data.toString());
                }
                outWriter.flush();
            } catch (SAXException e) {
                throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
            } 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("Text extraction failed", e);

                throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
            }
        }
    };

}

From source file:net.yacy.document.parser.GenericXMLParser.java

/**
 * {@inheritDoc}//from   w ww .ja v  a 2 s .  com
 * @param maxBytes the maximum number of content bytes to process. Be careful with to small values : 
 *    a Failure exception can eventually be thrown when maxBytes value is so small that the parser can even not fill its buffers on input stream and parse the document declaration.
 */
@Override
public Document[] parseWithLimits(DigestURL location, String mimeType, String charsetName,
        VocabularyScraper scraper, int timezoneOffset, InputStream source, int maxLinks, long maxBytes)
        throws Failure, InterruptedException, UnsupportedOperationException {
    /* Limit the size of the in-memory buffer to at most 25% of the available memory :
     * because some room is needed, and before being garbage collected the buffer will be converted to a String, then to a byte array. 
     * Eventual stricter limits should be handled by the caller (see for example crawler.[protocol].maxFileSize configuration setting). */
    final long availableMemory = MemoryControl.available();
    final long maxTextBytes = (long) (availableMemory * 0.25);
    final int maxChars;
    if ((maxTextBytes / Character.BYTES) > Integer.MAX_VALUE) {
        maxChars = Integer.MAX_VALUE;
    } else {
        maxChars = ((int) maxTextBytes) / Character.BYTES;
    }

    try (/* Automatically closed by this try-with-resources statement*/ CharBuffer writer = new CharBuffer(
            maxChars);) {

        final Set<AnchorURL> detectedURLs = new HashSet<>();
        final GenericXMLContentHandler saxHandler = new GenericXMLContentHandler(writer, detectedURLs,
                maxLinks);

        StrictLimitInputStream limitedSource = new StrictLimitInputStream(source, maxBytes);

        /* Use commons-io XmlStreamReader advanced rules to help with charset detection when source contains no BOM or XML declaration
         * (detection algorithm notably also include ContentType transmitted by HTTP headers, here eventually present as mimeType and charset parameters),  */
        final XmlStreamReader reader = new XmlStreamReader(limitedSource, mimeType, true, charsetName);
        final InputSource saxSource = new InputSource(reader);
        final String detectedCharset = reader.getEncoding();

        final SAXParser saxParser = getParser();
        boolean limitExceeded = false;
        try {
            saxParser.parse(saxSource, saxHandler);
        } catch (SAXException e) {
            if (!(e.getCause() instanceof SizeLimitExceededException)) {
                /* Only transmit to upper layer exceptions that are not caused by the maxLinks limit being reached */
                throw e;
            }
            limitExceeded = true;
        } catch (StreamLimitException e) {
            limitExceeded = true;
        }

        if (writer.isOverflow()) {
            throw new Parser.Failure("Not enough Memory available for generic the XML parser : "
                    + Formatter.bytesToString(availableMemory), location);
        }

        /* Create the parsed document with eventually only partial part of the text and links */
        final byte[] contentBytes = UTF8.getBytes(writer.toString());
        Document[] docs = new Document[] {
                new Document(location, mimeType, detectedCharset, this, null, null, null, null, "", null, null,
                        0.0d, 0.0d, contentBytes, detectedURLs, null, null, false, new Date()) };
        docs[0].setPartiallyParsed(limitExceeded);
        return docs;
    } catch (final Exception e) {
        throw new Parser.Failure("Unexpected error while parsing XML file. " + e.getMessage(), location);
    }
}

From source file:org.apache.fop.render.intermediate.IFParser.java

/**
 * Parses an intermediate file and paints it.
 * @param src the Source instance pointing to the intermediate file
 * @param documentHandler the intermediate format document handler used to process the IF events
 * @param userAgent the user agent// w w  w.j a  v  a 2s .co m
 * @throws TransformerException if an error occurs while parsing the area tree XML
 * @throws IFException if an IF-related error occurs inside the target document handler
 */
public void parse(Source src, IFDocumentHandler documentHandler, FOUserAgent userAgent)
        throws TransformerException, IFException {
    try {
        Transformer transformer = tFactory.newTransformer();
        transformer.setErrorListener(new DefaultErrorListener(log));

        SAXResult res = new SAXResult(getContentHandler(documentHandler, userAgent));

        transformer.transform(src, res);
    } catch (TransformerException te) {
        //Unpack original IFException if applicable
        if (te.getCause() instanceof SAXException) {
            SAXException se = (SAXException) te.getCause();
            if (se.getCause() instanceof IFException) {
                throw (IFException) se.getCause();
            }
        } else if (te.getCause() instanceof IFException) {
            throw (IFException) te.getCause();
        }
        throw te;
    }
}