private OpcPackage process(HashMap<String, ByteArray> partByteArrays) throws Docx4JException {

    long startTime = System.currentTimeMillis();

    // 2. Create a new Package
    //		Eventually, you'll also be able to create an Excel package etc
    //		but only the WordML package exists at present

    ContentTypeManager ctm = new ContentTypeManager();

    try {
      InputStream is = getInputStreamFromZippedPart(partByteArrays, "[Content_Types].xml");
      ctm.parseContentTypesFile(is);
    } catch (IOException e) {
      throw new Docx4JException("Couldn't get [Content_Types].xml from ZipFile", e);
    } catch (NullPointerException e) {
      throw new Docx4JException("Couldn't get [Content_Types].xml from ZipFile", e);
    }

    // .. now find the name of the main part
    String partName = "_rels/.rels";
    RelationshipsPart rp = getRelationshipsPartFromZip(null, partByteArrays, partName);
    if (rp == null) {
      throw new Docx4JException("_rels/.rels appears to be missing from this package!");
    }

    String mainPartName = PackageRelsUtil.getNameOfMainPart(rp);
    String pkgContentType = ctm.getContentType(new PartName("/" + mainPartName));

    // 2. Create a new Package; this'll return the appropriate subclass
    OpcPackage p = ctm.createPackage(pkgContentType);
    log.info("Instantiated package of type " + p.getClass().getName());

    p.setRelationships(rp);
    rp.setSourceP(p); //

    // 5. Now recursively
    //		(i) create new Parts for each thing listed
    //		in the relationships
    //		(ii) add the new Part to the package
    //		(iii) cross the PartName off unusedZipEntries
    addPartsFromRelationships(partByteArrays, p, rp, ctm);

    // 6. Check unusedZipEntries is empty
    //		if (log.isDebugEnabled()) {
    //			 Iterator myVeryOwnIterator = unusedZipEntries.keySet().iterator();
    //			 while(myVeryOwnIterator.hasNext()) {
    //			     String key = (String)myVeryOwnIterator.next();
    //			     log.info( key + "  " + unusedZipEntries.get(key));
    //			 }
    //		}

    registerCustomXmlDataStorageParts(p);

    long endTime = System.currentTimeMillis();
    log.info("package read;  elapsed time: " + Math.round((endTime - startTime)) + " ms");

    return p;
  }
Example #2
0
  private RelationshipsPart getRelationshipsPartFromZip(Base p, ZipFile zf, String partName)
      throws Docx4JException {
    //			Document contents = null;
    //			try {
    //				contents = getDocumentFromZippedPart( zf,  partName);
    //			} catch (Exception e) {
    //				e.printStackTrace();
    //				throw new Docx4JException("Error getting document from Zipped Part", e);
    //
    //			}
    //		// debugPrint(contents);
    //		// TODO - why don't any of the part names in this document start with "/"?
    //		return new RelationshipsPart( p, new PartName("/" + partName), contents );

    RelationshipsPart rp = null;

    InputStream is = null;
    try {
      is = getInputStreamFromZippedPart(zf, partName);
      // thePart = new RelationshipsPart( p, new PartName("/" + partName), is );
      rp = new RelationshipsPart(new PartName("/" + partName));
      rp.setSourceP(p);
      rp.unmarshal(is);

    } catch (Exception e) {
      e.printStackTrace();
      throw new Docx4JException("Error getting document from Zipped Part:" + partName, e);

    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException exc) {
          exc.printStackTrace();
        }
      }
    }

    return rp;
    // debugPrint(contents);
    // TODO - why don't any of the part names in this document start with "/"?
  }
Example #3
0
  private RelationshipsPart getRelationshipsPartFromZip(Base p, String partName)
      throws Docx4JException {

    RelationshipsPart rp = null;

    InputStream is = null;
    try {
      is = partStore.loadPart(partName);
      if (is == null) {
        return null; // that's ok
      }
      rp = new RelationshipsPart(new PartName("/" + partName));
      rp.setSourceP(p);
      rp.unmarshal(is);

    } catch (Exception e) {
      e.printStackTrace();
      throw new Docx4JException("Error getting document from Zipped Part:" + partName, e);

    } finally {
      IOUtils.closeQuietly(is);
    }
    return rp;
  }
Example #4
0
  public OpcPackage get() throws Docx4JException {

    long startTime = System.currentTimeMillis();

    // 1. Get [Content_Types].xml
    ContentTypeManager ctm = new ContentTypeManager();
    InputStream is = null;
    try {
      is = partStore.loadPart("[Content_Types].xml");
      ctm.parseContentTypesFile(is);
    } catch (Docx4JException e) {
      throw new Docx4JException("Couldn't get [Content_Types].xml from ZipFile", e);
    } catch (NullPointerException e) {
      throw new Docx4JException("Couldn't get [Content_Types].xml from ZipFile", e);
    } finally {
      IOUtils.closeQuietly(is);
    }

    // .. now find the name of the main part
    String partName = "_rels/.rels";
    RelationshipsPart rp = getRelationshipsPartFromZip(null, partName);
    if (rp == null) {
      throw new Docx4JException("_rels/.rels appears to be missing from this package!");
    }

    String mainPartName = PackageRelsUtil.getNameOfMainPart(rp);
    PartName mainPartNameObj;
    if (mainPartName.startsWith("/")) {
      // OpenXML SDK 2.0 writes Target="/word/document.xml" (note leading "/")
      mainPartNameObj = new PartName(mainPartName);
    } else {
      // Microsoft Word, docx4j etc write Target="word/document.xml"
      mainPartNameObj = new PartName("/" + mainPartName);
    }
    String pkgContentType = ctm.getContentType(mainPartNameObj);

    // 2. Create a new Package; this'll return the appropriate subclass
    OpcPackage p = ctm.createPackage(pkgContentType);
    log.info("Instantiated package of type " + p.getClass().getName());
    p.setPartStore(partStore);

    p.setRelationships(rp);
    rp.setSourceP(p); //

    // 5. Now recursively
    //		(i) create new Parts for each thing listed
    //		in the relationships
    //		(ii) add the new Part to the package
    //		(iii) cross the PartName off unusedZipEntries
    addPartsFromRelationships(p, rp, ctm);

    // 6.
    registerCustomXmlDataStorageParts(p);

    //		partStore.finishLoad();

    long endTime = System.currentTimeMillis();
    log.info("package read;  elapsed time: " + Math.round((endTime - startTime)) + " ms");

    return p;
  }