Source Code Cross Referenced for URI.java in  » Web-Server » Rimfaxe-Web-Server » org » apache » xerces » utils » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Server » Rimfaxe Web Server » org.apache.xerces.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * The Apache Software License, Version 1.1
0003:         *
0004:         *
0005:         * Copyright (c) 1999,2001 The Apache Software Foundation.  All rights
0006:         * reserved.
0007:         *
0008:         * Redistribution and use in source and binary forms, with or without
0009:         * modification, are permitted provided that the following conditions
0010:         * are met:
0011:         *
0012:         * 1. Redistributions of source code must retain the above copyright
0013:         *    notice, this list of conditions and the following disclaimer.
0014:         *
0015:         * 2. Redistributions in binary form must reproduce the above copyright
0016:         *    notice, this list of conditions and the following disclaimer in
0017:         *    the documentation and/or other materials provided with the
0018:         *    distribution.
0019:         *
0020:         * 3. The end-user documentation included with the redistribution,
0021:         *    if any, must include the following acknowledgment:
0022:         *       "This product includes software developed by the
0023:         *        Apache Software Foundation (http://www.apache.org/)."
0024:         *    Alternately, this acknowledgment may appear in the software itself,
0025:         *    if and wherever such third-party acknowledgments normally appear.
0026:         *
0027:         * 4. The names "Xerces" and "Apache Software Foundation" must
0028:         *    not be used to endorse or promote products derived from this
0029:         *    software without prior written permission. For written
0030:         *    permission, please contact apache@apache.org.
0031:         *
0032:         * 5. Products derived from this software may not be called "Apache",
0033:         *    nor may "Apache" appear in their name, without prior written
0034:         *    permission of the Apache Software Foundation.
0035:         *
0036:         * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
0037:         * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0038:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0039:         * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
0040:         * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0041:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0042:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
0043:         * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0044:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
0045:         * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
0046:         * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0047:         * SUCH DAMAGE.
0048:         * ====================================================================
0049:         *
0050:         * This software consists of voluntary contributions made by many
0051:         * individuals on behalf of the Apache Software Foundation and was
0052:         * originally based on software copyright (c) 1999, iClick Inc.,
0053:         * http://www.apache.org.  For more information on the Apache Software
0054:         * Foundation, please see <http://www.apache.org/>.
0055:         */
0056:
0057:        package org.apache.xerces.utils;
0058:
0059:        import java.io.IOException;
0060:        import java.io.Serializable;
0061:
0062:        /**********************************************************************
0063:         * A class to represent a Uniform Resource Identifier (URI). This class
0064:         * is designed to handle the parsing of URIs and provide access to
0065:         * the various components (scheme, host, port, userinfo, path, query
0066:         * string and fragment) that may constitute a URI.
0067:         * <p>
0068:         * Parsing of a URI specification is done according to the URI
0069:         * syntax described in RFC 2396
0070:         * <http://www.ietf.org/rfc/rfc2396.txt?number=2396>. Every URI consists
0071:         * of a scheme, followed by a colon (':'), followed by a scheme-specific
0072:         * part. For URIs that follow the "generic URI" syntax, the scheme-
0073:         * specific part begins with two slashes ("//") and may be followed
0074:         * by an authority segment (comprised of user information, host, and
0075:         * port), path segment, query segment and fragment. Note that RFC 2396
0076:         * no longer specifies the use of the parameters segment and excludes
0077:         * the "user:password" syntax as part of the authority segment. If
0078:         * "user:password" appears in a URI, the entire user/password string
0079:         * is stored as userinfo.
0080:         * <p>
0081:         * For URIs that do not follow the "generic URI" syntax (e.g. mailto),
0082:         * the entire scheme-specific part is treated as the "path" portion
0083:         * of the URI.
0084:         * <p>
0085:         * Note that, unlike the java.net.URL class, this class does not provide
0086:         * any built-in network access functionality nor does it provide any
0087:         * scheme-specific functionality (for example, it does not know a
0088:         * default port for a specific scheme). Rather, it only knows the
0089:         * grammar and basic set of operations that can be applied to a URI.
0090:         *
0091:         * @version  $Id: URI.java,v 1.8.2.1 2001/11/12 20:24:15 sandygao Exp $
0092:         *
0093:         **********************************************************************/
0094:        public class URI implements  Serializable {
0095:
0096:            /*******************************************************************
0097:             * MalformedURIExceptions are thrown in the process of building a URI
0098:             * or setting fields on a URI when an operation would result in an
0099:             * invalid URI specification.
0100:             *
0101:             ********************************************************************/
0102:            public static class MalformedURIException extends IOException {
0103:
0104:                /******************************************************************
0105:                 * Constructs a <code>MalformedURIException</code> with no specified
0106:                 * detail message.
0107:                 ******************************************************************/
0108:                public MalformedURIException() {
0109:                    super ();
0110:                }
0111:
0112:                /*****************************************************************
0113:                 * Constructs a <code>MalformedURIException</code> with the
0114:                 * specified detail message.
0115:                 *
0116:                 * @param p_msg the detail message.
0117:                 ******************************************************************/
0118:                public MalformedURIException(String p_msg) {
0119:                    super (p_msg);
0120:                }
0121:            }
0122:
0123:            /** reserved characters */
0124:            private static final String RESERVED_CHARACTERS = ";/?:@&=+$,";
0125:
0126:            /** URI punctuation mark characters - these, combined with
0127:                alphanumerics, constitute the "unreserved" characters */
0128:            private static final String MARK_CHARACTERS = "-_.!~*'() ";
0129:
0130:            /** scheme can be composed of alphanumerics and these characters */
0131:            private static final String SCHEME_CHARACTERS = "+-.";
0132:
0133:            /** userinfo can be composed of unreserved, escaped and these
0134:                characters */
0135:            private static final String USERINFO_CHARACTERS = ";:&=+$,";
0136:
0137:            /** Stores the scheme (usually the protocol) for this URI. */
0138:            private String m_scheme = null;
0139:
0140:            /** If specified, stores the userinfo for this URI; otherwise null */
0141:            private String m_userinfo = null;
0142:
0143:            /** If specified, stores the host for this URI; otherwise null */
0144:            private String m_host = null;
0145:
0146:            /** If specified, stores the port for this URI; otherwise -1 */
0147:            private int m_port = -1;
0148:
0149:            /** If specified, stores the path for this URI; otherwise null */
0150:            private String m_path = null;
0151:
0152:            /** If specified, stores the query string for this URI; otherwise
0153:                null.  */
0154:            private String m_queryString = null;
0155:
0156:            /** If specified, stores the fragment for this URI; otherwise null */
0157:            private String m_fragment = null;
0158:
0159:            private static boolean DEBUG = false;
0160:
0161:            /**
0162:             * Construct a new and uninitialized URI.
0163:             */
0164:            public URI() {
0165:            }
0166:
0167:            /**
0168:             * Construct a new URI from another URI. All fields for this URI are
0169:             * set equal to the fields of the URI passed in.
0170:             *
0171:             * @param p_other the URI to copy (cannot be null)
0172:             */
0173:            public URI(URI p_other) {
0174:                initialize(p_other);
0175:            }
0176:
0177:            /**
0178:             * Construct a new URI from a URI specification string. If the
0179:             * specification follows the "generic URI" syntax, (two slashes
0180:             * following the first colon), the specification will be parsed
0181:             * accordingly - setting the scheme, userinfo, host,port, path, query
0182:             * string and fragment fields as necessary. If the specification does
0183:             * not follow the "generic URI" syntax, the specification is parsed
0184:             * into a scheme and scheme-specific part (stored as the path) only.
0185:             *
0186:             * @param p_uriSpec the URI specification string (cannot be null or
0187:             *                  empty)
0188:             *
0189:             * @exception MalformedURIException if p_uriSpec violates any syntax
0190:             *                                   rules
0191:             */
0192:            public URI(String p_uriSpec) throws MalformedURIException {
0193:                this ((URI) null, p_uriSpec);
0194:            }
0195:
0196:            /**
0197:             * Construct a new URI from a base URI and a URI specification string.
0198:             * The URI specification string may be a relative URI.
0199:             *
0200:             * @param p_base the base URI (cannot be null if p_uriSpec is null or
0201:             *               empty)
0202:             * @param p_uriSpec the URI specification string (cannot be null or
0203:             *                  empty if p_base is null)
0204:             *
0205:             * @exception MalformedURIException if p_uriSpec violates any syntax
0206:             *                                  rules
0207:             */
0208:            public URI(URI p_base, String p_uriSpec)
0209:                    throws MalformedURIException {
0210:                initialize(p_base, p_uriSpec);
0211:            }
0212:
0213:            /**
0214:             * Construct a new URI that does not follow the generic URI syntax.
0215:             * Only the scheme and scheme-specific part (stored as the path) are
0216:             * initialized.
0217:             *
0218:             * @param p_scheme the URI scheme (cannot be null or empty)
0219:             * @param p_schemeSpecificPart the scheme-specific part (cannot be
0220:             *                             null or empty)
0221:             *
0222:             * @exception MalformedURIException if p_scheme violates any
0223:             *                                  syntax rules
0224:             */
0225:            public URI(String p_scheme, String p_schemeSpecificPart)
0226:                    throws MalformedURIException {
0227:                if (p_scheme == null || p_scheme.trim().length() == 0) {
0228:                    throw new MalformedURIException(
0229:                            "Cannot construct URI with null/empty scheme!");
0230:                }
0231:                if (p_schemeSpecificPart == null
0232:                        || p_schemeSpecificPart.trim().length() == 0) {
0233:                    throw new MalformedURIException(
0234:                            "Cannot construct URI with null/empty scheme-specific part!");
0235:                }
0236:                setScheme(p_scheme);
0237:                setPath(p_schemeSpecificPart);
0238:            }
0239:
0240:            /**
0241:             * Construct a new URI that follows the generic URI syntax from its
0242:             * component parts. Each component is validated for syntax and some
0243:             * basic semantic checks are performed as well.  See the individual
0244:             * setter methods for specifics.
0245:             *
0246:             * @param p_scheme the URI scheme (cannot be null or empty)
0247:             * @param p_host the hostname or IPv4 address for the URI
0248:             * @param p_path the URI path - if the path contains '?' or '#',
0249:             *               then the query string and/or fragment will be
0250:             *               set from the path; however, if the query and
0251:             *               fragment are specified both in the path and as
0252:             *               separate parameters, an exception is thrown
0253:             * @param p_queryString the URI query string (cannot be specified
0254:             *                      if path is null)
0255:             * @param p_fragment the URI fragment (cannot be specified if path
0256:             *                   is null)
0257:             *
0258:             * @exception MalformedURIException if any of the parameters violates
0259:             *                                  syntax rules or semantic rules
0260:             */
0261:            public URI(String p_scheme, String p_host, String p_path,
0262:                    String p_queryString, String p_fragment)
0263:                    throws MalformedURIException {
0264:                this (p_scheme, null, p_host, -1, p_path, p_queryString,
0265:                        p_fragment);
0266:            }
0267:
0268:            /**
0269:             * Construct a new URI that follows the generic URI syntax from its
0270:             * component parts. Each component is validated for syntax and some
0271:             * basic semantic checks are performed as well.  See the individual
0272:             * setter methods for specifics.
0273:             *
0274:             * @param p_scheme the URI scheme (cannot be null or empty)
0275:             * @param p_userinfo the URI userinfo (cannot be specified if host
0276:             *                   is null)
0277:             * @param p_host the hostname or IPv4 address for the URI
0278:             * @param p_port the URI port (may be -1 for "unspecified"; cannot
0279:             *               be specified if host is null)
0280:             * @param p_path the URI path - if the path contains '?' or '#',
0281:             *               then the query string and/or fragment will be
0282:             *               set from the path; however, if the query and
0283:             *               fragment are specified both in the path and as
0284:             *               separate parameters, an exception is thrown
0285:             * @param p_queryString the URI query string (cannot be specified
0286:             *                      if path is null)
0287:             * @param p_fragment the URI fragment (cannot be specified if path
0288:             *                   is null)
0289:             *
0290:             * @exception MalformedURIException if any of the parameters violates
0291:             *                                  syntax rules or semantic rules
0292:             */
0293:            public URI(String p_scheme, String p_userinfo, String p_host,
0294:                    int p_port, String p_path, String p_queryString,
0295:                    String p_fragment) throws MalformedURIException {
0296:                if (p_scheme == null || p_scheme.trim().length() == 0) {
0297:                    throw new MalformedURIException("Scheme is required!");
0298:                }
0299:
0300:                if (p_host == null) {
0301:                    if (p_userinfo != null) {
0302:                        throw new MalformedURIException(
0303:                                "Userinfo may not be specified if host is not specified!");
0304:                    }
0305:                    if (p_port != -1) {
0306:                        throw new MalformedURIException(
0307:                                "Port may not be specified if host is not specified!");
0308:                    }
0309:                }
0310:
0311:                if (p_path != null) {
0312:                    if (p_path.indexOf('?') != -1 && p_queryString != null) {
0313:                        throw new MalformedURIException(
0314:                                "Query string cannot be specified in path and query string!");
0315:                    }
0316:
0317:                    if (p_path.indexOf('#') != -1 && p_fragment != null) {
0318:                        throw new MalformedURIException(
0319:                                "Fragment cannot be specified in both the path and fragment!");
0320:                    }
0321:                }
0322:
0323:                setScheme(p_scheme);
0324:                setHost(p_host);
0325:                setPort(p_port);
0326:                setUserinfo(p_userinfo);
0327:                setPath(p_path);
0328:                setQueryString(p_queryString);
0329:                setFragment(p_fragment);
0330:            }
0331:
0332:            /**
0333:             * Initialize all fields of this URI from another URI.
0334:             *
0335:             * @param p_other the URI to copy (cannot be null)
0336:             */
0337:            private void initialize(URI p_other) {
0338:                m_scheme = p_other.getScheme();
0339:                m_userinfo = p_other.getUserinfo();
0340:                m_host = p_other.getHost();
0341:                m_port = p_other.getPort();
0342:                m_path = p_other.getPath();
0343:                m_queryString = p_other.getQueryString();
0344:                m_fragment = p_other.getFragment();
0345:            }
0346:
0347:            /**
0348:             * Initializes this URI from a base URI and a URI specification string.
0349:             * See RFC 2396 Section 4 and Appendix B for specifications on parsing
0350:             * the URI and Section 5 for specifications on resolving relative URIs
0351:             * and relative paths.
0352:             *
0353:             * @param p_base the base URI (may be null if p_uriSpec is an absolute
0354:             *               URI)
0355:             * @param p_uriSpec the URI spec string which may be an absolute or
0356:             *                  relative URI (can only be null/empty if p_base
0357:             *                  is not null)
0358:             *
0359:             * @exception MalformedURIException if p_base is null and p_uriSpec
0360:             *                                  is not an absolute URI or if
0361:             *                                  p_uriSpec violates syntax rules
0362:             */
0363:            private void initialize(URI p_base, String p_uriSpec)
0364:                    throws MalformedURIException {
0365:                if (p_base == null
0366:                        && (p_uriSpec == null || p_uriSpec.trim().length() == 0)) {
0367:                    throw new MalformedURIException(
0368:                            "Cannot initialize URI with empty parameters.");
0369:                }
0370:
0371:                // just make a copy of the base if spec is empty
0372:                if (p_uriSpec == null || p_uriSpec.trim().length() == 0) {
0373:                    initialize(p_base);
0374:                    return;
0375:                }
0376:
0377:                String uriSpec = p_uriSpec.trim();
0378:                int uriSpecLen = uriSpec.length();
0379:                int index = 0;
0380:
0381:                // Check for scheme, which must be before '/', '?' or '#'. Also handle
0382:                // names with DOS drive letters ('D:'), so 1-character schemes are not
0383:                // allowed.
0384:                int colonIdx = uriSpec.indexOf(':');
0385:                int slashIdx = uriSpec.indexOf('/');
0386:                int queryIdx = uriSpec.indexOf('?');
0387:                int fragmentIdx = uriSpec.indexOf('#');
0388:
0389:                if ((colonIdx < 2) || (colonIdx > slashIdx && slashIdx != -1)
0390:                        || (colonIdx > queryIdx && queryIdx != -1)
0391:                        || (colonIdx > fragmentIdx && fragmentIdx != -1)) {
0392:                    // A standalone base is a valid URI according to spec
0393:                    if (p_base == null && fragmentIdx != 0) {
0394:                        throw new MalformedURIException(
0395:                                "No scheme found in URI.");
0396:                    }
0397:                } else {
0398:                    initializeScheme(uriSpec);
0399:                    index = m_scheme.length() + 1;
0400:                }
0401:
0402:                // two slashes means generic URI syntax, so we get the authority
0403:                if (((index + 1) < uriSpecLen)
0404:                        && (uriSpec.substring(index).startsWith("//"))) {
0405:                    index += 2;
0406:                    int startPos = index;
0407:
0408:                    // get authority - everything up to path, query or fragment
0409:                    char testChar = '\0';
0410:                    while (index < uriSpecLen) {
0411:                        testChar = uriSpec.charAt(index);
0412:                        if (testChar == '/' || testChar == '?'
0413:                                || testChar == '#') {
0414:                            break;
0415:                        }
0416:                        index++;
0417:                    }
0418:
0419:                    // if we found authority, parse it out, otherwise we set the
0420:                    // host to empty string
0421:                    if (index > startPos) {
0422:                        initializeAuthority(uriSpec.substring(startPos, index));
0423:                    } else {
0424:                        m_host = "";
0425:                    }
0426:                }
0427:
0428:                initializePath(uriSpec.substring(index));
0429:
0430:                // Resolve relative URI to base URI - see RFC 2396 Section 5.2
0431:                // In some cases, it might make more sense to throw an exception
0432:                // (when scheme is specified is the string spec and the base URI
0433:                // is also specified, for example), but we're just following the
0434:                // RFC specifications
0435:                if (p_base != null) {
0436:
0437:                    // check to see if this is the current doc - RFC 2396 5.2 #2
0438:                    // note that this is slightly different from the RFC spec in that
0439:                    // we don't include the check for query string being null
0440:                    // - this handles cases where the urispec is just a query
0441:                    // string or a fragment (e.g. "?y" or "#s") -
0442:                    // see <http://www.ics.uci.edu/~fielding/url/test1.html> which
0443:                    // identified this as a bug in the RFC
0444:                    if (m_path.length() == 0 && m_scheme == null
0445:                            && m_host == null) {
0446:                        m_scheme = p_base.getScheme();
0447:                        m_userinfo = p_base.getUserinfo();
0448:                        m_host = p_base.getHost();
0449:                        m_port = p_base.getPort();
0450:                        m_path = p_base.getPath();
0451:
0452:                        if (m_queryString == null) {
0453:                            m_queryString = p_base.getQueryString();
0454:                        }
0455:                        return;
0456:                    }
0457:
0458:                    // check for scheme - RFC 2396 5.2 #3
0459:                    // if we found a scheme, it means absolute URI, so we're done
0460:                    if (m_scheme == null) {
0461:                        m_scheme = p_base.getScheme();
0462:                    } else {
0463:                        return;
0464:                    }
0465:
0466:                    // check for authority - RFC 2396 5.2 #4
0467:                    // if we found a host, then we've got a network path, so we're done
0468:                    if (m_host == null) {
0469:                        m_userinfo = p_base.getUserinfo();
0470:                        m_host = p_base.getHost();
0471:                        m_port = p_base.getPort();
0472:                    } else {
0473:                        return;
0474:                    }
0475:
0476:                    // check for absolute path - RFC 2396 5.2 #5
0477:                    if (m_path.length() > 0 && m_path.startsWith("/")) {
0478:                        return;
0479:                    }
0480:
0481:                    // if we get to this point, we need to resolve relative path
0482:                    // RFC 2396 5.2 #6
0483:                    String path = new String();
0484:                    String basePath = p_base.getPath();
0485:
0486:                    // 6a - get all but the last segment of the base URI path
0487:                    if (basePath != null) {
0488:                        int lastSlash = basePath.lastIndexOf('/');
0489:                        if (lastSlash != -1) {
0490:                            path = basePath.substring(0, lastSlash + 1);
0491:                        }
0492:                    }
0493:
0494:                    // 6b - append the relative URI path
0495:                    path = path.concat(m_path);
0496:
0497:                    // 6c - remove all "./" where "." is a complete path segment
0498:                    index = -1;
0499:                    while ((index = path.indexOf("/./")) != -1) {
0500:                        path = path.substring(0, index + 1).concat(
0501:                                path.substring(index + 3));
0502:                    }
0503:
0504:                    // 6d - remove "." if path ends with "." as a complete path segment
0505:                    if (path.endsWith("/.")) {
0506:                        path = path.substring(0, path.length() - 1);
0507:                    }
0508:
0509:                    // 6e - remove all "<segment>/../" where "<segment>" is a complete
0510:                    // path segment not equal to ".."
0511:                    index = 1;
0512:                    int segIndex = -1;
0513:
0514:                    while ((index = path.indexOf("/../", index)) > 0) {
0515:                        segIndex = path.lastIndexOf('/', index - 1);
0516:                        if (segIndex != -1
0517:                                && !path.substring(segIndex + 1, index).equals(
0518:                                        "..")) {
0519:                            path = path.substring(0, segIndex).concat(
0520:                                    path.substring(index + 3));
0521:                            index = segIndex;
0522:                        } else {
0523:                            index += 4;
0524:                        }
0525:                    }
0526:
0527:                    // 6f - remove ending "<segment>/.." where "<segment>" is a
0528:                    // complete path segment
0529:                    if (path.endsWith("/..")) {
0530:                        index = path.length() - 3;
0531:                        segIndex = path.lastIndexOf('/', index - 1);
0532:                        if (segIndex != -1
0533:                                && !path.substring(segIndex + 1, index).equals(
0534:                                        "..")) {
0535:                            path = path.substring(0, segIndex + 1);
0536:                        }
0537:                    }
0538:
0539:                    m_path = path;
0540:                }
0541:            }
0542:
0543:            /**
0544:             * Initialize the scheme for this URI from a URI string spec.
0545:             *
0546:             * @param p_uriSpec the URI specification (cannot be null)
0547:             *
0548:             * @exception MalformedURIException if URI does not have a conformant
0549:             *                                  scheme
0550:             */
0551:            private void initializeScheme(String p_uriSpec)
0552:                    throws MalformedURIException {
0553:                int uriSpecLen = p_uriSpec.length();
0554:                int index = 0;
0555:                String scheme = null;
0556:                char testChar = '\0';
0557:
0558:                while (index < uriSpecLen) {
0559:                    testChar = p_uriSpec.charAt(index);
0560:                    if (testChar == ':' || testChar == '/' || testChar == '?'
0561:                            || testChar == '#') {
0562:                        break;
0563:                    }
0564:                    index++;
0565:                }
0566:                scheme = p_uriSpec.substring(0, index);
0567:
0568:                if (scheme.length() == 0) {
0569:                    throw new MalformedURIException("No scheme found in URI.");
0570:                } else {
0571:                    setScheme(scheme);
0572:                }
0573:            }
0574:
0575:            /**
0576:             * Initialize the authority (userinfo, host and port) for this
0577:             * URI from a URI string spec.
0578:             *
0579:             * @param p_uriSpec the URI specification (cannot be null)
0580:             *
0581:             * @exception MalformedURIException if p_uriSpec violates syntax rules
0582:             */
0583:            private void initializeAuthority(String p_uriSpec)
0584:                    throws MalformedURIException {
0585:                int index = 0;
0586:                int start = 0;
0587:                int end = p_uriSpec.length();
0588:                char testChar = '\0';
0589:                String userinfo = null;
0590:
0591:                // userinfo is everything up @
0592:                if (p_uriSpec.indexOf('@', start) != -1) {
0593:                    while (index < end) {
0594:                        testChar = p_uriSpec.charAt(index);
0595:                        if (testChar == '@') {
0596:                            break;
0597:                        }
0598:                        index++;
0599:                    }
0600:                    userinfo = p_uriSpec.substring(start, index);
0601:                    index++;
0602:                }
0603:
0604:                // host is everything up to ':'
0605:                String host = null;
0606:                start = index;
0607:                while (index < end) {
0608:                    testChar = p_uriSpec.charAt(index);
0609:                    if (testChar == ':') {
0610:                        break;
0611:                    }
0612:                    index++;
0613:                }
0614:                host = p_uriSpec.substring(start, index);
0615:                int port = -1;
0616:                if (host.length() > 0) {
0617:                    // port
0618:                    if (testChar == ':') {
0619:                        index++;
0620:                        start = index;
0621:                        while (index < end) {
0622:                            index++;
0623:                        }
0624:                        String portStr = p_uriSpec.substring(start, index);
0625:                        if (portStr.length() > 0) {
0626:                            for (int i = 0; i < portStr.length(); i++) {
0627:                                if (!isDigit(portStr.charAt(i))) {
0628:                                    throw new MalformedURIException(
0629:                                            portStr
0630:                                                    + " is invalid. Port should only contain digits!");
0631:                                }
0632:                            }
0633:                            try {
0634:                                port = Integer.parseInt(portStr);
0635:                            } catch (NumberFormatException nfe) {
0636:                                // can't happen
0637:                            }
0638:                        }
0639:                    }
0640:                }
0641:                setHost(host);
0642:                setPort(port);
0643:                setUserinfo(userinfo);
0644:            }
0645:
0646:            /**
0647:             * Initialize the path for this URI from a URI string spec.
0648:             *
0649:             * @param p_uriSpec the URI specification (cannot be null)
0650:             *
0651:             * @exception MalformedURIException if p_uriSpec violates syntax rules
0652:             */
0653:            private void initializePath(String p_uriSpec)
0654:                    throws MalformedURIException {
0655:                if (p_uriSpec == null) {
0656:                    throw new MalformedURIException(
0657:                            "Cannot initialize path from null string!");
0658:                }
0659:
0660:                int index = 0;
0661:                int start = 0;
0662:                int end = p_uriSpec.length();
0663:                char testChar = '\0';
0664:
0665:                // path - everything up to query string or fragment
0666:                while (index < end) {
0667:                    testChar = p_uriSpec.charAt(index);
0668:                    if (testChar == '?' || testChar == '#') {
0669:                        break;
0670:                    }
0671:                    // check for valid escape sequence
0672:                    if (testChar == '%') {
0673:                        if (index + 2 >= end
0674:                                || !isHex(p_uriSpec.charAt(index + 1))
0675:                                || !isHex(p_uriSpec.charAt(index + 2))) {
0676:                            throw new MalformedURIException(
0677:                                    "Path contains invalid escape sequence!");
0678:                        }
0679:                    } else if (!isReservedCharacter(testChar)
0680:                            && !isUnreservedCharacter(testChar)) {
0681:                        throw new MalformedURIException(
0682:                                "Path contains invalid character: " + testChar);
0683:                    }
0684:                    index++;
0685:                }
0686:                m_path = p_uriSpec.substring(start, index);
0687:
0688:                // query - starts with ? and up to fragment or end
0689:                if (testChar == '?') {
0690:                    index++;
0691:                    start = index;
0692:                    while (index < end) {
0693:                        testChar = p_uriSpec.charAt(index);
0694:                        if (testChar == '#') {
0695:                            break;
0696:                        }
0697:                        if (testChar == '%') {
0698:                            if (index + 2 >= end
0699:                                    || !isHex(p_uriSpec.charAt(index + 1))
0700:                                    || !isHex(p_uriSpec.charAt(index + 2))) {
0701:                                throw new MalformedURIException(
0702:                                        "Query string contains invalid escape sequence!");
0703:                            }
0704:                        } else if (!isReservedCharacter(testChar)
0705:                                && !isUnreservedCharacter(testChar)) {
0706:                            throw new MalformedURIException(
0707:                                    "Query string contains invalid character:"
0708:                                            + testChar);
0709:                        }
0710:                        index++;
0711:                    }
0712:                    m_queryString = p_uriSpec.substring(start, index);
0713:                }
0714:
0715:                // fragment - starts with #
0716:                if (testChar == '#') {
0717:                    index++;
0718:                    start = index;
0719:                    while (index < end) {
0720:                        testChar = p_uriSpec.charAt(index);
0721:
0722:                        if (testChar == '%') {
0723:                            if (index + 2 >= end
0724:                                    || !isHex(p_uriSpec.charAt(index + 1))
0725:                                    || !isHex(p_uriSpec.charAt(index + 2))) {
0726:                                throw new MalformedURIException(
0727:                                        "Fragment contains invalid escape sequence!");
0728:                            }
0729:                        } else if (!isReservedCharacter(testChar)
0730:                                && !isUnreservedCharacter(testChar)) {
0731:                            throw new MalformedURIException(
0732:                                    "Fragment contains invalid character:"
0733:                                            + testChar);
0734:                        }
0735:                        index++;
0736:                    }
0737:                    m_fragment = p_uriSpec.substring(start, index);
0738:                }
0739:            }
0740:
0741:            /**
0742:             * Get the scheme for this URI.
0743:             *
0744:             * @return the scheme for this URI
0745:             */
0746:            public String getScheme() {
0747:                return m_scheme;
0748:            }
0749:
0750:            /**
0751:             * Get the scheme-specific part for this URI (everything following the
0752:             * scheme and the first colon). See RFC 2396 Section 5.2 for spec.
0753:             *
0754:             * @return the scheme-specific part for this URI
0755:             */
0756:            public String getSchemeSpecificPart() {
0757:                StringBuffer schemespec = new StringBuffer();
0758:
0759:                if (m_userinfo != null || m_host != null || m_port != -1) {
0760:                    schemespec.append("//");
0761:                }
0762:
0763:                if (m_userinfo != null) {
0764:                    schemespec.append(m_userinfo);
0765:                    schemespec.append('@');
0766:                }
0767:
0768:                if (m_host != null) {
0769:                    schemespec.append(m_host);
0770:                }
0771:
0772:                if (m_port != -1) {
0773:                    schemespec.append(':');
0774:                    schemespec.append(m_port);
0775:                }
0776:
0777:                if (m_path != null) {
0778:                    schemespec.append((m_path));
0779:                }
0780:
0781:                if (m_queryString != null) {
0782:                    schemespec.append('?');
0783:                    schemespec.append(m_queryString);
0784:                }
0785:
0786:                if (m_fragment != null) {
0787:                    schemespec.append('#');
0788:                    schemespec.append(m_fragment);
0789:                }
0790:
0791:                return schemespec.toString();
0792:            }
0793:
0794:            /**
0795:             * Get the userinfo for this URI.
0796:             *
0797:             * @return the userinfo for this URI (null if not specified).
0798:             */
0799:            public String getUserinfo() {
0800:                return m_userinfo;
0801:            }
0802:
0803:            /**
0804:             * Get the host for this URI.
0805:             *
0806:             * @return the host for this URI (null if not specified).
0807:             */
0808:            public String getHost() {
0809:                return m_host;
0810:            }
0811:
0812:            /**
0813:             * Get the port for this URI.
0814:             *
0815:             * @return the port for this URI (-1 if not specified).
0816:             */
0817:            public int getPort() {
0818:                return m_port;
0819:            }
0820:
0821:            /**
0822:             * Get the path for this URI (optionally with the query string and
0823:             * fragment).
0824:             *
0825:             * @param p_includeQueryString if true (and query string is not null),
0826:             *                             then a "?" followed by the query string
0827:             *                             will be appended
0828:             * @param p_includeFragment if true (and fragment is not null),
0829:             *                             then a "#" followed by the fragment
0830:             *                             will be appended
0831:             *
0832:             * @return the path for this URI possibly including the query string
0833:             *         and fragment
0834:             */
0835:            public String getPath(boolean p_includeQueryString,
0836:                    boolean p_includeFragment) {
0837:                StringBuffer pathString = new StringBuffer(m_path);
0838:
0839:                if (p_includeQueryString && m_queryString != null) {
0840:                    pathString.append('?');
0841:                    pathString.append(m_queryString);
0842:                }
0843:
0844:                if (p_includeFragment && m_fragment != null) {
0845:                    pathString.append('#');
0846:                    pathString.append(m_fragment);
0847:                }
0848:                return pathString.toString();
0849:            }
0850:
0851:            /**
0852:             * Get the path for this URI. Note that the value returned is the path
0853:             * only and does not include the query string or fragment.
0854:             *
0855:             * @return the path for this URI.
0856:             */
0857:            public String getPath() {
0858:                return m_path;
0859:            }
0860:
0861:            /**
0862:             * Get the query string for this URI.
0863:             *
0864:             * @return the query string for this URI. Null is returned if there
0865:             *         was no "?" in the URI spec, empty string if there was a
0866:             *         "?" but no query string following it.
0867:             */
0868:            public String getQueryString() {
0869:                return m_queryString;
0870:            }
0871:
0872:            /**
0873:             * Get the fragment for this URI.
0874:             *
0875:             * @return the fragment for this URI. Null is returned if there
0876:             *         was no "#" in the URI spec, empty string if there was a
0877:             *         "#" but no fragment following it.
0878:             */
0879:            public String getFragment() {
0880:                return m_fragment;
0881:            }
0882:
0883:            /**
0884:             * Set the scheme for this URI. The scheme is converted to lowercase
0885:             * before it is set.
0886:             *
0887:             * @param p_scheme the scheme for this URI (cannot be null)
0888:             *
0889:             * @exception MalformedURIException if p_scheme is not a conformant
0890:             *                                  scheme name
0891:             */
0892:            public void setScheme(String p_scheme) throws MalformedURIException {
0893:                if (p_scheme == null) {
0894:                    throw new MalformedURIException(
0895:                            "Cannot set scheme from null string!");
0896:                }
0897:                if (!isConformantSchemeName(p_scheme)) {
0898:                    throw new MalformedURIException(
0899:                            "The scheme is not conformant.");
0900:                }
0901:
0902:                m_scheme = p_scheme.toLowerCase();
0903:            }
0904:
0905:            /**
0906:             * Set the userinfo for this URI. If a non-null value is passed in and
0907:             * the host value is null, then an exception is thrown.
0908:             *
0909:             * @param p_userinfo the userinfo for this URI
0910:             *
0911:             * @exception MalformedURIException if p_userinfo contains invalid
0912:             *                                  characters
0913:             */
0914:            public void setUserinfo(String p_userinfo)
0915:                    throws MalformedURIException {
0916:                if (p_userinfo == null) {
0917:                    m_userinfo = null;
0918:                } else {
0919:                    if (m_host == null) {
0920:                        throw new MalformedURIException(
0921:                                "Userinfo cannot be set when host is null!");
0922:                    }
0923:
0924:                    // userinfo can contain alphanumerics, mark characters, escaped
0925:                    // and ';',':','&','=','+','$',','
0926:                    int index = 0;
0927:                    int end = p_userinfo.length();
0928:                    char testChar = '\0';
0929:                    while (index < end) {
0930:                        testChar = p_userinfo.charAt(index);
0931:                        if (testChar == '%') {
0932:                            if (index + 2 >= end
0933:                                    || !isHex(p_userinfo.charAt(index + 1))
0934:                                    || !isHex(p_userinfo.charAt(index + 2))) {
0935:                                throw new MalformedURIException(
0936:                                        "Userinfo contains invalid escape sequence!");
0937:                            }
0938:                        } else if (!isUnreservedCharacter(testChar)
0939:                                && USERINFO_CHARACTERS.indexOf(testChar) == -1) {
0940:                            throw new MalformedURIException(
0941:                                    "Userinfo contains invalid character:"
0942:                                            + testChar);
0943:                        }
0944:                        index++;
0945:                    }
0946:                }
0947:                m_userinfo = p_userinfo;
0948:            }
0949:
0950:            /**
0951:             * Set the host for this URI. If null is passed in, the userinfo
0952:             * field is also set to null and the port is set to -1.
0953:             *
0954:             * @param p_host the host for this URI
0955:             *
0956:             * @exception MalformedURIException if p_host is not a valid IP
0957:             *                                  address or DNS hostname.
0958:             */
0959:            public void setHost(String p_host) throws MalformedURIException {
0960:                if (p_host == null || p_host.trim().length() == 0) {
0961:                    m_host = p_host;
0962:                    m_userinfo = null;
0963:                    m_port = -1;
0964:                } else if (!isWellFormedAddress(p_host)) {
0965:                    throw new MalformedURIException(
0966:                            "Host is not a well formed address!");
0967:                }
0968:                m_host = p_host;
0969:            }
0970:
0971:            /**
0972:             * Set the port for this URI. -1 is used to indicate that the port is
0973:             * not specified, otherwise valid port numbers are  between 0 and 65535.
0974:             * If a valid port number is passed in and the host field is null,
0975:             * an exception is thrown.
0976:             *
0977:             * @param p_port the port number for this URI
0978:             *
0979:             * @exception MalformedURIException if p_port is not -1 and not a
0980:             *                                  valid port number
0981:             */
0982:            public void setPort(int p_port) throws MalformedURIException {
0983:                if (p_port >= 0 && p_port <= 65535) {
0984:                    if (m_host == null) {
0985:                        throw new MalformedURIException(
0986:                                "Port cannot be set when host is null!");
0987:                    }
0988:                } else if (p_port != -1) {
0989:                    throw new MalformedURIException("Invalid port number!");
0990:                }
0991:                m_port = p_port;
0992:            }
0993:
0994:            /**
0995:             * Set the path for this URI. If the supplied path is null, then the
0996:             * query string and fragment are set to null as well. If the supplied
0997:             * path includes a query string and/or fragment, these fields will be
0998:             * parsed and set as well. Note that, for URIs following the "generic
0999:             * URI" syntax, the path specified should start with a slash.
1000:             * For URIs that do not follow the generic URI syntax, this method
1001:             * sets the scheme-specific part.
1002:             *
1003:             * @param p_path the path for this URI (may be null)
1004:             *
1005:             * @exception MalformedURIException if p_path contains invalid
1006:             *                                  characters
1007:             */
1008:            public void setPath(String p_path) throws MalformedURIException {
1009:                if (p_path == null) {
1010:                    m_path = null;
1011:                    m_queryString = null;
1012:                    m_fragment = null;
1013:                } else {
1014:                    initializePath(p_path);
1015:                }
1016:            }
1017:
1018:            /**
1019:             * Append to the end of the path of this URI. If the current path does
1020:             * not end in a slash and the path to be appended does not begin with
1021:             * a slash, a slash will be appended to the current path before the
1022:             * new segment is added. Also, if the current path ends in a slash
1023:             * and the new segment begins with a slash, the extra slash will be
1024:             * removed before the new segment is appended.
1025:             *
1026:             * @param p_addToPath the new segment to be added to the current path
1027:             *
1028:             * @exception MalformedURIException if p_addToPath contains syntax
1029:             *                                  errors
1030:             */
1031:            public void appendPath(String p_addToPath)
1032:                    throws MalformedURIException {
1033:                if (p_addToPath == null || p_addToPath.trim().length() == 0) {
1034:                    return;
1035:                }
1036:
1037:                if (!isURIString(p_addToPath)) {
1038:                    throw new MalformedURIException(
1039:                            "Path contains invalid character!");
1040:                }
1041:
1042:                if (m_path == null || m_path.trim().length() == 0) {
1043:                    if (p_addToPath.startsWith("/")) {
1044:                        m_path = p_addToPath;
1045:                    } else {
1046:                        m_path = "/" + p_addToPath;
1047:                    }
1048:                } else if (m_path.endsWith("/")) {
1049:                    if (p_addToPath.startsWith("/")) {
1050:                        m_path = m_path.concat(p_addToPath.substring(1));
1051:                    } else {
1052:                        m_path = m_path.concat(p_addToPath);
1053:                    }
1054:                } else {
1055:                    if (p_addToPath.startsWith("/")) {
1056:                        m_path = m_path.concat(p_addToPath);
1057:                    } else {
1058:                        m_path = m_path.concat("/" + p_addToPath);
1059:                    }
1060:                }
1061:            }
1062:
1063:            /**
1064:             * Set the query string for this URI. A non-null value is valid only
1065:             * if this is an URI conforming to the generic URI syntax and
1066:             * the path value is not null.
1067:             *
1068:             * @param p_queryString the query string for this URI
1069:             *
1070:             * @exception MalformedURIException if p_queryString is not null and this
1071:             *                                  URI does not conform to the generic
1072:             *                                  URI syntax or if the path is null
1073:             */
1074:            public void setQueryString(String p_queryString)
1075:                    throws MalformedURIException {
1076:                if (p_queryString == null) {
1077:                    m_queryString = null;
1078:                } else if (!isGenericURI()) {
1079:                    throw new MalformedURIException(
1080:                            "Query string can only be set for a generic URI!");
1081:                } else if (getPath() == null) {
1082:                    throw new MalformedURIException(
1083:                            "Query string cannot be set when path is null!");
1084:                } else if (!isURIString(p_queryString)) {
1085:                    throw new MalformedURIException(
1086:                            "Query string contains invalid character!");
1087:                } else {
1088:                    m_queryString = p_queryString;
1089:                }
1090:            }
1091:
1092:            /**
1093:             * Set the fragment for this URI. A non-null value is valid only
1094:             * if this is a URI conforming to the generic URI syntax and
1095:             * the path value is not null.
1096:             *
1097:             * @param p_fragment the fragment for this URI
1098:             *
1099:             * @exception MalformedURIException if p_fragment is not null and this
1100:             *                                  URI does not conform to the generic
1101:             *                                  URI syntax or if the path is null
1102:             */
1103:            public void setFragment(String p_fragment)
1104:                    throws MalformedURIException {
1105:                if (p_fragment == null) {
1106:                    m_fragment = null;
1107:                } else if (!isGenericURI()) {
1108:                    throw new MalformedURIException(
1109:                            "Fragment can only be set for a generic URI!");
1110:                } else if (getPath() == null) {
1111:                    throw new MalformedURIException(
1112:                            "Fragment cannot be set when path is null!");
1113:                } else if (!isURIString(p_fragment)) {
1114:                    throw new MalformedURIException(
1115:                            "Fragment contains invalid character!");
1116:                } else {
1117:                    m_fragment = p_fragment;
1118:                }
1119:            }
1120:
1121:            /**
1122:             * Determines if the passed-in Object is equivalent to this URI.
1123:             *
1124:             * @param p_test the Object to test for equality.
1125:             *
1126:             * @return true if p_test is a URI with all values equal to this
1127:             *         URI, false otherwise
1128:             */
1129:            public boolean equals(Object p_test) {
1130:                if (p_test instanceof  URI) {
1131:                    URI testURI = (URI) p_test;
1132:                    if (((m_scheme == null && testURI.m_scheme == null) || (m_scheme != null
1133:                            && testURI.m_scheme != null && m_scheme
1134:                            .equals(testURI.m_scheme)))
1135:                            && ((m_userinfo == null && testURI.m_userinfo == null) || (m_userinfo != null
1136:                                    && testURI.m_userinfo != null && m_userinfo
1137:                                    .equals(testURI.m_userinfo)))
1138:                            && ((m_host == null && testURI.m_host == null) || (m_host != null
1139:                                    && testURI.m_host != null && m_host
1140:                                    .equals(testURI.m_host)))
1141:                            && m_port == testURI.m_port
1142:                            && ((m_path == null && testURI.m_path == null) || (m_path != null
1143:                                    && testURI.m_path != null && m_path
1144:                                    .equals(testURI.m_path)))
1145:                            && ((m_queryString == null && testURI.m_queryString == null) || (m_queryString != null
1146:                                    && testURI.m_queryString != null && m_queryString
1147:                                    .equals(testURI.m_queryString)))
1148:                            && ((m_fragment == null && testURI.m_fragment == null) || (m_fragment != null
1149:                                    && testURI.m_fragment != null && m_fragment
1150:                                    .equals(testURI.m_fragment)))) {
1151:                        return true;
1152:                    }
1153:                }
1154:                return false;
1155:            }
1156:
1157:            /**
1158:             * Get the URI as a string specification. See RFC 2396 Section 5.2.
1159:             *
1160:             * @return the URI string specification
1161:             */
1162:            public String toString() {
1163:                StringBuffer uriSpecString = new StringBuffer();
1164:
1165:                if (m_scheme != null) {
1166:                    uriSpecString.append(m_scheme);
1167:                    uriSpecString.append(':');
1168:                }
1169:                uriSpecString.append(getSchemeSpecificPart());
1170:                return uriSpecString.toString();
1171:            }
1172:
1173:            /**
1174:             * Get the indicator as to whether this URI uses the "generic URI"
1175:             * syntax.
1176:             *
1177:             * @return true if this URI uses the "generic URI" syntax, false
1178:             *         otherwise
1179:             */
1180:            public boolean isGenericURI() {
1181:                // presence of the host (whether valid or empty) means
1182:                // double-slashes which means generic uri
1183:                return (m_host != null);
1184:            }
1185:
1186:            /**
1187:             * Determine whether a scheme conforms to the rules for a scheme name.
1188:             * A scheme is conformant if it starts with an alphanumeric, and
1189:             * contains only alphanumerics, '+','-' and '.'.
1190:             *
1191:             * @return true if the scheme is conformant, false otherwise
1192:             */
1193:            public static boolean isConformantSchemeName(String p_scheme) {
1194:                if (p_scheme == null || p_scheme.trim().length() == 0) {
1195:                    return false;
1196:                }
1197:
1198:                if (!isAlpha(p_scheme.charAt(0))) {
1199:                    return false;
1200:                }
1201:
1202:                char testChar;
1203:                for (int i = 1; i < p_scheme.length(); i++) {
1204:                    testChar = p_scheme.charAt(i);
1205:                    if (!isAlphanum(testChar)
1206:                            && SCHEME_CHARACTERS.indexOf(testChar) == -1) {
1207:                        return false;
1208:                    }
1209:                }
1210:
1211:                return true;
1212:            }
1213:
1214:            /**
1215:             * Determine whether a string is syntactically capable of representing
1216:             * a valid IPv4 address or the domain name of a network host. A valid
1217:             * IPv4 address consists of four decimal digit groups separated by a
1218:             * '.'. A hostname consists of domain labels (each of which must
1219:             * begin and end with an alphanumeric but may contain '-') separated
1220:             & by a '.'. See RFC 2396 Section 3.2.2.
1221:             *
1222:             * @return true if the string is a syntactically valid IPv4 address
1223:             *              or hostname
1224:             */
1225:            public static boolean isWellFormedAddress(String p_address) {
1226:                if (p_address == null) {
1227:                    return false;
1228:                }
1229:
1230:                String address = p_address.trim();
1231:                int addrLength = address.length();
1232:                if (addrLength == 0 || addrLength > 255) {
1233:                    return false;
1234:                }
1235:
1236:                if (address.startsWith(".") || address.startsWith("-")) {
1237:                    return false;
1238:                }
1239:
1240:                // rightmost domain label starting with digit indicates IP address
1241:                // since top level domain label can only start with an alpha
1242:                // see RFC 2396 Section 3.2.2
1243:                int index = address.lastIndexOf('.');
1244:                if (address.endsWith(".")) {
1245:                    index = address.substring(0, index).lastIndexOf('.');
1246:                }
1247:
1248:                if (index + 1 < addrLength
1249:                        && isDigit(p_address.charAt(index + 1))) {
1250:                    char testChar;
1251:                    int numDots = 0;
1252:
1253:                    // make sure that 1) we see only digits and dot separators, 2) that
1254:                    // any dot separator is preceded and followed by a digit and
1255:                    // 3) that we find 3 dots
1256:                    for (int i = 0; i < addrLength; i++) {
1257:                        testChar = address.charAt(i);
1258:                        if (testChar == '.') {
1259:                            if (!isDigit(address.charAt(i - 1))
1260:                                    || (i + 1 < addrLength && !isDigit(address
1261:                                            .charAt(i + 1)))) {
1262:                                return false;
1263:                            }
1264:                            numDots++;
1265:                        } else if (!isDigit(testChar)) {
1266:                            return false;
1267:                        }
1268:                    }
1269:                    if (numDots != 3) {
1270:                        return false;
1271:                    }
1272:                } else {
1273:                    // domain labels can contain alphanumerics and '-"
1274:                    // but must start and end with an alphanumeric
1275:                    char testChar;
1276:
1277:                    for (int i = 0; i < addrLength; i++) {
1278:                        testChar = address.charAt(i);
1279:                        if (testChar == '.') {
1280:                            if (!isAlphanum(address.charAt(i - 1))) {
1281:                                return false;
1282:                            }
1283:                            if (i + 1 < addrLength
1284:                                    && !isAlphanum(address.charAt(i + 1))) {
1285:                                return false;
1286:                            }
1287:                        } else if (!isAlphanum(testChar) && testChar != '-') {
1288:                            return false;
1289:                        }
1290:                    }
1291:                }
1292:                return true;
1293:            }
1294:
1295:            /**
1296:             * Determine whether a char is a digit.
1297:             *
1298:             * @return true if the char is betweeen '0' and '9', false otherwise
1299:             */
1300:            private static boolean isDigit(char p_char) {
1301:                return p_char >= '0' && p_char <= '9';
1302:            }
1303:
1304:            /**
1305:             * Determine whether a character is a hexadecimal character.
1306:             *
1307:             * @return true if the char is betweeen '0' and '9', 'a' and 'f'
1308:             *         or 'A' and 'F', false otherwise
1309:             */
1310:            private static boolean isHex(char p_char) {
1311:                return (isDigit(p_char) || (p_char >= 'a' && p_char <= 'f') || (p_char >= 'A' && p_char <= 'F'));
1312:            }
1313:
1314:            /**
1315:             * Determine whether a char is an alphabetic character: a-z or A-Z
1316:             *
1317:             * @return true if the char is alphabetic, false otherwise
1318:             */
1319:            private static boolean isAlpha(char p_char) {
1320:                return ((p_char >= 'a' && p_char <= 'z') || (p_char >= 'A' && p_char <= 'Z'));
1321:            }
1322:
1323:            /**
1324:             * Determine whether a char is an alphanumeric: 0-9, a-z or A-Z
1325:             *
1326:             * @return true if the char is alphanumeric, false otherwise
1327:             */
1328:            private static boolean isAlphanum(char p_char) {
1329:                return (isAlpha(p_char) || isDigit(p_char));
1330:            }
1331:
1332:            /**
1333:             * Determine whether a character is a reserved character:
1334:             * ';', '/', '?', ':', '@', '&', '=', '+', '$' or ','
1335:             *
1336:             * @return true if the string contains any reserved characters
1337:             */
1338:            private static boolean isReservedCharacter(char p_char) {
1339:                return RESERVED_CHARACTERS.indexOf(p_char) != -1;
1340:            }
1341:
1342:            /**
1343:             * Determine whether a char is an unreserved character.
1344:             *
1345:             * @return true if the char is unreserved, false otherwise
1346:             */
1347:            private static boolean isUnreservedCharacter(char p_char) {
1348:                return (isAlphanum(p_char) || MARK_CHARACTERS.indexOf(p_char) != -1);
1349:            }
1350:
1351:            /**
1352:             * Determine whether a given string contains only URI characters (also
1353:             * called "uric" in RFC 2396). uric consist of all reserved
1354:             * characters, unreserved characters and escaped characters.
1355:             *
1356:             * @return true if the string is comprised of uric, false otherwise
1357:             */
1358:            private static boolean isURIString(String p_uric) {
1359:                if (p_uric == null) {
1360:                    return false;
1361:                }
1362:                int end = p_uric.length();
1363:                char testChar = '\0';
1364:                for (int i = 0; i < end; i++) {
1365:                    testChar = p_uric.charAt(i);
1366:                    if (testChar == '%') {
1367:                        if (i + 2 >= end || !isHex(p_uric.charAt(i + 1))
1368:                                || !isHex(p_uric.charAt(i + 2))) {
1369:                            return false;
1370:                        } else {
1371:                            i += 2;
1372:                            continue;
1373:                        }
1374:                    }
1375:                    if (isReservedCharacter(testChar)
1376:                            || isUnreservedCharacter(testChar)) {
1377:                        continue;
1378:                    } else {
1379:                        return false;
1380:                    }
1381:                }
1382:                return true;
1383:            }
1384:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.