You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PackagePartCollection.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.openxml4j.opc;
  16. import java.io.Serializable;
  17. import java.util.Collection;
  18. import java.util.Collections;
  19. import java.util.HashSet;
  20. import java.util.Set;
  21. import java.util.TreeMap;
  22. import java.util.function.ToIntFunction;
  23. import java.util.regex.Matcher;
  24. import java.util.regex.Pattern;
  25. import com.zaxxer.sparsebits.SparseBitSet;
  26. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  27. import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
  28. /**
  29. * A package part collection.
  30. */
  31. public final class PackagePartCollection implements Serializable {
  32. private static final long serialVersionUID = 2515031135957635517L;
  33. /**
  34. * HashSet use to store this collection part names as string for rule
  35. * M1.11 optimized checking.
  36. */
  37. private final Set<String> registerPartNameStr = new HashSet<>();
  38. private final TreeMap<String, PackagePart> packagePartLookup =
  39. new TreeMap<>(PackagePartName::compare);
  40. /**
  41. * Check rule [M1.11]: a package implementer shall neither create nor
  42. * recognize a part with a part name derived from another part name by
  43. * appending segments to it.
  44. *
  45. * @param partName name of part
  46. * @param part part to put
  47. * @return the previous value associated with {@code partName}, or
  48. * {@code null} if there was no mapping for {@code partName}.
  49. * @exception InvalidOperationException
  50. * Throws if you try to add a part with a name derived from
  51. * another part name.
  52. */
  53. public PackagePart put(final PackagePartName partName, final PackagePart part) {
  54. final String ppName = partName.getName();
  55. final StringBuilder concatSeg = new StringBuilder();
  56. // split at slash, but keep leading slash
  57. final String delim = "(?=["+PackagingURIHelper.FORWARD_SLASH_STRING+".])";
  58. for (String seg : ppName.split(delim)) {
  59. concatSeg.append(seg);
  60. if (registerPartNameStr.contains(concatSeg.toString())) {
  61. throw new InvalidOperationException(
  62. "You can't add a part with a part name derived from another part ! [M1.11]");
  63. }
  64. }
  65. registerPartNameStr.add(ppName);
  66. return packagePartLookup.put(ppName, part);
  67. }
  68. public PackagePart remove(PackagePartName key) {
  69. if (key == null) {
  70. return null;
  71. }
  72. final String ppName = key.getName();
  73. PackagePart pp = packagePartLookup.remove(ppName);
  74. if (pp != null) {
  75. this.registerPartNameStr.remove(ppName);
  76. }
  77. return pp;
  78. }
  79. /**
  80. * The values themselves should be returned in sorted order. Doing it here
  81. * avoids paying the high cost of Natural Ordering per insertion.
  82. * @return unmodifiable collection of parts
  83. */
  84. public Collection<PackagePart> sortedValues() {
  85. return Collections.unmodifiableCollection(packagePartLookup.values());
  86. }
  87. public boolean containsKey(PackagePartName partName) {
  88. return partName != null && packagePartLookup.containsKey(partName.getName());
  89. }
  90. public PackagePart get(PackagePartName partName) {
  91. return partName == null ? null : packagePartLookup.get(partName.getName());
  92. }
  93. public int size() {
  94. return packagePartLookup.size();
  95. }
  96. /**
  97. * Get an unused part index based on the namePattern, which doesn't exist yet
  98. * and has the lowest positive index
  99. *
  100. * @param nameTemplate
  101. * The template for new part names containing a {@code '#'} for the index,
  102. * e.g. "/ppt/slides/slide#.xml"
  103. * @return the next available part name index
  104. * @throws InvalidFormatException if the nameTemplate is null or doesn't contain
  105. * the index char (#) or results in an invalid part name
  106. */
  107. public int getUnusedPartIndex(final String nameTemplate) throws InvalidFormatException {
  108. if (nameTemplate == null || !nameTemplate.contains("#")) {
  109. throw new InvalidFormatException("name template must not be null and contain an index char (#)");
  110. }
  111. final Pattern pattern = Pattern.compile(nameTemplate.replace("#", "([0-9]+)"));
  112. final ToIntFunction<String> indexFromName = name -> {
  113. Matcher m = pattern.matcher(name);
  114. return m.matches() ? Integer.parseInt(m.group(1)) : 0;
  115. };
  116. return packagePartLookup.keySet().stream()
  117. .mapToInt(indexFromName)
  118. .collect(MySparseBitSet::new, MySparseBitSet::set, MySparseBitSet::myOr).nextClearBit(1);
  119. }
  120. private class MySparseBitSet extends SparseBitSet {
  121. public void myOr(MySparseBitSet other) {
  122. this.or(other);
  123. }
  124. }
  125. }