Search in sources :

Example 96 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project tika by apache.

the class AbstractPDF2XHTML method extractAcroForm.

void extractAcroForm(PDDocument pdf) throws IOException, SAXException, TikaException {
    //Thank you, Ben Litchfield, for org.apache.pdfbox.examples.fdf.PrintFields
    //this code derives from Ben's code
    PDDocumentCatalog catalog = pdf.getDocumentCatalog();
    if (catalog == null)
        return;
    PDAcroForm form = catalog.getAcroForm();
    if (form == null)
        return;
    //if it has xfa, try that.
    //if it doesn't exist or there's an exception,
    //go with traditional AcroForm
    PDXFAResource pdxfa = form.getXFA();
    if (pdxfa != null) {
        //if successful, return
        XFAExtractor xfaExtractor = new XFAExtractor();
        InputStream is = null;
        try {
            is = new BufferedInputStream(new ByteArrayInputStream(pdxfa.getBytes()));
        } catch (IOException e) {
            EmbeddedDocumentUtil.recordEmbeddedStreamException(e, metadata);
        }
        if (is != null) {
            try {
                xfaExtractor.extract(is, xhtml, metadata, context);
                return;
            } catch (XMLStreamException e) {
                //if there was an xml parse exception in xfa, try the AcroForm
                EmbeddedDocumentUtil.recordException(e, metadata);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    }
    @SuppressWarnings("rawtypes") List fields = form.getFields();
    if (fields == null)
        return;
    @SuppressWarnings("rawtypes") ListIterator itr = fields.listIterator();
    if (itr == null)
        return;
    xhtml.startElement("div", "class", "acroform");
    xhtml.startElement("ol");
    while (itr.hasNext()) {
        Object obj = itr.next();
        if (obj != null && obj instanceof PDField) {
            processAcroField((PDField) obj, 0);
        }
    }
    xhtml.endElement("ol");
    xhtml.endElement("div");
}
Also used : PDField(org.apache.pdfbox.pdmodel.interactive.form.PDField) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) TikaInputStream(org.apache.tika.io.TikaInputStream) InputStream(java.io.InputStream) PDAcroForm(org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm) IOException(java.io.IOException) ListIterator(java.util.ListIterator) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog) XMLStreamException(javax.xml.stream.XMLStreamException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PDXFAResource(org.apache.pdfbox.pdmodel.interactive.form.PDXFAResource) List(java.util.List) ArrayList(java.util.ArrayList)

Example 97 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project webservices-axiom by apache.

the class ByteArrayCustomBuilder method create.

@Override
public OMDataSource create(OMElement element) throws OMException {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        element.serializeAndConsume(baos);
        byte[] bytes = baos.toByteArray();
        return new ByteArrayDataSource(bytes, encoding);
    } catch (XMLStreamException e) {
        throw new OMException(e);
    } catch (OMException e) {
        throw e;
    } catch (Throwable t) {
        throw new OMException(t);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayDataSource(org.apache.axiom.om.ds.ByteArrayDataSource) OMException(org.apache.axiom.om.OMException)

Example 98 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project webservices-axiom by apache.

the class InputStreamDataSource method serialize.

@Override
public void serialize(OutputStream output, OMOutputFormat format) throws XMLStreamException {
    if (data == null) {
        throw new OMException("The InputStreamDataSource does not have a backing object");
    }
    String encoding = format.getCharSetEncoding();
    try {
        if (!data.encoding.equalsIgnoreCase(encoding)) {
            byte[] bytes = getXMLBytes(encoding);
            output.write(bytes);
        } else {
            // Write the input stream to the output stream
            inputStream2OutputStream(data.is, output);
        }
    } catch (UnsupportedEncodingException e) {
        throw new XMLStreamException(e);
    } catch (IOException e) {
        throw new XMLStreamException(e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) OMException(org.apache.axiom.om.OMException)

Example 99 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project webservices-axiom by apache.

the class TestNextAfterEndDocument method runTest.

protected void runTest() throws Throwable {
    XMLInputFactory factory = staxImpl.newNormalizedXMLInputFactory();
    XMLStreamReader reader = factory.createXMLStreamReader(new StringReader("<root/>"));
    while (reader.next() != XMLStreamReader.END_DOCUMENT) {
    // Just loop
    }
    try {
        reader.next();
        fail("Expected exception");
    } catch (IllegalStateException ex) {
    // Expected
    } catch (NoSuchElementException ex) {
    // This is also OK
    } catch (XMLStreamException ex) {
        fail("Expected IllegalStateException or NoSuchElementException");
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) NoSuchElementException(java.util.NoSuchElementException)

Example 100 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project webservices-axiom by apache.

the class ParserInputStreamDataSource method getXMLBytes.

@Override
public byte[] getXMLBytes(String encoding) {
    if (log.isDebugEnabled()) {
        log.debug("Entry ParserInputStreamDataSource.getXMLBytes(encoding)");
    }
    try {
        InputStream is = data.readParserInputStream();
        if (is != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            OMOutputFormat format = new OMOutputFormat();
            format.setCharSetEncoding(encoding);
            try {
                BufferUtils.inputStream2OutputStream(is, baos);
                if (log.isDebugEnabled()) {
                    log.debug("Exit ParserInputStreamDataSource.getXMLBytes(encoding)");
                }
                return baos.toByteArray();
            } catch (IOException e) {
                throw new OMException(e);
            }
        } else {
            //via SerializeAndConsume call
            if (log.isDebugEnabled()) {
                log.warn("Parser was already read, recovering by just returning new byte[0]");
                log.debug("Exit ParserInputStreamDataSource.getXMLBytes(encoding)");
            }
            return new byte[0];
        }
    } catch (XMLStreamException e) {
        throw new OMException(e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) BAAInputStream(org.apache.axiom.attachments.utils.BAAInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) OMException(org.apache.axiom.om.OMException)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)274 IOException (java.io.IOException)75 XMLStreamReader (javax.xml.stream.XMLStreamReader)74 XMLInputFactory (javax.xml.stream.XMLInputFactory)60 InputStream (java.io.InputStream)43 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)34 XMLEvent (javax.xml.stream.events.XMLEvent)24 StringReader (java.io.StringReader)22 JAXBException (javax.xml.bind.JAXBException)22 XMLEventReader (javax.xml.stream.XMLEventReader)19 ModelNode (org.jboss.dmr.ModelNode)18 ArrayList (java.util.ArrayList)16 XMLExtendedStreamReader (org.jboss.staxmapper.XMLExtendedStreamReader)15 ModelElement (org.wildfly.extension.picketlink.common.model.ModelElement)15 SAXException (org.xml.sax.SAXException)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 StringWriter (java.io.StringWriter)12 Unmarshaller (javax.xml.bind.Unmarshaller)11 Characters (javax.xml.stream.events.Characters)11