?? internetaddress.java
字號:
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. * Sun designates this particular file as subject to the "Classpath" exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code. If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: "Portions Copyrighted [year] * [name of copyright owner]" * * Contributor(s): * * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. *//* * @(#)InternetAddress.java 1.49 07/05/04 */package javax.mail.internet;import java.io.UnsupportedEncodingException;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.Vector;import java.util.StringTokenizer;import java.util.Locale;import javax.mail.*;/** * This class represents an Internet email address using the syntax * of <a href="http://www.ietf.org/rfc/rfc822.txt">RFC822</a>. * Typical address syntax is of the form "user@host.domain" or * "Personal Name <user@host.domain>". * * @author Bill Shannon * @author John Mani */public class InternetAddress extends Address implements Cloneable { protected String address; // email address /** * The personal name. */ protected String personal; /** * The RFC 2047 encoded version of the personal name. <p> * * This field and the <code>personal</code> field track each * other, so if a subclass sets one of these fields directly, it * should set the other to <code>null</code>, so that it is * suitably recomputed. */ protected String encodedPersonal; private static final long serialVersionUID = -7507595530758302903L; /** * Default constructor. */ public InternetAddress() { } /** * Constructor. <p> * * Parse the given string and create an InternetAddress. * See the <code>parse</code> method for details of the parsing. * The address is parsed using "strict" parsing. * This constructor does <b>not</b> perform the additional * syntax checks that the * <code>InternetAddress(String address, boolean strict)</code> * constructor does when <code>strict</code> is <code>true</code>. * This constructor is equivalent to * <code>InternetAddress(address, false)</code>. * * @param address the address in RFC822 format * @exception AddressException if the parse failed */ public InternetAddress(String address) throws AddressException { // use our address parsing utility routine to parse the string InternetAddress a[] = parse(address, true); // if we got back anything other than a single address, it's an error if (a.length != 1) throw new AddressException("Illegal address", address); /* * Now copy the contents of the single address we parsed * into the current object, which will be returned from the * constructor. * XXX - this sure is a round-about way of getting this done. */ this.address = a[0].address; this.personal = a[0].personal; this.encodedPersonal = a[0].encodedPersonal; } /** * Parse the given string and create an InternetAddress. * If <code>strict</code> is false, the detailed syntax of the * address isn't checked. * * @param address the address in RFC822 format * @param strict enforce RFC822 syntax * @exception AddressException if the parse failed * @since JavaMail 1.3 */ public InternetAddress(String address, boolean strict) throws AddressException { this(address); if (strict) checkAddress(this.address, true, true); } /** * Construct an InternetAddress given the address and personal name. * The address is assumed to be a syntactically valid RFC822 address. * * @param address the address in RFC822 format * @param personal the personal name */ public InternetAddress(String address, String personal) throws UnsupportedEncodingException { this(address, personal, null); } /** * Construct an InternetAddress given the address and personal name. * The address is assumed to be a syntactically valid RFC822 address. * * @param address the address in RFC822 format * @param personal the personal name * @param charset the MIME charset for the name */ public InternetAddress(String address, String personal, String charset) throws UnsupportedEncodingException { this.address = address; setPersonal(personal, charset); } /** * Return a copy of this InternetAddress object. * @since JavaMail 1.2 */ public Object clone() { InternetAddress a = null; try { a = (InternetAddress)super.clone(); } catch (CloneNotSupportedException e) {} // Won't happen return a; } /** * Return the type of this address. The type of an InternetAddress * is "rfc822". */ public String getType() { return "rfc822"; } /** * Set the email address. * * @param address email address */ public void setAddress(String address) { this.address = address; } /** * Set the personal name. If the name contains non US-ASCII * characters, then the name will be encoded using the specified * charset as per RFC 2047. If the name contains only US-ASCII * characters, no encoding is done and the name is used as is. <p> * * @param name personal name * @param charset MIME charset to be used to encode the name as * per RFC 2047 * @see #setPersonal(String) * @exception UnsupportedEncodingException if the charset encoding * fails. */ public void setPersonal(String name, String charset) throws UnsupportedEncodingException { personal = name; if (name != null) encodedPersonal = MimeUtility.encodeWord(name, charset, null); else encodedPersonal = null; } /** * Set the personal name. If the name contains non US-ASCII * characters, then the name will be encoded using the platform's * default charset. If the name contains only US-ASCII characters, * no encoding is done and the name is used as is. <p> * * @param name personal name * @see #setPersonal(String name, String charset) * @exception UnsupportedEncodingException if the charset encoding * fails. */ public void setPersonal(String name) throws UnsupportedEncodingException { personal = name; if (name != null) encodedPersonal = MimeUtility.encodeWord(name); else encodedPersonal = null; } /** * Get the email address. * @return email address */ public String getAddress() { return address; } /** * Get the personal name. If the name is encoded as per RFC 2047, * it is decoded and converted into Unicode. If the decoding or * conversion fails, the raw data is returned as is. * * @return personal name */ public String getPersonal() { if (personal != null) return personal; if (encodedPersonal != null) { try { personal = MimeUtility.decodeText(encodedPersonal); return personal; } catch (Exception ex) { // 1. ParseException: either its an unencoded string or // it can't be parsed // 2. UnsupportedEncodingException: can't decode it. return encodedPersonal; } } // No personal or encodedPersonal, return null return null; } /** * Convert this address into a RFC 822 / RFC 2047 encoded address. * The resulting string contains only US-ASCII characters, and * hence is mail-safe. * * @return possibly encoded address string */ public String toString() { if (encodedPersonal == null && personal != null) try { encodedPersonal = MimeUtility.encodeWord(personal); } catch (UnsupportedEncodingException ex) { } if (encodedPersonal != null) return quotePhrase(encodedPersonal) + " <" + address + ">"; else if (isGroup() || isSimple()) return address; else return "<" + address + ">"; } /** * Returns a properly formatted address (RFC 822 syntax) of * Unicode characters. * * @return Unicode address string * @since JavaMail 1.2 */ public String toUnicodeString() { String p = getPersonal(); if (p != null) return quotePhrase(p) + " <" + address + ">"; else if (isGroup() || isSimple()) return address; else return "<" + address + ">"; } /* * quotePhrase() quotes the words within a RFC822 phrase. * * This is tricky, since a phrase is defined as 1 or more * RFC822 words, separated by LWSP. Now, a word that contains * LWSP is supposed to be quoted, and this is exactly what the * MimeUtility.quote() method does. However, when dealing with * a phrase, any LWSP encountered can be construed to be the * separator between words, and not part of the words themselves. * To deal with this funkiness, we have the below variant of * MimeUtility.quote(), which essentially ignores LWSP when * deciding whether to quote a word. * * It aint pretty, but it gets the job done :) */ private static final String rfc822phrase = HeaderTokenizer.RFC822.replace(' ', '\0').replace('\t', '\0'); private static String quotePhrase(String phrase) { int len = phrase.length(); boolean needQuoting = false; for (int i = 0; i < len; i++) { char c = phrase.charAt(i); if (c == '"' || c == '\\') { // need to escape them and then quote the whole string StringBuffer sb = new StringBuffer(len + 3); sb.append('"'); for (int j = 0; j < len; j++) { char cc = phrase.charAt(j); if (cc == '"' || cc == '\\') // Escape the character sb.append('\\'); sb.append(cc); } sb.append('"'); return sb.toString(); } else if ((c < 040 && c != '\r' && c != '\n' && c != '\t') || c >= 0177 || rfc822phrase.indexOf(c) >= 0) // These characters cause the string to be quoted needQuoting = true; } if (needQuoting) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -