poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r755699 [4/7] - in /poi/trunk/src: examples/src/org/apache/poi/xssf/eventusermodel/examples/ ooxml/java/org/apache/poi/ ooxml/java/org/apache/poi/dev/ ooxml/java/org/apache/poi/extractor/ ooxml/java/org/apache/poi/openxml4j/opc/ ooxml/java/...
Date Wed, 18 Mar 2009 18:54:02 GMT
Modified: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java?rev=755699&r1=755698&r2=755699&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java Wed Mar 18 18:54:01 2009
@@ -1,451 +1,451 @@
-/* ====================================================================
-   Licensed to the Apache Software Foundation (ASF) under one or more
-   contributor license agreements.  See the NOTICE file distributed with
-   this work for additional information regarding copyright ownership.
-   The ASF licenses this file to You under the Apache License, Version 2.0
-   (the "License"); you may not use this file except in compliance with
-   the License.  You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-==================================================================== */
-package org.apache.poi.openxml4j.opc;
-
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.TreeMap;
-
-import org.dom4j.Attribute;
-import org.dom4j.Document;
-import org.dom4j.Element;
-import org.dom4j.io.SAXReader;
-import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
-import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
-import org.apache.poi.util.POILogger;
-import org.apache.poi.util.POILogFactory;
-
-/**
- * Represents a collection of PackageRelationship elements that are owned by a
- * given PackagePart or the Package.
- * 
- * @author Julien Chable, CDubettier
- * @version 0.1
- */
-public final class PackageRelationshipCollection implements
-		Iterable<PackageRelationship> {
-
-    private static POILogger logger = POILogFactory.getLogger(PackageRelationshipCollection.class);
-
-	/**
-	 * Package relationships ordered by ID.
-	 */
-	private TreeMap<String, PackageRelationship> relationshipsByID;
-
-	/**
-	 * Package relationships ordered by type.
-	 */
-	private TreeMap<String, PackageRelationship> relationshipsByType;
-
-	/**
-	 * This relationshipPart.
-	 */
-	private PackagePart relationshipPart;
-
-	/**
-	 * Source part.
-	 */
-	private PackagePart sourcePart;
-
-	/**
-	 * This part name.
-	 */
-	private PackagePartName partName;
-
-	/**
-	 * Reference to the package.
-	 */
-	private Package container;
-
-	/**
-	 * Constructor.
-	 */
-	PackageRelationshipCollection() {
-		relationshipsByID = new TreeMap<String, PackageRelationship>();
-		relationshipsByType = new TreeMap<String, PackageRelationship>();
-	}
-
-	/**
-	 * Copy constructor.
-	 * 
-	 * This collection will contain only elements from the specified collection
-	 * for which the type is compatible with the specified relationship type
-	 * filter.
-	 * 
-	 * @param coll
-	 *            Collection to import.
-	 * @param filter
-	 *            Relationship type filter.
-	 */
-	public PackageRelationshipCollection(PackageRelationshipCollection coll,
-			String filter) {
-		this();
-		for (PackageRelationship rel : coll.relationshipsByID.values()) {
-			if (filter == null || rel.getRelationshipType().equals(filter))
-				addRelationship(rel);
-		}
-	}
-
-	/**
-	 * Constructor.
-	 */
-	public PackageRelationshipCollection(Package container)
-			throws InvalidFormatException {
-		this(container, null);
-	}
-
-	/**
-	 * 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)
-			throws InvalidFormatException {
-		this(part.container, part);
-	}
-
-	/**
-	 * Constructor. Parse the existing package relationship part if one exists.
-	 * 
-	 * @param container
-	 *            The parent package.
-	 * @param part
-	 *            The part that own this relationships collection. If <b>null</b>
-	 *            then this part is considered as the package root.
-	 * @throws InvalidFormatException
-	 *             If an error occurs during the parsing of the relatinships
-	 *             part fo the specified part.
-	 */
-	public PackageRelationshipCollection(Package container, PackagePart part)
-			throws InvalidFormatException {
-		this();
-
-		if (container == null)
-			throw new IllegalArgumentException("container");
-
-		// Check if the specified part is not a relationship part
-		if (part != null && part.isRelationshipPart())
-			throw new IllegalArgumentException("part");
-
-		this.container = container;
-		this.sourcePart = part;
-		this.partName = getRelationshipPartName(part);
-		if ((container.getPackageAccess() != PackageAccess.WRITE)
-				&& container.containPart(this.partName)) {
-			relationshipPart = container.getPart(this.partName);
-			parseRelationshipsPart(relationshipPart);
-		}
-	}
-
-	/**
-	 * Get the relationship part name of the specified part.
-	 * 
-	 * @param part
-	 *            The part .
-	 * @return The relationship part name of the specified part. Be careful,
-	 *         only the correct name is returned, this method does not check if
-	 *         the part really exist in a package !
-	 * @throws InvalidOperationException
-	 *             Throws if the specified part is a relationship part.
-	 */
-	private static PackagePartName getRelationshipPartName(PackagePart part)
-			throws InvalidOperationException {
-		PackagePartName partName;
-		if (part == null) {
-			partName = PackagingURIHelper.PACKAGE_ROOT_PART_NAME;
-		} else {
-			partName = part.getPartName();
-		}
-		return PackagingURIHelper.getRelationshipPartName(partName);
-	}
-
-	/**
-	 * Add the specified relationship to the collection.
-	 * 
-	 * @param relPart
-	 *            The relationship to add.
-	 */
-	public void addRelationship(PackageRelationship relPart) {
-		relationshipsByID.put(relPart.getId(), relPart);
-		relationshipsByType.put(relPart.getRelationshipType(), relPart);
-	}
-
-	/**
-	 * Add a relationship to the collection.
-	 * 
-	 * @param targetUri
-	 *            Target URI.
-	 * @param targetMode
-	 *            The target mode : INTERNAL or EXTERNAL
-	 * @param relationshipType
-	 *            Relationship type.
-	 * @param id
-	 *            Relationship ID.
-	 * @return The newly created relationship.
-	 * @see PackageAccess
-	 */
-	public PackageRelationship addRelationship(URI targetUri,
-			TargetMode targetMode, String relationshipType, String id) {
-
-		if (id == null) {
-			// Generate a unique ID is id parameter is null.
-			int i = 0;
-			do {
-				id = "rId" + ++i;
-			} while (relationshipsByID.get(id) != null);
-		}
-
-		PackageRelationship rel = new PackageRelationship(container,
-				sourcePart, targetUri, targetMode, relationshipType, id);
-		relationshipsByID.put(rel.getId(), rel);
-		relationshipsByType.put(rel.getRelationshipType(), rel);
-		return rel;
-	}
-
-	/**
-	 * Remove a relationship by its ID.
-	 * 
-	 * @param id
-	 *            The relationship ID to remove.
-	 */
-	public void removeRelationship(String id) {
-		if (relationshipsByID != null && relationshipsByType != null) {
-			PackageRelationship rel = relationshipsByID.get(id);
-			if (rel != null) {
-				relationshipsByID.remove(rel.getId());
-				relationshipsByType.values().remove(rel);
-			}
-		}
-	}
-
-	/**
-	 * Remove a relationship by its reference.
-	 * 
-	 * @param rel
-	 *            The relationship to delete.
-	 */
-	public void removeRelationship(PackageRelationship rel) {
-		if (rel == null)
-			throw new IllegalArgumentException("rel");
-
-		relationshipsByID.values().remove(rel);
-		relationshipsByType.values().remove(rel);
-	}
-
-	/**
-	 * Retrieves a relationship by its index in the collection.
-	 * 
-	 * @param index
-	 *            Must be a value between [0-relationships_count-1]
-	 */
-	public PackageRelationship getRelationship(int index) {
-		if (index < 0 || index > relationshipsByID.values().size())
-			throw new IllegalArgumentException("index");
-
-		PackageRelationship retRel = null;
-		int i = 0;
-		for (PackageRelationship rel : relationshipsByID.values()) {
-			if (index == i++)
-				return rel;
-		}
-		return retRel;
-	}
-
-	/**
-	 * Retrieves a package relationship based on its id.
-	 * 
-	 * @param id
-	 *            ID of the package relationship to retrieve.
-	 * @return The package relationship identified by the specified id.
-	 */
-	public PackageRelationship getRelationshipByID(String id) {
-		return relationshipsByID.get(id);
-	}
-
-	/**
-	 * Get the numbe rof relationships in the collection.
-	 */
-	public int size() {
-		return relationshipsByID.values().size();
-	}
-
-	/**
-	 * 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)
-			throws InvalidFormatException {
-		try {
-			SAXReader reader = new SAXReader();
-			logger.log(POILogger.DEBUG, "Parsing relationship: " + relPart.getPartName());
-			Document xmlRelationshipsDoc = reader
-					.read(relPart.getInputStream());
-
-			// Browse default types
-			Element root = xmlRelationshipsDoc.getRootElement();
-
-			// Check OPC compliance M4.1 rule
-			boolean fCorePropertiesRelationship = false;
-
-			for (Iterator i = root
-					.elementIterator(PackageRelationship.RELATIONSHIP_TAG_NAME); i
-					.hasNext();) {
-				Element element = (Element) i.next();
-				// Relationship ID
-				String id = element.attribute(
-						PackageRelationship.ID_ATTRIBUTE_NAME).getValue();
-				// Relationship type
-				String type = element.attribute(
-						PackageRelationship.TYPE_ATTRIBUTE_NAME).getValue();
-
-				/* 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")
-				Attribute targetModeAttr = element
-						.attribute(PackageRelationship.TARGET_MODE_ATTRIBUTE_NAME);
-				TargetMode targetMode = TargetMode.INTERNAL;
-				if (targetModeAttr != null) {
-					targetMode = targetModeAttr.getValue().toLowerCase()
-							.equals("internal") ? TargetMode.INTERNAL
-							: TargetMode.EXTERNAL;
-				}
-
-				// Target converted in URI
-				URI target;
-				String value = "";
-				try {
-					value = element.attribute(
-							PackageRelationship.TARGET_ATTRIBUTE_NAME)
-							.getValue();
-
-					if (value.indexOf("\\") != -1) {
-						logger
-								.log(POILogger.INFO, "target contains \\ therefore not a valid URI"
-										+ value + " replaced by /");
-						value = value.replaceAll("\\\\", "/");
-						// word can save external relationship with a \ instead
-						// of /
-					}
-
-					target = new URI(value);
-				} catch (URISyntaxException e) {
-					logger.log(POILogger.ERROR, "Cannot convert " + value
-							+ " in a valid relationship URI-> ignored", e);
-					continue;
-				}
-				addRelationship(target, targetMode, type, id);
-			}
-		} catch (Exception e) {
-			logger.log(POILogger.ERROR, e);
-			throw new InvalidFormatException(e.getMessage());
-		}
-	}
-
-	/**
-	 * Retrieves all relations with the specified type.
-	 * 
-	 * @param typeFilter
-	 *            Relationship type filter. If <b>null</b> then all
-	 *            relationships are returned.
-	 * @return All relationships of the type specified by the filter.
-	 */
-	public PackageRelationshipCollection getRelationships(String typeFilter) {
-		PackageRelationshipCollection coll = new PackageRelationshipCollection(
-				this, typeFilter);
-		return coll;
-	}
-
-	/**
-	 * Get this collection's iterator.
-	 */
-	public Iterator<PackageRelationship> iterator() {
-		return relationshipsByID.values().iterator();
-	}
-
-	/**
-	 * Get an iterator of a collection with all relationship with the specified
-	 * type.
-	 * 
-	 * @param typeFilter
-	 *            Type filter.
-	 * @return An iterator to a collection containing all relationships with the
-	 *         specified type contain in this collection.
-	 */
-	public Iterator<PackageRelationship> iterator(String typeFilter) {
-		ArrayList<PackageRelationship> retArr = new ArrayList<PackageRelationship>();
-		for (PackageRelationship rel : relationshipsByID.values()) {
-			if (rel.getRelationshipType().equals(typeFilter))
-				retArr.add(rel);
-		}
-		return retArr.iterator();
-	}
-
-	/**
-	 * Clear all relationships.
-	 */
-	public void clear() {
-		relationshipsByID.clear();
-		relationshipsByType.clear();
-	}
-
-	@Override
-	public String toString() {
-		String str;
-		if (relationshipsByID == null) {
-			str = "relationshipsByID=null";
-		} else {
-			str = relationshipsByID.size() + " relationship(s) = [";
-		}
-		if ((relationshipPart != null) && (relationshipPart.partName != null)) {
-			str = str + "," + relationshipPart.partName;
-		} else {
-			str = str + ",relationshipPart=null";
-		}
-
-		// Source of this relationship
-		if ((sourcePart != null) && (sourcePart.partName != null)) {
-			str = str + "," + sourcePart.partName;
-		} else {
-			str = str + ",sourcePart=null";
-		}
-		if (partName != null) {
-			str = str + "," + partName;
-		} else {
-			str = str + ",uri=null)";
-		}
-		return str + "]";
-	}
-}
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.openxml4j.opc;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+
+import org.dom4j.Attribute;
+import org.dom4j.Document;
+import org.dom4j.Element;
+import org.dom4j.io.SAXReader;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
+import org.apache.poi.util.POILogger;
+import org.apache.poi.util.POILogFactory;
+
+/**
+ * Represents a collection of PackageRelationship elements that are owned by a
+ * given PackagePart or the Package.
+ * 
+ * @author Julien Chable, CDubettier
+ * @version 0.1
+ */
+public final class PackageRelationshipCollection implements
+		Iterable<PackageRelationship> {
+
+    private static POILogger logger = POILogFactory.getLogger(PackageRelationshipCollection.class);
+
+	/**
+	 * Package relationships ordered by ID.
+	 */
+	private TreeMap<String, PackageRelationship> relationshipsByID;
+
+	/**
+	 * Package relationships ordered by type.
+	 */
+	private TreeMap<String, PackageRelationship> relationshipsByType;
+
+	/**
+	 * This relationshipPart.
+	 */
+	private PackagePart relationshipPart;
+
+	/**
+	 * Source part.
+	 */
+	private PackagePart sourcePart;
+
+	/**
+	 * This part name.
+	 */
+	private PackagePartName partName;
+
+	/**
+	 * Reference to the package.
+	 */
+	private OPCPackage container;
+
+	/**
+	 * Constructor.
+	 */
+	PackageRelationshipCollection() {
+		relationshipsByID = new TreeMap<String, PackageRelationship>();
+		relationshipsByType = new TreeMap<String, PackageRelationship>();
+	}
+
+	/**
+	 * Copy constructor.
+	 * 
+	 * This collection will contain only elements from the specified collection
+	 * for which the type is compatible with the specified relationship type
+	 * filter.
+	 * 
+	 * @param coll
+	 *            Collection to import.
+	 * @param filter
+	 *            Relationship type filter.
+	 */
+	public PackageRelationshipCollection(PackageRelationshipCollection coll,
+			String filter) {
+		this();
+		for (PackageRelationship rel : coll.relationshipsByID.values()) {
+			if (filter == null || rel.getRelationshipType().equals(filter))
+				addRelationship(rel);
+		}
+	}
+
+	/**
+	 * Constructor.
+	 */
+	public PackageRelationshipCollection(OPCPackage container)
+			throws InvalidFormatException {
+		this(container, null);
+	}
+
+	/**
+	 * 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)
+			throws InvalidFormatException {
+		this(part.container, part);
+	}
+
+	/**
+	 * Constructor. Parse the existing package relationship part if one exists.
+	 * 
+	 * @param container
+	 *            The parent package.
+	 * @param part
+	 *            The part that own this relationships collection. If <b>null</b>
+	 *            then this part is considered as the package root.
+	 * @throws InvalidFormatException
+	 *             If an error occurs during the parsing of the relatinships
+	 *             part fo the specified part.
+	 */
+	public PackageRelationshipCollection(OPCPackage container, PackagePart part)
+			throws InvalidFormatException {
+		this();
+
+		if (container == null)
+			throw new IllegalArgumentException("container");
+
+		// Check if the specified part is not a relationship part
+		if (part != null && part.isRelationshipPart())
+			throw new IllegalArgumentException("part");
+
+		this.container = container;
+		this.sourcePart = part;
+		this.partName = getRelationshipPartName(part);
+		if ((container.getPackageAccess() != PackageAccess.WRITE)
+				&& container.containPart(this.partName)) {
+			relationshipPart = container.getPart(this.partName);
+			parseRelationshipsPart(relationshipPart);
+		}
+	}
+
+	/**
+	 * Get the relationship part name of the specified part.
+	 * 
+	 * @param part
+	 *            The part .
+	 * @return The relationship part name of the specified part. Be careful,
+	 *         only the correct name is returned, this method does not check if
+	 *         the part really exist in a package !
+	 * @throws InvalidOperationException
+	 *             Throws if the specified part is a relationship part.
+	 */
+	private static PackagePartName getRelationshipPartName(PackagePart part)
+			throws InvalidOperationException {
+		PackagePartName partName;
+		if (part == null) {
+			partName = PackagingURIHelper.PACKAGE_ROOT_PART_NAME;
+		} else {
+			partName = part.getPartName();
+		}
+		return PackagingURIHelper.getRelationshipPartName(partName);
+	}
+
+	/**
+	 * Add the specified relationship to the collection.
+	 * 
+	 * @param relPart
+	 *            The relationship to add.
+	 */
+	public void addRelationship(PackageRelationship relPart) {
+		relationshipsByID.put(relPart.getId(), relPart);
+		relationshipsByType.put(relPart.getRelationshipType(), relPart);
+	}
+
+	/**
+	 * Add a relationship to the collection.
+	 * 
+	 * @param targetUri
+	 *            Target URI.
+	 * @param targetMode
+	 *            The target mode : INTERNAL or EXTERNAL
+	 * @param relationshipType
+	 *            Relationship type.
+	 * @param id
+	 *            Relationship ID.
+	 * @return The newly created relationship.
+	 * @see PackageAccess
+	 */
+	public PackageRelationship addRelationship(URI targetUri,
+			TargetMode targetMode, String relationshipType, String id) {
+
+		if (id == null) {
+			// Generate a unique ID is id parameter is null.
+			int i = 0;
+			do {
+				id = "rId" + ++i;
+			} while (relationshipsByID.get(id) != null);
+		}
+
+		PackageRelationship rel = new PackageRelationship(container,
+				sourcePart, targetUri, targetMode, relationshipType, id);
+		relationshipsByID.put(rel.getId(), rel);
+		relationshipsByType.put(rel.getRelationshipType(), rel);
+		return rel;
+	}
+
+	/**
+	 * Remove a relationship by its ID.
+	 * 
+	 * @param id
+	 *            The relationship ID to remove.
+	 */
+	public void removeRelationship(String id) {
+		if (relationshipsByID != null && relationshipsByType != null) {
+			PackageRelationship rel = relationshipsByID.get(id);
+			if (rel != null) {
+				relationshipsByID.remove(rel.getId());
+				relationshipsByType.values().remove(rel);
+			}
+		}
+	}
+
+	/**
+	 * Remove a relationship by its reference.
+	 * 
+	 * @param rel
+	 *            The relationship to delete.
+	 */
+	public void removeRelationship(PackageRelationship rel) {
+		if (rel == null)
+			throw new IllegalArgumentException("rel");
+
+		relationshipsByID.values().remove(rel);
+		relationshipsByType.values().remove(rel);
+	}
+
+	/**
+	 * Retrieves a relationship by its index in the collection.
+	 * 
+	 * @param index
+	 *            Must be a value between [0-relationships_count-1]
+	 */
+	public PackageRelationship getRelationship(int index) {
+		if (index < 0 || index > relationshipsByID.values().size())
+			throw new IllegalArgumentException("index");
+
+		PackageRelationship retRel = null;
+		int i = 0;
+		for (PackageRelationship rel : relationshipsByID.values()) {
+			if (index == i++)
+				return rel;
+		}
+		return retRel;
+	}
+
+	/**
+	 * Retrieves a package relationship based on its id.
+	 * 
+	 * @param id
+	 *            ID of the package relationship to retrieve.
+	 * @return The package relationship identified by the specified id.
+	 */
+	public PackageRelationship getRelationshipByID(String id) {
+		return relationshipsByID.get(id);
+	}
+
+	/**
+	 * Get the numbe rof relationships in the collection.
+	 */
+	public int size() {
+		return relationshipsByID.values().size();
+	}
+
+	/**
+	 * 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)
+			throws InvalidFormatException {
+		try {
+			SAXReader reader = new SAXReader();
+			logger.log(POILogger.DEBUG, "Parsing relationship: " + relPart.getPartName());
+			Document xmlRelationshipsDoc = reader
+					.read(relPart.getInputStream());
+
+			// Browse default types
+			Element root = xmlRelationshipsDoc.getRootElement();
+
+			// Check OPC compliance M4.1 rule
+			boolean fCorePropertiesRelationship = false;
+
+			for (Iterator i = root
+					.elementIterator(PackageRelationship.RELATIONSHIP_TAG_NAME); i
+					.hasNext();) {
+				Element element = (Element) i.next();
+				// Relationship ID
+				String id = element.attribute(
+						PackageRelationship.ID_ATTRIBUTE_NAME).getValue();
+				// Relationship type
+				String type = element.attribute(
+						PackageRelationship.TYPE_ATTRIBUTE_NAME).getValue();
+
+				/* 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")
+				Attribute targetModeAttr = element
+						.attribute(PackageRelationship.TARGET_MODE_ATTRIBUTE_NAME);
+				TargetMode targetMode = TargetMode.INTERNAL;
+				if (targetModeAttr != null) {
+					targetMode = targetModeAttr.getValue().toLowerCase()
+							.equals("internal") ? TargetMode.INTERNAL
+							: TargetMode.EXTERNAL;
+				}
+
+				// Target converted in URI
+				URI target;
+				String value = "";
+				try {
+					value = element.attribute(
+							PackageRelationship.TARGET_ATTRIBUTE_NAME)
+							.getValue();
+
+					if (value.indexOf("\\") != -1) {
+						logger
+								.log(POILogger.INFO, "target contains \\ therefore not a valid URI"
+										+ value + " replaced by /");
+						value = value.replaceAll("\\\\", "/");
+						// word can save external relationship with a \ instead
+						// of /
+					}
+
+					target = new URI(value);
+				} catch (URISyntaxException e) {
+					logger.log(POILogger.ERROR, "Cannot convert " + value
+							+ " in a valid relationship URI-> ignored", e);
+					continue;
+				}
+				addRelationship(target, targetMode, type, id);
+			}
+		} catch (Exception e) {
+			logger.log(POILogger.ERROR, e);
+			throw new InvalidFormatException(e.getMessage());
+		}
+	}
+
+	/**
+	 * Retrieves all relations with the specified type.
+	 * 
+	 * @param typeFilter
+	 *            Relationship type filter. If <b>null</b> then all
+	 *            relationships are returned.
+	 * @return All relationships of the type specified by the filter.
+	 */
+	public PackageRelationshipCollection getRelationships(String typeFilter) {
+		PackageRelationshipCollection coll = new PackageRelationshipCollection(
+				this, typeFilter);
+		return coll;
+	}
+
+	/**
+	 * Get this collection's iterator.
+	 */
+	public Iterator<PackageRelationship> iterator() {
+		return relationshipsByID.values().iterator();
+	}
+
+	/**
+	 * Get an iterator of a collection with all relationship with the specified
+	 * type.
+	 * 
+	 * @param typeFilter
+	 *            Type filter.
+	 * @return An iterator to a collection containing all relationships with the
+	 *         specified type contain in this collection.
+	 */
+	public Iterator<PackageRelationship> iterator(String typeFilter) {
+		ArrayList<PackageRelationship> retArr = new ArrayList<PackageRelationship>();
+		for (PackageRelationship rel : relationshipsByID.values()) {
+			if (rel.getRelationshipType().equals(typeFilter))
+				retArr.add(rel);
+		}
+		return retArr.iterator();
+	}
+
+	/**
+	 * Clear all relationships.
+	 */
+	public void clear() {
+		relationshipsByID.clear();
+		relationshipsByType.clear();
+	}
+
+	@Override
+	public String toString() {
+		String str;
+		if (relationshipsByID == null) {
+			str = "relationshipsByID=null";
+		} else {
+			str = relationshipsByID.size() + " relationship(s) = [";
+		}
+		if ((relationshipPart != null) && (relationshipPart.partName != null)) {
+			str = str + "," + relationshipPart.partName;
+		} else {
+			str = str + ",relationshipPart=null";
+		}
+
+		// Source of this relationship
+		if ((sourcePart != null) && (sourcePart.partName != null)) {
+			str = str + "," + sourcePart.partName;
+		} else {
+			str = str + ",sourcePart=null";
+		}
+		if (partName != null) {
+			str = str + "," + partName;
+		} else {
+			str = str + ",uri=null)";
+		}
+		return str + "]";
+	}
+}

Modified: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackagePart.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackagePart.java?rev=755699&r1=755698&r2=755699&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackagePart.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackagePart.java Wed Mar 18 18:54:01 2009
@@ -1,135 +1,135 @@
-/* ====================================================================
-   Licensed to the Apache Software Foundation (ASF) under one or more
-   contributor license agreements.  See the NOTICE file distributed with
-   this work for additional information regarding copyright ownership.
-   The ASF licenses this file to You under the Apache License, Version 2.0
-   (the "License"); you may not use this file except in compliance with
-   the License.  You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-==================================================================== */
-
-package org.apache.poi.openxml4j.opc;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.zip.ZipEntry;
-
-import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
-import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
-import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
-import org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller;
-
-/**
- * Zip implementation of a PackagePart.
- * 
- * @author Julien Chable
- * @version 1.0
- * @see PackagePart
- */
-public class ZipPackagePart extends PackagePart {
-
-	/**
-	 * The zip entry corresponding to this part.
-	 */
-	private ZipEntry zipEntry;
-
-	/**
-	 * Constructor.
-	 * 
-	 * @param container
-	 *            The container package.
-	 * @param partName
-	 *            Part name.
-	 * @param contentType
-	 *            Content type.
-	 * @throws InvalidFormatException
-	 *             Throws if the content of this part invalid.
-	 */
-	public ZipPackagePart(Package container, PackagePartName partName,
-			String contentType) throws InvalidFormatException {
-		super(container, partName, contentType);
-	}
-
-	/**
-	 * Constructor.
-	 * 
-	 * @param container
-	 *            The container package.
-	 * @param zipEntry
-	 *            The zip entry corresponding to this part.
-	 * @param partName
-	 *            The part name.
-	 * @param contentType
-	 *            Content type.
-	 * @throws InvalidFormatException
-	 *             Throws if the content of this part is invalid.
-	 */
-	public ZipPackagePart(Package container, ZipEntry zipEntry,
-			PackagePartName partName, String contentType)
-			throws InvalidFormatException {
-		super(container, partName, contentType);
-		this.zipEntry = zipEntry;
-	}
-
-	/**
-	 * Get the zip entry of this part.
-	 * 
-	 * @return The zip entry in the zip structure coresponding to this part.
-	 */
-	public ZipEntry getZipArchive() {
-		return zipEntry;
-	}
-
-	/**
-	 * Implementation of the getInputStream() which return the inputStream of
-	 * this part zip entry.
-	 * 
-	 * @return Input stream of this part zip entry.
-	 */
-	@Override
-	protected InputStream getInputStreamImpl() throws IOException {
-		// We use the getInputStream() method from java.util.zip.ZipFile
-		// class which return an InputStream to this part zip entry.
-		return ((ZipPackage) container).getZipArchive()
-				.getInputStream(zipEntry);
-	}
-
-	/**
-	 * Implementation of the getOutputStream(). Return <b>null</b>. Normally
-	 * will never be called since the MemoryPackage is use instead.
-	 * 
-	 * @return <b>null</b>
-	 */
-	@Override
-	protected OutputStream getOutputStreamImpl() {
-		return null;
-	}
-
-	@Override
-	public boolean save(OutputStream os) throws OpenXML4JException {
-		return new ZipPartMarshaller().marshall(this, os);
-	}
-
-	@Override
-	public boolean load(InputStream ios) throws InvalidFormatException {
-		throw new InvalidOperationException("Method not implemented !");
-	}
-
-	@Override
-	public void close() {
-		throw new InvalidOperationException("Method not implemented !");
-	}
-
-	@Override
-	public void flush() {
-		throw new InvalidOperationException("Method not implemented !");
-	}
-}
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.openxml4j.opc;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.zip.ZipEntry;
+
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
+import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
+import org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller;
+
+/**
+ * Zip implementation of a PackagePart.
+ * 
+ * @author Julien Chable
+ * @version 1.0
+ * @see PackagePart
+ */
+public class ZipPackagePart extends PackagePart {
+
+	/**
+	 * The zip entry corresponding to this part.
+	 */
+	private ZipEntry zipEntry;
+
+	/**
+	 * Constructor.
+	 * 
+	 * @param container
+	 *            The container package.
+	 * @param partName
+	 *            Part name.
+	 * @param contentType
+	 *            Content type.
+	 * @throws InvalidFormatException
+	 *             Throws if the content of this part invalid.
+	 */
+	public ZipPackagePart(OPCPackage container, PackagePartName partName,
+			String contentType) throws InvalidFormatException {
+		super(container, partName, contentType);
+	}
+
+	/**
+	 * Constructor.
+	 * 
+	 * @param container
+	 *            The container package.
+	 * @param zipEntry
+	 *            The zip entry corresponding to this part.
+	 * @param partName
+	 *            The part name.
+	 * @param contentType
+	 *            Content type.
+	 * @throws InvalidFormatException
+	 *             Throws if the content of this part is invalid.
+	 */
+	public ZipPackagePart(OPCPackage container, ZipEntry zipEntry,
+			PackagePartName partName, String contentType)
+			throws InvalidFormatException {
+		super(container, partName, contentType);
+		this.zipEntry = zipEntry;
+	}
+
+	/**
+	 * Get the zip entry of this part.
+	 * 
+	 * @return The zip entry in the zip structure coresponding to this part.
+	 */
+	public ZipEntry getZipArchive() {
+		return zipEntry;
+	}
+
+	/**
+	 * Implementation of the getInputStream() which return the inputStream of
+	 * this part zip entry.
+	 * 
+	 * @return Input stream of this part zip entry.
+	 */
+	@Override
+	protected InputStream getInputStreamImpl() throws IOException {
+		// We use the getInputStream() method from java.util.zip.ZipFile
+		// class which return an InputStream to this part zip entry.
+		return ((ZipPackage) container).getZipArchive()
+				.getInputStream(zipEntry);
+	}
+
+	/**
+	 * Implementation of the getOutputStream(). Return <b>null</b>. Normally
+	 * will never be called since the MemoryPackage is use instead.
+	 * 
+	 * @return <b>null</b>
+	 */
+	@Override
+	protected OutputStream getOutputStreamImpl() {
+		return null;
+	}
+
+	@Override
+	public boolean save(OutputStream os) throws OpenXML4JException {
+		return new ZipPartMarshaller().marshall(this, os);
+	}
+
+	@Override
+	public boolean load(InputStream ios) throws InvalidFormatException {
+		throw new InvalidOperationException("Method not implemented !");
+	}
+
+	@Override
+	public void close() {
+		throw new InvalidOperationException("Method not implemented !");
+	}
+
+	@Override
+	public void flush() {
+		throw new InvalidOperationException("Method not implemented !");
+	}
+}

Modified: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentTypeManager.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentTypeManager.java?rev=755699&r1=755698&r2=755699&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentTypeManager.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentTypeManager.java Wed Mar 18 18:54:01 2009
@@ -1,496 +1,491 @@
-/* ====================================================================
-   Licensed to the Apache Software Foundation (ASF) under one or more
-   contributor license agreements.  See the NOTICE file distributed with
-   this work for additional information regarding copyright ownership.
-   The ASF licenses this file to You under the Apache License, Version 2.0
-   (the "License"); you may not use this file except in compliance with
-   the License.  You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-==================================================================== */
-
-package org.apache.poi.openxml4j.opc.internal;
-
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.Iterator;
-import java.util.List;
-import java.util.TreeMap;
-import java.util.Map.Entry;
-import java.util.zip.ZipOutputStream;
-
-import org.dom4j.Document;
-import org.dom4j.DocumentException;
-import org.dom4j.DocumentHelper;
-import org.dom4j.Element;
-import org.dom4j.Namespace;
-import org.dom4j.QName;
-import org.dom4j.io.SAXReader;
-import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
-import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
-import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException;
-import org.apache.poi.openxml4j.opc.Package;
-import org.apache.poi.openxml4j.opc.PackagePart;
-import org.apache.poi.openxml4j.opc.PackagePartName;
-import org.apache.poi.openxml4j.opc.PackagingURIHelper;
-import org.apache.poi.util.POILogger;
-import org.apache.poi.util.POILogFactory;
-
-/**
- * Manage package content types ([Content_Types].xml part).
- * 
- * @author Julien Chable
- * @version 1.0
- */
-public abstract class ContentTypeManager {
-
-    private static POILogger logger = POILogFactory.getLogger(ContentTypeManager.class);
-
-	/**
-	 * Reference to the package using this content type manager.
-	 */
-	protected Package container;
-
-	/**
-	 * Content type part name.
-	 */
-	public static final String CONTENT_TYPES_PART_NAME = "[Content_Types].xml";
-
-	/**
-	 * Content type namespace
-	 */
-	public static final String TYPES_NAMESPACE_URI = "http://schemas.openxmlformats.org/package/2006/content-types";
-
-	/* Xml elements in content type part */
-
-	private static final String TYPES_TAG_NAME = "Types";
-
-	private static final String DEFAULT_TAG_NAME = "Default";
-
-	private static final String EXTENSION_ATTRIBUTE_NAME = "Extension";
-
-	private static final String CONTENT_TYPE_ATTRIBUTE_NAME = "ContentType";
-
-	private static final String OVERRIDE_TAG_NAME = "Override";
-
-	private static final String PART_NAME_ATTRIBUTE_NAME = "PartName";
-
-	/**
-	 * Default content type tree. <Extension, ContentType>
-	 */
-	private TreeMap<String, String> defaultContentType;
-
-	/**
-	 * Override content type tree.
-	 */
-	private TreeMap<PackagePartName, String> overrideContentType;
-
-	/**
-	 * Constructor. Parses the content of the specified input stream.
-	 * 
-	 * @param in
-	 *            If different of <i>null</i> then the content types part is
-	 *            retrieve and parse.
-	 * @throws InvalidFormatException
-	 *             If the content types part content is not valid.
-	 */
-	public ContentTypeManager(InputStream in, Package pkg)
-			throws InvalidFormatException {
-		this.container = pkg;
-		this.defaultContentType = new TreeMap<String, String>();
-		if (in != null) {
-			try {
-				parseContentTypesFile(in);
-			} catch (InvalidFormatException e) {
-				throw new InvalidFormatException(
-						"Can't read content types part !");
-			}
-		}
-	}
-
-	/**
-	 * Build association extention-> content type (will be stored in
-	 * [Content_Types].xml) for example ContentType="image/png" Extension="png"
-	 * <p>
-	 * [M2.8]: When adding a new part to a package, the package implementer
-	 * shall ensure that a content type for that part is specified in the
-	 * Content Types stream; the package implementer shall perform the steps
-	 * described in &#167;9.1.2.3:
-	 * </p><p>
-	 * 1. Get the extension from the part name by taking the substring to the
-	 * right of the rightmost occurrence of the dot character (.) from the
-	 * rightmost segment.
-	 * </p><p>
-	 * 2. If a part name has no extension, a corresponding Override element
-	 * shall be added to the Content Types stream.
-	 * </p><p>
-	 * 3. Compare the resulting extension with the values specified for the
-	 * Extension attributes of the Default elements in the Content Types stream.
-	 * The comparison shall be case-insensitive ASCII.
-	 * </p><p>
-	 * 4. If there is a Default element with a matching Extension attribute,
-	 * then the content type of the new part shall be compared with the value of
-	 * the ContentType attribute. The comparison might be case-sensitive and
-	 * include every character regardless of the role it plays in the
-	 * content-type grammar of RFC 2616, or it might follow the grammar of RFC
-	 * 2616.
-	 * </p><p>
-	 * a. If the content types match, no further action is required.
-	 * </p><p>
-	 * b. If the content types do not match, a new Override element shall be
-	 * added to the Content Types stream. .
-	 * </p><p>
-	 * 5. If there is no Default element with a matching Extension attribute, a
-	 * new Default element or Override element shall be added to the Content
-	 * Types stream.
-	 * </p>
-	 */
-	public void addContentType(PackagePartName partName, String contentType) {
-		boolean defaultCTExists = false;
-		String extension = partName.getExtension().toLowerCase();
-		if ((extension.length() == 0)
-				|| (this.defaultContentType.containsKey(extension) && !(defaultCTExists = this.defaultContentType
-						.containsValue(contentType))))
-			this.addOverrideContentType(partName, contentType);
-		else if (!defaultCTExists)
-			this.addDefaultContentType(extension, contentType);
-	}
-
-	/**
-	 * Add an override content type for a specific part.
-	 * 
-	 * @param partName
-	 *            Name of the part.
-	 * @param contentType
-	 *            Content type of the part.
-	 */
-	private void addOverrideContentType(PackagePartName partName,
-			String contentType) {
-		if (overrideContentType == null)
-			overrideContentType = new TreeMap<PackagePartName, String>();
-		overrideContentType.put(partName, contentType);
-	}
-
-	/**
-	 * Add a content type associated with the specified extension.
-	 * 
-	 * @param extension
-	 *            The part name extension to bind to a content type.
-	 * @param contentType
-	 *            The content type associated with the specified extension.
-	 */
-	private void addDefaultContentType(String extension, String contentType) {
-		// Remark : Originally the latest parameter was :
-		// contentType.toLowerCase(). Change due to a request ID 1996748.
-		defaultContentType.put(extension.toLowerCase(), contentType);
-	}
-
-	/**
-     * <p>
-	 * Delete a content type based on the specified part name. If the specified
-	 * part name is register with an override content type, then this content
-	 * type is remove, else the content type is remove in the default content
-	 * type list if it exists and if no part is associated with it yet.
-	 * </p><p>
-	 * Check rule M2.4: The package implementer shall require that the Content
-	 * Types stream contain one of the following for every part in the package:
-	 * One matching Default element One matching Override element Both a
-	 * matching Default element and a matching Override element, in which case
-	 * the Override element takes precedence.
-	 * </p>
-	 * @param partName
-	 *            The part URI associated with the override content type to
-	 *            delete.
-	 * @exception InvalidOperationException
-	 *                Throws if
-	 */
-	public void removeContentType(PackagePartName partName)
-			throws InvalidOperationException {
-		if (partName == null)
-			throw new IllegalArgumentException("partName");
-
-		/* Override content type */
-		if (this.overrideContentType != null
-				&& (this.overrideContentType.get(partName) != null)) {
-			// Remove the override definition for the specified part.
-			this.overrideContentType.remove(partName);
-			return;
-		}
-
-		/* Default content type */
-		String extensionToDelete = partName.getExtension();
-		boolean deleteDefaultContentTypeFlag = true;
-		if (this.container != null) {
-			try {
-				for (PackagePart part : this.container.getParts()) {
-					if (!part.getPartName().equals(partName)
-							&& part.getPartName().getExtension()
-									.equalsIgnoreCase(extensionToDelete)) {
-						deleteDefaultContentTypeFlag = false;
-						break;
-					}
-				}
-			} catch (InvalidFormatException e) {
-				throw new InvalidOperationException(e.getMessage());
-			}
-		}
-
-		// Remove the default content type, no other part use this content type.
-		if (deleteDefaultContentTypeFlag) {
-			this.defaultContentType.remove(extensionToDelete);
-		}
-
-		/*
-		 * Check rule 2.4: The package implementer shall require that the
-		 * Content Types stream contain one of the following for every part in
-		 * the package: One matching Default element One matching Override
-		 * element Both a matching Default element and a matching Override
-		 * element, in which case the Override element takes precedence.
-		 */
-		if (this.container != null) {
-			try {
-				for (PackagePart part : this.container.getParts()) {
-					if (!part.getPartName().equals(partName)
-							&& this.getContentType(part.getPartName()) == null)
-						throw new InvalidOperationException(
-								"Rule M2.4 is not respected: Nor a default element or override element is associated with the part: "
-										+ part.getPartName().getName());
-				}
-			} catch (InvalidFormatException e) {
-				throw new InvalidOperationException(e.getMessage());
-			}
-		}
-	}
-
-	/**
-	 * Check if the specified content type is already register.
-	 * 
-	 * @param contentType
-	 *            The content type to check.
-	 * @return <code>true</code> if the specified content type is already
-	 *         register, then <code>false</code>.
-	 */
-	public boolean isContentTypeRegister(String contentType) {
-		if (contentType == null)
-			throw new IllegalArgumentException("contentType");
-
-		return (this.defaultContentType.values().contains(contentType) || (this.overrideContentType != null && this.overrideContentType
-				.values().contains(contentType)));
-	}
-
-	/**
-	 * Get the content type for the specified part, if any.
-	 * <p>
-	 * Rule [M2.9]: To get the content type of a part, the package implementer
-	 * shall perform the steps described in &#167;9.1.2.4:
-	 * </p><p>
-	 * 1. Compare the part name with the values specified for the PartName
-	 * attribute of the Override elements. The comparison shall be
-	 * case-insensitive ASCII.
-	 * </p><p>
-	 * 2. If there is an Override element with a matching PartName attribute,
-	 * return the value of its ContentType attribute. No further action is
-	 * required.
-	 * </p><p>
-	 * 3. If there is no Override element with a matching PartName attribute,
-	 * then a. Get the extension from the part name by taking the substring to
-	 * the right of the rightmost occurrence of the dot character (.) from the
-	 * rightmost segment. b. Check the Default elements of the Content Types
-	 * stream, comparing the extension with the value of the Extension
-	 * attribute. The comparison shall be case-insensitive ASCII.
-	 * </p><p>
-	 * 4. If there is a Default element with a matching Extension attribute,
-	 * return the value of its ContentType attribute. No further action is
-	 * required.
-	 * </p><p>
-	 * 5. If neither Override nor Default elements with matching attributes are
-	 * found for the specified part name, the implementation shall not map this
-	 * part name to a part.
-	 * </p>
-	 * @param partName
-	 *            The URI part to check.
-	 * @return The content type associated with the URI (in case of an override
-	 *         content type) or the extension (in case of default content type),
-	 *         else <code>null</code>.
-	 * 
-	 * @exception OpenXML4JRuntimeException
-	 *                Throws if the content type manager is not able to find the
-	 *                content from an existing part.
-	 */
-	public String getContentType(PackagePartName partName) {
-		if (partName == null)
-			throw new IllegalArgumentException("partName");
-
-		if ((this.overrideContentType != null)
-				&& this.overrideContentType.containsKey(partName))
-			return this.overrideContentType.get(partName);
-
-		String extension = partName.getExtension().toLowerCase();
-		if (this.defaultContentType.containsKey(extension))
-			return this.defaultContentType.get(extension);
-
-		/*
-		 * [M2.4] : The package implementer shall require that the Content Types
-		 * stream contain one of the following for every part in the package:
-		 * One matching Default element, One matching Override element, Both a
-		 * matching Default element and a matching Override element, in which
-		 * case the Override element takes precedence.
-		 */
-		if (this.container != null && this.container.getPart(partName) != null) {
-			throw new OpenXML4JRuntimeException(
-					"Rule M2.4 exception : this error should NEVER happen, if so please send a mail to the developers team, thanks !");
-		} else {
-			return null;
-		}
-	}
-
-	/**
-	 * Clear all content types.
-	 */
-	public void clearAll() {
-		this.defaultContentType.clear();
-		if (this.overrideContentType != null)
-			this.overrideContentType.clear();
-	}
-
-	/**
-	 * Clear all override content types.
-	 * 
-	 */
-	public void clearOverrideContentTypes() {
-		if (this.overrideContentType != null)
-			this.overrideContentType.clear();
-	}
-
-	/**
-	 * Parse the content types part.
-	 * 
-	 * @throws InvalidFormatException
-	 *             Throws if the content type doesn't exist or the XML format is
-	 *             invalid.
-	 */
-	private void parseContentTypesFile(InputStream in)
-			throws InvalidFormatException {
-		try {
-			SAXReader xmlReader = new SAXReader();
-			Document xmlContentTypetDoc = xmlReader.read(in);
-
-			// Default content types
-			List defaultTypes = xmlContentTypetDoc.getRootElement().elements(
-					DEFAULT_TAG_NAME);
-			Iterator elementIteratorDefault = defaultTypes.iterator();
-			while (elementIteratorDefault.hasNext()) {
-				Element element = (Element) elementIteratorDefault.next();
-				String extension = element.attribute(EXTENSION_ATTRIBUTE_NAME)
-						.getValue();
-				String contentType = element.attribute(
-						CONTENT_TYPE_ATTRIBUTE_NAME).getValue();
-				addDefaultContentType(extension, contentType);
-			}
-
-			// Overriden content types
-			List overrideTypes = xmlContentTypetDoc.getRootElement().elements(
-					OVERRIDE_TAG_NAME);
-			Iterator elementIteratorOverride = overrideTypes.iterator();
-			while (elementIteratorOverride.hasNext()) {
-				Element element = (Element) elementIteratorOverride.next();
-				URI uri = new URI(element.attribute(PART_NAME_ATTRIBUTE_NAME)
-						.getValue());
-				PackagePartName partName = PackagingURIHelper
-						.createPartName(uri);
-				String contentType = element.attribute(
-						CONTENT_TYPE_ATTRIBUTE_NAME).getValue();
-				addOverrideContentType(partName, contentType);
-			}
-		} catch (URISyntaxException urie) {
-			throw new InvalidFormatException(urie.getMessage());
-		} catch (DocumentException e) {
-			throw new InvalidFormatException(e.getMessage());
-		}
-	}
-
-	/**
-	 * Save the contents type part.
-	 * 
-	 * @param outStream
-	 *            The output stream use to save the XML content of the content
-	 *            types part.
-	 * @return <b>true</b> if the operation success, else <b>false</b>.
-	 */
-	public boolean save(OutputStream outStream) {
-		Document xmlOutDoc = DocumentHelper.createDocument();
-
-		// Building namespace
-		Namespace dfNs = Namespace.get("", TYPES_NAMESPACE_URI);
-		Element typesElem = xmlOutDoc
-				.addElement(new QName(TYPES_TAG_NAME, dfNs));
-
-		// Adding default types
-		for (Entry<String, String> entry : defaultContentType.entrySet()) {
-			appendDefaultType(typesElem, entry);
-		}
-
-		// Adding specific types if any exist
-		if (overrideContentType != null) {
-			for (Entry<PackagePartName, String> entry : overrideContentType
-					.entrySet()) {
-				appendSpecificTypes(typesElem, entry);
-			}
-		}
-		xmlOutDoc.normalize();
-
-		// Save content in the specified output stream
-		return this.saveImpl(xmlOutDoc, outStream);
-	}
-
-	/**
-	 * Use to append specific type XML elements, use by the save() method.
-	 * 
-	 * @param root
-	 *            XML parent element use to append this override type element.
-	 * @param entry
-	 *            The values to append.
-	 * @see #save(java.io.OutputStream)
-	 */
-	private void appendSpecificTypes(Element root,
-			Entry<PackagePartName, String> entry) {
-		root.addElement(OVERRIDE_TAG_NAME).addAttribute(
-				PART_NAME_ATTRIBUTE_NAME,
-				((PackagePartName) entry.getKey()).getName()).addAttribute(
-				CONTENT_TYPE_ATTRIBUTE_NAME, (String) entry.getValue());
-	}
-
-	/**
-	 * Use to append default types XML elements, use by the save() metid.
-	 * 
-	 * @param root
-	 *            XML parent element use to append this default type element.
-	 * @param entry
-	 *            The values to append.
-	 * @see #save(java.io.OutputStream)
-	 */
-	private void appendDefaultType(Element root, Entry<String, String> entry) {
-		root.addElement(DEFAULT_TAG_NAME).addAttribute(
-				EXTENSION_ATTRIBUTE_NAME, (String) entry.getKey())
-				.addAttribute(CONTENT_TYPE_ATTRIBUTE_NAME,
-						(String) entry.getValue());
-
-	}
-
-	/**
-	 * Specific implementation of the save method. Call by the save() method,
-	 * call before exiting.
-	 * 
-	 * @param out
-	 *            The output stream use to write the content type XML.
-	 */
-	public abstract boolean saveImpl(Document content, OutputStream out);
-}
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.openxml4j.opc.internal;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.TreeMap;
+import java.util.Map.Entry;
+
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
+import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException;
+import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.openxml4j.opc.PackagePartName;
+import org.apache.poi.openxml4j.opc.PackagingURIHelper;
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
+import org.dom4j.Namespace;
+import org.dom4j.QName;
+import org.dom4j.io.SAXReader;
+
+/**
+ * Manage package content types ([Content_Types].xml part).
+ * 
+ * @author Julien Chable
+ * @version 1.0
+ */
+public abstract class ContentTypeManager {
+
+	/**
+	 * Content type part name.
+	 */
+	public static final String CONTENT_TYPES_PART_NAME = "[Content_Types].xml";
+
+	/**
+	 * Content type namespace
+	 */
+	public static final String TYPES_NAMESPACE_URI = "http://schemas.openxmlformats.org/package/2006/content-types";
+
+	/* Xml elements in content type part */
+
+	private static final String TYPES_TAG_NAME = "Types";
+
+	private static final String DEFAULT_TAG_NAME = "Default";
+
+	private static final String EXTENSION_ATTRIBUTE_NAME = "Extension";
+
+	private static final String CONTENT_TYPE_ATTRIBUTE_NAME = "ContentType";
+
+	private static final String OVERRIDE_TAG_NAME = "Override";
+
+	private static final String PART_NAME_ATTRIBUTE_NAME = "PartName";
+
+	/**
+	 * Reference to the package using this content type manager.
+	 */
+	protected OPCPackage container;
+
+	/**
+	 * Default content type tree. <Extension, ContentType>
+	 */
+	private TreeMap<String, String> defaultContentType;
+
+	/**
+	 * Override content type tree.
+	 */
+	private TreeMap<PackagePartName, String> overrideContentType;
+
+	/**
+	 * Constructor. Parses the content of the specified input stream.
+	 * 
+	 * @param in
+	 *            If different of <i>null</i> then the content types part is
+	 *            retrieve and parse.
+	 * @throws InvalidFormatException
+	 *             If the content types part content is not valid.
+	 */
+	public ContentTypeManager(InputStream in, OPCPackage pkg)
+			throws InvalidFormatException {
+		this.container = pkg;
+		this.defaultContentType = new TreeMap<String, String>();
+		if (in != null) {
+			try {
+				parseContentTypesFile(in);
+			} catch (InvalidFormatException e) {
+				throw new InvalidFormatException(
+						"Can't read content types part !");
+			}
+		}
+	}
+
+	/**
+	 * Build association extention-> content type (will be stored in
+	 * [Content_Types].xml) for example ContentType="image/png" Extension="png"
+	 * <p>
+	 * [M2.8]: When adding a new part to a package, the package implementer
+	 * shall ensure that a content type for that part is specified in the
+	 * Content Types stream; the package implementer shall perform the steps
+	 * described in &#167;9.1.2.3:
+	 * </p><p>
+	 * 1. Get the extension from the part name by taking the substring to the
+	 * right of the rightmost occurrence of the dot character (.) from the
+	 * rightmost segment.
+	 * </p><p>
+	 * 2. If a part name has no extension, a corresponding Override element
+	 * shall be added to the Content Types stream.
+	 * </p><p>
+	 * 3. Compare the resulting extension with the values specified for the
+	 * Extension attributes of the Default elements in the Content Types stream.
+	 * The comparison shall be case-insensitive ASCII.
+	 * </p><p>
+	 * 4. If there is a Default element with a matching Extension attribute,
+	 * then the content type of the new part shall be compared with the value of
+	 * the ContentType attribute. The comparison might be case-sensitive and
+	 * include every character regardless of the role it plays in the
+	 * content-type grammar of RFC 2616, or it might follow the grammar of RFC
+	 * 2616.
+	 * </p><p>
+	 * a. If the content types match, no further action is required.
+	 * </p><p>
+	 * b. If the content types do not match, a new Override element shall be
+	 * added to the Content Types stream. .
+	 * </p><p>
+	 * 5. If there is no Default element with a matching Extension attribute, a
+	 * new Default element or Override element shall be added to the Content
+	 * Types stream.
+	 * </p>
+	 */
+	public void addContentType(PackagePartName partName, String contentType) {
+		boolean defaultCTExists = false;
+		String extension = partName.getExtension().toLowerCase();
+		if ((extension.length() == 0)
+				|| (this.defaultContentType.containsKey(extension) && !(defaultCTExists = this.defaultContentType
+						.containsValue(contentType))))
+			this.addOverrideContentType(partName, contentType);
+		else if (!defaultCTExists)
+			this.addDefaultContentType(extension, contentType);
+	}
+
+	/**
+	 * Add an override content type for a specific part.
+	 * 
+	 * @param partName
+	 *            Name of the part.
+	 * @param contentType
+	 *            Content type of the part.
+	 */
+	private void addOverrideContentType(PackagePartName partName,
+			String contentType) {
+		if (overrideContentType == null)
+			overrideContentType = new TreeMap<PackagePartName, String>();
+		overrideContentType.put(partName, contentType);
+	}
+
+	/**
+	 * Add a content type associated with the specified extension.
+	 * 
+	 * @param extension
+	 *            The part name extension to bind to a content type.
+	 * @param contentType
+	 *            The content type associated with the specified extension.
+	 */
+	private void addDefaultContentType(String extension, String contentType) {
+		// Remark : Originally the latest parameter was :
+		// contentType.toLowerCase(). Change due to a request ID 1996748.
+		defaultContentType.put(extension.toLowerCase(), contentType);
+	}
+
+	/**
+     * <p>
+	 * Delete a content type based on the specified part name. If the specified
+	 * part name is register with an override content type, then this content
+	 * type is remove, else the content type is remove in the default content
+	 * type list if it exists and if no part is associated with it yet.
+	 * </p><p>
+	 * Check rule M2.4: The package implementer shall require that the Content
+	 * Types stream contain one of the following for every part in the package:
+	 * One matching Default element One matching Override element Both a
+	 * matching Default element and a matching Override element, in which case
+	 * the Override element takes precedence.
+	 * </p>
+	 * @param partName
+	 *            The part URI associated with the override content type to
+	 *            delete.
+	 * @exception InvalidOperationException
+	 *                Throws if
+	 */
+	public void removeContentType(PackagePartName partName)
+			throws InvalidOperationException {
+		if (partName == null)
+			throw new IllegalArgumentException("partName");
+
+		/* Override content type */
+		if (this.overrideContentType != null
+				&& (this.overrideContentType.get(partName) != null)) {
+			// Remove the override definition for the specified part.
+			this.overrideContentType.remove(partName);
+			return;
+		}
+
+		/* Default content type */
+		String extensionToDelete = partName.getExtension();
+		boolean deleteDefaultContentTypeFlag = true;
+		if (this.container != null) {
+			try {
+				for (PackagePart part : this.container.getParts()) {
+					if (!part.getPartName().equals(partName)
+							&& part.getPartName().getExtension()
+									.equalsIgnoreCase(extensionToDelete)) {
+						deleteDefaultContentTypeFlag = false;
+						break;
+					}
+				}
+			} catch (InvalidFormatException e) {
+				throw new InvalidOperationException(e.getMessage());
+			}
+		}
+
+		// Remove the default content type, no other part use this content type.
+		if (deleteDefaultContentTypeFlag) {
+			this.defaultContentType.remove(extensionToDelete);
+		}
+
+		/*
+		 * Check rule 2.4: The package implementer shall require that the
+		 * Content Types stream contain one of the following for every part in
+		 * the package: One matching Default element One matching Override
+		 * element Both a matching Default element and a matching Override
+		 * element, in which case the Override element takes precedence.
+		 */
+		if (this.container != null) {
+			try {
+				for (PackagePart part : this.container.getParts()) {
+					if (!part.getPartName().equals(partName)
+							&& this.getContentType(part.getPartName()) == null)
+						throw new InvalidOperationException(
+								"Rule M2.4 is not respected: Nor a default element or override element is associated with the part: "
+										+ part.getPartName().getName());
+				}
+			} catch (InvalidFormatException e) {
+				throw new InvalidOperationException(e.getMessage());
+			}
+		}
+	}
+
+	/**
+	 * Check if the specified content type is already register.
+	 * 
+	 * @param contentType
+	 *            The content type to check.
+	 * @return <code>true</code> if the specified content type is already
+	 *         register, then <code>false</code>.
+	 */
+	public boolean isContentTypeRegister(String contentType) {
+		if (contentType == null)
+			throw new IllegalArgumentException("contentType");
+
+		return (this.defaultContentType.values().contains(contentType) || (this.overrideContentType != null && this.overrideContentType
+				.values().contains(contentType)));
+	}
+
+	/**
+	 * Get the content type for the specified part, if any.
+	 * <p>
+	 * Rule [M2.9]: To get the content type of a part, the package implementer
+	 * shall perform the steps described in &#167;9.1.2.4:
+	 * </p><p>
+	 * 1. Compare the part name with the values specified for the PartName
+	 * attribute of the Override elements. The comparison shall be
+	 * case-insensitive ASCII.
+	 * </p><p>
+	 * 2. If there is an Override element with a matching PartName attribute,
+	 * return the value of its ContentType attribute. No further action is
+	 * required.
+	 * </p><p>
+	 * 3. If there is no Override element with a matching PartName attribute,
+	 * then a. Get the extension from the part name by taking the substring to
+	 * the right of the rightmost occurrence of the dot character (.) from the
+	 * rightmost segment. b. Check the Default elements of the Content Types
+	 * stream, comparing the extension with the value of the Extension
+	 * attribute. The comparison shall be case-insensitive ASCII.
+	 * </p><p>
+	 * 4. If there is a Default element with a matching Extension attribute,
+	 * return the value of its ContentType attribute. No further action is
+	 * required.
+	 * </p><p>
+	 * 5. If neither Override nor Default elements with matching attributes are
+	 * found for the specified part name, the implementation shall not map this
+	 * part name to a part.
+	 * </p>
+	 * @param partName
+	 *            The URI part to check.
+	 * @return The content type associated with the URI (in case of an override
+	 *         content type) or the extension (in case of default content type),
+	 *         else <code>null</code>.
+	 * 
+	 * @exception OpenXML4JRuntimeException
+	 *                Throws if the content type manager is not able to find the
+	 *                content from an existing part.
+	 */
+	public String getContentType(PackagePartName partName) {
+		if (partName == null)
+			throw new IllegalArgumentException("partName");
+
+		if ((this.overrideContentType != null)
+				&& this.overrideContentType.containsKey(partName))
+			return this.overrideContentType.get(partName);
+
+		String extension = partName.getExtension().toLowerCase();
+		if (this.defaultContentType.containsKey(extension))
+			return this.defaultContentType.get(extension);
+
+		/*
+		 * [M2.4] : The package implementer shall require that the Content Types
+		 * stream contain one of the following for every part in the package:
+		 * One matching Default element, One matching Override element, Both a
+		 * matching Default element and a matching Override element, in which
+		 * case the Override element takes precedence.
+		 */
+		if (this.container != null && this.container.getPart(partName) != null) {
+			throw new OpenXML4JRuntimeException(
+					"Rule M2.4 exception : this error should NEVER happen, if so please send a mail to the developers team, thanks !");
+		} else {
+			return null;
+		}
+	}
+
+	/**
+	 * Clear all content types.
+	 */
+	public void clearAll() {
+		this.defaultContentType.clear();
+		if (this.overrideContentType != null)
+			this.overrideContentType.clear();
+	}
+
+	/**
+	 * Clear all override content types.
+	 * 
+	 */
+	public void clearOverrideContentTypes() {
+		if (this.overrideContentType != null)
+			this.overrideContentType.clear();
+	}
+
+	/**
+	 * Parse the content types part.
+	 * 
+	 * @throws InvalidFormatException
+	 *             Throws if the content type doesn't exist or the XML format is
+	 *             invalid.
+	 */
+	private void parseContentTypesFile(InputStream in)
+			throws InvalidFormatException {
+		try {
+			SAXReader xmlReader = new SAXReader();
+			Document xmlContentTypetDoc = xmlReader.read(in);
+
+			// Default content types
+			List defaultTypes = xmlContentTypetDoc.getRootElement().elements(
+					DEFAULT_TAG_NAME);
+			Iterator elementIteratorDefault = defaultTypes.iterator();
+			while (elementIteratorDefault.hasNext()) {
+				Element element = (Element) elementIteratorDefault.next();
+				String extension = element.attribute(EXTENSION_ATTRIBUTE_NAME)
+						.getValue();
+				String contentType = element.attribute(
+						CONTENT_TYPE_ATTRIBUTE_NAME).getValue();
+				addDefaultContentType(extension, contentType);
+			}
+
+			// Overriden content types
+			List overrideTypes = xmlContentTypetDoc.getRootElement().elements(
+					OVERRIDE_TAG_NAME);
+			Iterator elementIteratorOverride = overrideTypes.iterator();
+			while (elementIteratorOverride.hasNext()) {
+				Element element = (Element) elementIteratorOverride.next();
+				URI uri = new URI(element.attribute(PART_NAME_ATTRIBUTE_NAME)
+						.getValue());
+				PackagePartName partName = PackagingURIHelper
+						.createPartName(uri);
+				String contentType = element.attribute(
+						CONTENT_TYPE_ATTRIBUTE_NAME).getValue();
+				addOverrideContentType(partName, contentType);
+			}
+		} catch (URISyntaxException urie) {
+			throw new InvalidFormatException(urie.getMessage());
+		} catch (DocumentException e) {
+			throw new InvalidFormatException(e.getMessage());
+		}
+	}
+
+	/**
+	 * Save the contents type part.
+	 * 
+	 * @param outStream
+	 *            The output stream use to save the XML content of the content
+	 *            types part.
+	 * @return <b>true</b> if the operation success, else <b>false</b>.
+	 */
+	public boolean save(OutputStream outStream) {
+		Document xmlOutDoc = DocumentHelper.createDocument();
+
+		// Building namespace
+		Namespace dfNs = Namespace.get("", TYPES_NAMESPACE_URI);
+		Element typesElem = xmlOutDoc
+				.addElement(new QName(TYPES_TAG_NAME, dfNs));
+
+		// Adding default types
+		for (Entry<String, String> entry : defaultContentType.entrySet()) {
+			appendDefaultType(typesElem, entry);
+		}
+
+		// Adding specific types if any exist
+		if (overrideContentType != null) {
+			for (Entry<PackagePartName, String> entry : overrideContentType
+					.entrySet()) {
+				appendSpecificTypes(typesElem, entry);
+			}
+		}
+		xmlOutDoc.normalize();
+
+		// Save content in the specified output stream
+		return this.saveImpl(xmlOutDoc, outStream);
+	}
+
+	/**
+	 * Use to append specific type XML elements, use by the save() method.
+	 * 
+	 * @param root
+	 *            XML parent element use to append this override type element.
+	 * @param entry
+	 *            The values to append.
+	 * @see #save(java.io.OutputStream)
+	 */
+	private void appendSpecificTypes(Element root,
+			Entry<PackagePartName, String> entry) {
+		root.addElement(OVERRIDE_TAG_NAME).addAttribute(
+				PART_NAME_ATTRIBUTE_NAME,
+				((PackagePartName) entry.getKey()).getName()).addAttribute(
+				CONTENT_TYPE_ATTRIBUTE_NAME, (String) entry.getValue());
+	}
+
+	/**
+	 * Use to append default types XML elements, use by the save() metid.
+	 * 
+	 * @param root
+	 *            XML parent element use to append this default type element.
+	 * @param entry
+	 *            The values to append.
+	 * @see #save(java.io.OutputStream)
+	 */
+	private void appendDefaultType(Element root, Entry<String, String> entry) {
+		root.addElement(DEFAULT_TAG_NAME).addAttribute(
+				EXTENSION_ATTRIBUTE_NAME, (String) entry.getKey())
+				.addAttribute(CONTENT_TYPE_ATTRIBUTE_NAME,
+						(String) entry.getValue());
+
+	}
+
+	/**
+	 * Specific implementation of the save method. Call by the save() method,
+	 * call before exiting.
+	 * 
+	 * @param out
+	 *            The output stream use to write the content type XML.
+	 */
+	public abstract boolean saveImpl(Document content, OutputStream out);
+}

Modified: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java?rev=755699&r1=755698&r2=755699&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java Wed Mar 18 18:54:01 2009
@@ -1,126 +1,126 @@
-/* ====================================================================
-   Licensed to the Apache Software Foundation (ASF) under one or more
-   contributor license agreements.  See the NOTICE file distributed with
-   this work for additional information regarding copyright ownership.
-   The ASF licenses this file to You under the Apache License, Version 2.0
-   (the "License"); you may not use this file except in compliance with
-   the License.  You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-==================================================================== */
-
-package org.apache.poi.openxml4j.opc.internal;
-
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-import java.io.OutputStream;
-
-import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
-import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
-import org.apache.poi.openxml4j.opc.Package;
-import org.apache.poi.openxml4j.opc.PackagePart;
-import org.apache.poi.openxml4j.opc.PackagePartName;
-import org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller;
-
-/**
- * Memory version of a package part. Use to
- * 
- * @author Julien Chable
- * @version 1.0
- */
-public final class MemoryPackagePart extends PackagePart {
-
-	/**
-	 * Storage for the part data.
-	 */
-	protected byte[] data;
-
-	/**
-	 * Size of data.
-	 */
-	protected int length;
-
-	/**
-	 * Constructor.
-	 * 
-	 * @param pack
-	 *            The owner package.
-	 * @param partName
-	 *            The part name.
-	 * @param contentType
-	 *            The content type.
-	 * @throws InvalidFormatException
-	 *             If the specified URI is not OPC compliant.
-	 */
-	public MemoryPackagePart(Package pack, PackagePartName partName,
-			String contentType) throws InvalidFormatException {
-		super(pack, partName, contentType);
-	}
-
-	/**
-	 * Constructor.
-	 * 
-	 * @param pack
-	 *            The owner package.
-	 * @param partName
-	 *            The part name.
-	 * @param contentType
-	 *            The content type.
-	 * @param loadRelationships
-	 *            Specify if the relationships will be loaded.
-	 * @throws InvalidFormatException
-	 *             If the specified URI is not OPC compliant.
-	 */
-	public MemoryPackagePart(Package pack, PackagePartName partName,
-			String contentType, boolean loadRelationships)
-			throws InvalidFormatException {
-		super(pack, partName, new ContentType(contentType), loadRelationships);
-	}
-
-	@Override
-	protected InputStream getInputStreamImpl() {
-		// If this part has been created from scratch and/or the data buffer is
-		// not
-		// initialize, so we do it now.
-		if (data == null) {
-			data = new byte[0];
-		}
-		return new ByteArrayInputStream(data);
-	}
-
-	@Override
-	protected OutputStream getOutputStreamImpl() {
-		return new MemoryPackagePartOutputStream(this);
-	}
-
-	public void clear() {
-		data = null;
-		length = 0;
-	}
-
-	@Override
-	public boolean save(OutputStream os) throws OpenXML4JException {
-		return new ZipPartMarshaller().marshall(this, os);
-	}
-
-	@Override
-	public boolean load(InputStream ios) throws InvalidFormatException {
-		throw new InvalidFormatException("Method not implemented");
-	}
-
-	@Override
-	public void close() {
-		// Do nothing
-	}
-
-	@Override
-	public void flush() {
-		// Do nothing
-	}
-}
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.openxml4j.opc.internal;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
+import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.openxml4j.opc.PackagePartName;
+import org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller;
+
+/**
+ * Memory version of a package part. Use to
+ * 
+ * @author Julien Chable
+ * @version 1.0
+ */
+public final class MemoryPackagePart extends PackagePart {
+
+	/**
+	 * Storage for the part data.
+	 */
+	protected byte[] data;
+
+	/**
+	 * Size of data.
+	 */
+	protected int length;
+
+	/**
+	 * Constructor.
+	 * 
+	 * @param pack
+	 *            The owner package.
+	 * @param partName
+	 *            The part name.
+	 * @param contentType
+	 *            The content type.
+	 * @throws InvalidFormatException
+	 *             If the specified URI is not OPC compliant.
+	 */
+	public MemoryPackagePart(OPCPackage pack, PackagePartName partName,
+			String contentType) throws InvalidFormatException {
+		super(pack, partName, contentType);
+	}
+
+	/**
+	 * Constructor.
+	 * 
+	 * @param pack
+	 *            The owner package.
+	 * @param partName
+	 *            The part name.
+	 * @param contentType
+	 *            The content type.
+	 * @param loadRelationships
+	 *            Specify if the relationships will be loaded.
+	 * @throws InvalidFormatException
+	 *             If the specified URI is not OPC compliant.
+	 */
+	public MemoryPackagePart(OPCPackage pack, PackagePartName partName,
+			String contentType, boolean loadRelationships)
+			throws InvalidFormatException {
+		super(pack, partName, new ContentType(contentType), loadRelationships);
+	}
+
+	@Override
+	protected InputStream getInputStreamImpl() {
+		// If this part has been created from scratch and/or the data buffer is
+		// not
+		// initialize, so we do it now.
+		if (data == null) {
+			data = new byte[0];
+		}
+		return new ByteArrayInputStream(data);
+	}
+
+	@Override
+	protected OutputStream getOutputStreamImpl() {
+		return new MemoryPackagePartOutputStream(this);
+	}
+
+	public void clear() {
+		data = null;
+		length = 0;
+	}
+
+	@Override
+	public boolean save(OutputStream os) throws OpenXML4JException {
+		return new ZipPartMarshaller().marshall(this, os);
+	}
+
+	@Override
+	public boolean load(InputStream ios) throws InvalidFormatException {
+		throw new InvalidFormatException("Method not implemented");
+	}
+
+	@Override
+	public void close() {
+		// Do nothing
+	}
+
+	@Override
+	public void flush() {
+		// Do nothing
+	}
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message