/** * Returns the {@link PackageRelationship#GetId()} of the * {@link PackageRelationship}, that sources from the {@link PackagePart} of * this {@link POIXMLDocumentPart} to the {@link PackagePart} of the given * parameter value. * * @param part * The {@link POIXMLDocumentPart} for which the according * relation-id shall be found. * @return The value of the {@link PackageRelationship#GetId()} or null, if * parts are not related. */ public String GetRelationId(POIXMLDocumentPart part) { foreach (KeyValuePair <String, POIXMLDocumentPart> entry in relations) { if (entry.Value == part) { return(entry.Key); } } return(null); }
public string GetRelationId(POIXMLDocumentPart part) { foreach (KeyValuePair <string, POIXMLDocumentPart> relation in this.relations) { if (relation.Value == part) { return(relation.Key); } } return((string)null); }
protected void Read(POIXMLFactory factory, Dictionary <PackagePart, POIXMLDocumentPart> context) { try { foreach (PackageRelationship relationship in this.packagePart.Relationships) { TargetMode?targetMode = relationship.TargetMode; if ((targetMode.GetValueOrDefault() != TargetMode.Internal ? 0 : (targetMode.HasValue ? 1 : 0)) != 0) { Uri targetUri = relationship.TargetUri; PackagePart index; if (targetUri.OriginalString.IndexOf('#') >= 0) { index = (PackagePart)null; } else { index = this.packagePart.Package.GetPart(PackagingUriHelper.CreatePartName(targetUri)); if (index == null) { POIXMLDocumentPart.logger.Log(7, (object)("Skipped invalid entry " + (object)relationship.TargetUri)); continue; } } if (index == null || !context.ContainsKey(index)) { POIXMLDocumentPart documentPart = factory.CreateDocumentPart(this, relationship, index); documentPart.parent = this; this.AddRelation(relationship.Id, documentPart); if (index != null) { context[index] = documentPart; if (index.HasRelationships) { documentPart.Read(factory, context); } } } else { this.AddRelation(relationship.Id, context[index]); } } } } catch (Exception ex) { if (ex.InnerException != null && ex.InnerException.InnerException != null) { POIXMLDocumentPart.logger.Log(1, ex.InnerException.InnerException); } throw; } }
/** * Add a new child POIXMLDocumentPart * * @param pr the relationship of the child * @param part the child to add */ private void AddRelation(PackageRelationship pr, POIXMLDocumentPart part) { if (relations.ContainsKey(pr.Id)) { relations[pr.Id] = new RelationPart(pr, part); } else { relations.Add(pr.Id, new RelationPart(pr, part)); } part.IncrementRelationCounter(); }
/** * Save Changes in the underlying OOXML namespace. * Recursively fires {@link #commit()} for each namespace part * * @param alreadySaved context set Containing already visited nodes */ protected internal void OnSave(List <PackagePart> alreadySaved) { // this usually clears out previous content in the part... PrepareForCommit(); Commit(); alreadySaved.Add(this.GetPackagePart()); foreach (RelationPart rp in relations.Values) { POIXMLDocumentPart p = rp.DocumentPart; if (!alreadySaved.Contains(p.GetPackagePart())) { p.OnSave(alreadySaved); } } }
/** * Recursively Traverse a OOXML document and assert that same logical parts have the same physical instances */ private void Traverse(POIXMLDocumentPart part, Dictionary<String, POIXMLDocumentPart> context) { context[part.GetPackageRelationship().TargetUri.ToString()] = part; foreach (POIXMLDocumentPart p in part.GetRelations()) { Assert.IsNotNull(p.ToString()); String uri = p.GetPackageRelationship().TargetUri.ToString(); if (!context.ContainsKey(uri)) { Traverse(p, context); } else { POIXMLDocumentPart prev = context[uri]; Assert.AreSame(prev, p, "Duplicate POIXMLDocumentPart instance for targetURI=" + uri); } } }
/** * Retrieves the package relationship of the child part within the parent * * @since POI 3.14-Beta1 */ protected PackageRelationship GetPackageRelationship(POIXMLDocumentPart parent, PackagePart part) { try { String partName = part.PartName.Name; foreach (PackageRelationship pr in parent.GetPackagePart().Relationships) { String packName = pr.TargetUri.OriginalString;// toASCIIString(); if (packName.Equals(partName, StringComparison.CurrentCultureIgnoreCase)) { return(pr); } } } catch (InvalidFormatException e) { throw new POIXMLException("error while determining package relations", e); } throw new POIXMLException("package part isn't a child of the parent document."); }
/** * Returns the {@link PackageRelationship#GetId()} of the * {@link PackageRelationship}, that sources from the {@link PackagePart} of * this {@link POIXMLDocumentPart} to the {@link PackagePart} of the given * parameter value. * * @param part * The {@link POIXMLDocumentPart} for which the according * relation-id shall be found. * @return The value of the {@link PackageRelationship#GetId()} or null, if * parts are not related. */ public String GetRelationId(POIXMLDocumentPart part) { foreach (KeyValuePair<String, POIXMLDocumentPart> entry in relations) { if (entry.Value == part) { return entry.Key; } } return null; }
/** * Iterate through the underlying PackagePart and create child POIXMLFactory instances * using the specified factory * * @param factory the factory object that Creates POIXMLFactory instances * @param context context map Containing already visited noted keyed by tarGetURI */ protected void Read(POIXMLFactory factory, Dictionary <PackagePart, POIXMLDocumentPart> context) { PackagePart pp = GetPackagePart(); // add mapping a second time, in case of initial caller hasn't done so POIXMLDocumentPart otherChild = PutDictionary(context, pp, this); if (otherChild != null && otherChild != this) { throw new POIXMLException("Unique PackagePart-POIXMLDocumentPart relation broken!"); } if (!pp.HasRelationships) { return; } PackageRelationshipCollection rels = packagePart.Relationships; List <POIXMLDocumentPart> readLater = new List <POIXMLDocumentPart>(); // scan breadth-first, so parent-relations are hopefully the shallowest element foreach (PackageRelationship rel in rels) { if (rel.TargetMode == TargetMode.Internal) { Uri uri = rel.TargetUri; // check for internal references (e.g. '#Sheet1!A1') PackagePartName relName; //if (uri.getRawFragment() != null) if (uri.OriginalString.IndexOf('#') >= 0) { string path = string.Empty; try { path = uri.AbsolutePath; } catch (InvalidOperationException) { path = uri.OriginalString.Substring(0, uri.OriginalString.IndexOf('#')); } relName = PackagingUriHelper.CreatePartName(path); } else { relName = PackagingUriHelper.CreatePartName(uri); } PackagePart p = packagePart.Package.GetPart(relName); if (p == null) { //logger.log(POILogger.ERROR, "Skipped invalid entry " + rel.TargetUri); continue; } POIXMLDocumentPart childPart = GetDictionary(context, p); if (childPart == null) { childPart = factory.CreateDocumentPart(this, p); childPart.parent = this; // already add child to context, so other children can reference it PutDictionary(context, p, childPart); readLater.Add(childPart); } AddRelation(rel, childPart); } } foreach (POIXMLDocumentPart childPart in readLater) { childPart.Read(factory, context); } }
/** * 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; }
internal RelationPart(PackageRelationship relationship, POIXMLDocumentPart documentPart) { this.relationship = relationship; this.documentPart = documentPart; }
public void TestCommitNullPart() { POIXMLDocumentPart part = new POIXMLDocumentPart(); part.PrepareForCommit(); part.Commit(); part.OnSave(new List<PackagePart>()); Assert.IsNull(part.GetRelationById(null)); Assert.IsNull(part.GetRelationId(null)); Assert.IsFalse(part.RemoveRelation(null, true)); part.RemoveRelation(null); Assert.AreEqual(string.Empty, part.ToString()); part.OnDocumentCreate(); //part.GetTargetPart(null); }
/** * Remove the relation to the specified part in this namespace and remove the * part, if it is no longer needed. */ protected internal void RemoveRelation(POIXMLDocumentPart part) { RemoveRelation(part, true); }
protected void RemoveRelation(POIXMLDocumentPart part) { this.RemoveRelation(part, true); }
public override POIXMLDocumentPart CreateDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part) { return new POIXMLDocumentPart(part, rel); }
/** * Remove the relation to the specified part in this namespace and remove the * part, if it is no longer needed and flag is set to true. * * @param part * The related part, to which the relation shall be Removed. * @param RemoveUnusedParts * true, if the part shall be Removed from the namespace if not * needed any longer. */ protected bool RemoveRelation(POIXMLDocumentPart part, bool RemoveUnusedParts) { String id = GetRelationId(part); if (id == null) { // part is not related with this POIXMLDocumentPart return false; } /* decrement usage counter */ part.DecrementRelationCounter(); /* remove namespacepart relationship */ GetPackagePart().RemoveRelationship(id); /* remove POIXMLDocument from relations */ relations.Remove(id); if (RemoveUnusedParts) { /* if last relation to target part was Removed, delete according target part */ if (part.GetRelationCounter() == 0) { try { part.onDocumentRemove(); } catch (IOException e) { throw new POIXMLException(e); } GetPackagePart().Package.RemovePart(part.GetPackagePart()); } } return true; }
/** * Add a new child POIXMLDocumentPart * * @param part the child to add */ public void AddRelation(String id, POIXMLDocumentPart part) { relations[id] = part; part.IncrementRelationCounter(); }
public virtual POIXMLDocumentPart CreateDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part) { return(CreateDocumentPart(parent, part)); }
/** * Create a POIXMLDocumentPart from existing namespace part and relation. This method is called * from {@link POIXMLDocument#load(POIXMLFactory)} when parsing a document * * @param parent parent part * @param rel the namespace part relationship * @param part the PackagePart representing the Created instance * @return A new instance of a POIXMLDocumentPart. */ public abstract POIXMLDocumentPart CreateDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part);
/** * Remove the relation to the specified part in this namespace and remove the * part, if it is no longer needed. */ protected void RemoveRelation(POIXMLDocumentPart part) { RemoveRelation(part, true); }
/** * Creates an POIXMLDocumentPart representing the given package part, relationship and parent * Called by {@link #read(POIXMLFactory, java.util.Map)} when reading in an existing file. * * @param parent - Parent part * @param part - The package part that holds xml data representing this sheet. * @see #read(POIXMLFactory, java.util.Map) * * @since POI 3.14-Beta1 */ public POIXMLDocumentPart(POIXMLDocumentPart parent, PackagePart part) { this.packagePart = part; this.parent = parent; }
public void AddRelation(String id, POIXMLDocumentPart part) { PackageRelationship pr = part.GetPackagePart().GetRelationship(id); AddRelation(pr, part); }
/** * Add a new child POIXMLDocumentPart * * @param relId the preferred relation id, when null the next free relation id will be used * @param relationshipType the package relationship type * @param part the child to add * * @since 3.14-Beta1 */ public RelationPart AddRelation(String relId, POIXMLRelation relationshipType, POIXMLDocumentPart part) { PackageRelationship pr = FindExistingRelation(part); if (pr == null) { PackagePartName ppn = part.GetPackagePart().PartName; String relType = relationshipType.Relation; pr = packagePart.AddRelationship(ppn, TargetMode.Internal, relType, relId); } AddRelation(pr, part); return(new RelationPart(pr, part)); }
/** * Iterate through the underlying PackagePart and create child POIXMLFactory instances * using the specified factory * * @param factory the factory object that Creates POIXMLFactory instances * @param context context map Containing already visited noted keyed by tarGetURI */ protected void Read(POIXMLFactory factory, Dictionary <PackagePart, POIXMLDocumentPart> context) { try { PackageRelationshipCollection rels = packagePart.Relationships; foreach (PackageRelationship rel in rels) { if (rel.TargetMode == TargetMode.Internal) { Uri uri = rel.TargetUri; PackagePart p; if (uri.OriginalString.IndexOf('#') >= 0) { /* * For internal references (e.g. '#Sheet1!A1') the namespace part is null */ p = null; } else { PackagePartName relName = PackagingUriHelper.CreatePartName(uri); p = packagePart.Package.GetPart(relName); if (p == null) { logger.Log(POILogger.ERROR, "Skipped invalid entry " + rel.TargetUri); continue; } } if (p == null || !context.ContainsKey(p)) { POIXMLDocumentPart childPart = factory.CreateDocumentPart(this, rel, p); childPart.parent = this; AddRelation(rel.Id, childPart); if (p != null) { context[p] = childPart; if (p.HasRelationships) { childPart.Read(factory, context); } } } else { AddRelation(rel.Id, context[p]); } } } } catch (Exception ex) { if ((null != ex.InnerException) && (null != ex.InnerException.InnerException)) { // this type of exception is thrown when the XML Serialization does not match the input. logger.Log(1, ex.InnerException.InnerException); } throw; } }