diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java index b5da3d9bcb..9a024688f8 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java @@ -40,251 +40,251 @@ public final class PackagingURIHelper { // This class should heavily leverage Java library code to reduce the number of lines of code that POI has to maintain and test private final static POILogger _logger = POILogFactory.getLogger(PackagingURIHelper.class); - /** - * Package root URI. - */ - private static URI packageRootUri; + /** + * Package root URI. + */ + private static URI packageRootUri; - /** - * Extension name of a relationship part. - */ - public static final String RELATIONSHIP_PART_EXTENSION_NAME = ".rels"; + /** + * Extension name of a relationship part. + */ + public static final String RELATIONSHIP_PART_EXTENSION_NAME = ".rels"; - /** - * Segment name of a relationship part. - */ - public static final String RELATIONSHIP_PART_SEGMENT_NAME = "_rels"; + /** + * Segment name of a relationship part. + */ + public static final String RELATIONSHIP_PART_SEGMENT_NAME = "_rels"; - /** - * Segment name of the package properties folder. - */ - public static final String PACKAGE_PROPERTIES_SEGMENT_NAME = "docProps"; + /** + * Segment name of the package properties folder. + */ + public static final String PACKAGE_PROPERTIES_SEGMENT_NAME = "docProps"; - /** - * Core package properties art name. - */ - public static final String PACKAGE_CORE_PROPERTIES_NAME = "core.xml"; + /** + * Core package properties art name. + */ + public static final String PACKAGE_CORE_PROPERTIES_NAME = "core.xml"; - /** - * Forward slash URI separator. - */ - public static final char FORWARD_SLASH_CHAR = '/'; + /** + * Forward slash URI separator. + */ + public static final char FORWARD_SLASH_CHAR = '/'; - /** - * Forward slash URI separator. - */ - public static final String FORWARD_SLASH_STRING = "/"; + /** + * Forward slash URI separator. + */ + public static final String FORWARD_SLASH_STRING = "/"; - /** - * Package relationships part URI - */ - public static final URI PACKAGE_RELATIONSHIPS_ROOT_URI; + /** + * Package relationships part URI + */ + public static final URI PACKAGE_RELATIONSHIPS_ROOT_URI; - /** - * Package relationships part name. - */ - public static final PackagePartName PACKAGE_RELATIONSHIPS_ROOT_PART_NAME; + /** + * Package relationships part name. + */ + public static final PackagePartName PACKAGE_RELATIONSHIPS_ROOT_PART_NAME; - /** - * Core properties part URI. - */ - public static final URI CORE_PROPERTIES_URI; + /** + * Core properties part URI. + */ + public static final URI CORE_PROPERTIES_URI; - /** - * Core properties partname. - */ - public static final PackagePartName CORE_PROPERTIES_PART_NAME; + /** + * Core properties partname. + */ + public static final PackagePartName CORE_PROPERTIES_PART_NAME; - /** - * Root package URI. - */ - public static final URI PACKAGE_ROOT_URI; + /** + * Root package URI. + */ + public static final URI PACKAGE_ROOT_URI; - /** - * Root package part name. - */ - public static final PackagePartName PACKAGE_ROOT_PART_NAME; + /** + * Root package part name. + */ + public static final PackagePartName PACKAGE_ROOT_PART_NAME; - /* Static initialization */ - static { - // Make URI - URI uriPACKAGE_ROOT_URI = null; - URI uriPACKAGE_RELATIONSHIPS_ROOT_URI = null; - URI uriPACKAGE_PROPERTIES_URI = null; - try { - uriPACKAGE_ROOT_URI = new URI("/"); - uriPACKAGE_RELATIONSHIPS_ROOT_URI = new URI(FORWARD_SLASH_CHAR - + RELATIONSHIP_PART_SEGMENT_NAME + FORWARD_SLASH_CHAR - + RELATIONSHIP_PART_EXTENSION_NAME); - packageRootUri = new URI("/"); - uriPACKAGE_PROPERTIES_URI = new URI(FORWARD_SLASH_CHAR - + PACKAGE_PROPERTIES_SEGMENT_NAME + FORWARD_SLASH_CHAR - + PACKAGE_CORE_PROPERTIES_NAME); - } catch (URISyntaxException e) { - // Should never happen in production as all data are fixed - } - PACKAGE_ROOT_URI = uriPACKAGE_ROOT_URI; - PACKAGE_RELATIONSHIPS_ROOT_URI = uriPACKAGE_RELATIONSHIPS_ROOT_URI; - CORE_PROPERTIES_URI = uriPACKAGE_PROPERTIES_URI; + /* Static initialization */ + static { + // Make URI + URI uriPACKAGE_ROOT_URI = null; + URI uriPACKAGE_RELATIONSHIPS_ROOT_URI = null; + URI uriPACKAGE_PROPERTIES_URI = null; + try { + uriPACKAGE_ROOT_URI = new URI("/"); + uriPACKAGE_RELATIONSHIPS_ROOT_URI = new URI(FORWARD_SLASH_CHAR + + RELATIONSHIP_PART_SEGMENT_NAME + FORWARD_SLASH_CHAR + + RELATIONSHIP_PART_EXTENSION_NAME); + packageRootUri = new URI("/"); + uriPACKAGE_PROPERTIES_URI = new URI(FORWARD_SLASH_CHAR + + PACKAGE_PROPERTIES_SEGMENT_NAME + FORWARD_SLASH_CHAR + + PACKAGE_CORE_PROPERTIES_NAME); + } catch (URISyntaxException e) { + // Should never happen in production as all data are fixed + } + PACKAGE_ROOT_URI = uriPACKAGE_ROOT_URI; + PACKAGE_RELATIONSHIPS_ROOT_URI = uriPACKAGE_RELATIONSHIPS_ROOT_URI; + CORE_PROPERTIES_URI = uriPACKAGE_PROPERTIES_URI; - // Make part name from previous URI - PackagePartName tmpPACKAGE_ROOT_PART_NAME = null; - PackagePartName tmpPACKAGE_RELATIONSHIPS_ROOT_PART_NAME = null; - PackagePartName tmpCORE_PROPERTIES_URI = null; - try { - tmpPACKAGE_RELATIONSHIPS_ROOT_PART_NAME = createPartName(PACKAGE_RELATIONSHIPS_ROOT_URI); - tmpCORE_PROPERTIES_URI = createPartName(CORE_PROPERTIES_URI); - tmpPACKAGE_ROOT_PART_NAME = new PackagePartName(PACKAGE_ROOT_URI, - false); - } catch (InvalidFormatException e) { - // Should never happen in production as all data are fixed - } - PACKAGE_RELATIONSHIPS_ROOT_PART_NAME = tmpPACKAGE_RELATIONSHIPS_ROOT_PART_NAME; - CORE_PROPERTIES_PART_NAME = tmpCORE_PROPERTIES_URI; - PACKAGE_ROOT_PART_NAME = tmpPACKAGE_ROOT_PART_NAME; - } + // Make part name from previous URI + PackagePartName tmpPACKAGE_ROOT_PART_NAME = null; + PackagePartName tmpPACKAGE_RELATIONSHIPS_ROOT_PART_NAME = null; + PackagePartName tmpCORE_PROPERTIES_URI = null; + try { + tmpPACKAGE_RELATIONSHIPS_ROOT_PART_NAME = createPartName(PACKAGE_RELATIONSHIPS_ROOT_URI); + tmpCORE_PROPERTIES_URI = createPartName(CORE_PROPERTIES_URI); + tmpPACKAGE_ROOT_PART_NAME = new PackagePartName(PACKAGE_ROOT_URI, + false); + } catch (InvalidFormatException e) { + // Should never happen in production as all data are fixed + } + PACKAGE_RELATIONSHIPS_ROOT_PART_NAME = tmpPACKAGE_RELATIONSHIPS_ROOT_PART_NAME; + CORE_PROPERTIES_PART_NAME = tmpCORE_PROPERTIES_URI; + PACKAGE_ROOT_PART_NAME = tmpPACKAGE_ROOT_PART_NAME; + } private static final Pattern missingAuthPattern = Pattern.compile("\\w+://"); - /** - * Gets the URI for the package root. - * - * @return URI of the package root. - */ - public static URI getPackageRootUri() { - return packageRootUri; - } + /** + * Gets the URI for the package root. + * + * @return URI of the package root. + */ + public static URI getPackageRootUri() { + return packageRootUri; + } - /** - * Know if the specified URI is a relationship part name. - * - * @param partUri - * URI to check. - * @return true if the URI false. - */ - public static boolean isRelationshipPartURI(URI partUri) { - if (partUri == null) - throw new IllegalArgumentException("partUri"); + /** + * Know if the specified URI is a relationship part name. + * + * @param partUri + * URI to check. + * @return true if the URI false. + */ + public static boolean isRelationshipPartURI(URI partUri) { + if (partUri == null) + throw new IllegalArgumentException("partUri"); - return partUri.getPath().matches( - ".*" + RELATIONSHIP_PART_SEGMENT_NAME + ".*" - + RELATIONSHIP_PART_EXTENSION_NAME + "$"); - } + return partUri.getPath().matches( + ".*" + RELATIONSHIP_PART_SEGMENT_NAME + ".*" + + RELATIONSHIP_PART_EXTENSION_NAME + "$"); + } - /** - * Get file name from the specified URI. - */ - public static String getFilename(URI uri) { - if (uri != null) { - String path = uri.getPath(); - int len = path.length(); - int num2 = len; - while (--num2 >= 0) { - char ch1 = path.charAt(num2); - if (ch1 == PackagingURIHelper.FORWARD_SLASH_CHAR) - return path.substring(num2 + 1, len); - } - } - return ""; - } + /** + * Get file name from the specified URI. + */ + public static String getFilename(URI uri) { + if (uri != null) { + String path = uri.getPath(); + int len = path.length(); + int num2 = len; + while (--num2 >= 0) { + char ch1 = path.charAt(num2); + if (ch1 == PackagingURIHelper.FORWARD_SLASH_CHAR) + return path.substring(num2 + 1, len); + } + } + return ""; + } - /** - * Get the file name without the trailing extension. - */ - public static String getFilenameWithoutExtension(URI uri) { - String filename = getFilename(uri); - int dotIndex = filename.lastIndexOf("."); - if (dotIndex == -1) - return filename; - return filename.substring(0, dotIndex); - } + /** + * Get the file name without the trailing extension. + */ + public static String getFilenameWithoutExtension(URI uri) { + String filename = getFilename(uri); + int dotIndex = filename.lastIndexOf("."); + if (dotIndex == -1) + return filename; + return filename.substring(0, dotIndex); + } - /** - * Get the directory path from the specified URI. - */ - public static URI getPath(URI uri) { - if (uri != null) { - String path = uri.getPath(); + /** + * Get the directory path from the specified URI. + */ + public static URI getPath(URI uri) { + if (uri != null) { + String path = uri.getPath(); int num2 = path.length(); - while (--num2 >= 0) { - char ch1 = path.charAt(num2); - if (ch1 == PackagingURIHelper.FORWARD_SLASH_CHAR) { - try { - return new URI(path.substring(0, num2)); - } catch (URISyntaxException e) { - return null; - } - } - } - } - return null; - } + while (--num2 >= 0) { + char ch1 = path.charAt(num2); + if (ch1 == PackagingURIHelper.FORWARD_SLASH_CHAR) { + try { + return new URI(path.substring(0, num2)); + } catch (URISyntaxException e) { + return null; + } + } + } + } + return null; + } - /** - * Combine two URIs. - * - * @param prefix the prefix URI - * @param suffix the suffix URI - * - * @return the combined URI - */ - public static URI combine(URI prefix, URI suffix) { - URI retUri; - try { - retUri = new URI(combine(prefix.getPath(), suffix.getPath())); - } catch (URISyntaxException e) { - throw new IllegalArgumentException( - "Prefix and suffix can't be combine !"); - } - return retUri; - } + /** + * Combine two URIs. + * + * @param prefix the prefix URI + * @param suffix the suffix URI + * + * @return the combined URI + */ + public static URI combine(URI prefix, URI suffix) { + URI retUri; + try { + retUri = new URI(combine(prefix.getPath(), suffix.getPath())); + } catch (URISyntaxException e) { + throw new IllegalArgumentException( + "Prefix and suffix can't be combine !"); + } + return retUri; + } - /** - * Combine a string URI with a prefix and a suffix. - */ - public static String combine(String prefix, String suffix) { - if (!prefix.endsWith(FORWARD_SLASH_STRING) && !suffix.startsWith(FORWARD_SLASH_STRING)) - return prefix + FORWARD_SLASH_CHAR + suffix; - else if (prefix.endsWith(FORWARD_SLASH_STRING) ^ suffix.startsWith(FORWARD_SLASH_STRING)) - return prefix + suffix; - else - return ""; - } + /** + * Combine a string URI with a prefix and a suffix. + */ + public static String combine(String prefix, String suffix) { + if (!prefix.endsWith(FORWARD_SLASH_STRING) && !suffix.startsWith(FORWARD_SLASH_STRING)) + return prefix + FORWARD_SLASH_CHAR + suffix; + else if (prefix.endsWith(FORWARD_SLASH_STRING) ^ suffix.startsWith(FORWARD_SLASH_STRING)) + return prefix + suffix; + else + return ""; + } - /** - * Fully relativize the source part URI against the target part URI. - * - * @param sourceURI - * The source part URI. - * @param targetURI - * The target part URI. + /** + * Fully relativize the source part URI against the target part URI. + * + * @param sourceURI + * The source part URI. + * @param targetURI + * The target part URI. * @param msCompatible if true then remove leading slash from the relativized URI. * This flag violates [M1.4]: A part name shall start with a forward slash ('/') character, but * allows generating URIs compatible with MS Office and OpenOffice. - * @return A fully relativize part name URI ('word/media/image1.gif', - * '/word/document.xml' => 'media/image1.gif') else - * null. - */ - public static URI relativizeURI(URI sourceURI, URI targetURI, boolean msCompatible) { - StringBuilder retVal = new StringBuilder(); - String[] segmentsSource = sourceURI.getPath().split("/", -1); - String[] segmentsTarget = targetURI.getPath().split("/", -1); + * @return A fully relativize part name URI ('word/media/image1.gif', + * '/word/document.xml' => 'media/image1.gif') else + * null. + */ + public static URI relativizeURI(URI sourceURI, URI targetURI, boolean msCompatible) { + StringBuilder retVal = new StringBuilder(); + String[] segmentsSource = sourceURI.getPath().split("/", -1); + String[] segmentsTarget = targetURI.getPath().split("/", -1); - // If the source URI is empty - if (segmentsSource.length == 0) { - throw new IllegalArgumentException( - "Can't relativize an empty source URI !"); - } + // If the source URI is empty + if (segmentsSource.length == 0) { + throw new IllegalArgumentException( + "Can't relativize an empty source URI !"); + } - // If target URI is empty - if (segmentsTarget.length == 0) { - throw new IllegalArgumentException( - "Can't relativize an empty target URI !"); - } + // If target URI is empty + if (segmentsTarget.length == 0) { + throw new IllegalArgumentException( + "Can't relativize an empty target URI !"); + } - // If the source is the root, then the relativized - // form must actually be an absolute URI - if(sourceURI.toString().equals("/")) { + // If the source is the root, then the relativized + // form must actually be an absolute URI + if(sourceURI.toString().equals("/")) { String path = targetURI.getPath(); if(msCompatible && path.length() > 0 && path.charAt(0) == '/') { try { @@ -294,79 +294,79 @@ public final class PackagingURIHelper { return null; } } - return targetURI; - } + return targetURI; + } - // Relativize the source URI against the target URI. - // First up, figure out how many steps along we can go - // and still have them be the same - int segmentsTheSame = 0; - for (int i = 0; i < segmentsSource.length && i < segmentsTarget.length; i++) { - if (segmentsSource[i].equals(segmentsTarget[i])) { - // Match so far, good - segmentsTheSame++; - } else { - break; - } - } + // Relativize the source URI against the target URI. + // First up, figure out how many steps along we can go + // and still have them be the same + int segmentsTheSame = 0; + for (int i = 0; i < segmentsSource.length && i < segmentsTarget.length; i++) { + if (segmentsSource[i].equals(segmentsTarget[i])) { + // Match so far, good + segmentsTheSame++; + } else { + break; + } + } - // If we didn't have a good match or at least except a first empty element - if ((segmentsTheSame == 0 || segmentsTheSame == 1) && - segmentsSource[0].isEmpty() && segmentsTarget[0].isEmpty()) { - for (int i = 0; i < segmentsSource.length - 2; i++) { - retVal.append("../"); - } - for (int i = 0; i < segmentsTarget.length; i++) { - if (segmentsTarget[i].isEmpty()) - continue; - retVal.append(segmentsTarget[i]); - if (i != segmentsTarget.length - 1) - retVal.append("/"); - } + // If we didn't have a good match or at least except a first empty element + if ((segmentsTheSame == 0 || segmentsTheSame == 1) && + segmentsSource[0].isEmpty() && segmentsTarget[0].isEmpty()) { + for (int i = 0; i < segmentsSource.length - 2; i++) { + retVal.append("../"); + } + for (int i = 0; i < segmentsTarget.length; i++) { + if (segmentsTarget[i].isEmpty()) + continue; + retVal.append(segmentsTarget[i]); + if (i != segmentsTarget.length - 1) + retVal.append("/"); + } - try { - return new URI(retVal.toString()); - } catch (Exception e) { - _logger.log(POILogger.WARN, e); - return null; - } - } + try { + return new URI(retVal.toString()); + } catch (Exception e) { + _logger.log(POILogger.WARN, e); + return null; + } + } - // Special case for where the two are the same - if (segmentsTheSame == segmentsSource.length - && segmentsTheSame == segmentsTarget.length) { + // Special case for where the two are the same + if (segmentsTheSame == segmentsSource.length + && segmentsTheSame == segmentsTarget.length) { if(sourceURI.equals(targetURI)){ // if source and target are the same they should be resolved to the last segment, // Example: if a slide references itself, e.g. the source URI is // "/ppt/slides/slide1.xml" and the targetURI is "slide1.xml" then // this it should be relativized as "slide1.xml", i.e. the last segment. retVal.append(segmentsSource[segmentsSource.length - 1]); - } + } - } else { - // Matched for so long, but no more + } else { + // Matched for so long, but no more - // Do we need to go up a directory or two from - // the source to get here? - // (If it's all the way up, then don't bother!) - if (segmentsTheSame == 1) { - retVal.append("/"); - } else { - for (int j = segmentsTheSame; j < segmentsSource.length - 1; j++) { - retVal.append("../"); - } - } + // Do we need to go up a directory or two from + // the source to get here? + // (If it's all the way up, then don't bother!) + if (segmentsTheSame == 1) { + retVal.append("/"); + } else { + for (int j = segmentsTheSame; j < segmentsSource.length - 1; j++) { + retVal.append("../"); + } + } - // Now go from here on down - for (int j = segmentsTheSame; j < segmentsTarget.length; j++) { - if (retVal.length() > 0 - && retVal.charAt(retVal.length() - 1) != '/') { - retVal.append("/"); - } - retVal.append(segmentsTarget[j]); - } - } + // Now go from here on down + for (int j = segmentsTheSame; j < segmentsTarget.length; j++) { + if (retVal.length() > 0 + && retVal.charAt(retVal.length() - 1) != '/') { + retVal.append("/"); + } + retVal.append(segmentsTarget[j]); + } + } // if the target had a fragment then append it to the result String fragment = targetURI.getRawFragment(); @@ -374,13 +374,13 @@ public final class PackagingURIHelper { retVal.append("#").append(fragment); } - try { - return new URI(retVal.toString()); - } catch (Exception e) { - _logger.log(POILogger.WARN, e); - return null; - } - } + try { + return new URI(retVal.toString()); + } catch (Exception e) { + _logger.log(POILogger.WARN, e); + return null; + } + } /** * Fully relativize the source part URI against the target part URI. @@ -398,267 +398,267 @@ public final class PackagingURIHelper { } /** - * Resolve a source uri against a target. - * - * @param sourcePartUri - * The source URI. - * @param targetUri - * The target URI. - * @return The resolved URI. - */ - public static URI resolvePartUri(URI sourcePartUri, URI targetUri) { - if (sourcePartUri == null || sourcePartUri.isAbsolute()) { - throw new IllegalArgumentException("sourcePartUri invalid - " - + sourcePartUri); - } + * Resolve a source uri against a target. + * + * @param sourcePartUri + * The source URI. + * @param targetUri + * The target URI. + * @return The resolved URI. + */ + public static URI resolvePartUri(URI sourcePartUri, URI targetUri) { + if (sourcePartUri == null || sourcePartUri.isAbsolute()) { + throw new IllegalArgumentException("sourcePartUri invalid - " + + sourcePartUri); + } - if (targetUri == null || targetUri.isAbsolute()) { - throw new IllegalArgumentException("targetUri invalid - " - + targetUri); - } + if (targetUri == null || targetUri.isAbsolute()) { + throw new IllegalArgumentException("targetUri invalid - " + + targetUri); + } - return sourcePartUri.resolve(targetUri); - } + return sourcePartUri.resolve(targetUri); + } - /** - * Get URI from a string path. - */ - public static URI getURIFromPath(String path) { - URI retUri; - try { - retUri = toURI(path); - } catch (URISyntaxException e) { - throw new IllegalArgumentException("path"); - } - return retUri; - } + /** + * Get URI from a string path. + */ + public static URI getURIFromPath(String path) { + URI retUri; + try { + retUri = toURI(path); + } catch (URISyntaxException e) { + throw new IllegalArgumentException("path"); + } + return retUri; + } - /** - * Get the source part URI from a specified relationships part. - * - * @param relationshipPartUri - * The relationship part use to retrieve the source part. - * @return The source part URI from the specified relationships part. - */ - public static URI getSourcePartUriFromRelationshipPartUri( - URI relationshipPartUri) { - if (relationshipPartUri == null) - throw new IllegalArgumentException( - "Must not be null"); + /** + * Get the source part URI from a specified relationships part. + * + * @param relationshipPartUri + * The relationship part use to retrieve the source part. + * @return The source part URI from the specified relationships part. + */ + public static URI getSourcePartUriFromRelationshipPartUri( + URI relationshipPartUri) { + if (relationshipPartUri == null) + throw new IllegalArgumentException( + "Must not be null"); - if (!isRelationshipPartURI(relationshipPartUri)) - throw new IllegalArgumentException( - "Must be a relationship part"); + if (!isRelationshipPartURI(relationshipPartUri)) + throw new IllegalArgumentException( + "Must be a relationship part"); - if (relationshipPartUri.compareTo(PACKAGE_RELATIONSHIPS_ROOT_URI) == 0) - return PACKAGE_ROOT_URI; + if (relationshipPartUri.compareTo(PACKAGE_RELATIONSHIPS_ROOT_URI) == 0) + return PACKAGE_ROOT_URI; - String filename = relationshipPartUri.getPath(); - String filenameWithoutExtension = getFilenameWithoutExtension(relationshipPartUri); - filename = filename - .substring(0, ((filename.length() - filenameWithoutExtension - .length()) - RELATIONSHIP_PART_EXTENSION_NAME.length())); - filename = filename.substring(0, filename.length() - - RELATIONSHIP_PART_SEGMENT_NAME.length() - 1); - filename = combine(filename, filenameWithoutExtension); - return getURIFromPath(filename); - } + String filename = relationshipPartUri.getPath(); + String filenameWithoutExtension = getFilenameWithoutExtension(relationshipPartUri); + filename = filename + .substring(0, ((filename.length() - filenameWithoutExtension + .length()) - RELATIONSHIP_PART_EXTENSION_NAME.length())); + filename = filename.substring(0, filename.length() + - RELATIONSHIP_PART_SEGMENT_NAME.length() - 1); + filename = combine(filename, filenameWithoutExtension); + return getURIFromPath(filename); + } - /** - * Create an OPC compliant part name by throwing an exception if the URI is - * not valid. - * - * @param partUri - * The part name URI to validate. - * @return A valid part name object, else null. - * @throws InvalidFormatException - * Throws if the specified URI is not OPC compliant. - */ - public static PackagePartName createPartName(URI partUri) - throws InvalidFormatException { - if (partUri == null) - throw new IllegalArgumentException("partName"); + /** + * Create an OPC compliant part name by throwing an exception if the URI is + * not valid. + * + * @param partUri + * The part name URI to validate. + * @return A valid part name object, else null. + * @throws InvalidFormatException + * Throws if the specified URI is not OPC compliant. + */ + public static PackagePartName createPartName(URI partUri) + throws InvalidFormatException { + if (partUri == null) + throw new IllegalArgumentException("partName"); - return new PackagePartName(partUri, true); - } + return new PackagePartName(partUri, true); + } - /** - * Create an OPC compliant part name. - * - * @param partName - * The part name to validate. - * @return The correspondant part name if valid, else null. - * @throws InvalidFormatException - * Throws if the specified part name is not OPC compliant. - * @see #createPartName(URI) - */ - public static PackagePartName createPartName(String partName) - throws InvalidFormatException { - URI partNameURI; - try { - partNameURI = toURI(partName); - } catch (URISyntaxException e) { - throw new InvalidFormatException(e.getMessage()); - } - return createPartName(partNameURI); - } + /** + * Create an OPC compliant part name. + * + * @param partName + * The part name to validate. + * @return The correspondant part name if valid, else null. + * @throws InvalidFormatException + * Throws if the specified part name is not OPC compliant. + * @see #createPartName(URI) + */ + public static PackagePartName createPartName(String partName) + throws InvalidFormatException { + URI partNameURI; + try { + partNameURI = toURI(partName); + } catch (URISyntaxException e) { + throw new InvalidFormatException(e.getMessage()); + } + return createPartName(partNameURI); + } - /** - * Create an OPC compliant part name by resolving it using a base part. - * - * @param partName - * The part name to validate. - * @param relativePart - * The relative base part. - * @return The correspondant part name if valid, else null. - * @throws InvalidFormatException - * Throws if the specified part name is not OPC compliant. - * @see #createPartName(URI) - */ - public static PackagePartName createPartName(String partName, - PackagePart relativePart) throws InvalidFormatException { - URI newPartNameURI; - try { - newPartNameURI = resolvePartUri( - relativePart.getPartName().getURI(), new URI(partName)); - } catch (URISyntaxException e) { - throw new InvalidFormatException(e.getMessage()); - } - return createPartName(newPartNameURI); - } + /** + * Create an OPC compliant part name by resolving it using a base part. + * + * @param partName + * The part name to validate. + * @param relativePart + * The relative base part. + * @return The correspondant part name if valid, else null. + * @throws InvalidFormatException + * Throws if the specified part name is not OPC compliant. + * @see #createPartName(URI) + */ + public static PackagePartName createPartName(String partName, + PackagePart relativePart) throws InvalidFormatException { + URI newPartNameURI; + try { + newPartNameURI = resolvePartUri( + relativePart.getPartName().getURI(), new URI(partName)); + } catch (URISyntaxException e) { + throw new InvalidFormatException(e.getMessage()); + } + return createPartName(newPartNameURI); + } - /** - * Create an OPC compliant part name by resolving it using a base part. - * - * @param partName - * The part name URI to validate. - * @param relativePart - * The relative base part. - * @return The correspondant part name if valid, else null. - * @throws InvalidFormatException - * Throws if the specified part name is not OPC compliant. - * @see #createPartName(URI) - */ - public static PackagePartName createPartName(URI partName, - PackagePart relativePart) throws InvalidFormatException { - URI newPartNameURI = resolvePartUri( - relativePart.getPartName().getURI(), partName); - return createPartName(newPartNameURI); - } + /** + * Create an OPC compliant part name by resolving it using a base part. + * + * @param partName + * The part name URI to validate. + * @param relativePart + * The relative base part. + * @return The correspondant part name if valid, else null. + * @throws InvalidFormatException + * Throws if the specified part name is not OPC compliant. + * @see #createPartName(URI) + */ + public static PackagePartName createPartName(URI partName, + PackagePart relativePart) throws InvalidFormatException { + URI newPartNameURI = resolvePartUri( + relativePart.getPartName().getURI(), partName); + return createPartName(newPartNameURI); + } - /** - * Validate a part URI by returning a boolean. - * ([M1.1],[M1.3],[M1.4],[M1.5],[M1.6]) - * - * (OPC Specifications 8.1.1 Part names) : - * - * Part Name Syntax - * - * The part name grammar is defined as follows: - * - * part_name = 1*( "/" segment ) - * - * segment = 1*( pchar ) - * - * - * (pchar is defined in RFC 3986) - * - * @param partUri - * The URI to validate. - * @return true if the URI is valid to the OPC Specifications, else - * false - * - * @see #createPartName(URI) - */ - public static boolean isValidPartName(URI partUri) { - if (partUri == null) - throw new IllegalArgumentException("partUri"); + /** + * Validate a part URI by returning a boolean. + * ([M1.1],[M1.3],[M1.4],[M1.5],[M1.6]) + * + * (OPC Specifications 8.1.1 Part names) : + * + * Part Name Syntax + * + * The part name grammar is defined as follows: + * + * part_name = 1*( "/" segment ) + * + * segment = 1*( pchar ) + * + * + * (pchar is defined in RFC 3986) + * + * @param partUri + * The URI to validate. + * @return true if the URI is valid to the OPC Specifications, else + * false + * + * @see #createPartName(URI) + */ + public static boolean isValidPartName(URI partUri) { + if (partUri == null) + throw new IllegalArgumentException("partUri"); - try { - createPartName(partUri); - return true; - } catch (Exception e) { - return false; - } - } + try { + createPartName(partUri); + return true; + } catch (Exception e) { + return false; + } + } - /** - * Decode a URI by converting all percent encoded character into a String - * character. - * - * @param uri - * The URI to decode. - * @return The specified URI in a String with converted percent encoded - * characters. - */ - public static String decodeURI(URI uri) { - StringBuilder retVal = new StringBuilder(64); - String uriStr = uri.toASCIIString(); - char c; - final int length = uriStr.length(); - for (int i = 0; i < length; ++i) { - c = uriStr.charAt(i); - if (c == '%') { - // We certainly found an encoded character, check for length - // now ( '%' HEXDIGIT HEXDIGIT) - if (((length - i) < 2)) { - throw new IllegalArgumentException("The uri " + uriStr - + " contain invalid encoded character !"); - } + /** + * Decode a URI by converting all percent encoded character into a String + * character. + * + * @param uri + * The URI to decode. + * @return The specified URI in a String with converted percent encoded + * characters. + */ + public static String decodeURI(URI uri) { + StringBuilder retVal = new StringBuilder(64); + String uriStr = uri.toASCIIString(); + char c; + final int length = uriStr.length(); + for (int i = 0; i < length; ++i) { + c = uriStr.charAt(i); + if (c == '%') { + // We certainly found an encoded character, check for length + // now ( '%' HEXDIGIT HEXDIGIT) + if (((length - i) < 2)) { + throw new IllegalArgumentException("The uri " + uriStr + + " contain invalid encoded character !"); + } - // Decode the encoded character - char decodedChar = (char) Integer.parseInt(uriStr.substring( - i + 1, i + 3), 16); - retVal.append(decodedChar); - i += 2; - continue; - } - retVal.append(c); - } - return retVal.toString(); - } + // Decode the encoded character + char decodedChar = (char) Integer.parseInt(uriStr.substring( + i + 1, i + 3), 16); + retVal.append(decodedChar); + i += 2; + continue; + } + retVal.append(c); + } + return retVal.toString(); + } - /** - * Build a part name where the relationship should be stored ((ex - * /word/document.xml -> /word/_rels/document.xml.rels) - * - * @param partName - * Source part URI - * @return the full path (as URI) of the relation file - * @throws InvalidOperationException - * Throws if the specified URI is a relationshp part. - */ - public static PackagePartName getRelationshipPartName( - PackagePartName partName) { - if (partName == null) - throw new IllegalArgumentException("partName"); + /** + * Build a part name where the relationship should be stored ((ex + * /word/document.xml -> /word/_rels/document.xml.rels) + * + * @param partName + * Source part URI + * @return the full path (as URI) of the relation file + * @throws InvalidOperationException + * Throws if the specified URI is a relationshp part. + */ + public static PackagePartName getRelationshipPartName( + PackagePartName partName) { + if (partName == null) + throw new IllegalArgumentException("partName"); - if (PackagingURIHelper.PACKAGE_ROOT_URI.getPath().equals( + if (PackagingURIHelper.PACKAGE_ROOT_URI.getPath().equals( partName.getURI().getPath()) ) - return PackagingURIHelper.PACKAGE_RELATIONSHIPS_ROOT_PART_NAME; + return PackagingURIHelper.PACKAGE_RELATIONSHIPS_ROOT_PART_NAME; - if (partName.isRelationshipPartURI()) - throw new InvalidOperationException("Can't be a relationship part"); + if (partName.isRelationshipPartURI()) + throw new InvalidOperationException("Can't be a relationship part"); - String fullPath = partName.getURI().getPath(); - String filename = getFilename(partName.getURI()); - fullPath = fullPath.substring(0, fullPath.length() - filename.length()); - fullPath = combine(fullPath, - PackagingURIHelper.RELATIONSHIP_PART_SEGMENT_NAME); - fullPath = combine(fullPath, filename); - fullPath = fullPath - + PackagingURIHelper.RELATIONSHIP_PART_EXTENSION_NAME; + String fullPath = partName.getURI().getPath(); + String filename = getFilename(partName.getURI()); + fullPath = fullPath.substring(0, fullPath.length() - filename.length()); + fullPath = combine(fullPath, + PackagingURIHelper.RELATIONSHIP_PART_SEGMENT_NAME); + fullPath = combine(fullPath, filename); + fullPath = fullPath + + PackagingURIHelper.RELATIONSHIP_PART_EXTENSION_NAME; - PackagePartName retPartName; - try { - retPartName = createPartName(fullPath); - } catch (InvalidFormatException e) { - // Should never happen in production as all data are fixed but in - // case of return null: - return null; - } - return retPartName; - } + PackagePartName retPartName; + try { + retPartName = createPartName(fullPath); + } catch (InvalidFormatException e) { + // Should never happen in production as all data are fixed but in + // case of return null: + return null; + } + return retPartName; + } /** * Convert a string to {@link java.net.URI} @@ -759,7 +759,7 @@ public final class PackagingURIHelper { }; private static boolean isUnsafe(int ch) { - return ch > 0x80 || Character.isWhitespace(ch); + return ch >= 0x80 || Character.isWhitespace(ch); } } diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackagingURIHelper.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackagingURIHelper.java index c9980c8fa5..9e1f00ead6 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackagingURIHelper.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackagingURIHelper.java @@ -25,36 +25,36 @@ import org.apache.poi.openxml4j.exceptions.InvalidFormatException; public class TestPackagingURIHelper extends TestCase { - /** - * Test relativizePartName() method. - */ - public void testRelativizeURI() throws Exception { - URI uri1 = new URI("/word/document.xml"); - URI uri2 = new URI("/word/media/image1.gif"); + /** + * Test relativizePartName() method. + */ + public void testRelativizeURI() throws Exception { + URI uri1 = new URI("/word/document.xml"); + URI uri2 = new URI("/word/media/image1.gif"); URI uri3 = new URI("/word/media/image1.gif#Sheet1!A1"); URI uri4 = new URI("#'My%20Sheet1'!A1"); - // Document to image is down a directory - URI retURI1to2 = PackagingURIHelper.relativizeURI(uri1, uri2); - assertEquals("media/image1.gif", retURI1to2.getPath()); - // Image to document is up a directory - URI retURI2to1 = PackagingURIHelper.relativizeURI(uri2, uri1); - assertEquals("../document.xml", retURI2to1.getPath()); - - // Document and CustomXML parts totally different [Julien C.] - URI uriCustomXml = new URI("/customXml/item1.xml"); - URI uriRes = PackagingURIHelper.relativizeURI(uri1, uriCustomXml); - assertEquals("../customXml/item1.xml", uriRes.toString()); + // Document to image is down a directory + URI retURI1to2 = PackagingURIHelper.relativizeURI(uri1, uri2); + assertEquals("media/image1.gif", retURI1to2.getPath()); + // Image to document is up a directory + URI retURI2to1 = PackagingURIHelper.relativizeURI(uri2, uri1); + assertEquals("../document.xml", retURI2to1.getPath()); - // Document to itself is the same place (empty URI) - URI retURI2 = PackagingURIHelper.relativizeURI(uri1, uri1); - // YK: the line below used to assert empty string which is wrong + // Document and CustomXML parts totally different [Julien C.] + URI uriCustomXml = new URI("/customXml/item1.xml"); + URI uriRes = PackagingURIHelper.relativizeURI(uri1, uriCustomXml); + assertEquals("../customXml/item1.xml", uriRes.toString()); + + // Document to itself is the same place (empty URI) + URI retURI2 = PackagingURIHelper.relativizeURI(uri1, uri1); + // YK: the line below used to assert empty string which is wrong // if source and target are the same they should be relaitivized as the last segment, // see Bugzilla 51187 assertEquals("document.xml", retURI2.getPath()); - // relativization against root - URI root = new URI("/"); + // relativization against root + URI root = new URI("/"); uriRes = PackagingURIHelper.relativizeURI(root, uri1); assertEquals("/word/document.xml", uriRes.toString()); @@ -69,48 +69,48 @@ public class TestPackagingURIHelper extends TestCase { assertEquals("#'My%20Sheet1'!A1", uriRes.toString()); } - /** - * Test createPartName(String, y) - */ - public void testCreatePartNameRelativeString() - throws InvalidFormatException { - PackagePartName partNameToValid = PackagingURIHelper - .createPartName("/word/media/image1.gif"); + /** + * Test createPartName(String, y) + */ + public void testCreatePartNameRelativeString() + throws InvalidFormatException { + PackagePartName partNameToValid = PackagingURIHelper + .createPartName("/word/media/image1.gif"); - OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx"); - // Base part - PackagePartName nameBase = PackagingURIHelper - .createPartName("/word/document.xml"); - PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML); - // Relative part name - PackagePartName relativeName = PackagingURIHelper.createPartName( - "media/image1.gif", partBase); - assertTrue("The part name must be equal to " - + partNameToValid.getName(), partNameToValid - .equals(relativeName)); - pkg.revert(); - } + OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx"); + // Base part + PackagePartName nameBase = PackagingURIHelper + .createPartName("/word/document.xml"); + PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML); + // Relative part name + PackagePartName relativeName = PackagingURIHelper.createPartName( + "media/image1.gif", partBase); + assertTrue("The part name must be equal to " + + partNameToValid.getName(), partNameToValid + .equals(relativeName)); + pkg.revert(); + } - /** - * Test createPartName(URI, y) - */ - public void testCreatePartNameRelativeURI() throws Exception { - PackagePartName partNameToValid = PackagingURIHelper - .createPartName("/word/media/image1.gif"); + /** + * Test createPartName(URI, y) + */ + public void testCreatePartNameRelativeURI() throws Exception { + PackagePartName partNameToValid = PackagingURIHelper + .createPartName("/word/media/image1.gif"); - OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx"); - // Base part - PackagePartName nameBase = PackagingURIHelper - .createPartName("/word/document.xml"); - PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML); - // Relative part name - PackagePartName relativeName = PackagingURIHelper.createPartName( - new URI("media/image1.gif"), partBase); - assertTrue("The part name must be equal to " - + partNameToValid.getName(), partNameToValid - .equals(relativeName)); - pkg.revert(); - } + OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx"); + // Base part + PackagePartName nameBase = PackagingURIHelper + .createPartName("/word/document.xml"); + PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML); + // Relative part name + PackagePartName relativeName = PackagingURIHelper.createPartName( + new URI("media/image1.gif"), partBase); + assertTrue("The part name must be equal to " + + partNameToValid.getName(), partNameToValid + .equals(relativeName)); + pkg.revert(); + } public void testCreateURIFromString() throws Exception { String[] href = { @@ -119,6 +119,7 @@ public class TestPackagingURIHelper extends TestCase { "file:///D:\\seva\\1981\\r810102ns.mp3", "..\\cygwin\\home\\yegor\\dinom\\%5baccess%5d.2010-10-26.log", "#'Instructions (Text)'!B21", + "#'性'!B21", "javascript://" }; for(String s : href){