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");
}
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);
}
}
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);
}
}
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");
}
}
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);
}
}
Aggregations