Package org.apache.poi.openxml4j.opc

Examples of org.apache.poi.openxml4j.opc.Package


      is = new ByteArrayInputStream((byte[]) obj);
    } else {
      throw new IllegalArgumentException("Parameter must be instance of byte[]");
    }
    try {
      Package pkg = Package.open(is);

      XSSFReader r = new XSSFReader(pkg);
      SharedStringsTable sst = r.getSharedStringsTable();
      SheetHandler handler = new SheetHandler(sst);
View Full Code Here


  public void testCreatePartNameRelativeString()
      throws InvalidFormatException {
    PackagePartName partNameToValid = PackagingURIHelper
        .createPartName("/word/media/image1.gif");

    Package pkg = Package.create("DELETEIFEXISTS.docx");
    // Base part
    PackagePartName nameBase = PackagingURIHelper
        .createPartName("/word/document.xml");
    PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML);
    // Relative part name
    PackagePartName relativeName = PackagingURIHelper.createPartName(
        "media/image1.gif", partBase);
    assertTrue("The part name must be equal to "
        + partNameToValid.getName(), partNameToValid
        .equals(relativeName));
    pkg.revert();
  }
View Full Code Here

   */
  public void testCreatePartNameRelativeURI() throws Exception {
    PackagePartName partNameToValid = PackagingURIHelper
        .createPartName("/word/media/image1.gif");

    Package pkg = Package.create("DELETEIFEXISTS.docx");
    // Base part
    PackagePartName nameBase = PackagingURIHelper
        .createPartName("/word/document.xml");
    PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML);
    // Relative part name
    PackagePartName relativeName = PackagingURIHelper.createPartName(
        new URI("media/image1.gif"), partBase);
    assertTrue("The part name must be equal to "
        + partNameToValid.getName(), partNameToValid
        .equals(relativeName));
    pkg.revert();
  }
View Full Code Here

   * A package implementer shall neither create nor recognize a part with a
   * part name derived from another part name by appending segments to it.
   * [M1.11]
   */
  public void testPartNameDerivationAdditionFailure() {
    Package pkg = Package.create("TODELETEIFEXIST.docx");
    try {
      PackagePartName name = PackagingURIHelper
          .createPartName("/word/document.xml");
      PackagePartName nameDerived = PackagingURIHelper
          .createPartName("/word/document.xml/image1.gif");
      pkg.createPart(name, ContentTypes.XML);
      pkg.createPart(nameDerived, ContentTypes.EXTENSION_GIF);
    } catch (InvalidOperationException e) {
      pkg.revert();
      return;
    } catch (InvalidFormatException e) {
      fail(e.getMessage());
    }
    fail("A package implementer shall neither create nor recognize a part with a"
View Full Code Here

   * Rule M1.12 : Packages shall not contain equivalent part names and package
   * implementers shall neither create nor recognize packages with equivalent
   * part names.
   */
  public void testAddPackageAlreadyAddFailure() throws Exception {
    Package pkg = Package.create("DELETEIFEXISTS.docx");
    PackagePartName name1 = null;
    PackagePartName name2 = null;
    try {
      name1 = PackagingURIHelper.createPartName("/word/document.xml");
      name2 = PackagingURIHelper.createPartName("/word/document.xml");
    } catch (InvalidFormatException e) {
      throw new Exception(e.getMessage());
    }
    pkg.createPart(name1, ContentTypes.XML);
    try {
      pkg.createPart(name2, ContentTypes.XML);
    } catch (InvalidOperationException e) {
      return;
    }
    fail("Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12]");
  }
View Full Code Here

   * Rule M1.12 : Packages shall not contain equivalent part names and package
   * implementers shall neither create nor recognize packages with equivalent
   * part names.
   */
  public void testAddPackageAlreadyAddFailure2() throws Exception {
    Package pkg = Package.create("DELETEIFEXISTS.docx");
    PackagePartName partName = null;
    try {
      partName = PackagingURIHelper.createPartName("/word/document.xml");
    } catch (InvalidFormatException e) {
      throw new Exception(e.getMessage());
    }
    pkg.createPart(partName, ContentTypes.XML);
    try {
      pkg.createPart(partName, ContentTypes.XML);
    } catch (InvalidOperationException e) {
      return;
    }
    fail("Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12]");
  }
View Full Code Here

   * any other part. Package implementers shall enforce this requirement upon
   * the attempt to create such a relationship and shall treat any such
   * relationship as invalid.
   */
  public void testAddRelationshipRelationshipsPartFailure() {
    Package pkg = Package.create("DELETEIFEXISTS.docx");
    PackagePartName name1 = null;
    try {
      name1 = PackagingURIHelper
          .createPartName("/test/_rels/document.xml.rels");
    } catch (InvalidFormatException e) {
      fail("This exception should never happen !");
    }

    try {
      pkg.addRelationship(name1, TargetMode.INTERNAL,
          PackageRelationshipTypes.CORE_DOCUMENT);
    } catch (InvalidOperationException e) {
      return;
    }
    fail("Fail test -> M1.25: The Relationships part shall not have relationships to any other part");
View Full Code Here

* @author Julien Chable
*/
public final class TestOPCComplianceCoreProperties extends TestCase {

  public void testCorePropertiesPart() {
    Package pkg;
    try {
      InputStream is = OpenXML4JTestDataSamples.openComplianceSampleStream("OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx");
      pkg = Package.open(is);
    } catch (InvalidFormatException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    pkg.revert();
  }
View Full Code Here

  }

  private static String extractInvalidFormatMessage(String sampleNameSuffix) {

    InputStream is = OpenXML4JTestDataSamples.openComplianceSampleStream("OPCCompliance_CoreProperties_" + sampleNameSuffix);
    Package pkg;
    try {
      pkg = Package.open(is);
    } catch (InvalidFormatException e) {
      // expected during successful test
      return e.getMessage();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    pkg.revert();
    // Normally must thrown an InvalidFormatException exception.
    throw new AssertionFailedError("expected OPC compliance exception was not thrown");
  }
View Full Code Here

  /**
   * Test M4.1 rule.
   */
  public void testOnlyOneCorePropertiesPart_AddRelationship() {
    InputStream is = OpenXML4JTestDataSamples.openComplianceSampleStream("OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx");
    Package pkg;
    try {
      pkg = Package.open(is);
    } catch (InvalidFormatException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    URI partUri = createURI("/docProps/core2.xml");
    try {
      pkg.addRelationship(PackagingURIHelper.createPartName(partUri), TargetMode.INTERNAL,
          PackageRelationshipTypes.CORE_PROPERTIES);
      fail("expected OPC compliance exception was not thrown");
    } catch (InvalidFormatException e) {
      throw new RuntimeException(e);
    } catch (InvalidOperationException e) {
      // expected during successful test
      assertEquals("OPC Compliance error [M4.1]: can't add another core properties part ! Use the built-in package method instead.", e.getMessage());
    }
    pkg.revert();
  }
View Full Code Here

TOP

Related Classes of org.apache.poi.openxml4j.opc.Package

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.