/// <summary> /// Add a new child POIXMLDocumentPart /// </summary> /// <param name="relId">the preferred relation id, when null the next free relation id will be used</param> /// <param name="relationshipType">the package relationship type</param> /// <param name="part">the child to add</param> /// <returns></returns> public RelationPart AddRelation(String relId, POIXMLRelation relationshipType, POIXMLDocumentPart part) { PackageRelationship pr = this.packagePart.FindExistingRelation(part.GetPackagePart()); 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)); }
/** * 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 internal 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); }
/// <summary> /// Check if the new part was already added before via PackagePart.addRelationship() /// </summary> /// <param name="part"> to find the relationship for</param> /// <returns>The existing relationship, or null if there isn't yet one</returns> private PackageRelationship FindExistingRelation(POIXMLDocumentPart part) { String ppn = part.GetPackagePart().PartName.Name; try { foreach (PackageRelationship pr in packagePart.Relationships) { PackagePart pp = packagePart.GetRelatedPart(pr); if (pr.TargetMode == TargetMode.External) { continue; } if (ppn.Equals(pp.PartName.Name)) { return(pr); } } } catch (InvalidFormatException e) { throw new POIXMLException("invalid package relationships", e); } return(null); }
/** * Returns the index of the filename within the package for the given part. * e.g. 4 for /xl/comments4.xml */ public int GetFileNameIndex(POIXMLDocumentPart part) { Regex regex = new Regex(_defaultName.Replace("#", "(\\d+)")); return(int.Parse(regex.Match(part.GetPackagePart().PartName.Name).Value)); //return Integer.valueOf(part.getPackagePart().getPartName().getName().replaceAll(regex, "$1")); }
protected bool RemoveRelation(POIXMLDocumentPart part, bool RemoveUnusedParts) { string relationId = this.GetRelationId(part); if (relationId == null) { return(false); } part.DecrementRelationCounter(); this.GetPackagePart().RemoveRelationship(relationId); this.relations.Remove(relationId); if (RemoveUnusedParts) { if (part.GetRelationCounter() == 0) { try { part.onDocumentRemove(); } catch (IOException ex) { throw new POIXMLException((Exception)ex); } this.GetPackagePart().Package.RemovePart(part.GetPackagePart()); } } return(true); }
/** * 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); } } }
/** * 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."); }
/** * 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; }
public void AddRelation(String id, POIXMLDocumentPart part) { PackageRelationship pr = part.GetPackagePart().GetRelationship(id); AddRelation(pr, part); }