aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/PackagePartName.java
blob: 971064db69b31b6be4d98d1f76db066df92571ea (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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/* ====================================================================
   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.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Locale;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException;

/**
 * An immutable Open Packaging Convention compliant part name.
 *
 * @see <a href="http://www.ietf.org/rfc/rfc3986.txt">http://www.ietf.org/rfc/rfc3986.txt</a>
 */
public final class PackagePartName implements Comparable<PackagePartName> {

    /**
     * Part name stored as an URI.
     */
    private final URI partNameURI;

    /*
     * URI Characters definition (RFC 3986)
     */

    /**
     * Reserved characters for sub delimiters.
     */
    private static final String RFC3986_PCHAR_SUB_DELIMS = "!$&'()*+,;=";

    /**
     * Unreserved character (+ ALPHA & DIGIT).
     */
    private static final String RFC3986_PCHAR_UNRESERVED_SUP = "-._~";

    /**
     * Authorized reserved characters for pChar.
     */
    private static final String RFC3986_PCHAR_AUTHORIZED_SUP = ":@";

    /**
     * Flag to know if this part name is from a relationship part name.
     */
    private final boolean isRelationship;

    /**
     * Constructor. Makes a ValidPartName object from a java.net.URI
     *
     * @param uri
     *            The URI to validate and to transform into ValidPartName.
     * @param checkConformance
     *            Flag to specify if the constructor have to validate the OPC
     *            conformance. Must be always <code>true</code> except for
     *            special URI like '/' which is needed for internal use by
     *            OpenXML4J but is not valid.
     * @throws InvalidFormatException
     *             Throw if the specified part name is not conform to Open
     *             Packaging Convention specifications.
     * @see java.net.URI
     */
    PackagePartName(URI uri, boolean checkConformance)
            throws InvalidFormatException {
        if (checkConformance) {
            throwExceptionIfInvalidPartUri(uri);
        } else {
            if (!PackagingURIHelper.PACKAGE_ROOT_URI.equals(uri)) {
                throw new OpenXML4JRuntimeException(
                        "OCP conformance must be check for ALL part name except special cases : ['/']");
            }
        }
        this.partNameURI = uri;
        this.isRelationship = isRelationshipPartURI(this.partNameURI);
    }

    /**
     * Constructor. Makes a ValidPartName object from a String part name.
     *
     * @param partName
     *            Part name to valid and to create.
     * @param checkConformance
     *            Flag to specify if the constructor have to validate the OPC
     *            conformance. Must be always <code>true</code> except for
     *            special URI like '/' which is needed for internal use by
     *            OpenXML4J but is not valid.
     * @throws InvalidFormatException
     *             Throw if the specified part name is not conform to Open
     *             Packaging Convention specifications.
     */
    PackagePartName(String partName, boolean checkConformance)
            throws InvalidFormatException {
        URI partURI;
        try {
            partURI = new URI(partName);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(
                    "partName argument is not a valid OPC part name !");
        }

        if (checkConformance) {
            throwExceptionIfInvalidPartUri(partURI);
        } else {
            if (!PackagingURIHelper.PACKAGE_ROOT_URI.equals(partURI)) {
                throw new OpenXML4JRuntimeException(
                        "OCP conformance must be check for ALL part name except special cases : ['/']");
            }
        }
        this.partNameURI = partURI;
        this.isRelationship = isRelationshipPartURI(this.partNameURI);
    }

    /**
     * Check if the specified part name is a relationship part name.
     *
     * @param partUri
     *            The URI to check.
     * @return <code>true</code> if this part name respect the relationship
     *         part naming convention else <code>false</code>.
     */
    private boolean isRelationshipPartURI(URI partUri) {
        if (partUri == null) {
            throw new IllegalArgumentException("partUri");
        }

        final String uriPath = partUri.getPath();
        return uriPath != null && uriPath.matches(
                "^.*/" + PackagingURIHelper.RELATIONSHIP_PART_SEGMENT_NAME + "/.*\\"
                        + PackagingURIHelper.RELATIONSHIP_PART_EXTENSION_NAME
                        + "$");
    }

    /**
     * Know if this part name is a relationship part name.
     *
     * @return <code>true</code> if this part name respect the relationship
     *         part naming convention else <code>false</code>.
     */
    public boolean isRelationshipPartURI() {
        return this.isRelationship;
    }

    /**
     * Throws an exception (of any kind) if the specified part name does not
     * follow the Open Packaging Convention specifications naming rules.
     *
     * @param partUri
     *            The part name to check.
     * @throws InvalidFormatException
     *             Throws if the part name is invalid.
     */
    private static void throwExceptionIfInvalidPartUri(URI partUri)
            throws InvalidFormatException {
        if (partUri == null) {
            throw new IllegalArgumentException("partUri");
        }
        // Check if the part name URI is empty [M1.1]
        throwExceptionIfEmptyURI(partUri);

        // Check if the part name URI is absolute
        throwExceptionIfAbsoluteUri(partUri);

        // Check if the part name URI starts with a forward slash [M1.4]
        throwExceptionIfPartNameNotStartsWithForwardSlashChar(partUri);

        // Check if the part name URI ends with a forward slash [M1.5]
        throwExceptionIfPartNameEndsWithForwardSlashChar(partUri);

        // Check if the part name does not have empty segments. [M1.3]
        // Check if a segment ends with a dot ('.') character. [M1.9]
        throwExceptionIfPartNameHaveInvalidSegments(partUri);
    }

    /**
     * Throws an exception if the specified URI is empty. [M1.1]
     *
     * @param partURI
     *            Part URI to check.
     * @throws InvalidFormatException
     *             If the specified URI is empty.
     */
    private static void throwExceptionIfEmptyURI(URI partURI)
            throws InvalidFormatException {
        if (partURI == null) {
            throw new IllegalArgumentException("partURI");
        }

        String uriPath = partURI.getPath();
        if (uriPath == null || uriPath.isEmpty()
                || ((uriPath.length() == 1) && (uriPath.charAt(0) == PackagingURIHelper.FORWARD_SLASH_CHAR))) {
            throw new InvalidFormatException(
                    "A part name shall not be empty [M1.1]: "
                            + partURI.getPath());
        }
    }

    /**
     * Throws an exception if the part name has empty segments. [M1.3]
     *
     * Throws an exception if a segment any characters other than pchar
     * characters. [M1.6]
     *
     * Throws an exception if a segment contain percent-encoded forward slash
     * ('/'), or backward slash ('\') characters. [M1.7]
     *
     * Throws an exception if a segment contain percent-encoded unreserved
     * characters. [M1.8]
     *
     * Throws an exception if the specified part name's segments end with a dot
     * ('.') character. [M1.9]
     *
     * Throws an exception if a segment doesn't include at least one non-dot
     * character. [M1.10]
     *
     * @param partUri
     *            The part name to check.
     * @throws InvalidFormatException
     *             if the specified URI contain an empty segments or if one the
     *             segments contained in the part name, ends with a dot ('.')
     *             character.
     */
    private static void throwExceptionIfPartNameHaveInvalidSegments(URI partUri)
            throws InvalidFormatException {
        if (partUri == null) {
            throw new IllegalArgumentException("partUri");
        }

        // Split the URI into several part and analyze each
        String[] segments = partUri.toASCIIString()
            .replaceFirst("^"+PackagingURIHelper.FORWARD_SLASH_CHAR,"")
            .split(PackagingURIHelper.FORWARD_SLASH_STRING);

        if (segments.length < 1) {
            throw new InvalidFormatException(
                "A part name shall not have empty segments [M1.3]: " + partUri.getPath());
        }

        for (final String seg : segments) {
            if (seg == null || seg.isEmpty()) {
                throw new InvalidFormatException(
                    "A part name shall not have empty segments [M1.3]: " + partUri.getPath());
            }

            if (seg.endsWith(".")) {
                throw new InvalidFormatException(
                    "A segment shall not end with a dot ('.') character [M1.9]: " + partUri.getPath());
            }

            if (seg.replaceAll("\\\\.", "").isEmpty()) {
                // Normally will never been invoked with the previous
                // implementation rule [M1.9]
                throw new InvalidFormatException(
                    "A segment shall include at least one non-dot character. [M1.10]: " + partUri.getPath());
            }

            // Check for rule M1.6, M1.7, M1.8
            checkPCharCompliance(seg);
        }
    }

    /**
     * Throws an exception if a segment any characters other than pchar
     * characters. [M1.6]
     *
     * Throws an exception if a segment contain percent-encoded forward slash
     * ('/'), or backward slash ('\') characters. [M1.7]
     *
     * Throws an exception if a segment contain percent-encoded unreserved
     * characters. [M1.8]
     *
     * @param segment
     *            The segment to check
     */
    private static void checkPCharCompliance(String segment)
            throws InvalidFormatException {
        final int length = segment.length();
        for (int i = 0; i < length; ++i) {
            final char c = segment.charAt(i);

            /* Check rule M1.6 */

            if (
                // Check for digit or letter
                isDigitOrLetter(c) ||
                // Check "-", ".", "_", "~"
                RFC3986_PCHAR_UNRESERVED_SUP.indexOf(c) > -1 ||
                // Check ":", "@"
                RFC3986_PCHAR_AUTHORIZED_SUP.indexOf(c) > -1 ||
                // Check "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", "="
                RFC3986_PCHAR_SUB_DELIMS.indexOf(c) > -1
            ) {
                continue;
            }


            if (c != '%') {
                throw new InvalidFormatException(
                    "A segment shall not hold any characters other than pchar characters. [M1.6]");
            }

            // We certainly found an encoded character, check for length
            // now ( '%' HEXDIGIT HEXDIGIT)
            if ((length - i) < 2 || !isHexDigit(segment.charAt(i+1)) || !isHexDigit(segment.charAt(i+2))) {
                throw new InvalidFormatException("The segment " + segment + " contain invalid encoded character !");
            }

            // Decode the encoded character
            final char decodedChar = (char) Integer.parseInt(segment.substring(i + 1, i + 3), 16);
            i += 2;

            /* Check rule M1.7 */
            if (decodedChar == '/' || decodedChar == '\\') {
                throw new InvalidFormatException(
                    "A segment shall not contain percent-encoded forward slash ('/'), or backward slash ('\\') characters. [M1.7]");
            }

            /* Check rule M1.8 */
            if (
                 // Check for unreserved character like define in RFC3986
                isDigitOrLetter(decodedChar) ||
                // Check for unreserved character "-", ".", "_", "~"
                RFC3986_PCHAR_UNRESERVED_SUP.indexOf(decodedChar) > -1
            ) {
                throw new InvalidFormatException(
                    "A segment shall not contain percent-encoded unreserved characters. [M1.8]");
            }
        }
    }

    /**
     * Throws an exception if the specified part name doesn't start with a
     * forward slash character '/'. [M1.4]
     *
     * @param partUri
     *            The part name to check.
     * @throws InvalidFormatException
     *             If the specified part name doesn't start with a forward slash
     *             character '/'.
     */
    private static void throwExceptionIfPartNameNotStartsWithForwardSlashChar(
            URI partUri) throws InvalidFormatException {
        String uriPath = partUri.getPath();
        if (!uriPath.isEmpty()
                && uriPath.charAt(0) != PackagingURIHelper.FORWARD_SLASH_CHAR) {
            throw new InvalidFormatException(
                    "A part name shall start with a forward slash ('/') character [M1.4]: "
                            + partUri.getPath());
        }
    }

    /**
     * Throws an exception if the specified part name ends with a forward slash
     * character '/'. [M1.5]
     *
     * @param partUri
     *            The part name to check.
     * @throws InvalidFormatException
     *             If the specified part name ends with a forward slash character
     *             '/'.
     */
    private static void throwExceptionIfPartNameEndsWithForwardSlashChar(
            URI partUri) throws InvalidFormatException {
        String uriPath = partUri.getPath();
        if (!uriPath.isEmpty()
                && uriPath.charAt(uriPath.length() - 1) == PackagingURIHelper.FORWARD_SLASH_CHAR) {
            throw new InvalidFormatException(
                    "A part name shall not have a forward slash as the last character [M1.5]: "
                            + partUri.getPath());
        }
    }

    /**
     * Throws an exception if the specified URI is absolute.
     *
     * @param partUri
     *            The URI to check.
     * @throws InvalidFormatException
     *             Throws if the specified URI is absolute.
     */
    private static void throwExceptionIfAbsoluteUri(URI partUri) throws InvalidFormatException {
        if (partUri.isAbsolute()) {
            throw new InvalidFormatException("Absolute URI forbidden: " + partUri);
        }
    }

    /**
     * Compare two part names following the rule M1.12 :
     *
     * Part name equivalence is determined by comparing part names as
     * case-insensitive ASCII strings. Packages shall not contain equivalent
     * part names and package implementers shall neither create nor recognize
     * packages with equivalent part names. [M1.12]
     */
    @Override
    public int compareTo(PackagePartName other) {
        // compare with natural sort order
        return compare(this, other);
    }


    /**
     * Retrieves the extension of the part name if any. If there is no extension
     * returns an empty String. Example : '/document/content.xml' =&gt; 'xml'
     *
     * @return The extension of the part name.
     */
    public String getExtension() {
        String fragment = this.partNameURI.getPath();
        if (!fragment.isEmpty()) {
            int i = fragment.lastIndexOf('.');
            if (i > -1) {
                return fragment.substring(i + 1);
            }
        }
        return "";
    }

    /**
     * Get this part name.
     *
     * @return The name of this part name.
     */
    public String getName() {
        return getURI().toASCIIString();
    }

    /**
     * Part name equivalence is determined by comparing part names as
     * case-insensitive ASCII strings. Packages shall not contain equivalent
     * part names and package implementers shall neither create nor recognize
     * packages with equivalent part names. [M1.12]
     */
    @Override
    public boolean equals(Object other) {
        return (other instanceof PackagePartName) &&
            compare(this.getName(), ((PackagePartName)other).getName()) == 0;
    }

    @Override
    public int hashCode() {
        return getName().toLowerCase(Locale.ROOT).hashCode();
    }

    @Override
    public String toString() {
        return getName();
    }

    /* Getters and setters */

    /**
     * Part name property getter.
     *
     * @return This part name URI.
     */
    public URI getURI() {
        return this.partNameURI;
    }


    /**
     * A natural sort order for package part names, consistent with the
     * requirements of {@code java.util.Comparator}, but simply implemented
     * as a static method.
     * <p>
     * For example, this sorts "file10.png" after "file2.png" (comparing the
     * numerical portion), but sorts "File10.png" before "file2.png"
     * (lexigraphical sort)
     *
     * <p>
     * When comparing part names, the rule M1.12 is followed:
     *
     * Part name equivalence is determined by comparing part names as
     * case-insensitive ASCII strings. Packages shall not contain equivalent
     * part names and package implementers shall neither create nor recognize
     * packages with equivalent part names. [M1.12]
     *
     * @param obj1 first {@code PackagePartName} to compare
     * @param obj2 second {@code PackagePartName} to compare
     * @return a negative integer, zero, or a positive integer as the first argument is less than,
     *         equal to, or greater than the second.
     */
    public static int compare(PackagePartName obj1, PackagePartName obj2) {
        return compare (
            obj1 == null ? null : obj1.getName(),
            obj2 == null ? null : obj2.getName()
        );
    }


    /**
     * A natural sort order for strings, consistent with the
     * requirements of {@code java.util.Comparator}, but simply implemented
     * as a static method.
     * <p>
     * For example, this sorts "file10.png" after "file2.png" (comparing the
     * numerical portion), but sorts "File10.png" before "file2.png"
     * (lexigraphical sort)
     *
     * @param str1 first string to compare
     * @param str2 second string to compare
     * @return a negative integer, zero, or a positive integer as the first argument is less than,
     *         equal to, or greater than the second.
     */
    public static int compare(final String str1, final String str2)
    {
        if (str1 == null) {
            // (null) == (null), (null) < (non-null)
            return (str2 == null ? 0 : -1);
        } else if (str2 == null) {
            // (non-null) > (null)
            return 1;
        }

        if (str1.equalsIgnoreCase(str2)) {
            return 0;
        }
        final String name1 = str1.toLowerCase(Locale.ROOT);
        final String name2 = str2.toLowerCase(Locale.ROOT);

        final int len1 = name1.length();
        final int len2 = name2.length();
        for (int idx1 = 0, idx2 = 0; idx1 < len1 && idx2 < len2; /*nil*/) {
            final char c1 = name1.charAt(idx1++);
            final char c2 = name2.charAt(idx2++);

            if (Character.isDigit(c1) && Character.isDigit(c2)) {
                final int beg1 = idx1 - 1;  // undo previous increment
                while (idx1 < len1 && Character.isDigit(name1.charAt(idx1))) {
                    idx1++;
                }

                final int beg2 = idx2 - 1;  // undo previous increment
                while (idx2 < len2 && Character.isDigit(name2.charAt(idx2))) {
                    idx2++;
                }

                // note: BigInteger for extra safety
                final BigInteger b1 = new BigInteger(name1.substring(beg1, idx1));
                final BigInteger b2 = new BigInteger(name2.substring(beg2, idx2));
                final int cmp = b1.compareTo(b2);
                if (cmp != 0) {
                    return cmp;
                }
            }
            else if (c1 != c2) {
                return (c1 - c2);
            }
        }

        return (len1 - len2);
    }

    private static boolean isDigitOrLetter(char c) {
        return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
    }

    private static boolean isHexDigit(char c) {
        return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
    }
}