aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java
blob: 990a3bf4cb4f98c992132f2233f8d1bf2bf9d83a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
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 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 + "]";
	}
}