/** * Clone the specified namespace. * * @param pkg the namespace to clone * @param file the destination file * @return the Cloned namespace */ public static OPCPackage Clone(OPCPackage pkg, string path) { OPCPackage dest = OPCPackage.Create(path); PackageRelationshipCollection rels = pkg.Relationships; foreach (PackageRelationship rel in rels) { PackagePart part = pkg.GetPart(rel); PackagePart part_tgt; if (rel.RelationshipType.Equals(PackageRelationshipTypes.CORE_PROPERTIES)) { CopyProperties(pkg.GetPackageProperties(), dest.GetPackageProperties()); continue; } dest.AddRelationship(part.PartName, (TargetMode)rel.TargetMode, rel.RelationshipType); part_tgt = dest.CreatePart(part.PartName, part.ContentType); Stream out1 = part_tgt.GetOutputStream(); IOUtils.Copy(part.GetInputStream(), out1); out1.Close(); if (part.HasRelationships) { Copy(pkg, part, dest, part_tgt); } } dest.Close(); //the temp file will be deleted when JVM terminates //new File(path).deleteOnExit(); return OPCPackage.Open(path); }
/** * Construct POIXMLDocumentPart representing a "core document" namespace part. */ public POIXMLDocumentPart(OPCPackage pkg) { PackageRelationship coreRel = pkg.GetRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT).GetRelationship(0); this.packagePart = pkg.GetPart(coreRel); this.packageRel = coreRel; }
public XWPFDocument(OPCPackage pkg) : base(pkg) { ; //build a tree of POIXMLDocumentParts, this document being the root Load(XWPFFactory.GetInstance()); }
public XSSFEventBasedExcelExtractor(OPCPackage Container) : base(null) { this.Container = Container; properties = new POIXMLProperties(Container); }
/** * Get the PackagePart that is the target of a relationship. * * @param rel The relationship * @param pkg The namespace to fetch from * @return The target part * @throws InvalidFormatException */ protected static PackagePart GetTargetPart(OPCPackage pkg, PackageRelationship rel) { PackagePartName relName = PackagingUriHelper.CreatePartName(rel.TargetUri); PackagePart part = pkg.GetPart(relName); if (part == null) { throw new ArgumentException("No part found for relationship " + rel); } return part; }
/** * Constructor. * * @param pack * Parent package. * @param partName * The part name, relative to the parent Package root. * @param contentType * The content type. * @param loadRelationships * Specify if the relationships will be loaded * @throws InvalidFormatException * If the specified URI is not valid. */ protected PackagePart(OPCPackage pack, PackagePartName partName, ContentType contentType, bool loadRelationships) { this.partName = partName; this.contentType = contentType; this.container = (ZipPackage)pack; // TODO - enforcing ZipPackage here - perhaps should change constructor signature // Check if this part is a relationship part isRelationshipPart = this.partName.IsRelationshipPartURI(); // Load relationships if any if (loadRelationships) LoadRelationships(); }
/** * Construct POIXMLDocumentPart representing a "core document" namespace part. */ public POIXMLDocumentPart(OPCPackage pkg) { PackageRelationship coreRel = pkg.GetRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT).GetRelationship(0); if (coreRel == null) { coreRel = pkg.GetRelationshipsByType(PackageRelationshipTypes.STRICT_CORE_DOCUMENT).GetRelationship(0); if (coreRel != null) { throw new POIXMLException("Strict OOXML isn't currently supported, please see bug #57699"); } } if (coreRel == null) { throw new POIXMLException("OOXML file structure broken/invalid - no core document found!"); } this.packagePart = pkg.GetPart(coreRel); this.packageRel = coreRel; }
public void AssertReadWrite(OPCPackage pkg1) { OPCParser doc = new OPCParser(pkg1); doc.Parse(new TestFactory()); Dictionary<String, POIXMLDocumentPart> context = new Dictionary<String, POIXMLDocumentPart>(); Traverse(doc, context); context.Clear(); string tmp = TempFile.GetTempFilePath("poi-ooxml", ".tmp"); FileStream out1 = new FileStream(tmp, FileMode.CreateNew); doc.Write(out1); out1.Close(); OPCPackage pkg2 = OPCPackage.Open(tmp); doc = new OPCParser(pkg1); doc.Parse(new TestFactory()); context = new Dictionary<String, POIXMLDocumentPart>(); Traverse(doc, context); context.Clear(); Assert.AreEqual(pkg1.Relationships.Size, pkg2.Relationships.Size); List<PackagePart> l1 = pkg1.GetParts(); List<PackagePart> l2 = pkg2.GetParts(); Assert.AreEqual(l1.Count, l2.Count); for (int i = 0; i < l1.Count; i++) { PackagePart p1 = l1[i]; PackagePart p2 = l2[i]; Assert.AreEqual(p1.ContentType, p2.ContentType); Assert.AreEqual(p1.HasRelationships, p2.HasRelationships); if (p1.HasRelationships) { Assert.AreEqual(p1.Relationships.Size, p2.Relationships.Size); } Assert.AreEqual(p1.PartName, p2.PartName); } }
/** * Constructor. * * @param pkg * @param sourcePart * @param targetUri * @param targetMode * @param relationshipType * @param id */ public PackageRelationship(OPCPackage pkg, PackagePart sourcePart, Uri targetUri, TargetMode targetMode, String relationshipType, String id) { if (pkg == null) throw new ArgumentException("pkg"); if (targetUri == null) throw new ArgumentException("targetUri"); if (relationshipType == null) throw new ArgumentException("relationshipType"); if (id == null) throw new ArgumentException("id"); this.container = pkg; this.source = sourcePart; this.targetUri = targetUri; this.targetMode = targetMode; this.relationshipType = relationshipType; this.id = id; }
public POIXMLProperties(OPCPackage docPackage) { this.pkg = docPackage; // Core properties core = new CoreProperties((PackagePropertiesPart)pkg.GetPackageProperties()); // Extended properties PackageRelationshipCollection extRel = pkg.GetRelationshipsByType(PackageRelationshipTypes.EXTENDED_PROPERTIES); if (extRel.Size == 1) { extPart = pkg.GetPart(extRel.GetRelationship(0)); ExtendedPropertiesDocument props = ExtendedPropertiesDocument.Parse( extPart.GetInputStream() ); ext = new ExtendedProperties(props); } else { extPart = null; ext = new ExtendedProperties((ExtendedPropertiesDocument)NEW_EXT_INSTANCE.Copy()); } // Custom properties PackageRelationshipCollection custRel = pkg.GetRelationshipsByType(PackageRelationshipTypes.CUSTOM_PROPERTIES); if (custRel.Size == 1) { custPart = pkg.GetPart(custRel.GetRelationship(0)); CustomPropertiesDocument props = CustomPropertiesDocument.Parse( custPart.GetInputStream() ); cust = new CustomProperties(props); } else { custPart = null; cust = new CustomProperties((CustomPropertiesDocument)NEW_CUST_INSTANCE.Copy()); } }
public void Assert_50154(OPCPackage pkg) { Uri drawingUri = new Uri("/xl/drawings/drawing1.xml",UriKind.Relative); PackagePart drawingPart = pkg.GetPart(PackagingUriHelper.CreatePartName(drawingUri)); PackageRelationshipCollection drawingRels = drawingPart.Relationships; Assert.AreEqual(6, drawingRels.Size); // expected one image Assert.AreEqual(1, drawingPart.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image").Size); // and three hyperlinks Assert.AreEqual(5, drawingPart.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink").Size); PackageRelationship rId1 = drawingPart.GetRelationship("rId1"); Uri parent = drawingPart.PartName.URI; Uri rel1 = new Uri(Path.Combine(parent.ToString(),rId1.TargetUri.ToString()),UriKind.Relative); Uri rel11 = PackagingUriHelper.RelativizeUri(drawingPart.PartName.URI, rId1.TargetUri); Assert.AreEqual("#'Another Sheet'!A1", HttpUtility.UrlDecode(rel1.ToString().Split(new char[]{'/'})[3])); PackageRelationship rId2 = drawingPart.GetRelationship("rId2"); Uri rel2 = PackagingUriHelper.RelativizeUri(drawingPart.PartName.URI, rId2.TargetUri); Assert.AreEqual("../media/image1.png", rel2.OriginalString); PackageRelationship rId3 = drawingPart.GetRelationship("rId3"); Uri rel3 = new Uri(Path.Combine(parent.ToString(), rId3.TargetUri.ToString()), UriKind.Relative); Assert.AreEqual("#ThirdSheet!A1", rel3.OriginalString.Split(new char[] { '/' })[3]); PackageRelationship rId4 = drawingPart.GetRelationship("rId4"); Uri rel4 = new Uri(Path.Combine(parent.ToString(), rId4.TargetUri.ToString()), UriKind.Relative); Assert.AreEqual("#'\u0410\u043F\u0430\u0447\u0435 \u041F\u041E\u0418'!A1",HttpUtility.UrlDecode(rel4.OriginalString.Split(new char[] { '/' })[3])); //PackageRelationship rId5 = drawingPart.GetRelationship("rId5"); //Uri rel5 = new Uri(Path.Combine(parent.ToString(), rId5.TargetUri.ToString()), UriKind.Relative); //// back slashed have been Replaced with forward //Assert.AreEqual("file:///D:/chan-chan.mp3", rel5.ToString()); //PackageRelationship rId6 = drawingPart.GetRelationship("rId6"); //Uri rel6 = new Uri(ResolveRelativePath(parent.ToString(), HttpUtility.UrlDecode(rId6.TargetUri.ToString())), UriKind.Relative); //Assert.AreEqual("../../../../../../../cygwin/home/yegor/dinom/&&&[access].2010-10-26.log", rel6.OriginalString); //Assert.AreEqual("#'\u0410\u043F\u0430\u0447\u0435 \u041F\u041E\u0418'!A5", HttpUtility.UrlDecode(rel6.OriginalString.Split(new char[] { '/' })[3])); }
/** * When you open something like a theme, call this to * re-base the XML Document onto the core child of the * current core document */ protected void Rebase(OPCPackage pkg) { PackageRelationshipCollection cores = packagePart.GetRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT); if (cores.Size != 1) { throw new InvalidOperationException( "Tried to rebase using " + PackageRelationshipTypes.CORE_DOCUMENT + " but found " + cores.Size + " parts of the right type" ); } packageRel = cores.GetRelationship(0); packagePart = packagePart.GetRelatedPart(packageRel); }
/** * Constructs a XSSFWorkbook object given a OpenXML4J <code>Package</code> object, * see <a href="http://poi.apache.org/oxml4j/">http://poi.apache.org/oxml4j/</a>. * * Once you have finished working with the Workbook, you should close the package * by calling pkg.close, to avoid leaving file handles open. * * Creating a XSSFWorkbook from a file-backed OPC Package has a lower memory * footprint than an InputStream backed one. * * @param pkg the OpenXML4J <code>OPC Package</code> object. */ public XSSFWorkbook(OPCPackage pkg) : base(pkg) { //build a tree of POIXMLDocumentParts, this workbook being the root Load(XSSFFactory.GetInstance()); }
/** * Constructor. * * @param container * The container package. * @param partName * Part name. * @param contentType * Content type. * @throws InvalidFormatException * Throws if the content of this part invalid. */ public ZipPackagePart(OPCPackage container, PackagePartName partName, String contentType):base(container, partName, contentType) { }
/** * Constructor. * * @param pack * Parent package. * @param partName * The part name, relative to the parent Package root. * @param contentType * The content type. * @throws InvalidFormatException * If the specified URI is not valid. */ protected PackagePart(OPCPackage pack, PackagePartName partName, ContentType contentType) : this(pack, partName, contentType, true) { }
/** * Constructor. * * @param pack * Parent package. * @param partName * The part name, relative to the parent Package root. * @param contentType * The Multipurpose Internet Mail Extensions (MIME) content type * of the part's data stream. */ public PackagePart(OPCPackage pack, PackagePartName partName, String contentType) : this(pack, partName, new ContentType(contentType)) { }
/** * Constructor. * * @param pack * Container package. * @param partName * Name of this part. * @throws InvalidFormatException * Throws if the content is invalid. */ public PackagePropertiesPart(OPCPackage pack, PackagePartName partName) :base(pack, partName, ContentTypes.CORE_PROPERTIES_PART) { }
private static ContentTypeManager GetContentTypeManager(OPCPackage pkg) { FieldInfo f = typeof(OPCPackage).GetField("contentTypeManager", BindingFlags.NonPublic | BindingFlags.Instance); //f.SetAccessible(true); return (ContentTypeManager)f.GetValue(pkg); }
/** * Constructs a XSSFWorkbook object given a OpenXML4J <code>Package</code> object, * see <a href="http://poi.apache.org/oxml4j/">http://poi.apache.org/oxml4j/</a>. * * Once you have finished working with the Workbook, you should close the package * by calling pkg.close, to avoid leaving file handles open. * * Creating a XSSFWorkbook from a file-backed OPC Package has a lower memory * footprint than an InputStream backed one. * * @param pkg the OpenXML4J <code>OPC Package</code> object. */ public XSSFWorkbook(OPCPackage pkg) : base(pkg) { BeforeDocumentRead(); //build a tree of POIXMLDocumentParts, this workbook being the root Load(XSSFFactory.GetInstance()); // some broken Workbooks miss this... if (!workbook.IsSetBookViews()) { CT_BookViews bvs = workbook.AddNewBookViews(); CT_BookView bv = bvs.AddNewWorkbookView(); bv.activeTab = (0); } }
/** * Constructor. * * @param container * The container package. * @param zipEntry * The zip entry corresponding to this part. * @param partName * The part name. * @param contentType * Content type. * @throws InvalidFormatException * Throws if the content of this part is invalid. */ public ZipPackagePart(OPCPackage container, ZipEntry zipEntry, PackagePartName partName, String contentType): base(container, partName, contentType) { this.zipEntry = zipEntry; }
private void CompareProperties(OPCPackage p) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); DateTime expectedDate = DateTime.Parse("2007/05/12T08:00:00Z").ToUniversalTime(); // Gets the core properties PackageProperties props = p.GetPackageProperties(); Assert.AreEqual("MyCategory", props.GetCategoryProperty()); Assert.AreEqual("MyContentStatus", props.GetContentStatusProperty() ); Assert.AreEqual("MyContentType", props.GetContentTypeProperty()); Assert.AreEqual(expectedDate, props.GetCreatedProperty()); Assert.AreEqual("MyCreator", props.GetCreatorProperty()); Assert.AreEqual("MyDescription", props.GetDescriptionProperty()); Assert.AreEqual("MyIdentifier", props.GetIdentifierProperty()); Assert.AreEqual("MyKeywords", props.GetKeywordsProperty()); Assert.AreEqual("MyLanguage", props.GetLanguageProperty()); Assert.AreEqual("Julien Chable", props.GetLastModifiedByProperty() ); Assert.AreEqual(expectedDate, props.GetLastPrintedProperty()); Assert.AreEqual(expectedDate, props.GetModifiedProperty()); Assert.AreEqual("2", props.GetRevisionProperty()); Assert.AreEqual("MySubject", props.GetSubjectProperty()); Assert.AreEqual("MyTitle", props.GetTitleProperty()); Assert.AreEqual("2", props.GetVersionProperty()); }
/// <summary> /// Creates an XSSFWorkbook from the given OOXML Package /// </summary> public static IWorkbook Create(OPCPackage pkg) { return new XSSFWorkbook(pkg); }
private void assertMSCompatibility(OPCPackage pkg) { //PackagePartName relName = PackagingURIHelper.CreatePartName(PackageRelationship.GetContainerPartRelationship()); //PackagePart relPart = pkg.GetPart(relName); //SAXReader Reader = new SAXReader(); //Document xmlRelationshipsDoc = Reader // .Read(relPart.GetInputStream()); //Element root = xmlRelationshipsDoc.GetRootElement(); //for (Iterator i = root // .elementIterator(PackageRelationship.RELATIONSHIP_TAG_NAME); i // .HasNext(); ) //{ // Element element = (Element)i.next(); // String value = element.attribute( // PackageRelationship.TARGET_ATTRIBUTE_NAME) // .GetValue(); // Assert.IsTrue("Root target must not start with a leadng slash ('/'): " + value, value[0] != '/'); //} }
/** * Recursively copy namespace parts to the destination namespace */ private static void Copy(OPCPackage pkg, PackagePart part, OPCPackage tgt, PackagePart part_tgt) { PackageRelationshipCollection rels = part.Relationships; if(rels != null) foreach (PackageRelationship rel in rels) { PackagePart p; if(rel.TargetMode == TargetMode.External){ part_tgt.AddExternalRelationship(rel.TargetUri.ToString(), rel.RelationshipType, rel.Id); //external relations don't have associated namespace parts continue; } Uri uri = rel.TargetUri; if(uri.Fragment != null) { part_tgt.AddRelationship(uri, (TargetMode)rel.TargetMode, rel.RelationshipType, rel.Id); continue; } PackagePartName relName = PackagingUriHelper.CreatePartName(rel.TargetUri); p = pkg.GetPart(relName); part_tgt.AddRelationship(p.PartName, (TargetMode)rel.TargetMode, rel.RelationshipType, rel.Id); PackagePart dest; if(!tgt.ContainPart(p.PartName)){ dest = tgt.CreatePart(p.PartName, p.ContentType); Stream out1 = dest.GetOutputStream(); IOUtils.Copy(p.GetInputStream(), out1); out1.Close(); Copy(pkg, p, tgt, dest); } } }
public XSSFExcelExtractor(OPCPackage Container) : this(new XSSFWorkbook(Container)) { }
/** * Validates the package compliance with the OPC specifications. * * @return <b>true</b> if the package is valid else <b>false</b> */ public bool ValidatePackage(OPCPackage pkg) { throw new InvalidOperationException("Not implemented yet !!!"); }
public XWPFWordExtractor(OPCPackage Container):this(new XWPFDocument(Container)) { }
/** * Configure the package. * * @param pkg */ private static void ConfigurePackage(OPCPackage pkg) { try { // Content type manager pkg.contentTypeManager = new ZipContentTypeManager(null, pkg); // Add default content types for .xml and .rels pkg.contentTypeManager .AddContentType( PackagingUriHelper .CreatePartName(PackagingUriHelper.PACKAGE_RELATIONSHIPS_ROOT_URI), ContentTypes.RELATIONSHIPS_PART); pkg.contentTypeManager .AddContentType(PackagingUriHelper .CreatePartName("/default.xml"), ContentTypes.PLAIN_OLD_XML); // Init some PackageBase properties pkg.packageProperties = new PackagePropertiesPart(pkg, PackagingUriHelper.CORE_PROPERTIES_PART_NAME); pkg.packageProperties.SetCreatorProperty("Generated by OpenXml4Net"); pkg.packageProperties.SetCreatedProperty(new Nullable<DateTime>( DateTime.Now)); } catch (InvalidFormatException) { // Should never happen throw; } }
protected POIXMLDocument(OPCPackage pkg) : base(pkg) { this.pkg = pkg; }
public void Close() { if (container != null) { container.Close(); container = null; } base.close(); }