From 6e933cb24220cf191c449b89e18abf76178f7486 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 19 Feb 2014 23:12:56 +0000 Subject: [PATCH] Begin to implement some of the tests and logic for content types with parameters, based on the test file for bug #55026 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1569965 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/openxml4j/opc/PackagePart.java | 9 ++- .../openxml4j/opc/internal/ContentType.java | 81 ++++++++++++++----- .../poi/openxml4j/opc/TestContentType.java | 75 ++++++++++++++++- 3 files changed, 138 insertions(+), 27 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java index e97349447..1420fd441 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java @@ -572,12 +572,19 @@ public abstract class PackagePart implements RelationshipSource { } /** - * @return the contentType + * @return The Content Type of the part */ public String getContentType() { return _contentType.toString(); } + /** + * @return The Content Type, including parameters, of the part + */ + public ContentType getContentTypeDetails() { + return _contentType; + } + /** * Set the content type. * diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentType.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentType.java index 4d4c9283c..fcb77bc97 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentType.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentType.java @@ -17,7 +17,6 @@ package org.apache.poi.openxml4j.opc.internal; -import java.io.UnsupportedEncodingException; import java.util.Hashtable; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -66,9 +65,13 @@ public final class ContentType { private Hashtable parameters; /** - * Media type compiled pattern for parameters. + * Media type compiled pattern, without parameters */ - private final static Pattern patternMediaType; + private final static Pattern patternTypeSubType; + /** + * Media type compiled pattern, with parameters. + */ + private final static Pattern patternTypeSubTypeParams; static { /* @@ -90,8 +93,7 @@ public final class ContentType { * * value = token | quoted-string */ - // Keep for future use with parameter: - // String parameter = "(" + token + "+)=(\"?" + token + "+\"?)"; + String parameter = "(" + token + "+)=(\"?" + token + "+\"?)"; /* * Pattern for media type. * @@ -118,11 +120,10 @@ public final class ContentType { * quoted-pair = "\" CHAR */ - // Keep for future use with parameter: - // patternMediaType = Pattern.compile("^(" + token + "+)/(" + token - // + "+)(;" + parameter + ")*$"); - patternMediaType = Pattern.compile("^(" + token + "+)/(" + token - + "+)$"); + patternTypeSubType = Pattern.compile("^(" + token + "+)/(" + + token + "+)$"); + patternTypeSubTypeParams = Pattern.compile("^(" + token + "+)/(" + + token + "+)(;" + parameter + ")+$"); } /** @@ -134,19 +135,27 @@ public final class ContentType { * If the specified content type is not valid with RFC 2616. */ public ContentType(String contentType) throws InvalidFormatException { - Matcher mMediaType = patternMediaType.matcher(contentType); - if (!mMediaType.matches()) + Matcher mMediaType = patternTypeSubType.matcher(contentType); + if (!mMediaType.matches()) { + // How about with parameters? + mMediaType = patternTypeSubTypeParams.matcher(contentType); + } + if (!mMediaType.matches()) { throw new InvalidFormatException( "The specified content type '" - + contentType - + "' is not compliant with RFC 2616: malformed content type."); + + contentType + + "' is not compliant with RFC 2616: malformed content type."); + } // Type/subtype if (mMediaType.groupCount() >= 2) { this.type = mMediaType.group(1); this.subType = mMediaType.group(2); + // Parameters this.parameters = new Hashtable(1); +//System.out.println(mMediaType.groupCount() + " = " + contentType); +//for (int j=1; j