Search in sources :

Example 11 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class XWPFRun method getCTPictures.

private List<CTPicture> getCTPictures(XmlObject o) {
    List<CTPicture> pics = new ArrayList<CTPicture>();
    XmlObject[] picts = o.selectPath("declare namespace pic='" + CTPicture.type.getName().getNamespaceURI() + "' .//pic:pic");
    for (XmlObject pict : picts) {
        if (pict instanceof XmlAnyTypeImpl) {
            // Pesky XmlBeans bug - see Bugzilla #49934
            try {
                pict = CTPicture.Factory.parse(pict.toString(), DEFAULT_XML_OPTIONS);
            } catch (XmlException e) {
                throw new POIXMLException(e);
            }
        }
        if (pict instanceof CTPicture) {
            pics.add((CTPicture) pict);
        }
    }
    return pics;
}
Also used : XmlException(org.apache.xmlbeans.XmlException) ArrayList(java.util.ArrayList) CTPicture(org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture) XmlAnyTypeImpl(org.apache.xmlbeans.impl.values.XmlAnyTypeImpl) XmlObject(org.apache.xmlbeans.XmlObject) POIXMLException(org.apache.poi.POIXMLException)

Example 12 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class XWPFStyles method onDocumentRead.

/**
     * Read document
     */
@Override
protected void onDocumentRead() throws IOException {
    StylesDocument stylesDoc;
    InputStream is = getPackagePart().getInputStream();
    try {
        stylesDoc = StylesDocument.Factory.parse(is, DEFAULT_XML_OPTIONS);
        setStyles(stylesDoc.getStyles());
        latentStyles = new XWPFLatentStyles(ctStyles.getLatentStyles(), this);
    } catch (XmlException e) {
        throw new POIXMLException("Unable to read styles", e);
    } finally {
        is.close();
    }
}
Also used : StylesDocument(org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument) InputStream(java.io.InputStream) XmlException(org.apache.xmlbeans.XmlException) POIXMLException(org.apache.poi.POIXMLException)

Example 13 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class TestXSSFFont method testCharSet.

@Test
public void testCharSet() throws IOException {
    CTFont ctFont = CTFont.Factory.newInstance();
    CTIntProperty prop = ctFont.addNewCharset();
    prop.setVal(FontCharset.ANSI.getValue());
    ctFont.setCharsetArray(0, prop);
    XSSFFont xssfFont = new XSSFFont(ctFont);
    assertEquals(Font.ANSI_CHARSET, xssfFont.getCharSet());
    xssfFont.setCharSet(FontCharset.DEFAULT);
    assertEquals(FontCharset.DEFAULT.getValue(), ctFont.getCharsetArray(0).getVal());
    // Try with a few less usual ones:
    // Set with the Charset itself
    xssfFont.setCharSet(FontCharset.RUSSIAN);
    assertEquals(FontCharset.RUSSIAN.getValue(), xssfFont.getCharSet());
    // And set with the Charset index
    xssfFont.setCharSet(FontCharset.ARABIC.getValue());
    assertEquals(FontCharset.ARABIC.getValue(), xssfFont.getCharSet());
    xssfFont.setCharSet((byte) (FontCharset.ARABIC.getValue()));
    assertEquals(FontCharset.ARABIC.getValue(), xssfFont.getCharSet());
    // This one isn't allowed
    assertEquals(null, FontCharset.valueOf(9999));
    try {
        xssfFont.setCharSet(9999);
        fail("Shouldn't be able to set an invalid charset");
    } catch (POIXMLException e) {
    }
    // Now try with a few sample files
    // Normal charset
    XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
    assertEquals(0, wb1.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet());
    wb1.close();
    // GB2312 charact set
    XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("49273.xlsx");
    assertEquals(134, wb2.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet());
    wb2.close();
}
Also used : CTFont(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont) CTIntProperty(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIntProperty) POIXMLException(org.apache.poi.POIXMLException) Test(org.junit.Test)

Example 14 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class XSSFFileHandler method handleAdditional.

@SuppressWarnings("resource")
@Override
public void handleAdditional(File file) throws Exception {
    // redirect stdout as the examples often write lots of text
    PrintStream oldOut = System.out;
    try {
        System.setOut(new NullPrintStream());
        FromHowTo.main(new String[] { file.getAbsolutePath() });
        XLSX2CSV.main(new String[] { file.getAbsolutePath() });
        assertFalse("Expected Extraction to fail for file " + file + " and handler " + this + ", but did not fail!", EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName()));
    } catch (OLE2NotOfficeXmlFileException e) {
    // we have some files that are not actually OOXML and thus cannot be tested here
    } catch (IllegalArgumentException e) {
        if (!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) {
            throw e;
        }
    } catch (InvalidFormatException e) {
        if (!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) {
            throw e;
        }
    } catch (IOException e) {
        if (!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) {
            throw e;
        }
    } catch (POIXMLException e) {
        if (!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) {
            throw e;
        }
    } finally {
        System.setOut(oldOut);
    }
}
Also used : PrintStream(java.io.PrintStream) OLE2NotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException) IOException(java.io.IOException) POIXMLException(org.apache.poi.POIXMLException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 15 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class XMLSlideShow method onDocumentRead.

@Override
protected void onDocumentRead() throws IOException {
    try {
        PresentationDocument doc = PresentationDocument.Factory.parse(getCorePart().getInputStream(), DEFAULT_XML_OPTIONS);
        _presentation = doc.getPresentation();
        Map<String, XSLFSlideMaster> masterMap = new HashMap<String, XSLFSlideMaster>();
        Map<String, XSLFSlide> shIdMap = new HashMap<String, XSLFSlide>();
        for (RelationPart rp : getRelationParts()) {
            POIXMLDocumentPart p = rp.getDocumentPart();
            if (p instanceof XSLFSlide) {
                shIdMap.put(rp.getRelationship().getId(), (XSLFSlide) p);
            } else if (p instanceof XSLFSlideMaster) {
                masterMap.put(getRelationId(p), (XSLFSlideMaster) p);
            } else if (p instanceof XSLFTableStyles) {
                _tableStyles = (XSLFTableStyles) p;
            } else if (p instanceof XSLFNotesMaster) {
                _notesMaster = (XSLFNotesMaster) p;
            } else if (p instanceof XSLFCommentAuthors) {
                _commentAuthors = (XSLFCommentAuthors) p;
            }
        }
        _masters = new ArrayList<XSLFSlideMaster>(masterMap.size());
        for (CTSlideMasterIdListEntry masterId : _presentation.getSldMasterIdLst().getSldMasterIdList()) {
            XSLFSlideMaster master = masterMap.get(masterId.getId2());
            _masters.add(master);
        }
        _slides = new ArrayList<XSLFSlide>(shIdMap.size());
        if (_presentation.isSetSldIdLst()) {
            for (CTSlideIdListEntry slId : _presentation.getSldIdLst().getSldIdList()) {
                XSLFSlide sh = shIdMap.get(slId.getId2());
                if (sh == null) {
                    LOG.log(POILogger.WARN, "Slide with r:id " + slId.getId() + " was defined, but didn't exist in package, skipping");
                    continue;
                }
                _slides.add(sh);
            }
        }
    } catch (XmlException e) {
        throw new POIXMLException(e);
    }
}
Also used : HashMap(java.util.HashMap) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) POIXMLException(org.apache.poi.POIXMLException) CTSlideMasterIdListEntry(org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterIdListEntry) CTSlideIdListEntry(org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry) XmlException(org.apache.xmlbeans.XmlException) PresentationDocument(org.openxmlformats.schemas.presentationml.x2006.main.PresentationDocument)

Aggregations

POIXMLException (org.apache.poi.POIXMLException)50 XmlException (org.apache.xmlbeans.XmlException)19 IOException (java.io.IOException)18 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)11 InputStream (java.io.InputStream)9 POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)9 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)8 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 OutputStream (java.io.OutputStream)6 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)6 XmlObject (org.apache.xmlbeans.XmlObject)6 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)5 XmlCursor (org.apache.xmlbeans.XmlCursor)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)3 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)3 Workbook (org.apache.poi.ss.usermodel.Workbook)3