improved loading of package parts so that same logical parts correspond to the same physical instances, see followup in Bugzilla 47668

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@804303 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2009-08-14 17:25:22 +00:00
parent 76ccb2957d
commit 6fab98df5b
2 changed files with 12 additions and 9 deletions

View File

@ -192,7 +192,7 @@ public abstract class POIXMLDocument extends POIXMLDocumentPart{
}
protected final void load(POIXMLFactory factory) throws IOException {
Map<PackageRelationship, POIXMLDocumentPart> context = new HashMap<PackageRelationship, POIXMLDocumentPart>();
Map<PackagePart, POIXMLDocumentPart> context = new HashMap<PackagePart, POIXMLDocumentPart>();
try {
read(factory, context);
} catch (OpenXML4JException e){
@ -211,7 +211,7 @@ public abstract class POIXMLDocument extends POIXMLDocumentPart{
*/
public final void write(OutputStream stream) throws IOException {
//force all children to commit their changes into the underlying OOXML Package
Set<PackageRelationship> context = new HashSet<PackageRelationship>();
Set<PackagePart> context = new HashSet<PackagePart>();
onSave(context);
context.clear();

View File

@ -170,12 +170,14 @@ public class POIXMLDocumentPart {
/**
* Save changes in the underlying OOXML package.
* Recursively fires {@link #commit()} for each package part
*
* @param alreadySaved context set containing already visited nodes
*/
protected final void onSave(Set<PackageRelationship> alreadySaved) throws IOException{
protected final void onSave(Set<PackagePart> alreadySaved) throws IOException{
commit();
alreadySaved.add(this.getPackageRelationship());
alreadySaved.add(this.getPackagePart());
for(POIXMLDocumentPart p : relations){
if (!alreadySaved.contains(p.getPackageRelationship())) {
if (!alreadySaved.contains(p.getPackagePart())) {
p.onSave(alreadySaved);
}
}
@ -229,8 +231,9 @@ public class POIXMLDocumentPart {
* 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, Map<PackageRelationship, POIXMLDocumentPart> context) throws OpenXML4JException {
protected void read(POIXMLFactory factory, Map<PackagePart, POIXMLDocumentPart> context) throws OpenXML4JException {
PackageRelationshipCollection rels = packagePart.getRelationships();
for (PackageRelationship rel : rels) {
if(rel.getTargetMode() == TargetMode.INTERNAL){
@ -251,17 +254,17 @@ public class POIXMLDocumentPart {
}
}
if (!context.containsKey(rel)) {
if (!context.containsKey(p)) {
POIXMLDocumentPart childPart = factory.createDocumentPart(rel, p);
childPart.parent = this;
addRelation(childPart);
if(p != null){
context.put(rel, childPart);
context.put(p, childPart);
if(p.hasRelationships()) childPart.read(factory, context);
}
}
else {
addRelation(context.get(rel));
addRelation(context.get(p));
}
}
}