FileDocCategorySizeDatePackage
WebAddress.javaAPI DocAndroid 5.1 API5568Thu Mar 12 22:22:10 GMT 2015android.net

WebAddress

public class WebAddress extends Object
{@hide} Web Address Parser This is called WebAddress, rather than URL or URI, because it attempts to parse the stuff that a user will actually type into a browser address widget. Unlike java.net.uri, this parser will not choke on URIs missing schemes. It will only throw a ParseException if the input is really hosed. If given an https scheme but no port, fills in port

Fields Summary
private String
mScheme
private String
mHost
private int
mPort
private String
mPath
private String
mAuthInfo
static final int
MATCH_GROUP_SCHEME
static final int
MATCH_GROUP_AUTHORITY
static final int
MATCH_GROUP_HOST
static final int
MATCH_GROUP_PORT
static final int
MATCH_GROUP_PATH
static Pattern
sAddressPattern
Constructors Summary
public WebAddress(String address)
parses given uriString.


        
         
        if (address == null) {
            throw new NullPointerException();
        }

        // android.util.Log.d(LOGTAG, "WebAddress: " + address);

        mScheme = "";
        mHost = "";
        mPort = -1;
        mPath = "/";
        mAuthInfo = "";

        Matcher m = sAddressPattern.matcher(address);
        String t;
        if (m.matches()) {
            t = m.group(MATCH_GROUP_SCHEME);
            if (t != null) mScheme = t.toLowerCase(Locale.ROOT);
            t = m.group(MATCH_GROUP_AUTHORITY);
            if (t != null) mAuthInfo = t;
            t = m.group(MATCH_GROUP_HOST);
            if (t != null) mHost = t;
            t = m.group(MATCH_GROUP_PORT);
            if (t != null && t.length() > 0) {
                // The ':' character is not returned by the regex.
                try {
                    mPort = Integer.parseInt(t);
                } catch (NumberFormatException ex) {
                    throw new ParseException("Bad port");
                }
            }
            t = m.group(MATCH_GROUP_PATH);
            if (t != null && t.length() > 0) {
                /* handle busted myspace frontpage redirect with
                   missing initial "/" */
                if (t.charAt(0) == '/") {
                    mPath = t;
                } else {
                    mPath = "/" + t;
                }
            }

        } else {
            // nothing found... outa here
            throw new ParseException("Bad address");
        }

        /* Get port from scheme or scheme from port, if necessary and
           possible */
        if (mPort == 443 && mScheme.equals("")) {
            mScheme = "https";
        } else if (mPort == -1) {
            if (mScheme.equals("https"))
                mPort = 443;
            else
                mPort = 80; // default
        }
        if (mScheme.equals("")) mScheme = "http";
    
Methods Summary
public java.lang.StringgetAuthInfo()
{@hide}

      return mAuthInfo;
    
public java.lang.StringgetHost()
{@hide}

      return mHost;
    
public java.lang.StringgetPath()
{@hide}

      return mPath;
    
public intgetPort()
{@hide}

      return mPort;
    
public java.lang.StringgetScheme()
{@hide}

      return mScheme;
    
public voidsetAuthInfo(java.lang.String authInfo)
{@hide}

      mAuthInfo = authInfo;
    
public voidsetHost(java.lang.String host)
{@hide}

      mHost = host;
    
public voidsetPath(java.lang.String path)
{@hide}

      mPath = path;
    
public voidsetPort(int port)
{@hide}

      mPort = port;
    
public voidsetScheme(java.lang.String scheme)
{@hide}

      mScheme = scheme;
    
public java.lang.StringtoString()

        String port = "";
        if ((mPort != 443 && mScheme.equals("https")) ||
            (mPort != 80 && mScheme.equals("http"))) {
            port = ":" + Integer.toString(mPort);
        }
        String authInfo = "";
        if (mAuthInfo.length() > 0) {
            authInfo = mAuthInfo + "@";
        }

        return mScheme + "://" + authInfo + mHost + port + mPath;