james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject svn commit: r667891 - in /james/mime4j/trunk/src: main/java/org/apache/james/mime4j/ main/java/org/apache/james/mime4j/util/ main/javacc/org/apache/james/mime4j/field/contenttype/ main/javacc/org/apache/james/mime4j/field/mimeversion/ test/java/org/apa...
Date Sat, 14 Jun 2008 22:08:41 GMT
Author: rdonkin
Date: Sat Jun 14 15:08:41 2008
New Revision: 667891

URL: http://svn.apache.org/viewvc?rev=667891&view=rev
Log:
Parse for RFC 2045 mime-version field values

Added:
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/MaximalBodyDescriptor.java
    james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/mimeversion/
    james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/mimeversion/MimeVersionParser.jj
    james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/mimeversion/ParseException.java
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/MaximalBodyDescriptorTest.java
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/mimeversion/
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/mimeversion/MimeVersionParserTest.java
Modified:
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/MimeTokenStream.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java
    james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/contenttype/ContentTypeParser.jj
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/ExampleMail.java

Added: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/MaximalBodyDescriptor.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/MaximalBodyDescriptor.java?rev=667891&view=auto
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/MaximalBodyDescriptor.java (added)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/MaximalBodyDescriptor.java Sat
Jun 14 15:08:41 2008
@@ -0,0 +1,106 @@
+/*
+ * 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.james.mime4j;
+
+import java.io.StringReader;
+
+import org.apache.james.mime4j.field.mimeversion.MimeVersionParser;
+import org.apache.james.mime4j.util.MimeUtil;
+
+
+/**
+ * Parses and stores values for standard MIME header values.
+ * 
+ */
+public class MaximalBodyDescriptor extends DefaultBodyDescriptor {
+
+    private static final int DEFAULT_MINOR_VERSION = 0;
+    private static final int DEFAULT_MAJOR_VERSION = 1;
+    private boolean mimeVersionSet;
+    private int mimeMinorVersion;
+    private int mimeMajorVersion;
+    private MimeException mimeVersionException;
+    
+    protected MaximalBodyDescriptor() {
+        this(null);
+    }
+
+    protected MaximalBodyDescriptor(BodyDescriptor parent) {
+        super(parent);
+        mimeVersionSet = false;
+        mimeMajorVersion = DEFAULT_MAJOR_VERSION;
+        mimeMinorVersion = DEFAULT_MINOR_VERSION;
+    }
+    
+    
+    
+    public void addField(String name, String value) {
+        name = name.trim().toLowerCase();
+        if (MimeUtil.MIME_HEADER_MIME_VERSION.equals(name) && !mimeVersionSet) {
+            parseMimeVersion(value);
+        } else {
+            super.addField(name, value);
+        }
+    }
+
+    private void parseMimeVersion(String value) {
+        final StringReader reader = new StringReader(value);
+        final MimeVersionParser parser = new MimeVersionParser(reader);
+        try {
+            parser.parse();
+            final int major = parser.getMajorVersion();
+            if (major != MimeVersionParser.INITIAL_VERSION_VALUE) {
+                mimeMajorVersion = major;
+            }
+            final int minor = parser.getMinorVersion();
+            if (minor != MimeVersionParser.INITIAL_VERSION_VALUE) {
+                mimeMinorVersion = minor;
+            }
+        } catch (MimeException e) {
+            this.mimeVersionException = e;
+        }
+        mimeVersionSet = true;
+    }
+    
+    /**
+     * Gets the MIME major version
+     * as specified by the <code>MIME-Version</code>
+     * header.
+     * Defaults to one.
+     * @return positive integer
+     */
+    public int getMimeMajorVersion() {
+        return mimeMajorVersion;
+    }
+    
+    /**
+     * Gets the MIME minor version
+     * as specified by the <code>MIME-Version</code>
+     * header. 
+     * Defaults to zero.
+     * @return positive integer
+     */
+    public int getMimeMinorVersion() {
+        return mimeMinorVersion;
+    }
+    
+    public MimeException getMimeVersionParseException() {
+        return mimeVersionException;
+    }
+} 

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/MimeTokenStream.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/MimeTokenStream.java?rev=667891&r1=667890&r2=667891&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/MimeTokenStream.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/MimeTokenStream.java Sat Jun
14 15:08:41 2008
@@ -254,13 +254,21 @@
     }
     
     /**
+     * Creates a stream that creates a more detailed body descriptor.
+     * @return <code>MimeTokenStream</code>, not null
+     */
+    public static final MimeTokenStream createMaximalDescriptorStream() {
+        return new MimeTokenStream(false, true);
+    }
+    
+    /**
      * Creates a stream that strictly validates the input.
      * @return <code>MimeTokenStream</code> which throws a 
      * <code>MimeException</code> whenever possible issues 
      * are dedicated in the input
      */
     public static final MimeTokenStream createStrictValidationStream() {
-        return new MimeTokenStream(true);
+        return new MimeTokenStream(true, false);
     }
     
     /**
@@ -536,6 +544,7 @@
     }
     
     private final boolean strictParsing;
+    private final boolean maximalBodyDescriptor;
     private int state = T_END_OF_STREAM;
     private Cursor cursor;
     private StateMachine currentStateMachine;
@@ -550,11 +559,12 @@
      * a stream that strictly validates the input.
      */
     public MimeTokenStream() {
-        this(false);
+        this(false, false);
     }
     
-    private MimeTokenStream(final boolean strictParsing) {
+    protected MimeTokenStream(final boolean strictParsing, final boolean maximalBodyDescriptor)
{
         this.strictParsing = strictParsing;
+        this.maximalBodyDescriptor = maximalBodyDescriptor;
     }
     
     /** Instructs the {@code MimeTokenStream} to parse the given streams contents.
@@ -877,6 +887,12 @@
      * information.
      */
     protected BodyDescriptor newBodyDescriptor(BodyDescriptor pParent) {
-        return new DefaultBodyDescriptor(pParent);
+        final BodyDescriptor result;
+        if (maximalBodyDescriptor) {
+            result = new MaximalBodyDescriptor(pParent);
+        } else {
+            result = new DefaultBodyDescriptor(pParent);
+        }
+        return result;
     }
 }

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java?rev=667891&r1=667890&r2=667891&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java Sat Jun 14
15:08:41 2008
@@ -32,6 +32,7 @@
  */
 public final class MimeUtil {
     private static final Log log = LogFactory.getLog(MimeUtil.class);
+    
     /**
      * The <code>quoted-printable</code> encoding.
      */
@@ -53,6 +54,9 @@
      */
     public static final String ENC_7BIT = "7bit";
 
+    /** <code>MIME-Version</code> header name (lowercase) */
+    public static final String MIME_HEADER_MIME_VERSION = "mime-version";
+    
     private MimeUtil() {
         // this is an utility class to be used statically.
         // this constructor protect from instantiation.

Modified: james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/contenttype/ContentTypeParser.jj
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/contenttype/ContentTypeParser.jj?rev=667891&r1=667890&r2=667891&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/contenttype/ContentTypeParser.jj
(original)
+++ james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/contenttype/ContentTypeParser.jj
Sat Jun 14 15:08:41 2008
@@ -28,6 +28,7 @@
 options {
 	STATIC=false;
 	LOOKAHEAD=1;
+	JDK_VERSION = "1.4";
 	//DEBUG_PARSER=true;
 	//DEBUG_TOKEN_MANAGER=true;
 }

Added: james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/mimeversion/MimeVersionParser.jj
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/mimeversion/MimeVersionParser.jj?rev=667891&view=auto
==============================================================================
--- james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/mimeversion/MimeVersionParser.jj
(added)
+++ james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/mimeversion/MimeVersionParser.jj
Sat Jun 14 15:08:41 2008
@@ -0,0 +1,176 @@
+/****************************************************************
+ * 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.                                           *
+ ****************************************************************/
+
+options {
+  static=false;
+  JDK_VERSION = "1.4";
+  OUTPUT_DIRECTORY = "target/generated-sources/javacc";
+}
+
+PARSER_BEGIN(MimeVersionParser)
+/****************************************************************
+ * 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.james.mime4j.field.mimeversion;
+
+public class MimeVersionParser {
+	public static final int INITIAL_VERSION_VALUE = -1;
+	private int major=INITIAL_VERSION_VALUE;
+	private int minor=INITIAL_VERSION_VALUE;
+	
+	public int getMinorVersion() {
+		return minor;
+	}
+	
+	public int getMajorVersion() {
+		return major;
+	}
+}
+PARSER_END(MimeVersionParser)
+
+
+void parseLine() :
+{}
+{
+	parse() ["\r"] "\n"
+}
+
+void parseAll() :
+{}
+{
+	parse() <EOF>
+}
+
+void parse() :
+{
+	Token major;
+	Token minor;
+}
+{
+	major=<DIGITS> <DOT> minor=<DIGITS>
+	{
+		try {
+			this.major = Integer.parseInt(major.image);
+			this.minor = Integer.parseInt(minor.image);
+		} catch (NumberFormatException e) {
+			throw new ParseException(e.getMessage());
+		}
+	}
+}
+
+SPECIAL_TOKEN :
+{
+ 	< WS: ( [" ", "\t", "\r", "\n"] )+ >
+}
+
+TOKEN_MGR_DECLS :
+{
+	// Keeps track of how many levels of comment nesting
+	// we've encountered.  This is only used when the 2nd
+	// level is reached, for example ((this)), not (this).
+	// This is because the outermost level must be treated
+	// specially anyway, because the outermost ")" has a
+	// different token type than inner ")" instances.
+	int commentNest;
+}
+
+
+MORE :
+{
+	// starts a comment
+	"(" : INCOMMENT
+}
+
+<INCOMMENT>
+SKIP :
+{
+	// ends a comment
+	< COMMENT: ")" > : DEFAULT
+	// if this is ever changed to not be a SKIP, need
+	// to make sure matchedToken.token = token.toString()
+	// is called.
+}
+
+<INCOMMENT>
+MORE :
+{
+	< <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
+|	"(" { commentNest = 1; } : NESTED_COMMENT
+|	< <ANY>>
+}
+
+<NESTED_COMMENT>
+MORE :
+{
+	< <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
+|	"(" { ++commentNest; }
+|	")" { --commentNest; if (commentNest == 0) SwitchTo(INCOMMENT); }
+|	< <ANY>>
+}
+// QUOTED STRINGS
+
+MORE :
+{
+	"\"" { image.deleteCharAt(image.length() - 1); } : INQUOTEDSTRING
+}
+
+<INQUOTEDSTRING>
+MORE :
+{
+	< <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
+|	< (~["\"", "\\"])+ >
+}
+
+<INQUOTEDSTRING>
+TOKEN :
+{
+	< QUOTEDSTRING: "\"" > { matchedToken.image = image.substring(0, image.length() -
1); } : DEFAULT
+}
+
+TOKEN :
+{
+	< DIGITS: ( ["0"-"9"] )+ >
+}
+
+TOKEN :
+{
+	< DOT: "." >
+}
+
+<*>
+TOKEN :
+{
+	< #QUOTEDPAIR: "\\" <ANY> >
+|	< #ANY: ~[] >
+}
\ No newline at end of file

Added: james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/mimeversion/ParseException.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/mimeversion/ParseException.java?rev=667891&view=auto
==============================================================================
--- james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/mimeversion/ParseException.java
(added)
+++ james/mime4j/trunk/src/main/javacc/org/apache/james/mime4j/field/mimeversion/ParseException.java
Sat Jun 14 15:08:41 2008
@@ -0,0 +1,212 @@
+/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
+/****************************************************************
+ * 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.james.mime4j.field.mimeversion;
+
+import org.apache.james.mime4j.MimeException;
+
+/**
+ * This exception is thrown when parse errors are encountered.
+ * You can explicitly create objects of this exception type by
+ * calling the method generateParseException in the generated
+ * parser.
+ *
+ * This class was modified to extend MimeException
+ * and to add a seriaVersionUID.
+ */
+public class ParseException extends MimeException {
+
+  /**
+   * This constructor is used by the method "generateParseException"
+   * in the generated parser.  Calling this constructor generates
+   * a new object of this type with the fields "currentToken",
+   * "expectedTokenSequences", and "tokenImage" set.  The boolean
+   * flag "specialConstructor" is also set to true to indicate that
+   * this constructor was used to create this object.
+   * This constructor calls its super class with the empty string
+   * to force the "toString" method of parent class "Throwable" to
+   * print the error message in the form:
+   *     ParseException: <result of getMessage>
+   */
+  public ParseException(Token currentTokenVal,
+                        int[][] expectedTokenSequencesVal,
+                        String[] tokenImageVal
+                       )
+  {
+    super("Cannot parse field");
+    specialConstructor = true;
+    currentToken = currentTokenVal;
+    expectedTokenSequences = expectedTokenSequencesVal;
+    tokenImage = tokenImageVal;
+  }
+
+  /**
+   * The following constructors are for use by you for whatever
+   * purpose you can think of.  Constructing the exception in this
+   * manner makes the exception behave in the normal way - i.e., as
+   * documented in the class "Throwable".  The fields "errorToken",
+   * "expectedTokenSequences", and "tokenImage" do not contain
+   * relevant information.  The JavaCC generated code does not use
+   * these constructors.
+   */
+
+  public ParseException() {
+    super("Cannot parse field");
+    specialConstructor = false;
+  }
+
+  public ParseException(String message) {
+    super(message);
+    specialConstructor = false;
+  }
+
+  /**
+   * This variable determines which constructor was used to create
+   * this object and thereby affects the semantics of the
+   * "getMessage" method (see below).
+   */
+  protected boolean specialConstructor;
+
+  /**
+   * This is the last token that has been consumed successfully.  If
+   * this object has been created due to a parse error, the token
+   * followng this token will (therefore) be the first error token.
+   */
+  public Token currentToken;
+
+  /**
+   * Each entry in this array is an array of integers.  Each array
+   * of integers represents a sequence of tokens (by their ordinal
+   * values) that is expected at this point of the parse.
+   */
+  public int[][] expectedTokenSequences;
+
+  /**
+   * This is a reference to the "tokenImage" array of the generated
+   * parser within which the parse error occurred.  This array is
+   * defined in the generated ...Constants interface.
+   */
+  public String[] tokenImage;
+
+  /**
+   * This method has the standard behavior when this object has been
+   * created using the standard constructors.  Otherwise, it uses
+   * "currentToken" and "expectedTokenSequences" to generate a parse
+   * error message and returns it.  If this object has been created
+   * due to a parse error, and you do not catch it (it gets thrown
+   * from the parser), then this method is called during the printing
+   * of the final stack trace, and hence the correct error message
+   * gets displayed.
+   */
+  public String getMessage() {
+    if (!specialConstructor) {
+      return super.getMessage();
+    }
+    StringBuffer expected = new StringBuffer();
+    int maxSize = 0;
+    for (int i = 0; i < expectedTokenSequences.length; i++) {
+      if (maxSize < expectedTokenSequences[i].length) {
+        maxSize = expectedTokenSequences[i].length;
+      }
+      for (int j = 0; j < expectedTokenSequences[i].length; j++) {
+        expected.append(tokenImage[expectedTokenSequences[i][j]]).append(" ");
+      }
+      if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
+        expected.append("...");
+      }
+      expected.append(eol).append("    ");
+    }
+    String retval = "Encountered \"";
+    Token tok = currentToken.next;
+    for (int i = 0; i < maxSize; i++) {
+      if (i != 0) retval += " ";
+      if (tok.kind == 0) {
+        retval += tokenImage[0];
+        break;
+      }
+      retval += add_escapes(tok.image);
+      tok = tok.next; 
+    }
+    retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
+    retval += "." + eol;
+    if (expectedTokenSequences.length == 1) {
+      retval += "Was expecting:" + eol + "    ";
+    } else {
+      retval += "Was expecting one of:" + eol + "    ";
+    }
+    retval += expected.toString();
+    return retval;
+  }
+
+  /**
+   * The end of line string for this machine.
+   */
+  protected String eol = System.getProperty("line.separator", "\n");
+ 
+  /**
+   * Used to convert raw characters to their escaped version
+   * when these raw version cannot be used as part of an ASCII
+   * string literal.
+   */
+  protected String add_escapes(String str) {
+      StringBuffer retval = new StringBuffer();
+      char ch;
+      for (int i = 0; i < str.length(); i++) {
+        switch (str.charAt(i))
+        {
+           case 0 :
+              continue;
+           case '\b':
+              retval.append("\\b");
+              continue;
+           case '\t':
+              retval.append("\\t");
+              continue;
+           case '\n':
+              retval.append("\\n");
+              continue;
+           case '\f':
+              retval.append("\\f");
+              continue;
+           case '\r':
+              retval.append("\\r");
+              continue;
+           case '\"':
+              retval.append("\\\"");
+              continue;
+           case '\'':
+              retval.append("\\\'");
+              continue;
+           case '\\':
+              retval.append("\\\\");
+              continue;
+           default:
+              if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
+                 String s = "0000" + Integer.toString(ch, 16);
+                 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
+              } else {
+                 retval.append(ch);
+              }
+              continue;
+        }
+      }
+      return retval.toString();
+   }
+
+}

Modified: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/ExampleMail.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/ExampleMail.java?rev=667891&r1=667890&r2=667891&view=diff
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/ExampleMail.java (original)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/ExampleMail.java Sat Jun 14 15:08:41
2008
@@ -220,6 +220,34 @@
     "\r\n" +
     ONE_PART_MIME_ASCII_BODY;
     
+    public static final String ONE_PART_MIME_ASCII_COMMENT_IN_MIME_VERSION = "Received: by
10.114.126.16 with HTTP; Thu, 6 Mar 2008 10:02:03 -0800 (PST)\r\n" +
+    "Message-ID: <f470f68e0803061002n22bc4124he14015a4b6d6327f@mail.gmail.com>\r\n"
+
+    "Date: Thu, 6 Mar 2008 18:02:03 +0000\r\n" +
+    "From: \"Robert Burrell Donkin\" <robertburrelldonkin@gmail.com>\r\n" +
+    "To: \"James Developers List\" <server-dev@james.apache.org>\r\n" +
+    "Subject: [Mime4J] getReader\r\n" +
+    "MIME-Version: 2.(This is a comment)4\r\n" +
+    "Content-Type: text/plain; charset=US-ASCII\r\n" +
+    "Content-Transfer-Encoding: 7bit\r\n" +
+    "Content-Disposition: inline\r\n" +
+    "Delivered-To: robertburrelldonkin@gmail.com\r\n" +
+    "\r\n" + ONE_PART_MIME_ASCII_BODY;
+    
+    public static final String ONE_PART_MIME_ASCII_MIME_VERSION_SPANS_TWO_LINES = "Received:
by 10.114.126.16 with HTTP; Thu, 6 Mar 2008 10:02:03 -0800 (PST)\r\n" +
+    "Message-ID: <f470f68e0803061002n22bc4124he14015a4b6d6327f@mail.gmail.com>\r\n"
+
+    "Date: Thu, 6 Mar 2008 18:02:03 +0000\r\n" +
+    "From: \"Robert Burrell Donkin\" <robertburrelldonkin@gmail.com>\r\n" +
+    "To: \"James Developers List\" <server-dev@james.apache.org>\r\n" +
+    "Subject: [Mime4J] getReader\r\n" +
+    "MIME-Version: 4.   \r\n" +
+    "  1\r\n" +
+    "Content-Type: text/plain; charset=US-ASCII\r\n" +
+    "Content-Transfer-Encoding: 7bit\r\n" +
+    "Content-Disposition: inline\r\n" +
+    "Delivered-To: robertburrelldonkin@gmail.com\r\n" +
+    "\r\n" + ONE_PART_MIME_ASCII_BODY;
+    
+    
     public static final String ONE_PART_MIME_8859 = "Received: by 10.114.126.16 with HTTP;
Thu, 6 Mar 2008 10:02:03 -0800 (PST)\r\n" +
     "Message-ID: <f470f68e0803061002n22bc4124he14015a4b6d6327f@mail.gmail.com>\r\n"
+
     "Date: Thu, 6 Mar 2008 18:02:03 +0000\r\n" +
@@ -380,6 +408,7 @@
         "\r\n--1729--\r\n" +
         "This is the epilogue\r\n";
     
+    
     private static final byte[][] MIME_MIXED_MULTIPART_VARIOUS_ENCODINGS_BYTE_ARRAYS = {
         ascii(MIME_MIXED_MULTIPART_VARIOUS_ENCODINGS_ONE),
         ascii(MIME_MIXED_MULTIPART_VARIOUS_ENCODINGS_7BIT),
@@ -398,7 +427,10 @@
     public static final byte[] ONE_PART_MIME_ASCII_BYTES = US_ASCII.encode(ONE_PART_MIME_ASCII).array();
     public static final byte[] ONE_PART_MIME_8859_BYTES = LATIN1.encode(ONE_PART_MIME_8859).array();
     public static final byte[] MULTIPART_WITH_BINARY_ATTACHMENTS_BYTES = US_ASCII.encode(MULTIPART_WITH_BINARY_ATTACHMENTS).array();
-     
+    public static final byte[] ONE_PART_MIME_ASCII_COMMENT_IN_MIME_VERSION_BYTES = US_ASCII.encode(ONE_PART_MIME_ASCII_COMMENT_IN_MIME_VERSION).array();
+    public static final byte[] ONE_PART_MIME_ASCII_MIME_VERSION_SPANS_TWO_LINES_BYTES = US_ASCII.encode(ONE_PART_MIME_ASCII_MIME_VERSION_SPANS_TWO_LINES).array();
+    
+
     public static final byte[] ascii(String text) {
         
         return US_ASCII.encode(text).array();

Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/MaximalBodyDescriptorTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/MaximalBodyDescriptorTest.java?rev=667891&view=auto
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/MaximalBodyDescriptorTest.java
(added)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/MaximalBodyDescriptorTest.java
Sat Jun 14 15:08:41 2008
@@ -0,0 +1,74 @@
+/*
+ * 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.james.mime4j;
+
+import java.io.ByteArrayInputStream;
+
+import junit.framework.TestCase;
+
+public class MaximalBodyDescriptorTest extends TestCase {
+
+    MimeTokenStream parser;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        parser = MimeTokenStream.createMaximalDescriptorStream();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    public void testMimeVersionDefault() throws Exception {
+        MaximalBodyDescriptor descriptor = describe(ExampleMail.RFC822_SIMPLE_BYTES);
+        assertEquals(1, descriptor.getMimeMajorVersion());
+        assertEquals(0, descriptor.getMimeMinorVersion());
+        assertNull(descriptor.getMimeVersionParseException());
+    }
+    
+    public void testMimeVersion() throws Exception {
+        MaximalBodyDescriptor descriptor = describe(ExampleMail.ONE_PART_MIME_ASCII_COMMENT_IN_MIME_VERSION_BYTES);
+        assertEquals(2, descriptor.getMimeMajorVersion());
+        assertEquals(4, descriptor.getMimeMinorVersion());
+        assertNull(descriptor.getMimeVersionParseException());
+    }
+    
+    
+    public void testMimeVersionHeaderBreak() throws Exception {
+        MaximalBodyDescriptor descriptor = describe(ExampleMail.ONE_PART_MIME_ASCII_MIME_VERSION_SPANS_TWO_LINES_BYTES);
+        assertEquals(4, descriptor.getMimeMajorVersion());
+        assertEquals(1, descriptor.getMimeMinorVersion());
+        assertNull(descriptor.getMimeVersionParseException());
+    }
+    
+    private MaximalBodyDescriptor describe(byte[] mail) throws Exception {
+        ByteArrayInputStream bias = new ByteArrayInputStream(mail);
+        parser.parse(bias);
+        int state = parser.next();
+        while (state != MimeTokenStream.T_BODY && state != MimeTokenStream.T_END_OF_STREAM)

+        {
+            state = parser.next();
+        }
+        assertEquals(MimeTokenStream.T_BODY, state);
+        BodyDescriptor descriptor = parser.getBodyDescriptor();
+        assertNotNull(descriptor);
+        assertTrue("Parser is maximal so body descriptor should be maximal", descriptor instanceof
MaximalBodyDescriptor);
+        return (MaximalBodyDescriptor) descriptor;
+    }
+}

Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/mimeversion/MimeVersionParserTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/mimeversion/MimeVersionParserTest.java?rev=667891&view=auto
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/mimeversion/MimeVersionParserTest.java
(added)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/mimeversion/MimeVersionParserTest.java
Sat Jun 14 15:08:41 2008
@@ -0,0 +1,75 @@
+/****************************************************************
+ * 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.james.mime4j.field.mimeversion;
+
+import java.io.StringReader;
+
+import junit.framework.TestCase;
+
+public class MimeVersionParserTest extends TestCase {
+
+    protected void setUp() throws Exception {
+        super.setUp();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    public void testPlainLine() throws Exception {
+        check("2.4", 2, 4);
+        check("25.344", 25, 344);
+        check("0.1", 0, 1);
+        check("123234234.0", 123234234, 0);
+    }
+    
+    public void testLineWithComments() throws Exception {
+        check("2(A comment).4", 2, 4);
+        check("2(.8).4", 2, 4);
+        check("(A comment)2.4", 2, 4);
+        check("2.4(A comment)", 2, 4);
+        check("2.(A comment)4", 2, 4);
+    }
+    
+    public void testLineWithNestedComments() throws Exception {
+        check("2(4.45 ( Another ()comment () blah (Wobble(mix)))Whatever).4", 2, 4);
+    }
+    
+    public void testEmptyLine() throws Exception {
+        try {
+            parse("(This is just a comment)");
+            fail("Expected exception to be thrown");
+        } catch (ParseException e) {
+            //expected
+        }
+    }
+    
+    private void check(String input, int expectedMajorVersion, int expectedMinorVersion)
throws Exception {
+        MimeVersionParser parser = parse(input);
+        assertEquals("Major version number", expectedMajorVersion, parser.getMajorVersion());
+        assertEquals("Minor version number", expectedMinorVersion, parser.getMinorVersion());
+    }
+
+    private MimeVersionParser parse(String input) throws ParseException {
+        MimeVersionParser parser = new MimeVersionParser(new StringReader(input));
+        parser.parseAll();
+        return parser;
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message