public override POIXMLDocumentPart CreateDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part) { POIXMLRelation descriptor = XWPFRelation.GetInstance(rel.RelationshipType); if (descriptor == null || descriptor.RelationClass == null) { logger.Log(POILogger.DEBUG, "using default POIXMLDocumentPart for " + rel.RelationshipType); return new POIXMLDocumentPart(part, rel); } try { Type cls = descriptor.RelationClass; try { ConstructorInfo constructor = cls.GetConstructor(new Type[] { typeof(POIXMLDocumentPart), typeof(PackagePart), typeof(PackageRelationship) }); return constructor.Invoke(new object[] { parent, part, rel }) as POIXMLDocumentPart; } catch (Exception) { ConstructorInfo constructor = cls.GetConstructor(new Type[] { typeof(PackagePart), typeof(PackageRelationship) }); return constructor.Invoke(new object[] { part, rel }) as POIXMLDocumentPart; } } catch (Exception e) { throw new POIXMLException(e); } }
/** * 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; }
internal CommentsTable(PackagePart part, PackageRelationship rel) : base(part, rel) { XmlDocument xml = ConvertStreamToXml(part.GetInputStream()); ReadFrom(xml); }
/** * Construct a SpreadsheetML chart from a namespace part. * * @param part the namespace part holding the chart data, * the content type must be <code>application/vnd.Openxmlformats-officedocument.Drawingml.chart+xml</code> * @param rel the namespace relationship holding this chart, * the relationship type must be http://schemas.Openxmlformats.org/officeDocument/2006/relationships/chart */ protected XSSFChart(PackagePart part, PackageRelationship rel) : base(part, rel) { XmlDocument doc = ConvertStreamToXml(part.GetInputStream()); chartSpaceDocument = ChartSpaceDocument.Parse(doc, NamespaceManager); chart = chartSpaceDocument.GetChartSpace().chart; }
/** * Construct a SpreadsheetML chart from a namespace part. * * @param part the namespace part holding the chart data, * the content type must be <code>application/vnd.Openxmlformats-officedocument.Drawingml.chart+xml</code> * @param rel the namespace relationship holding this chart, * the relationship type must be http://schemas.Openxmlformats.org/officeDocument/2006/relationships/chart */ protected XSSFChart(PackagePart part, PackageRelationship rel) : base(part, rel) { chartSpace = ChartSpaceDocument.Parse(part.GetInputStream()).GetChartSpace(); chart = chartSpace.chart; }
/** * Construct a SpreadsheetML Drawing from a namespace part * * @param part the namespace part holding the Drawing data, * the content type must be <code>application/vnd.openxmlformats-officedocument.Drawing+xml</code> * @param rel the namespace relationship holding this Drawing, * the relationship type must be http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing */ internal XSSFDrawing(PackagePart part, PackageRelationship rel) : base(part, rel) { //XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS); ////Removing root element //options.setLoadReplaceDocumentElement(null); //drawing = CTDrawing.Factory.parse(part.getInputStream(), options); drawing = NPOI.OpenXmlFormats.Dml.Spreadsheet.CT_Drawing.Parse(part.GetInputStream()); }
/** * Save the specified part. * * @throws OpenXml4NetException * Throws if an internal exception is thrown. */ public bool Marshall(PackagePart part, Stream os) { if (!(os is ZipOutputStream)) { logger.Log(POILogger.ERROR,"Unexpected class " + os.GetType().Name); throw new OpenXml4NetException("ZipOutputStream expected !"); // Normally should happen only in developement phase, so just throw // exception } ZipOutputStream zos = (ZipOutputStream)os; string name = ZipHelper .GetZipItemNameFromOPCName(part.PartName.URI .OriginalString); ZipEntry partEntry = new ZipEntry(name); try { // Create next zip entry zos.PutNextEntry(partEntry); // Saving data in the ZIP file Stream ins = part.GetInputStream(); byte[] buff = new byte[ZipHelper.READ_WRITE_FILE_BUFFER_SIZE]; int totalRead = 0; while (true) { int resultRead = ins.Read(buff, 0, buff.Length); if (resultRead == 0) { // End of file reached break; } zos.Write(buff, 0, resultRead); totalRead += resultRead; } zos.CloseEntry(); } catch (IOException ioe) { logger.Log(POILogger.ERROR, "Cannot write: " + part.PartName + ": in ZIP", ioe); return false; } // Saving relationship part if (part.HasRelationships) { PackagePartName relationshipPartName = PackagingUriHelper .GetRelationshipPartName(part.PartName); MarshallRelationshipPart(part.Relationships, relationshipPartName, zos); } return true; }
public XWPFHeaderFooter(POIXMLDocumentPart parent, PackagePart part, PackageRelationship rel) : base(parent, part, rel) { ; this.document = (XWPFDocument)GetParent(); if (this.document == null) { throw new NullReferenceException(); } }
internal ThemesTable(PackagePart part, PackageRelationship rel) : base(part, rel) { //theme = ThemeDocument.Parse(part.GetInputStream()); try { theme = ThemeDocument.Parse(part.GetInputStream()); } catch (XmlException e) { throw new IOException(e.Message); } }
/** * Construct a ThemesTable. * @param part A PackagePart. * @param rel A PackageRelationship. */ internal ThemesTable(PackagePart part, PackageRelationship rel) : base(part, rel) { XmlDocument xmldoc = ConvertStreamToXml(part.GetInputStream()); try { theme = ThemeDocument.Parse(xmldoc, NamespaceManager); } catch (XmlException e) { throw new IOException(e.Message); } }
/** * 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()); } }
/** * Return the dimension of this image * * @param part the namespace part holding raw picture data * @param type type of the picture: {@link Workbook#PICTURE_TYPE_JPEG}, * {@link Workbook#PICTURE_TYPE_PNG} or {@link Workbook#PICTURE_TYPE_DIB} * * @return image dimension in pixels */ protected static Size GetImageDimension(PackagePart part, PictureType type) { try { //return Image.FromStream(part.GetInputStream()).Size; //java can only read png,jpeg,dib image //C# read the image that format defined by PictureType , maybe. return ImageUtils.GetImageDimension(part.GetInputStream()); } catch (IOException e) { //return a "singulariry" if ImageIO failed to read the image logger.Log(POILogger.WARN, e); return new Size(); } }
/** * Construct XWPFPictureData from a package part * * @param part the package part holding the Drawing data, * @param rel the package relationship holding this Drawing, * the relationship type must be http://schemas.Openxmlformats.org/officeDocument/2006/relationships/image */ public XWPFPictureData(PackagePart part, PackageRelationship rel) : base(part, rel) { }
public XWPFHeader(POIXMLDocumentPart parent, PackagePart part, PackageRelationship rel) : base(parent, part, rel) { ; }
internal MapInfo(PackagePart part, PackageRelationship rel) : base(part, rel) { ReadFrom(part.GetInputStream()); }
/** * Construct XSSFPictureData from a namespace part * * @param part the namespace part holding the Drawing data, * @param rel the namespace relationship holding this Drawing, * the relationship type must be http://schemas.Openxmlformats.org/officeDocument/2006/relationships/image */ internal XSSFPictureData(PackagePart part, PackageRelationship rel) : base(part, rel) { }
/** * Construct XWPFStyles from a package part * * @param part the package part holding the data of the styles, * @param rel the package relationship of type "http://schemas.Openxmlformats.org/officeDocument/2006/relationships/styles" */ public XWPFStyles(PackagePart part, PackageRelationship rel) : base(part, rel) { }
/** * Parse the relationship part and add all relationship in this collection. * * @param relPart * The package part to parse. * @throws InvalidFormatException * Throws if the relationship part is invalid. */ private void ParseRelationshipsPart(PackagePart relPart) { try { logger.Log(POILogger.DEBUG, "Parsing relationship: " + relPart.PartName); XPathDocument xmlRelationshipsDoc = DocumentHelper.ReadDocument(relPart.GetInputStream()); // Check OPC compliance M4.1 rule bool fCorePropertiesRelationship = false; ///xmlRelationshipsDoc.ChildNodes.GetEnumerator(); XPathNavigator xpathnav = xmlRelationshipsDoc.CreateNavigator(); XmlNamespaceManager nsMgr = new XmlNamespaceManager(xpathnav.NameTable); nsMgr.AddNamespace("x", PackageNamespaces.RELATIONSHIPS); XPathNodeIterator iterator = xpathnav.Select("//x:" + PackageRelationship.RELATIONSHIP_TAG_NAME, nsMgr); while (iterator.MoveNext()) { // Relationship ID String id = iterator.Current.GetAttribute(PackageRelationship.ID_ATTRIBUTE_NAME, xpathnav.NamespaceURI); // Relationship type String type = iterator.Current.GetAttribute( PackageRelationship.TYPE_ATTRIBUTE_NAME, xpathnav.NamespaceURI); /* Check OPC Compliance */ // Check Rule M4.1 if (type.Equals(PackageRelationshipTypes.CORE_PROPERTIES)) { if (!fCorePropertiesRelationship) { fCorePropertiesRelationship = true; } else { throw new InvalidFormatException( "OPC Compliance error [M4.1]: there is more than one core properties relationship in the package !"); } } /* End OPC Compliance */ // TargetMode (default value "Internal") string targetModeAttr = iterator.Current.GetAttribute(PackageRelationship.TARGET_MODE_ATTRIBUTE_NAME, xpathnav.NamespaceURI); TargetMode targetMode = TargetMode.Internal; if (targetModeAttr != string.Empty) { targetMode = targetModeAttr.ToLower() .Equals("internal") ? TargetMode.Internal : TargetMode.External; } // Target converted in URI Uri target = PackagingUriHelper.ToUri("http://invalid.uri"); // dummy url String value = iterator.Current.GetAttribute( PackageRelationship.TARGET_ATTRIBUTE_NAME, xpathnav.NamespaceURI);; try { // when parsing of the given uri fails, we can either // ignore this relationship, which leads to IllegalStateException // later on, or use a dummy value and thus enable processing of the // package target = PackagingUriHelper.ToUri(value); } catch (UriFormatException e) { logger.Log(POILogger.ERROR, "Cannot convert " + value + " in a valid relationship URI-> dummy-URI used", e); } AddRelationship(target, targetMode, type, id); } } catch (Exception e) { logger.Log(POILogger.ERROR, e); throw new InvalidFormatException(e.Message); } }
/** * Retrieves all the PackageParts which are defined as * relationships of the base document with the * specified content type. */ protected PackagePart[] GetRelatedByType(String contentType) { PackageRelationshipCollection partsC = GetPackagePart().GetRelationshipsByType(contentType); PackagePart[] parts = new PackagePart[partsC.Size]; int count = 0; foreach (PackageRelationship rel in partsC) { parts[count] = GetTargetPart(rel); count++; } return parts; }
internal SharedStringsTable(PackagePart part, PackageRelationship rel) : base(part, rel) { ReadFrom(part.GetInputStream()); }
/** * Retrieves the parts from this package. We assume that the package has not * been yet inspect to retrieve all the parts, this method will open the * archive and look for all parts contain inside it. If the package part * list is not empty, it will be emptied. * * @return All parts contain in this package. * @throws InvalidFormatException * Throws if the package is not valid. */ protected override PackagePart[] GetPartsImpl() { if (this.partList == null) { // The package has just been created, we create an empty part // list. this.partList = new PackagePartCollection(); } if (this.zipArchive == null) { PackagePart[] pp = new PackagePart[this.partList.Values.Count]; this.partList.Values.CopyTo(pp, 0); return(pp); } // First we need to parse the content type part IEnumerator entries = this.zipArchive.Entries; while (entries.MoveNext()) { ZipEntry entry = (ZipEntry)entries.Current; if (entry.Name.ToLower().Equals( ContentTypeManager.CONTENT_TYPES_PART_NAME.ToLower())) { try { this.contentTypeManager = new ZipContentTypeManager( ZipArchive.GetInputStream(entry), this); } catch (IOException e) { throw new InvalidFormatException(e.Message); } break; } } // At this point, we should have loaded the content type part if (this.contentTypeManager == null) { throw new InvalidFormatException( "Package should contain a content type part [M1.13]"); } // Now create all the relationships // (Need to create relationships before other // parts, otherwise we might create a part before // its relationship exists, and then it won't tie up) entries = this.zipArchive.Entries; while (entries.MoveNext()) { ZipEntry entry = (ZipEntry)entries.Current; PackagePartName partName = BuildPartName(entry); if (partName == null) { continue; } // Only proceed for Relationships at this stage String contentType = contentTypeManager.GetContentType(partName); if (contentType != null && contentType.Equals(ContentTypes.RELATIONSHIPS_PART)) { try { partList[partName] = new ZipPackagePart(this, entry, partName, contentType); } catch (InvalidOperationException e) { throw new InvalidFormatException(e.Message); } } } // Then we can go through all the other parts entries = this.zipArchive.Entries; while (entries.MoveNext()) { ZipEntry entry = entries.Current as ZipEntry; PackagePartName partName = BuildPartName(entry); if (partName == null) { continue; } String contentType = contentTypeManager .GetContentType(partName); if (contentType != null && contentType.Equals(ContentTypes.RELATIONSHIPS_PART)) { // Already handled } else if (contentType != null) { try { partList[partName] = new ZipPackagePart(this, entry, partName, contentType); } catch (InvalidOperationException e) { throw new InvalidFormatException(e.Message); } } else { throw new InvalidFormatException( "The part " + partName.URI.OriginalString + " does not have any content type ! Rule: Package require content types when retrieving a part from a package. [M.1.14]"); } } ZipPackagePart[] returnArray = new ZipPackagePart[partList.Count]; partList.Values.CopyTo(returnArray, 0); return(returnArray); }
/** * Creates an POIXMLDocumentPart representing the given namespace part, relationship and parent * Called by {@link #read(POIXMLFactory, java.util.Map)} when Reading in an exisiting file. * * @param parent - Parent part * @param part - The namespace part that holds xml data represenring this sheet. * @param rel - the relationship of the given namespace part * @see #read(POIXMLFactory, java.util.Map) */ public POIXMLDocumentPart(POIXMLDocumentPart parent, PackagePart part, PackageRelationship rel) { this.packagePart = part; this.packageRel = rel; this.parent = parent; }
/** * Return the dimension of this image * * @param part the namespace part holding raw picture data * @param type type of the picture: {@link Workbook#PICTURE_TYPE_JPEG}, * {@link Workbook#PICTURE_TYPE_PNG} or {@link Workbook#PICTURE_TYPE_DIB} * * @return image dimension in pixels */ protected static Size GetImageDimension(PackagePart part, int type) { try { return Image.FromStream(part.GetInputStream()).Size; } catch (IOException e) { //return a "singulariry" if ImageIO failed to read the image logger.Log(POILogger.WARN, e); return new Size(); } }
/** * Create an OPC compliant part name by resolving it using a base part. * * @param partName * The part name Uri to validate. * @param relativePart * The relative base part. * @return The correspondant part name if valid, else <code>null</code>. * @throws InvalidFormatException * Throws if the specified part name is not OPC compliant. * @see #CreatePartName(Uri) */ public static PackagePartName CreatePartName(Uri partName, PackagePart relativePart) { Uri newPartNameURI = ResolvePartUri( relativePart.PartName.URI, partName); return CreatePartName(newPartNameURI); }
/** * 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); }
/** * Creates an XSSFSheet representing the given namespace part and relationship. * Should only be called by XSSFWorkbook when Reading in an exisiting file. * * @param part - The namespace part that holds xml data represenring this sheet. * @param rel - the relationship of the given namespace part in the underlying OPC namespace */ internal XSSFSheet(PackagePart part, PackageRelationship rel) : base(part, rel) { dataValidationHelper = new XSSFDataValidationHelper(this); }
/** * Retrieves the parts from this package. We assume that the package has not * been yet inspect to retrieve all the parts, this method will open the * archive and look for all parts contain inside it. If the package part * list is not empty, it will be emptied. * * @return All parts contain in this package. * @throws InvalidFormatException * Throws if the package is not valid. */ protected override PackagePart[] GetPartsImpl(){ if (this.partList == null) { // The package has just been created, we create an empty part // list. this.partList = new PackagePartCollection(); } if (this.zipArchive == null) { PackagePart[] pp = new PackagePart[this.partList.Values.Count]; this.partList.Values.CopyTo(pp, 0); return pp; } // First we need to parse the content type part IEnumerator entries = this.zipArchive.Entries; while (entries.MoveNext()) { ZipEntry entry = (ZipEntry)entries.Current; if (entry.Name.ToLower().Equals( ContentTypeManager.CONTENT_TYPES_PART_NAME.ToLower())) { try { this.contentTypeManager = new ZipContentTypeManager( ZipArchive.GetInputStream(entry), this); } catch (IOException e) { throw new InvalidFormatException(e.Message); } break; } } // At this point, we should have loaded the content type part if (this.contentTypeManager == null) { throw new InvalidFormatException( "Package should contain a content type part [M1.13]"); } // Now create all the relationships // (Need to create relationships before other // parts, otherwise we might create a part before // its relationship exists, and then it won't tie up) entries = this.zipArchive.Entries; while (entries.MoveNext()) { ZipEntry entry = (ZipEntry)entries.Current; PackagePartName partName = BuildPartName(entry); if(partName == null) continue; // Only proceed for Relationships at this stage String contentType = contentTypeManager.GetContentType(partName); if (contentType != null && contentType.Equals(ContentTypes.RELATIONSHIPS_PART)) { try { partList[partName]= new ZipPackagePart(this, entry, partName, contentType); } catch (InvalidOperationException e) { throw new InvalidFormatException(e.Message); } } } // Then we can go through all the other parts entries = this.zipArchive.Entries; while (entries.MoveNext()) { ZipEntry entry = entries.Current as ZipEntry; PackagePartName partName = BuildPartName(entry); if(partName == null) continue; String contentType = contentTypeManager .GetContentType(partName); if (contentType != null && contentType.Equals(ContentTypes.RELATIONSHIPS_PART)) { // Already handled } else if (contentType != null) { try { partList[partName]= new ZipPackagePart(this, entry, partName, contentType); } catch (InvalidOperationException e) { throw new InvalidFormatException(e.Message); } } else { throw new InvalidFormatException( "The part " + partName.URI.OriginalString + " does not have any content type ! Rule: Package require content types when retrieving a part from a package. [M.1.14]"); } } ZipPackagePart[] returnArray =new ZipPackagePart[partList.Count]; partList.Values.CopyTo(returnArray,0); return returnArray; }
/** * Construct a SpreadsheetML Drawing from a namespace part * * @param part the namespace part holding the Drawing data, * the content type must be <code>application/vnd.Openxmlformats-officedocument.Drawing+xml</code> * @param rel the namespace relationship holding this Drawing, * the relationship type must be http://schemas.Openxmlformats.org/officeDocument/2006/relationships/drawing */ protected XSSFVMLDrawing(PackagePart part, PackageRelationship rel) : base(part, rel) { Read(GetPackagePart().GetInputStream()); }
/** * Construct a SpreadsheetML Drawing from a namespace part * * @param part the namespace part holding the Drawing data, * the content type must be <code>application/vnd.openxmlformats-officedocument.Drawing+xml</code> * @param rel the namespace relationship holding this Drawing, * the relationship type must be http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing */ internal XSSFDrawing(PackagePart part, PackageRelationship rel) : base(part, rel) { XmlDocument xmldoc = ConvertStreamToXml(part.GetInputStream()); drawing = NPOI.OpenXmlFormats.Dml.Spreadsheet.CT_Drawing.Parse(xmldoc, NamespaceManager); }
public XWPFSettings(PackagePart part, PackageRelationship rel) : base(part, rel) { }
/** * Creates an XSSFPivotTable representing the given package part and relationship. * Should only be called when Reading in an existing file. * * @param part - The package part that holds xml data representing this pivot table. * @param rel - the relationship of the given package part in the underlying OPC package */ protected XSSFPivotTable(PackagePart part, PackageRelationship rel) : base(part, rel) { ReadFrom(part.GetInputStream()); }
/** * Constructor. * * @throws InvalidFormatException * Throws if the format of the content part is invalid. * * @throws InvalidOperationException * Throws if the specified part is a relationship part. */ public PackageRelationshipCollection(PackagePart part) : this(part._container, part) { }