/** * Create a new child POIXMLDocumentPart * * @param descriptor the part descriptor * @param factory the factory that will create an instance of the requested relation * @param idx part number * @param noRelation if true, then no relationship is Added. * @return the Created child POIXMLDocumentPart */ protected POIXMLDocumentPart CreateRelationship(POIXMLRelation descriptor, POIXMLFactory factory, int idx, bool noRelation) { try { PackagePartName ppName = PackagingUriHelper.CreatePartName(descriptor.GetFileName(idx)); PackageRelationship rel = null; PackagePart part = packagePart.Package.CreatePart(ppName, descriptor.ContentType); if (!noRelation) { /* only add to relations, if according relationship is being Created. */ rel = packagePart.AddRelationship(ppName, TargetMode.Internal, descriptor.Relation); } POIXMLDocumentPart doc = factory.CreateDocumentPart(descriptor); doc.packageRel = rel; doc.packagePart = part; doc.parent = this; if (!noRelation) { /* only add to relations, if according relationship is being Created. */ AddRelation(rel.Id, doc); } return(doc); } catch (PartAlreadyExistsException pae) { // Return the specific exception so the user knows // that the name is already taken throw pae; } catch (Exception e) { // Give a general wrapped exception for the problem throw new POIXMLException(e); } }
/** * 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; } }