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;
  }
Esempio n. 2
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;
  }