james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r737442 - in /james/mime4j/trunk/src: main/java/org/apache/james/mime4j/field/address/Group.java main/java/org/apache/james/mime4j/field/address/Mailbox.java test/java/org/apache/james/mime4j/field/address/AddressTest.java
Date Sat, 24 Jan 2009 21:51:22 GMT
Author: mwiederkehr
Date: Sat Jan 24 21:51:21 2009
New Revision: 737442

URL: http://svn.apache.org/viewvc?rev=737442&view=rev
Log:
MIME4J-107: added Group.parse(String) and Mailbox.parse(String)

Modified:
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Group.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Mailbox.java
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/address/AddressTest.java

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Group.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Group.java?rev=737442&r1=737441&r2=737442&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Group.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Group.java Sat
Jan 24 21:51:21 2009
@@ -22,6 +22,7 @@
 import java.util.List;
 
 import org.apache.james.mime4j.codec.EncoderUtil;
+import org.apache.james.mime4j.field.address.parser.ParseException;
 
 /**
  * A named group of zero or more mailboxes.
@@ -50,6 +51,29 @@
     }
 
     /**
+     * Parses the specified raw string into a group address.
+     * 
+     * @param rawGroupString
+     *            string to parse.
+     * @return a <code>Group</code> object for the specified string.
+     * @throws IllegalArgumentException
+     *             if the raw string does not represent a single group address.
+     */
+    public static Group parse(String rawGroupString) {
+        try {
+            AddressList addressList = AddressList.parse(rawGroupString);
+            if (addressList.size() != 1)
+                throw new IllegalArgumentException("Not a single address");
+            Address address = addressList.get(0);
+            if (!(address instanceof Group))
+                throw new IllegalArgumentException("Not a group address");
+            return (Group) address;
+        } catch (ParseException e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
+
+    /**
      * Returns the group name.
      */
     public String getName() {

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Mailbox.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Mailbox.java?rev=737442&r1=737441&r2=737442&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Mailbox.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Mailbox.java Sat
Jan 24 21:51:21 2009
@@ -24,6 +24,7 @@
 import java.util.Locale;
 
 import org.apache.james.mime4j.codec.EncoderUtil;
+import org.apache.james.mime4j.field.address.parser.ParseException;
 
 /**
  * Represents a single e-mail address.
@@ -115,6 +116,30 @@
     }
 
     /**
+     * Parses the specified raw string into a mailbox address.
+     * 
+     * @param rawMailboxString
+     *            string to parse.
+     * @return a <code>Mailbox</code> object for the specified string.
+     * @throws IllegalArgumentException
+     *             if the raw string does not represent a single mailbox
+     *             address.
+     */
+    public static Mailbox parse(String rawMailboxString) {
+        try {
+            AddressList addressList = AddressList.parse(rawMailboxString);
+            if (addressList.size() != 1)
+                throw new IllegalArgumentException("Not a single address");
+            Address address = addressList.get(0);
+            if (!(address instanceof Mailbox))
+                throw new IllegalArgumentException("Not a mailbox address");
+            return (Mailbox) address;
+        } catch (ParseException e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
+
+    /**
      * Returns the name of the mailbox or <code>null</code> if it does not
      * have a name.
      */

Modified: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/address/AddressTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/address/AddressTest.java?rev=737442&r1=737441&r2=737442&view=diff
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/address/AddressTest.java
(original)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/address/AddressTest.java
Sat Jan 24 21:51:21 2009
@@ -404,4 +404,37 @@
         assertEquals("Undisclosed recipients:;", g.getEncodedString());
     }
     
+    public void testParseGroup() throws Exception {
+        Group group = Group
+                .parse("group: john.doe@acme.org, Mary Smith <mary@example.net>;");
+        assertEquals("group", group.getName());
+
+        MailboxList mailboxes = group.getMailboxes();
+        assertEquals(2, mailboxes.size());
+
+        Mailbox mailbox1 = mailboxes.get(0);
+        assertNull(mailbox1.getName());
+        assertEquals("john.doe@acme.org", mailbox1.getAddress());
+
+        Mailbox mailbox2 = mailboxes.get(1);
+        assertEquals("Mary Smith", mailbox2.getName());
+        assertEquals("mary@example.net", mailbox2.getAddress());
+    }
+
+    public void testParseMailbox() throws Exception {
+        Mailbox mailbox1 = Mailbox.parse("john.doe@acme.org");
+        assertNull(mailbox1.getName());
+        assertEquals("john.doe@acme.org", mailbox1.getAddress());
+
+        Mailbox mailbox2 = Mailbox.parse("Mary Smith <mary@example.net>");
+        assertEquals("Mary Smith", mailbox2.getName());
+        assertEquals("mary@example.net", mailbox2.getAddress());
+
+        // non-ascii should be allowed in quoted strings
+        Mailbox mailbox3 = Mailbox
+                .parse("\"Hans M\374ller\" <hans.mueller@acme.org>");
+        assertEquals("Hans M\374ller", mailbox3.getName());
+        assertEquals("hans.mueller@acme.org", mailbox3.getAddress());
+    }
+    
 }



---------------------------------------------------------------------
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