Source Code Cross Referenced for URI.java in  » XML » xalan » org » apache » xml » 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 » XML » xalan » org.apache.xml.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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