Search in sources :

Example 1 with OpenXML4JException

use of org.apache.poi.openxml4j.exceptions.OpenXML4JException in project translationstudio8 by heartsome.

the class Xlsx2Tmx method doCovnerter.

public void doCovnerter(File2TmxConvertBean file2TmxBean, IProgressMonitor monitor) throws Exception {
    Xlsx2TmxHelper tmxConver = new Xlsx2TmxHelper();
    attString = Model2String.getCustomArributeXML(file2TmxBean.customeAttr);
    AbstractWriter writer = getWriter(file2TmxBean);
    if (writer instanceof TmxWriter) {
        TmxWriter tmxWriter = (TmxWriter) writer;
        tmxWriter.setAttibuteString(attString);
    }
    // writer.writeHeader(file2TmxBean.srcLangCode);
    try {
        tmxConver.parseXlsxFileAndWriteTmxBody(file2TmxBean.sourceFilePath, writer, monitor);
    } catch (ParserConfigurationException e) {
        LOGGER.error("", e);
        throw new Exception(Messages.getString("converter.xlsx2tmx.parseExcle.error"));
    } catch (SAXException e) {
        LOGGER.error("", e);
        if ("LANG-CODE-ERORR".equals(e.getMessage())) {
            throw new Exception(Messages.getString("converter.common.vaild.langcode.error"));
        } else if ("DIFF--SRC-LANG-CODE".equals(e.getMessage())) {
            throw new Exception(Messages.getString("converter.common.appendtmx.diffsrcLang.error"));
        } else if ("EMPTY-LANG-CODE".equals(e.getMessage())) {
            throw new Exception(Messages.getString("converter.common.vaild.langcode.error"));
        } else if ("DUPLICATE-LANG-CODE-ERORR".equals(e.getMessage())) {
            throw new Exception(Messages.getString("converter.common.vaild.duplicatelangcode.error"));
        }
    } catch (IOException e) {
        LOGGER.error("", e);
        throw new Exception(Messages.getString("converter.xlsx2tmx.writeTmx.error"));
    } catch (OpenXML4JException e) {
        LOGGER.error("", e);
        throw new Exception(Messages.getString("converter.xlsx2tmx.parseExcle.error"));
    } finally {
        writer.writeEnd();
        writer.closeOutStream();
        monitor.done();
    }
}
Also used : OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) AbstractWriter(net.heartsome.cat.tmx.converter.AbstractWriter) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) TmxWriter(net.heartsome.cat.tmx.converter.TmxWriter) SAXException(org.xml.sax.SAXException)

Example 2 with OpenXML4JException

use of org.apache.poi.openxml4j.exceptions.OpenXML4JException in project poi by apache.

the class XSSFBEventBasedExcelExtractor method getText.

/**
     * Processes the file and returns the text
     */
public String getText() {
    try {
        XSSFBSharedStringsTable strings = new XSSFBSharedStringsTable(getPackage());
        XSSFBReader xssfbReader = new XSSFBReader(getPackage());
        XSSFBStylesTable styles = xssfbReader.getXSSFBStylesTable();
        XSSFBReader.SheetIterator iter = (XSSFBReader.SheetIterator) xssfbReader.getSheetsData();
        StringBuffer text = new StringBuffer();
        SheetTextExtractor sheetExtractor = new SheetTextExtractor();
        XSSFBHyperlinksTable hyperlinksTable = null;
        while (iter.hasNext()) {
            InputStream stream = iter.next();
            if (getIncludeSheetNames()) {
                text.append(iter.getSheetName());
                text.append('\n');
            }
            if (handleHyperlinksInCells) {
                hyperlinksTable = new XSSFBHyperlinksTable(iter.getSheetPart());
            }
            XSSFBCommentsTable comments = getIncludeCellComments() ? iter.getXSSFBSheetComments() : null;
            processSheet(sheetExtractor, styles, comments, strings, stream);
            if (getIncludeHeadersFooters()) {
                sheetExtractor.appendHeaderText(text);
            }
            sheetExtractor.appendCellText(text);
            if (getIncludeTextBoxes()) {
                processShapes(iter.getShapes(), text);
            }
            if (getIncludeHeadersFooters()) {
                sheetExtractor.appendFooterText(text);
            }
            sheetExtractor.reset();
            stream.close();
        }
        return text.toString();
    } catch (IOException e) {
        LOGGER.log(POILogger.WARN, e);
        return null;
    } catch (SAXException se) {
        LOGGER.log(POILogger.WARN, se);
        return null;
    } catch (OpenXML4JException o4je) {
        LOGGER.log(POILogger.WARN, o4je);
        return null;
    }
}
Also used : XSSFBReader(org.apache.poi.xssf.eventusermodel.XSSFBReader) XSSFBCommentsTable(org.apache.poi.xssf.binary.XSSFBCommentsTable) InputStream(java.io.InputStream) IOException(java.io.IOException) XSSFBStylesTable(org.apache.poi.xssf.binary.XSSFBStylesTable) SAXException(org.xml.sax.SAXException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) XSSFBHyperlinksTable(org.apache.poi.xssf.binary.XSSFBHyperlinksTable) XSSFBSharedStringsTable(org.apache.poi.xssf.binary.XSSFBSharedStringsTable)

Example 3 with OpenXML4JException

use of org.apache.poi.openxml4j.exceptions.OpenXML4JException in project poi by apache.

the class ExtractorFactory method getEmbededDocsTextExtractors.

/**
     * Returns an array of text extractors, one for each of
     *  the embedded documents in the file (if there are any).
     * If there are no embedded documents, you'll get back an
     *  empty array. Otherwise, you'll get one open
     *  {@link POITextExtractor} for each embedded file.
     */
public static POITextExtractor[] getEmbededDocsTextExtractors(POIOLE2TextExtractor ext) throws IOException, OpenXML4JException, XmlException {
    // All the embedded directories we spotted
    ArrayList<Entry> dirs = new ArrayList<Entry>();
    // For anything else not directly held in as a POIFS directory
    ArrayList<InputStream> nonPOIFS = new ArrayList<InputStream>();
    // Find all the embedded directories
    DirectoryEntry root = ext.getRoot();
    if (root == null) {
        throw new IllegalStateException("The extractor didn't know which POIFS it came from!");
    }
    if (ext instanceof ExcelExtractor) {
        // These are in MBD... under the root
        Iterator<Entry> it = root.getEntries();
        while (it.hasNext()) {
            Entry entry = it.next();
            if (entry.getName().startsWith("MBD")) {
                dirs.add(entry);
            }
        }
    } else if (ext instanceof WordExtractor) {
        // These are in ObjectPool -> _... under the root
        try {
            DirectoryEntry op = (DirectoryEntry) root.getEntry("ObjectPool");
            Iterator<Entry> it = op.getEntries();
            while (it.hasNext()) {
                Entry entry = it.next();
                if (entry.getName().startsWith("_")) {
                    dirs.add(entry);
                }
            }
        } catch (FileNotFoundException e) {
            logger.log(POILogger.INFO, "Ignoring FileNotFoundException while extracting Word document", e.getLocalizedMessage());
        // ignored here
        }
    //} else if(ext instanceof PowerPointExtractor) {
    // Tricky, not stored directly in poifs
    // TODO
    } else if (ext instanceof OutlookTextExtactor) {
        // Stored in the Attachment blocks
        MAPIMessage msg = ((OutlookTextExtactor) ext).getMAPIMessage();
        for (AttachmentChunks attachment : msg.getAttachmentFiles()) {
            if (attachment.getAttachData() != null) {
                byte[] data = attachment.getAttachData().getValue();
                nonPOIFS.add(new ByteArrayInputStream(data));
            } else if (attachment.getAttachmentDirectory() != null) {
                dirs.add(attachment.getAttachmentDirectory().getDirectory());
            }
        }
    }
    // Create the extractors
    if (dirs.size() == 0 && nonPOIFS.size() == 0) {
        return new POITextExtractor[0];
    }
    ArrayList<POITextExtractor> textExtractors = new ArrayList<POITextExtractor>();
    for (Entry dir : dirs) {
        textExtractors.add(createExtractor((DirectoryNode) dir));
    }
    for (InputStream nonPOIF : nonPOIFS) {
        try {
            textExtractors.add(createExtractor(nonPOIF));
        } catch (IllegalArgumentException e) {
            // Ignore, just means it didn't contain
            //  a format we support as yet
            logger.log(POILogger.INFO, "Format not supported yet", e.getLocalizedMessage());
        } catch (XmlException e) {
            throw new IOException(e.getMessage(), e);
        } catch (OpenXML4JException e) {
            throw new IOException(e.getMessage(), e);
        }
    }
    return textExtractors.toArray(new POITextExtractor[textExtractors.size()]);
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) IOException(java.io.IOException) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) WordExtractor(org.apache.poi.hwpf.extractor.WordExtractor) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) MAPIMessage(org.apache.poi.hsmf.MAPIMessage) Entry(org.apache.poi.poifs.filesystem.Entry) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) OutlookTextExtactor(org.apache.poi.hsmf.extractor.OutlookTextExtactor) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) ByteArrayInputStream(java.io.ByteArrayInputStream) POITextExtractor(org.apache.poi.POITextExtractor) XSSFExcelExtractor(org.apache.poi.xssf.extractor.XSSFExcelExtractor) ExcelExtractor(org.apache.poi.hssf.extractor.ExcelExtractor) XSSFEventBasedExcelExtractor(org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor) XSSFBEventBasedExcelExtractor(org.apache.poi.xssf.extractor.XSSFBEventBasedExcelExtractor) XmlException(org.apache.xmlbeans.XmlException) Iterator(java.util.Iterator) AttachmentChunks(org.apache.poi.hsmf.datatypes.AttachmentChunks)

Example 4 with OpenXML4JException

use of org.apache.poi.openxml4j.exceptions.OpenXML4JException in project poi by apache.

the class ZipPartMarshaller method marshall.

/**
	 * Save the specified part.
	 *
	 * @throws OpenXML4JException
	 *             Throws if an internal exception is thrown.
	 */
@Override
public boolean marshall(PackagePart part, OutputStream os) throws OpenXML4JException {
    if (!(os instanceof ZipOutputStream)) {
        logger.log(POILogger.ERROR, "Unexpected class " + os.getClass().getName());
        throw new OpenXML4JException("ZipOutputStream expected !");
    // Normally should happen only in developement phase, so just throw
    // exception
    }
    // might depend on empty parts being saved, e.g. some unit tests verify this currently.
    if (part.getSize() == 0 && part.getPartName().getName().equals(XSSFRelation.SHARED_STRINGS.getDefaultFileName())) {
        return true;
    }
    ZipOutputStream zos = (ZipOutputStream) os;
    ZipEntry partEntry = new ZipEntry(ZipHelper.getZipItemNameFromOPCName(part.getPartName().getURI().getPath()));
    try {
        // Create next zip entry
        zos.putNextEntry(partEntry);
        // Saving data in the ZIP file
        InputStream ins = part.getInputStream();
        byte[] buff = new byte[ZipHelper.READ_WRITE_FILE_BUFFER_SIZE];
        while (ins.available() > 0) {
            int resultRead = ins.read(buff);
            if (resultRead == -1) {
                // End of file reached
                break;
            }
            zos.write(buff, 0, resultRead);
        }
        zos.closeEntry();
    } catch (IOException ioe) {
        logger.log(POILogger.ERROR, "Cannot write: " + part.getPartName() + ": in ZIP", ioe);
        return false;
    }
    // Saving relationship part
    if (part.hasRelationships()) {
        PackagePartName relationshipPartName = PackagingURIHelper.getRelationshipPartName(part.getPartName());
        marshallRelationshipPart(part.getRelationships(), relationshipPartName, zos);
    }
    return true;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) ZipOutputStream(java.util.zip.ZipOutputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 5 with OpenXML4JException

use of org.apache.poi.openxml4j.exceptions.OpenXML4JException in project poi by apache.

the class ExtractorFactory method createExtractor.

public static POITextExtractor createExtractor(File f) throws IOException, OpenXML4JException, XmlException {
    NPOIFSFileSystem fs = null;
    try {
        fs = new NPOIFSFileSystem(f);
        POIOLE2TextExtractor extractor = createExtractor(fs);
        extractor.setFilesystem(fs);
        return extractor;
    } catch (OfficeXmlFileException e) {
        // ensure file-handle release
        IOUtils.closeQuietly(fs);
        return createExtractor(OPCPackage.open(f.toString(), PackageAccess.READ));
    } catch (NotOLE2FileException ne) {
        // ensure file-handle release
        IOUtils.closeQuietly(fs);
        throw new IllegalArgumentException("Your File was neither an OLE2 file, nor an OOXML file");
    } catch (OpenXML4JException e) {
        // ensure file-handle release
        IOUtils.closeQuietly(fs);
        throw e;
    } catch (XmlException e) {
        // ensure file-handle release
        IOUtils.closeQuietly(fs);
        throw e;
    } catch (IOException e) {
        // ensure file-handle release
        IOUtils.closeQuietly(fs);
        throw e;
    } catch (RuntimeException e) {
        // ensure file-handle release
        IOUtils.closeQuietly(fs);
        throw e;
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) OfficeXmlFileException(org.apache.poi.poifs.filesystem.OfficeXmlFileException) NotOLE2FileException(org.apache.poi.poifs.filesystem.NotOLE2FileException) XmlException(org.apache.xmlbeans.XmlException) IOException(java.io.IOException) POIOLE2TextExtractor(org.apache.poi.POIOLE2TextExtractor)

Aggregations

OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)16 IOException (java.io.IOException)13 InputStream (java.io.InputStream)7 XmlException (org.apache.xmlbeans.XmlException)7 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)5 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)5 FileNotFoundException (java.io.FileNotFoundException)4 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)4 SAXException (org.xml.sax.SAXException)4 ZipOutputStream (java.util.zip.ZipOutputStream)3 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)3 ZipEntry (java.util.zip.ZipEntry)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)2 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)2 XSSFBCommentsTable (org.apache.poi.xssf.binary.XSSFBCommentsTable)2 XSSFBSharedStringsTable (org.apache.poi.xssf.binary.XSSFBSharedStringsTable)2 XSSFBStylesTable (org.apache.poi.xssf.binary.XSSFBStylesTable)2 XSSFBReader (org.apache.poi.xssf.eventusermodel.XSSFBReader)2 XSSFBEventBasedExcelExtractor (org.apache.poi.xssf.extractor.XSSFBEventBasedExcelExtractor)2