Example #1
0
 /**
  * @param pkg
  * @throws IOException
  * @throws SAXException
  * @throws ParserConfigurationException
  */
 public ReadonlySharedStringsTable(OPCPackage pkg)
     throws IOException, SAXException, ParserConfigurationException {
   ArrayList<PackagePart> parts =
       pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType());
   PackagePart sstPart = parts.get(0);
   readFrom(sstPart.getInputStream());
 }
 @Override
 protected void commit() throws IOException {
   PackagePart part = getPackagePart();
   OutputStream out = part.getOutputStream();
   writeTo(out);
   out.close();
 }
  /**
   * Called to update the embedded Excel workbook. As the format and structire of the workbook are
   * known in advance, all this code attempts to do is write a new value into the first cell on the
   * first row of the first worksheet. Prior to executing this method, that cell will contain the
   * value 1.
   *
   * @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException Rather than use the specific
   *     classes (HSSF/XSSF) to handle the embedded workbook this method uses those defeined in the
   *     SS stream. As a result, it might be the case that a SpreadsheetML file is opened for
   *     processing, throwing this exception if that file is invalid.
   * @throws java.io.IOException Thrown if a problem occurs in the underlying file system.
   */
  public void updateEmbeddedDoc() throws OpenXML4JException, IOException {
    Workbook workbook = null;
    Sheet sheet = null;
    Row row = null;
    Cell cell = null;
    PackagePart pPart = null;
    Iterator<PackagePart> pIter = null;
    List<PackagePart> embeddedDocs = this.doc.getAllEmbedds();
    if (embeddedDocs != null && !embeddedDocs.isEmpty()) {
      pIter = embeddedDocs.iterator();
      while (pIter.hasNext()) {
        pPart = pIter.next();
        if (pPart.getPartName().getExtension().equals(BINARY_EXTENSION)
            || pPart.getPartName().getExtension().equals(OPENXML_EXTENSION)) {

          // Get an InputStream from the pacage part and pass that
          // to the create method of the WorkbookFactory class. Update
          // the resulting Workbook and then stream that out again
          // using an OutputStream obtained from the same PackagePart.
          workbook = WorkbookFactory.create(pPart.getInputStream());
          sheet = workbook.getSheetAt(SHEET_NUM);
          row = sheet.getRow(ROW_NUM);
          cell = row.getCell(CELL_NUM);
          cell.setCellValue(NEW_VALUE);
          workbook.write(pPart.getOutputStream());
        }
      }

      // Finally, write the newly modified Word document out to file.
      this.doc.write(new FileOutputStream(this.docFile));
    }
  }
Example #4
0
  @Override
  protected void commit() throws IOException {
    XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
    Map<String, String> map = new HashMap<String, String>();
    map.put(STRelationshipId.type.getName().getNamespaceURI(), "r");
    xmlOptions.setSaveSuggestedPrefixes(map);

    PackagePart part = getPackagePart();
    OutputStream out = part.getOutputStream();
    _presentation.save(out, xmlOptions);
    out.close();
  }
Example #5
0
  public MediaType detect(TikaInputStream input, Metadata metadata) throws IOException {
    ZipFile zip = new ZipFile(input.getFile());
    for (ZipEntry entry : Collections.list(zip.entries())) {
      // Is it an Open Document file?
      if (entry.getName().equals("mimetype")) {
        InputStream stream = zip.getInputStream(entry);
        try {
          return fromString(IOUtils.toString(stream, "UTF-8"));
        } finally {
          stream.close();
        }
      } else if (entry.getName().equals("_rels/.rels")
          || entry.getName().equals("[Content_Types].xml")) {
        // Office Open XML File
        // As POI to open and investigate it for us
        try {
          OPCPackage pkg = OPCPackage.open(input.getFile().toString());
          input.setOpenContainer(pkg);

          PackageRelationshipCollection core =
              pkg.getRelationshipsByType(ExtractorFactory.CORE_DOCUMENT_REL);
          if (core.size() != 1) {
            throw new IOException(
                "Invalid OOXML Package received - expected 1 core document, found " + core.size());
          }

          // Get the type of the core document part
          PackagePart corePart = pkg.getPart(core.getRelationship(0));
          String coreType = corePart.getContentType();

          // Turn that into the type of the overall document
          String docType = coreType.substring(0, coreType.lastIndexOf('.'));
          return fromString(docType);
        } catch (InvalidFormatException e) {
          throw new IOException("Office Open XML File detected, but corrupted - " + e.getMessage());
        }
      } else if (entry.getName().equals("buildVersionHistory.plist")) {
        // This is an iWork document

        // Reset and ask
        zip.close();
        zip = new ZipFile(input.getFile());
        return IWorkPackageParser.identifyType(zip);
      } else if (entry.getName().equals("META-INF/")) {
        // Java Jar
        return MediaType.application("java-archive");
      }
    }

    return MediaType.APPLICATION_ZIP;
  }
 /** save and commit numbering */
 @Override
 protected void commit() throws IOException {
   XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
   xmlOptions.setSaveSyntheticDocumentElement(
       new QName(CTNumbering.type.getName().getNamespaceURI(), "numbering"));
   Map map = new HashMap();
   map.put("http://schemas.openxmlformats.org/markup-compatibility/2006", "ve");
   map.put("urn:schemas-microsoft-com:office:office", "o");
   map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r");
   map.put("http://schemas.openxmlformats.org/officeDocument/2006/math", "m");
   map.put("urn:schemas-microsoft-com:vml", "v");
   map.put("http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", "wp");
   map.put("urn:schemas-microsoft-com:office:word", "w10");
   map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w");
   map.put("http://schemas.microsoft.com/office/word/2006/wordml", "wne");
   xmlOptions.setSaveSuggestedPrefixes(map);
   PackagePart part = getPackagePart();
   OutputStream out = part.getOutputStream();
   ctNumbering.save(out, xmlOptions);
   out.close();
 }
 /**
  * Called to test whether or not the embedded workbook was correctly updated. This method simply
  * recovers the first cell from the first row of the first workbook and tests the value it
  * contains.
  *
  * <p>Note that execution will not continue up to the assertion as the embedded workbook is now
  * corrupted and causes an IllegalArgumentException with the following message
  *
  * <p><em>java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an
  * OOXML stream</em>
  *
  * <p>to be thrown when the WorkbookFactory.createWorkbook(InputStream) method is executed.
  *
  * @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException Rather than use the specific
  *     classes (HSSF/XSSF) to handle the embedded workbook this method uses those defeined in the
  *     SS stream. As a result, it might be the case that a SpreadsheetML file is opened for
  *     processing, throwing this exception if that file is invalid.
  * @throws java.io.IOException Thrown if a problem occurs in the underlying file system.
  */
 public void checkUpdatedDoc() throws OpenXML4JException, IOException {
   Workbook workbook = null;
   Sheet sheet = null;
   Row row = null;
   Cell cell = null;
   PackagePart pPart = null;
   Iterator<PackagePart> pIter = null;
   List<PackagePart> embeddedDocs = this.doc.getAllEmbedds();
   if (embeddedDocs != null && !embeddedDocs.isEmpty()) {
     pIter = embeddedDocs.iterator();
     while (pIter.hasNext()) {
       pPart = pIter.next();
       if (pPart.getPartName().getExtension().equals(BINARY_EXTENSION)
           || pPart.getPartName().getExtension().equals(OPENXML_EXTENSION)) {
         workbook = WorkbookFactory.create(pPart.getInputStream());
         sheet = workbook.getSheetAt(SHEET_NUM);
         row = sheet.getRow(ROW_NUM);
         cell = row.getCell(CELL_NUM);
         assertEquals(cell.getNumericCellValue(), NEW_VALUE, 0.0001);
       }
     }
   }
 }
 public Table(PackagePart part, PackageRelationship rel) throws IOException {
   super(part, rel);
   readFrom(part.getInputStream());
 }
Example #9
0
 /**
  * Like POIXMLDocumentPart constructor
  *
  * @param part
  * @param rel_ignored
  * @throws IOException
  */
 public ReadonlySharedStringsTable(PackagePart part, PackageRelationship rel_ignored)
     throws IOException, SAXException, ParserConfigurationException {
   readFrom(part.getInputStream());
 }