Subversion Repositories javautils

Compare Revisions

No changes between revisions

Regard whitespace Rev 16 → Rev 17

/ViaThinkSoft Java Utils/test/de/viathinksoft/utils/mail/EMailAddressTest.java
File deleted
Property changes:
Deleted: svn:mime-type
-text/plain
\ No newline at end of property
/ViaThinkSoft Java Utils/test/de/viathinksoft/utils/mail/sender/RawMailSenderPlainTextTest.java
10,7 → 10,7
 
import org.junit.Test;
 
import de.viathinksoft.utils.mail.EMailAddress;
import de.viathinksoft.utils.mail.address.EMailAddress;
import de.viathinksoft.utils.mail.sender.PlainTextMailSender;
import eMailTests.TestConfiguration;
 
/ViaThinkSoft Java Utils/test/de/viathinksoft/utils/mail/address/EMailAddressTest.java
0,0 → 1,168
package de.viathinksoft.utils.mail.address;
 
import static org.junit.Assert.*;
 
import java.net.IDN;
 
import org.junit.Test;
 
import de.viathinksoft.utils.mail.address.EMailAddress;
 
public class EMailAddressTest {
private static final String ExamplePunycode = "xn--zckzah"; // Japanese IDN Test TLD
private static final String ExampleUnicode = IDN.toUnicode(ExamplePunycode);
@Test
public void testAddressParsing() {
try {
new EMailAddress(null);
fail();
} catch (NullPointerException e) {
}
EMailAddress a;
a = new EMailAddress("");
assertEquals("", a.getLocalPart());
// assertEquals("", a.getLocalPartASCII());
assertEquals("", a.getDomainPartUnicode());
assertEquals("", a.getDomainPartPunycode());
assertEquals("", a.getTldUnicode());
assertEquals("", a.getTldPunycode());
assertEquals("", a.toString());
assertEquals("", a.getMailAddressUnicode());
assertEquals("", a.getMailAddressPunycodedDomain());
 
a = new EMailAddress("bla");
assertEquals("bla", a.getLocalPart());
// assertEquals("", a.getLocalPartASCII());
assertEquals("", a.getDomainPartUnicode());
assertEquals("", a.getDomainPartPunycode());
assertEquals("", a.getTldUnicode());
assertEquals("", a.getTldPunycode());
assertEquals("bla", a.toString());
assertEquals("bla", a.getMailAddressUnicode());
assertEquals("bla", a.getMailAddressPunycodedDomain());
a = new EMailAddress(ExampleUnicode);
assertEquals(ExampleUnicode, a.getLocalPart());
// assertEquals("", a.getLocalPartASCII());
assertEquals("", a.getDomainPartUnicode());
assertEquals("", a.getDomainPartPunycode());
assertEquals("", a.getTldUnicode());
assertEquals("", a.getTldPunycode());
assertEquals(ExampleUnicode, a.toString());
assertEquals(ExampleUnicode, a.getMailAddressUnicode());
assertEquals(ExampleUnicode, a.getMailAddressPunycodedDomain());
 
a = new EMailAddress("@");
assertEquals("", a.getLocalPart());
// assertEquals("", a.getLocalPartASCII());
assertEquals("", a.getDomainPartUnicode());
assertEquals("", a.getDomainPartPunycode());
assertEquals("", a.getTldUnicode());
assertEquals("", a.getTldPunycode());
assertEquals("", a.toString());
assertEquals("", a.getMailAddressUnicode());
assertEquals("", a.getMailAddressPunycodedDomain());
 
a = new EMailAddress("local@domain");
assertEquals("local", a.getLocalPart());
// assertEquals("local", a.getLocalPartASCII());
assertEquals("domain", a.getDomainPartUnicode());
assertEquals("domain", a.getDomainPartPunycode());
assertEquals("", a.getTldUnicode());
assertEquals("", a.getTldPunycode());
assertEquals("local@domain", a.toString());
assertEquals("local@domain", a.getMailAddressUnicode());
assertEquals("local@domain", a.getMailAddressPunycodedDomain());
 
a = new EMailAddress("local@domain.tld");
assertEquals("local", a.getLocalPart());
// assertEquals("local", a.getlocalPartASCII());
assertEquals("domain.tld", a.getDomainPartUnicode());
assertEquals("domain.tld", a.getDomainPartPunycode());
assertEquals("tld", a.getTldUnicode());
assertEquals("tld", a.getTldPunycode());
assertEquals("local@domain.tld", a.toString());
assertEquals("local@domain.tld", a.getMailAddressUnicode());
assertEquals("local@domain.tld", a.getMailAddressPunycodedDomain());
a = new EMailAddress("local@"+ExampleUnicode+".jp");
assertEquals("local", a.getLocalPart());
// assertEquals("local", a.getlocalPartASCII());
assertEquals(ExampleUnicode+".jp", a.getDomainPartUnicode());
assertEquals(ExamplePunycode+".jp", a.getDomainPartPunycode());
assertEquals("jp", a.getTldUnicode());
assertEquals("jp", a.getTldPunycode());
assertEquals("local@"+ExampleUnicode+".jp", a.getMailAddressUnicode());
assertEquals("local@"+ExamplePunycode+".jp", a.getMailAddressPunycodedDomain());
EMailAddress.USE_UNICODE_AS_STANDARD = true;
assertEquals("local@"+ExampleUnicode+".jp", a.toString());
EMailAddress.USE_UNICODE_AS_STANDARD = false;
assertEquals("local@"+ExamplePunycode+".jp", a.toString());
 
a = new EMailAddress("local@example."+ExampleUnicode);
assertEquals("local", a.getLocalPart());
// assertEquals("local", a.getlocalPartASCII());
assertEquals("example."+ExampleUnicode, a.getDomainPartUnicode());
assertEquals("example."+ExamplePunycode, a.getDomainPartPunycode());
assertEquals(ExampleUnicode, a.getTldUnicode());
assertEquals(ExamplePunycode, a.getTldPunycode());
assertEquals("local@example."+ExampleUnicode, a.getMailAddressUnicode());
assertEquals("local@example."+ExamplePunycode, a.getMailAddressPunycodedDomain());
EMailAddress.USE_UNICODE_AS_STANDARD = true;
assertEquals("local@example."+ExampleUnicode, a.toString());
EMailAddress.USE_UNICODE_AS_STANDARD = false;
assertEquals("local@example."+ExamplePunycode, a.toString());
}
@Test
public void testIsUnicode() {
assertFalse(EMailAddress.isUnicode(null));
assertFalse(EMailAddress.isUnicode(""));
assertFalse(EMailAddress.isUnicode(ExamplePunycode));
assertTrue(EMailAddress.isUnicode(ExampleUnicode));
}
@Test
public void testIsPunycode() {
assertFalse(EMailAddress.isPunycode(null));
assertFalse(EMailAddress.isPunycode(""));
assertTrue(EMailAddress.isPunycode(ExamplePunycode));
assertFalse(EMailAddress.isPunycode(ExampleUnicode));
}
@Test
public void testClone() throws CloneNotSupportedException {
EMailAddress a = new EMailAddress("local@example."+ExampleUnicode);
EMailAddress b = (EMailAddress) a.clone();
assertFalse(a == b);
assertTrue(a.equals(b));
assertTrue(b.equals(a));
assertEquals(b.getDomainPartPunycode(), a.getDomainPartPunycode());
assertEquals(b.getDomainPartUnicode(), a.getDomainPartUnicode());
assertEquals(b.getLocalPart(), a.getLocalPart());
assertEquals(b.getMailAddressPunycodedDomain(), a.getMailAddressPunycodedDomain());
assertEquals(b.getMailAddressUnicode(), a.getMailAddressUnicode());
assertEquals(b.getTldPunycode(), a.getTldPunycode());
assertEquals(b.getTldUnicode(), a.getTldUnicode());
EMailAddress.USE_UNICODE_AS_STANDARD = true;
assertEquals(b.toString(), a.toString());
EMailAddress.USE_UNICODE_AS_STANDARD = false;
assertEquals(b.toString(), a.toString());
}
@Test
public void testEquals() {
EMailAddress a = new EMailAddress("local@example."+ExampleUnicode);
EMailAddress b = new EMailAddress("local@example."+ExampleUnicode);
assertFalse(a == b);
assertTrue(a.equals(b));
assertTrue(b.equals(a));
}
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/ViaThinkSoft Java Utils/test/de/viathinksoft/utils/mail/syntaxchecker/MailSyntaxCheckerTest.java
16,7 → 16,7
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
 
import de.viathinksoft.utils.mail.EMailAddress;
import de.viathinksoft.utils.mail.address.EMailAddress;
 
public class MailSyntaxCheckerTest {
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/EMailAddress.java
File deleted
Property changes:
Deleted: svn:mime-type
-text/plain
\ No newline at end of property
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/EMailPreprocessor.java
File deleted
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/sender/PlainTextMailSender.java
2,7 → 2,7
 
import javax.mail.MessagingException;
 
import de.viathinksoft.utils.mail.EMailAddress;
import de.viathinksoft.utils.mail.address.EMailAddress;
 
public class PlainTextMailSender extends RawMailSender {
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/address/EMailAddress.java
0,0 → 1,315
package de.viathinksoft.utils.mail.address;
 
import java.net.IDN;
 
/**
*
* This class parses an email address (trims whitespaces from it) and stores it
* in its original form as well as store a RFC-compatible punycoded domainpart.
* So, if you enter a Unicode-Mail-Address you can easily access the trimmed and
* punycoded domain-part mail-address. Warning! This class does NOT check if the
* email address is fully valid. Please use the syntax checker class for this.
*
* @author Daniel Marschall
*
*/
public class EMailAddress {
 
// Constants
 
/**
* This constant is used by toString() and tells if whether
* getMailAddressUnicode() or getMailAddressPunycodedDomain() should be
* returned.
*/
static boolean USE_UNICODE_AS_STANDARD = false;
 
// Attributes
 
/**
* The local part of our parsed mail address. (Part before "@") It is
* allways Unicode, since the mail servers have to take care about it. Even
* if Unicode mail addresses will become popular in future, the local part
* will probably not punycoded.
*/
private String localPart;
/**
* The domain part of our parsed mail address (part after "@") inclusive our
* top level domain (TLD). It is in its Unicode form.
*/
private String domainPartUnicode;
 
/**
* The domain part of our parsed mail address (part after "@") inclusive our
* top level domain (TLD). It is in its Punycode (ASCII) form.
*/
private String domainPartPunycode;
 
/**
* The top level domain (COM, ORG, BIZ...) of our parsed mail address. The
* dot is not included. It is in its Unicode form.
*/
private String tldUnicode;
 
/**
* The top level domain (COM, ORG, BIZ...) of our parsed mail address. The
* dot is not included. It is in its Punycode form.
*/
private String tldPunycode;
 
// Getter and Setter
 
/**
* The local part of our parsed mail address. (Part before "@") It is
* allways Unicode, since the mail servers have to take care about it. Even
* if Unicode mail addresses will become popular in future, the local part
* will probably not punycoded.
*
* @return The local part
*/
public String getLocalPart() {
return localPart;
}
 
/**
* The domain part of our parsed mail address (part after "@") inclusive our
* top level domain (TLD). It is in its Unicode form.
*
* @return The domain part in Unicode.
*/
public String getDomainPartUnicode() {
return domainPartUnicode;
}
 
/**
* The domain part of our parsed mail address (part after "@") inclusive our
* top level domain (TLD). It is in its Punycode (ASCII) form.
*
* @return The domain part in Punycode.
*/
public String getDomainPartPunycode() {
return domainPartPunycode;
}
 
/**
* The top level domain (COM, ORG, BIZ...) of our parsed mail address. The
* dot is not included. It is in its Unicode form.
*
* @return The TLD in Unicode.
*/
public String getTldUnicode() {
return tldUnicode;
}
 
/**
* The top level domain (COM, ORG, BIZ...) of our parsed mail address. The
* dot is not included. It is in its Punycode form.
*
* @return The TLD in Punycode.
*/
public String getTldPunycode() {
return tldPunycode;
}
 
// Constructors
 
/**
* Creates an email address object out of an email address string.
*
* @param eMailAddress
* bare computer email address. e.g. roedyg@mindprod.com No
* "Roedy Green" <roedyg@mindprod.com> style addresses. No local
* addresses, e.g. roedy.
*/
public EMailAddress(String eMailAddress) {
super();
 
// Zuerst trimmen (z.B. für Formulardaten)
eMailAddress = eMailAddress.trim();
 
// Wir splitten dann beim At-Zeichen (@)
String localPart = "";
String domainPart = "";
int atIndex = eMailAddress.lastIndexOf('@');
if (atIndex == -1) {
localPart = eMailAddress;
domainPart = "";
} else {
localPart = eMailAddress.substring(0, atIndex);
domainPart = eMailAddress.substring(atIndex + 1);
}
 
// We parse the local part.
 
if (localPart == null)
localPart = "";
this.localPart = localPart;
 
// We parse the domainPart and allocate punycode and unicode fields.
 
if (domainPart == null)
domainPart = "";
if (isUnicode(domainPart)) {
this.domainPartUnicode = domainPart;
this.domainPartPunycode = IDN.toASCII(domainPart);
} else /* if (isPunycode(domainPart)) */{
this.domainPartUnicode = IDN.toUnicode(domainPart);
this.domainPartPunycode = domainPart;
}
 
// We additionally parse the TLD and also determinate if it is punycode
// or not.
 
int dotIdx;
 
dotIdx = this.domainPartUnicode.lastIndexOf('.');
if (dotIdx >= 0) {
this.tldUnicode = this.domainPartUnicode.substring(dotIdx + 1);
} else {
// We do not throw an exception here because it could be an email to
// a network computer or an IP address.
this.tldUnicode = "";
}
 
dotIdx = this.domainPartPunycode.lastIndexOf('.');
if (dotIdx >= 0) {
this.tldPunycode = this.domainPartPunycode.substring(dotIdx + 1);
} else {
// We do not throw an exception here because it could be an email to
// a network computer or an IP address.
this.tldPunycode = "";
}
}
 
// Methods
 
/**
* Returns the email address with punycoded domain name and TLD. You should
* use this method to send emails.
*
* @return The email address with punycoded domain name and TLD.
*/
public String getMailAddressPunycodedDomain() {
if (this.domainPartPunycode.isEmpty()) {
return this.localPart;
} else {
return this.localPart + "@" + this.domainPartPunycode;
}
}
 
/**
* Returns the email address with internationalized domain names and TLD.
*
* @return The email address with internationalized domain name and TLD.
*/
public String getMailAddressUnicode() {
if (this.domainPartUnicode.isEmpty()) {
return this.localPart;
} else {
return this.localPart + "@" + this.domainPartUnicode;
}
}
 
/**
* Returns a string which represents the mail address. If the constant
* USE_UNICODE_AS_STANDARD is true, the internationalized domain names will
* not translated into the corresponding Punycode. If false, then not.
*
* @return The string which represents the mail address. Warning! Since this
* method is rather designed to show a formatted mail address, it
* should NOT be used to send emails. Please only use this function
* if you want to output.
*/
@Override
public String toString() {
if (USE_UNICODE_AS_STANDARD) {
return this.getMailAddressUnicode();
} else {
return this.getMailAddressPunycodedDomain();
}
}
 
/**
* Checks if an object is equal to our email address object.
*
* @return Boolean which describes if it is equal or not.
*/
@Override
public boolean equals(Object obj) {
// Initial checks
 
if (this == obj)
return true;
if (obj == null)
return false;
if (obj.getClass() != getClass())
return false;
 
// Compare the fields
 
if (!this.domainPartPunycode
.equals(((EMailAddress) obj).domainPartPunycode)) {
return false;
}
if (!this.domainPartUnicode
.equals(((EMailAddress) obj).domainPartUnicode)) {
return false;
}
if (!this.localPart.equals(((EMailAddress) obj).localPart)) {
return false;
}
if (!this.tldUnicode.equals(((EMailAddress) obj).tldUnicode)) {
return false;
}
if (!this.tldPunycode.equals(((EMailAddress) obj).tldPunycode)) {
return false;
}
 
// Everything's fine ^^
 
return true;
 
// return this.toString().equals(obj.toString());
}
 
/**
* Creates a deep copy of the email address object.
*
* @return A new instance of the email address object with the same
* properties.
*/
@Override
protected EMailAddress clone() throws CloneNotSupportedException {
return new EMailAddress(this.toString());
}
 
// ---------- STATIC FUNCTIONS ----------
 
/**
* Determinates if a given string can be converted into Punycode.
*
* @param str
* The string which should be checked
* @return Boolean which shows if the string is not yet punicoded.
*/
protected static boolean isUnicode(String str) {
if (str == null) {
return false;
}
return (!IDN.toASCII(str).equals(str));
}
 
/**
* Determinates if a given string is in Punycode format.
*
* @param str
* The string which should be checked
* @return Boolean which shows if the string is punycoded or not.
*/
protected static boolean isPunycode(String str) {
if (str == null) {
return false;
}
return (!IDN.toUnicode(str).equals(str));
}
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/address/EMailPreprocessor.java
0,0 → 1,24
package de.viathinksoft.utils.mail.address;
 
/**
* This class contains a function which "preproceses an email address. Following
* steps will be performed: 1. The email address will be trimmed (in case of
* user inputs) 2. E-Mail-Addresses with internationalized domain names will be
* converted into ASCII compatible punycode (the local part will be left as it
* is!)
*
* @author Daniel Marschall
*
*/
public class EMailPreprocessor {
public static String preprocess(String eMailAddress) {
EMailAddress email = new EMailAddress(eMailAddress);
return email.getMailAddressPunycodedDomain();
}
private EMailPreprocessor() {
}
 
}
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/syntaxchecker/MailSyntaxChecker.java
4,7 → 4,7
import java.util.HashSet;
import java.util.regex.Pattern;
 
import de.viathinksoft.utils.mail.EMailAddress;
import de.viathinksoft.utils.mail.address.EMailAddress;
 
/**
* This class is not stable. For a good syntax check, please use the classes of