james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r740733 - in /james/mime4j/trunk/src: main/java/org/apache/james/mime4j/field/address/ main/jjtree/org/apache/james/mime4j/field/address/ test/java/org/apache/james/mime4j/field/address/
Date Wed, 04 Feb 2009 13:32:13 GMT
Author: mwiederkehr
Date: Wed Feb  4 13:32:12 2009
New Revision: 740733

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

Modified:
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Address.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/AddressList.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Builder.java
    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/main/jjtree/org/apache/james/mime4j/field/address/AddressListParser.jjt
    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/Address.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Address.java?rev=740733&r1=740732&r2=740733&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Address.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Address.java Wed
Feb  4 13:32:12 2009
@@ -20,8 +20,12 @@
 package org.apache.james.mime4j.field.address;
 
 import java.io.Serializable;
+import java.io.StringReader;
 import java.util.List;
 
+import org.apache.james.mime4j.field.address.parser.AddressListParser;
+import org.apache.james.mime4j.field.address.parser.ParseException;
+
 /**
  * The abstract base for classes that represent RFC2822 addresses. This includes
  * groups and mailboxes.
@@ -91,6 +95,25 @@
      */
     public abstract String getEncodedString();
 
+    /**
+     * Parses the specified raw string into an address.
+     * 
+     * @param rawAddressString
+     *            string to parse.
+     * @return an <code>Address</code> object for the specified string.
+     * @throws IllegalArgumentException
+     *             if the raw string does not represent a single address.
+     */
+    public static Address parse(String rawAddressString) {
+        AddressListParser parser = new AddressListParser(new StringReader(
+                rawAddressString));
+        try {
+            return Builder.getInstance().buildAddress(parser.parseAddress());
+        } catch (ParseException e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
+
     @Override
     public String toString() {
         return getDisplayString(false);

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/AddressList.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/AddressList.java?rev=740733&r1=740732&r2=740733&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/AddressList.java
(original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/AddressList.java
Wed Feb  4 13:32:12 2009
@@ -119,7 +119,7 @@
             throws ParseException {
         AddressListParser parser = new AddressListParser(new StringReader(
                 rawAddressList));
-        return Builder.getInstance().buildAddressList(parser.parse());
+        return Builder.getInstance().buildAddressList(parser.parseAddressList());
     }
 
     /**

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Builder.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Builder.java?rev=740733&r1=740732&r2=740733&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Builder.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Builder.java Wed
Feb  4 13:32:12 2009
@@ -61,7 +61,7 @@
         return new AddressList(list, true);
     }
 
-    private Address buildAddress(ASTaddress node) {
+    public Address buildAddress(ASTaddress node) {
         ChildNodeIterator it = new ChildNodeIterator(node);
         Node n = it.next();
         if (n instanceof ASTaddr_spec) {
@@ -97,7 +97,7 @@
         return new MailboxList(results, true);
     }
 
-    private Mailbox buildMailbox(ASTmailbox node) {
+    public Mailbox buildMailbox(ASTmailbox node) {
         ChildNodeIterator it = new ChildNodeIterator(node);
         Node n = it.next();
         if (n instanceof ASTaddr_spec) {

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=740733&r1=740732&r2=740733&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 Wed
Feb  4 13:32:12 2009
@@ -25,7 +25,6 @@
 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.
@@ -83,17 +82,11 @@
      *             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);
-        }
+        Address address = Address.parse(rawGroupString);
+        if (!(address instanceof Group))
+            throw new IllegalArgumentException("Not a group address");
+
+        return (Group) address;
     }
 
     /**

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=740733&r1=740732&r2=740733&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 Wed
Feb  4 13:32:12 2009
@@ -19,11 +19,13 @@
 
 package org.apache.james.mime4j.field.address;
 
+import java.io.StringReader;
 import java.util.Collections;
 import java.util.List;
 import java.util.Locale;
 
 import org.apache.james.mime4j.codec.EncoderUtil;
+import org.apache.james.mime4j.field.address.parser.AddressListParser;
 import org.apache.james.mime4j.field.address.parser.ParseException;
 
 /**
@@ -126,14 +128,10 @@
      *             address.
      */
     public static Mailbox parse(String rawMailboxString) {
+        AddressListParser parser = new AddressListParser(new StringReader(
+                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;
+            return Builder.getInstance().buildMailbox(parser.parseMailbox());
         } catch (ParseException e) {
             throw new IllegalArgumentException(e);
         }

Modified: james/mime4j/trunk/src/main/jjtree/org/apache/james/mime4j/field/address/AddressListParser.jjt
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/jjtree/org/apache/james/mime4j/field/address/AddressListParser.jjt?rev=740733&r1=740732&r2=740733&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/jjtree/org/apache/james/mime4j/field/address/AddressListParser.jjt
(original)
+++ james/mime4j/trunk/src/main/jjtree/org/apache/james/mime4j/field/address/AddressListParser.jjt
Wed Feb  4 13:32:12 2009
@@ -60,38 +60,51 @@
 
 public class AddressListParser {
     public static void main(String args[]) throws ParseException {
-		while (true) {
-		    try {
-				AddressListParser parser = new AddressListParser(System.in);
-		    	parser.parseLine();
-		    	((SimpleNode)parser.jjtree.rootNode()).dump("> ");
-		    } catch (Exception x) {
-				x.printStackTrace();
-				return;
-		    }
-		}
+        while (true) {
+            try {
+                AddressListParser parser = new AddressListParser(System.in);
+                parser.parseLine();
+                ((SimpleNode) parser.jjtree.rootNode()).dump("> ");
+            } catch (Exception x) {
+                x.printStackTrace();
+                return;
+            }
+        }
     }
-    
-    private static void log(String msg) {
-    	System.out.print(msg);
+
+    public ASTaddress_list parseAddressList() throws ParseException {
+        try {
+            parseAddressList0();
+            return (ASTaddress_list) jjtree.rootNode();
+        } catch (TokenMgrError tme) {
+            throw new ParseException(tme.getMessage());
+        }
     }
-    
-    public ASTaddress_list parse() throws ParseException {
+
+    public ASTaddress parseAddress() throws ParseException {
         try {
-    	    parseAll();
-    	    return (ASTaddress_list)jjtree.rootNode();
-    	} catch (TokenMgrError tme) {
-    	    throw new ParseException(tme.getMessage());
-    	}
+            parseAddress0();
+            return (ASTaddress) jjtree.rootNode();
+        } catch (TokenMgrError tme) {
+            throw new ParseException(tme.getMessage());
+        }
     }
-    
-    
+
+    public ASTmailbox parseMailbox() throws ParseException {
+        try {
+            parseMailbox0();
+            return (ASTmailbox) jjtree.rootNode();
+        } catch (TokenMgrError tme) {
+            throw new ParseException(tme.getMessage());
+        }
+    }
+
     void jjtreeOpenNodeScope(Node n) {
-    	((SimpleNode)n).firstToken = getToken(1);
+        ((SimpleNode) n).firstToken = getToken(1);
     }
-    
+
     void jjtreeCloseNodeScope(Node n) {
-    	((SimpleNode)n).lastToken = getToken(0);
+        ((SimpleNode) n).lastToken = getToken(0);
     }
 }
 
@@ -103,12 +116,24 @@
 	address_list() ["\r"] "\n"
 }
 
-void parseAll() #void :
+void parseAddressList0() #void :
 {}
 {
 	address_list() <EOF>
 }
 
+void parseAddress0() #void :
+{}
+{
+	address() <EOF>
+}
+
+void parseMailbox0() #void :
+{}
+{
+	mailbox() <EOF>
+}
+
 void address_list() :
 {}
 {

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=740733&r1=740732&r2=740733&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
Wed Feb  4 13:32:12 2009
@@ -403,6 +403,27 @@
         Group g = new Group("Undisclosed recipients", emptyMailboxes);
         assertEquals("Undisclosed recipients:;", g.getEncodedString());
     }
+
+    public void testParseAddress() throws Exception {
+        Address address = Address.parse("Mary Smith <mary@example.net>");
+        assertTrue(address instanceof Mailbox);
+        assertEquals("Mary Smith", ((Mailbox) address).getName());
+        assertEquals("mary@example.net", ((Mailbox) address).getAddress());
+
+        address = Address.parse("group: Mary Smith <mary@example.net>;");
+        assertTrue(address instanceof Group);
+        assertEquals("group", ((Group) address).getName());
+        assertEquals("Mary Smith", ((Group) address).getMailboxes().get(0)
+                .getName());
+        assertEquals("mary@example.net", ((Group) address).getMailboxes()
+                .get(0).getAddress());
+
+        try {
+            Group.parse("john.doe@acme.org, jane.doe@acme.org");
+            fail();
+        } catch (IllegalArgumentException expected) {
+        }
+    }
     
     public void testParseGroup() throws Exception {
         Group group = Group
@@ -419,6 +440,18 @@
         Mailbox mailbox2 = mailboxes.get(1);
         assertEquals("Mary Smith", mailbox2.getName());
         assertEquals("mary@example.net", mailbox2.getAddress());
+
+        try {
+            Group.parse("john.doe@acme.org");
+            fail();
+        } catch (IllegalArgumentException expected) {
+        }
+
+        try {
+            Group.parse("g1: john.doe@acme.org;, g2: mary@example.net;");
+            fail();
+        } catch (IllegalArgumentException expected) {
+        }
     }
 
     public void testParseMailbox() throws Exception {
@@ -435,6 +468,18 @@
                 .parse("\"Hans M\374ller\" <hans.mueller@acme.org>");
         assertEquals("Hans M\374ller", mailbox3.getName());
         assertEquals("hans.mueller@acme.org", mailbox3.getAddress());
+
+        try {
+            Mailbox.parse("g: Mary Smith <mary@example.net>;");
+            fail();
+        } catch (IllegalArgumentException expected) {
+        }
+
+        try {
+            Mailbox.parse("Mary Smith <mary@example.net>, hans.mueller@acme.org");
+            fail();
+        } catch (IllegalArgumentException expected) {
+        }
     }
     
 }



Mime
View raw message