Author: rdonkin
Date: Sat Jun 21 06:31:09 2008
New Revision: 670195

URL: http://svn.apache.org/viewvc?rev=670195&view=rev
Log:
Pushed trimming and unfolding logic into utility method. Factored out contents 
of long if statement.

Modified:
    
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/DefaultBodyDescriptor.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java

Modified: 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/DefaultBodyDescriptor.java
URL: 
http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/DefaultBodyDescriptor.java?rev=670195&r1=670194&r2=670195&view=diff
==============================================================================
--- 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/DefaultBodyDescriptor.java
 (original)
+++ 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/DefaultBodyDescriptor.java
 Sat Jun 21 06:31:09 2008
@@ -111,77 +111,67 @@
                 log.error("Invalid content-length: " + value);
             }
         } else if (name.equals("content-type") && !contentTypeSet) {
-            contentTypeSet = true;
-            
-            value = value.trim();
-            
-            /*
-             * Unfold Content-Type value
-             */
-            StringBuffer sb = new StringBuffer();
-            for (int i = 0; i < value.length(); i++) {
-                char c = value.charAt(i);
-                if (c == '\r' || c == '\n') {
-                    continue;
-                }
-                sb.append(c);
-            }
-            
-            Map params = MimeUtil.getHeaderParams(sb.toString());
-            
-            String main = (String) params.get("");
-            String type = null;
-            String subtype = null;
-            if (main != null) {
-                main = main.toLowerCase().trim();
-                int index = main.indexOf('/');
-                boolean valid = false;
-                if (index != -1) {
-                    type = main.substring(0, index).trim();
-                    subtype = main.substring(index + 1).trim();
-                    if (type.length() > 0 && subtype.length() > 0) {
-                        main = type + "/" + subtype;
-                        valid = true;
-                    }
-                }
-                
-                if (!valid) {
-                    main = null;
-                    type = null;
-                    subtype = null;
+            parseContentType(value);
+        }
+    }
+
+    private void parseContentType(String value) {
+        contentTypeSet = true;
+        
+        Map params = MimeUtil.getHeaderParams(value);
+        
+        String main = (String) params.get("");
+        String type = null;
+        String subtype = null;
+        if (main != null) {
+            main = main.toLowerCase().trim();
+            int index = main.indexOf('/');
+            boolean valid = false;
+            if (index != -1) {
+                type = main.substring(0, index).trim();
+                subtype = main.substring(index + 1).trim();
+                if (type.length() > 0 && subtype.length() > 0) {
+                    main = type + "/" + subtype;
+                    valid = true;
                 }
             }
-            String b = (String) params.get("boundary");
-            
-            if (main != null 
-                    && ((main.startsWith("multipart/") && b != null) 
-                            || !main.startsWith("multipart/"))) {
-                
-                mimeType = main;
-                this.subType = subtype;
-                this.mediaType = type;
-            }
             
-            if (MimeUtil.isMultipart(mimeType)) {
-                boundary = b;
+            if (!valid) {
+                main = null;
+                type = null;
+                subtype = null;
             }
-            
-            String c = (String) params.get("charset");
-            if (c != null) {
-                c = c.trim();
-                if (c.length() > 0) {
-                    charset = c.toLowerCase();
-                }
+        }
+        String b = (String) params.get("boundary");
+        
+        if (main != null 
+                && ((main.startsWith("multipart/") && b != null) 
+                        || !main.startsWith("multipart/"))) {
+            
+            mimeType = main;
+            this.subType = subtype;
+            this.mediaType = type;
+        }
+        
+        if (MimeUtil.isMultipart(mimeType)) {
+            boundary = b;
+        }
+        
+        String c = (String) params.get("charset");
+        if (c != null) {
+            c = c.trim();
+            if (c.length() > 0) {
+                charset = c.toLowerCase();
             }
-            
-            /*
-             * Add all other parameters to parameters.
-             */
-            parameters.putAll(params);
-            parameters.remove("");
-            parameters.remove("boundary");
-            parameters.remove("charset");
         }
+        
+        /*
+         * Add all other parameters to parameters.
+         */
+        parameters.putAll(params);
+        parameters.remove("");
+        parameters.remove("boundary");
+        parameters.remove("charset");
     }
 
     /**

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=670195&r1=670194&r2=670195&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 21 06:31:09 2008
@@ -102,17 +102,35 @@
     }
 
     /**
-     * Parses a complex field value into a map of key/value pairs. You may
+     * <p>Parses a complex field value into a map of key/value pairs. You may
      * use this, for example, to parse a definition like
      * <pre>
      *   text/plain; charset=UTF-8; boundary=foobar
      * </pre>
      * The above example would return a map with the keys "", "charset",
      * and "boundary", and the values "text/plain", "UTF-8", and "foobar".
+     * </p><p>
+     * Header value will be unfolded and excess white space trimmed.
+     * </p>
      * @param pValue The field value to parse.
      * @return The result map; use the key "" to retrieve the first value.
      */
     public static Map getHeaderParams(String pValue) {
+        pValue = pValue.trim();
+        
+        /*
+         * Unfold Content-Type value
+         */
+        StringBuffer sb = new StringBuffer();
+        for (int i = 0; i < pValue.length(); i++) {
+            char c = pValue.charAt(i);
+            if (c == '\r' || c == '\n') {
+                continue;
+            }
+            sb.append(c);
+        }
+        pValue = sb.toString();
+        
         Map result = new HashMap();
 
         // split main value and parameters



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to