1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xml.utils;
  58. import java.io.IOException;
  59. import java.io.Serializable;
  60. import org.apache.xalan.res.XSLTErrorResources;
  61. import org.apache.xalan.res.XSLMessages;
  62. /**
  63. * A class to represent a Uniform Resource Identifier (URI). This class
  64. * is designed to handle the parsing of URIs and provide access to
  65. * the various components (scheme, host, port, userinfo, path, query
  66. * string and fragment) that may constitute a URI.
  67. * <p>
  68. * Parsing of a URI specification is done according to the URI
  69. * syntax described in RFC 2396
  70. * <http://www.ietf.org/rfc/rfc2396.txt?number=2396>. Every URI consists
  71. * of a scheme, followed by a colon (':'), followed by a scheme-specific
  72. * part. For URIs that follow the "generic URI" syntax, the scheme-
  73. * specific part begins with two slashes ("//") and may be followed
  74. * by an authority segment (comprised of user information, host, and
  75. * port), path segment, query segment and fragment. Note that RFC 2396
  76. * no longer specifies the use of the parameters segment and excludes
  77. * the "user:password" syntax as part of the authority segment. If
  78. * "user:password" appears in a URI, the entire user/password string
  79. * is stored as userinfo.
  80. * <p>
  81. * For URIs that do not follow the "generic URI" syntax (e.g. mailto),
  82. * the entire scheme-specific part is treated as the "path" portion
  83. * of the URI.
  84. * <p>
  85. * Note that, unlike the java.net.URL class, this class does not provide
  86. * any built-in network access functionality nor does it provide any
  87. * scheme-specific functionality (for example, it does not know a
  88. * default port for a specific scheme). Rather, it only knows the
  89. * grammar and basic set of operations that can be applied to a URI.
  90. *
  91. *
  92. */
  93. public class URI implements Serializable
  94. {
  95. /**
  96. * MalformedURIExceptions are thrown in the process of building a URI
  97. * or setting fields on a URI when an operation would result in an
  98. * invalid URI specification.
  99. *
  100. */
  101. public static class MalformedURIException extends IOException
  102. {
  103. /**
  104. * Constructs a <code>MalformedURIException</code> with no specified
  105. * detail message.
  106. */
  107. public MalformedURIException()
  108. {
  109. super();
  110. }
  111. /**
  112. * Constructs a <code>MalformedURIException</code> with the
  113. * specified detail message.
  114. *
  115. * @param p_msg the detail message.
  116. */
  117. public MalformedURIException(String p_msg)
  118. {
  119. super(p_msg);
  120. }
  121. }
  122. /** reserved characters */
  123. private static final String RESERVED_CHARACTERS = ";/?:@&=+$,";
  124. /**
  125. * URI punctuation mark characters - these, combined with
  126. * alphanumerics, constitute the "unreserved" characters
  127. */
  128. private static final String MARK_CHARACTERS = "-_.!~*'() ";
  129. /** scheme can be composed of alphanumerics and these characters */
  130. private static final String SCHEME_CHARACTERS = "+-.";
  131. /**
  132. * userinfo can be composed of unreserved, escaped and these
  133. * characters
  134. */
  135. private static final String USERINFO_CHARACTERS = ";:&=+$,";
  136. /** Stores the scheme (usually the protocol) for this URI.
  137. * @serial */
  138. private String m_scheme = null;
  139. /** If specified, stores the userinfo for this URI; otherwise null.
  140. * @serial */
  141. private String m_userinfo = null;
  142. /** If specified, stores the host for this URI; otherwise null.
  143. * @serial */
  144. private String m_host = null;
  145. /** If specified, stores the port for this URI; otherwise -1.
  146. * @serial */
  147. private int m_port = -1;
  148. /** If specified, stores the path for this URI; otherwise null.
  149. * @serial */
  150. private String m_path = null;
  151. /**
  152. * If specified, stores the query string for this URI; otherwise
  153. * null.
  154. * @serial
  155. */
  156. private String m_queryString = null;
  157. /** If specified, stores the fragment for this URI; otherwise null.
  158. * @serial */
  159. private String m_fragment = null;
  160. /** Indicate whether in DEBUG mode */
  161. private static boolean DEBUG = false;
  162. /**
  163. * Construct a new and uninitialized URI.
  164. */
  165. public URI(){}
  166. /**
  167. * Construct a new URI from another URI. All fields for this URI are
  168. * set equal to the fields of the URI passed in.
  169. *
  170. * @param p_other the URI to copy (cannot be null)
  171. */
  172. public URI(URI p_other)
  173. {
  174. initialize(p_other);
  175. }
  176. /**
  177. * Construct a new URI from a URI specification string. If the
  178. * specification follows the "generic URI" syntax, (two slashes
  179. * following the first colon), the specification will be parsed
  180. * accordingly - setting the scheme, userinfo, host,port, path, query
  181. * string and fragment fields as necessary. If the specification does
  182. * not follow the "generic URI" syntax, the specification is parsed
  183. * into a scheme and scheme-specific part (stored as the path) only.
  184. *
  185. * @param p_uriSpec the URI specification string (cannot be null or
  186. * empty)
  187. *
  188. * @throws MalformedURIException if p_uriSpec violates any syntax
  189. * rules
  190. */
  191. public URI(String p_uriSpec) throws MalformedURIException
  192. {
  193. this((URI) null, p_uriSpec);
  194. }
  195. /**
  196. * Construct a new URI from a base URI and a URI specification string.
  197. * The URI specification string may be a relative URI.
  198. *
  199. * @param p_base the base URI (cannot be null if p_uriSpec is null or
  200. * empty)
  201. * @param p_uriSpec the URI specification string (cannot be null or
  202. * empty if p_base is null)
  203. *
  204. * @throws MalformedURIException if p_uriSpec violates any syntax
  205. * rules
  206. */
  207. public URI(URI p_base, String p_uriSpec) throws MalformedURIException
  208. {
  209. initialize(p_base, p_uriSpec);
  210. }
  211. /**
  212. * Construct a new URI that does not follow the generic URI syntax.
  213. * Only the scheme and scheme-specific part (stored as the path) are
  214. * initialized.
  215. *
  216. * @param p_scheme the URI scheme (cannot be null or empty)
  217. * @param p_schemeSpecificPart the scheme-specific part (cannot be
  218. * null or empty)
  219. *
  220. * @throws MalformedURIException if p_scheme violates any
  221. * syntax rules
  222. */
  223. public URI(String p_scheme, String p_schemeSpecificPart)
  224. throws MalformedURIException
  225. {
  226. if (p_scheme == null || p_scheme.trim().length() == 0)
  227. {
  228. throw new MalformedURIException(
  229. "Cannot construct URI with null/empty scheme!");
  230. }
  231. if (p_schemeSpecificPart == null
  232. || p_schemeSpecificPart.trim().length() == 0)
  233. {
  234. throw new MalformedURIException(
  235. "Cannot construct URI with null/empty scheme-specific part!");
  236. }
  237. setScheme(p_scheme);
  238. setPath(p_schemeSpecificPart);
  239. }
  240. /**
  241. * Construct a new URI that follows the generic URI syntax from its
  242. * component parts. Each component is validated for syntax and some
  243. * basic semantic checks are performed as well. See the individual
  244. * setter methods for specifics.
  245. *
  246. * @param p_scheme the URI scheme (cannot be null or empty)
  247. * @param p_host the hostname or IPv4 address for the URI
  248. * @param p_path the URI path - if the path contains '?' or '#',
  249. * then the query string and/or fragment will be
  250. * set from the path; however, if the query and
  251. * fragment are specified both in the path and as
  252. * separate parameters, an exception is thrown
  253. * @param p_queryString the URI query string (cannot be specified
  254. * if path is null)
  255. * @param p_fragment the URI fragment (cannot be specified if path
  256. * is null)
  257. *
  258. * @throws MalformedURIException if any of the parameters violates
  259. * syntax rules or semantic rules
  260. */
  261. public URI(String p_scheme, String p_host, String p_path, String p_queryString, String p_fragment)
  262. throws MalformedURIException
  263. {
  264. this(p_scheme, null, p_host, -1, p_path, p_queryString, p_fragment);
  265. }
  266. /**
  267. * Construct a new URI that follows the generic URI syntax from its
  268. * component parts. Each component is validated for syntax and some
  269. * basic semantic checks are performed as well. See the individual
  270. * setter methods for specifics.
  271. *
  272. * @param p_scheme the URI scheme (cannot be null or empty)
  273. * @param p_userinfo the URI userinfo (cannot be specified if host
  274. * is null)
  275. * @param p_host the hostname or IPv4 address for the URI
  276. * @param p_port the URI port (may be -1 for "unspecified"; cannot
  277. * be specified if host is null)
  278. * @param p_path the URI path - if the path contains '?' or '#',
  279. * then the query string and/or fragment will be
  280. * set from the path; however, if the query and
  281. * fragment are specified both in the path and as
  282. * separate parameters, an exception is thrown
  283. * @param p_queryString the URI query string (cannot be specified
  284. * if path is null)
  285. * @param p_fragment the URI fragment (cannot be specified if path
  286. * is null)
  287. *
  288. * @throws MalformedURIException if any of the parameters violates
  289. * syntax rules or semantic rules
  290. */
  291. public URI(String p_scheme, String p_userinfo, String p_host, int p_port, String p_path, String p_queryString, String p_fragment)
  292. throws MalformedURIException
  293. {
  294. if (p_scheme == null || p_scheme.trim().length() == 0)
  295. {
  296. throw new MalformedURIException(XSLMessages.createMessage(XSLTErrorResources.ER_SCHEME_REQUIRED, null)); //"Scheme is required!");
  297. }
  298. if (p_host == null)
  299. {
  300. if (p_userinfo != null)
  301. {
  302. throw new MalformedURIException(
  303. XSLMessages.createMessage(XSLTErrorResources.ER_NO_USERINFO_IF_NO_HOST, null)); //"Userinfo may not be specified if host is not specified!");
  304. }
  305. if (p_port != -1)
  306. {
  307. throw new MalformedURIException(
  308. XSLMessages.createMessage(XSLTErrorResources.ER_NO_PORT_IF_NO_HOST, null)); //"Port may not be specified if host is not specified!");
  309. }
  310. }
  311. if (p_path != null)
  312. {
  313. if (p_path.indexOf('?') != -1 && p_queryString != null)
  314. {
  315. throw new MalformedURIException(
  316. XSLMessages.createMessage(XSLTErrorResources.ER_NO_QUERY_STRING_IN_PATH, null)); //"Query string cannot be specified in path and query string!");
  317. }
  318. if (p_path.indexOf('#') != -1 && p_fragment != null)
  319. {
  320. throw new MalformedURIException(
  321. XSLMessages.createMessage(XSLTErrorResources.ER_NO_FRAGMENT_STRING_IN_PATH, null)); //"Fragment cannot be specified in both the path and fragment!");
  322. }
  323. }
  324. setScheme(p_scheme);
  325. setHost(p_host);
  326. setPort(p_port);
  327. setUserinfo(p_userinfo);
  328. setPath(p_path);
  329. setQueryString(p_queryString);
  330. setFragment(p_fragment);
  331. }
  332. /**
  333. * Initialize all fields of this URI from another URI.
  334. *
  335. * @param p_other the URI to copy (cannot be null)
  336. */
  337. private void initialize(URI p_other)
  338. {
  339. m_scheme = p_other.getScheme();
  340. m_userinfo = p_other.getUserinfo();
  341. m_host = p_other.getHost();
  342. m_port = p_other.getPort();
  343. m_path = p_other.getPath();
  344. m_queryString = p_other.getQueryString();
  345. m_fragment = p_other.getFragment();
  346. }
  347. /**
  348. * Initializes this URI from a base URI and a URI specification string.
  349. * See RFC 2396 Section 4 and Appendix B for specifications on parsing
  350. * the URI and Section 5 for specifications on resolving relative URIs
  351. * and relative paths.
  352. *
  353. * @param p_base the base URI (may be null if p_uriSpec is an absolute
  354. * URI)
  355. * @param p_uriSpec the URI spec string which may be an absolute or
  356. * relative URI (can only be null/empty if p_base
  357. * is not null)
  358. *
  359. * @throws MalformedURIException if p_base is null and p_uriSpec
  360. * is not an absolute URI or if
  361. * p_uriSpec violates syntax rules
  362. */
  363. private void initialize(URI p_base, String p_uriSpec)
  364. throws MalformedURIException
  365. {
  366. if (p_base == null
  367. && (p_uriSpec == null || p_uriSpec.trim().length() == 0))
  368. {
  369. throw new MalformedURIException(
  370. XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_INIT_URI_EMPTY_PARMS, null)); //"Cannot initialize URI with empty parameters.");
  371. }
  372. // just make a copy of the base if spec is empty
  373. if (p_uriSpec == null || p_uriSpec.trim().length() == 0)
  374. {
  375. initialize(p_base);
  376. return;
  377. }
  378. String uriSpec = p_uriSpec.trim();
  379. int uriSpecLen = uriSpec.length();
  380. int index = 0;
  381. // check for scheme
  382. if (uriSpec.indexOf(':') == -1)
  383. {
  384. if (p_base == null)
  385. {
  386. throw new MalformedURIException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_SCHEME_IN_URI, new Object[]{uriSpec})); //"No scheme found in URI: "+uriSpec);
  387. }
  388. }
  389. else
  390. {
  391. initializeScheme(uriSpec);
  392. index = m_scheme.length() + 1;
  393. }
  394. // two slashes means generic URI syntax, so we get the authority
  395. if (((index + 1) < uriSpecLen)
  396. && (uriSpec.substring(index).startsWith("//")))
  397. {
  398. index += 2;
  399. int startPos = index;
  400. // get authority - everything up to path, query or fragment
  401. char testChar = '\0';
  402. while (index < uriSpecLen)
  403. {
  404. testChar = uriSpec.charAt(index);
  405. if (testChar == '/' || testChar == '?' || testChar == '#')
  406. {
  407. break;
  408. }
  409. index++;
  410. }
  411. // if we found authority, parse it out, otherwise we set the
  412. // host to empty string
  413. if (index > startPos)
  414. {
  415. initializeAuthority(uriSpec.substring(startPos, index));
  416. }
  417. else
  418. {
  419. m_host = "";
  420. }
  421. }
  422. initializePath(uriSpec.substring(index));
  423. // Resolve relative URI to base URI - see RFC 2396 Section 5.2
  424. // In some cases, it might make more sense to throw an exception
  425. // (when scheme is specified is the string spec and the base URI
  426. // is also specified, for example), but we're just following the
  427. // RFC specifications
  428. if (p_base != null)
  429. {
  430. // check to see if this is the current doc - RFC 2396 5.2 #2
  431. // note that this is slightly different from the RFC spec in that
  432. // we don't include the check for query string being null
  433. // - this handles cases where the urispec is just a query
  434. // string or a fragment (e.g. "?y" or "#s") -
  435. // see <http://www.ics.uci.edu/~fielding/url/test1.html> which
  436. // identified this as a bug in the RFC
  437. if (m_path.length() == 0 && m_scheme == null && m_host == null)
  438. {
  439. m_scheme = p_base.getScheme();
  440. m_userinfo = p_base.getUserinfo();
  441. m_host = p_base.getHost();
  442. m_port = p_base.getPort();
  443. m_path = p_base.getPath();
  444. if (m_queryString == null)
  445. {
  446. m_queryString = p_base.getQueryString();
  447. }
  448. return;
  449. }
  450. // check for scheme - RFC 2396 5.2 #3
  451. // if we found a scheme, it means absolute URI, so we're done
  452. if (m_scheme == null)
  453. {
  454. m_scheme = p_base.getScheme();
  455. }
  456. else
  457. {
  458. return;
  459. }
  460. // check for authority - RFC 2396 5.2 #4
  461. // if we found a host, then we've got a network path, so we're done
  462. if (m_host == null)
  463. {
  464. m_userinfo = p_base.getUserinfo();
  465. m_host = p_base.getHost();
  466. m_port = p_base.getPort();
  467. }
  468. else
  469. {
  470. return;
  471. }
  472. // check for absolute path - RFC 2396 5.2 #5
  473. if (m_path.length() > 0 && m_path.startsWith("/"))
  474. {
  475. return;
  476. }
  477. // if we get to this point, we need to resolve relative path
  478. // RFC 2396 5.2 #6
  479. String path = new String();
  480. String basePath = p_base.getPath();
  481. // 6a - get all but the last segment of the base URI path
  482. if (basePath != null)
  483. {
  484. int lastSlash = basePath.lastIndexOf('/');
  485. if (lastSlash != -1)
  486. {
  487. path = basePath.substring(0, lastSlash + 1);
  488. }
  489. }
  490. // 6b - append the relative URI path
  491. path = path.concat(m_path);
  492. // 6c - remove all "./" where "." is a complete path segment
  493. index = -1;
  494. while ((index = path.indexOf("/./")) != -1)
  495. {
  496. path = path.substring(0, index + 1).concat(path.substring(index + 3));
  497. }
  498. // 6d - remove "." if path ends with "." as a complete path segment
  499. if (path.endsWith("/."))
  500. {
  501. path = path.substring(0, path.length() - 1);
  502. }
  503. // 6e - remove all "<segment>/../" where "<segment>" is a complete
  504. // path segment not equal to ".."
  505. index = -1;
  506. int segIndex = -1;
  507. String tempString = null;
  508. while ((index = path.indexOf("/../")) > 0)
  509. {
  510. tempString = path.substring(0, path.indexOf("/../"));
  511. segIndex = tempString.lastIndexOf('/');
  512. if (segIndex != -1)
  513. {
  514. if (!tempString.substring(segIndex++).equals(".."))
  515. {
  516. path = path.substring(0, segIndex).concat(path.substring(index
  517. + 4));
  518. }
  519. }
  520. }
  521. // 6f - remove ending "<segment>/.." where "<segment>" is a
  522. // complete path segment
  523. if (path.endsWith("/.."))
  524. {
  525. tempString = path.substring(0, path.length() - 3);
  526. segIndex = tempString.lastIndexOf('/');
  527. if (segIndex != -1)
  528. {
  529. path = path.substring(0, segIndex + 1);
  530. }
  531. }
  532. m_path = path;
  533. }
  534. }
  535. /**
  536. * Initialize the scheme for this URI from a URI string spec.
  537. *
  538. * @param p_uriSpec the URI specification (cannot be null)
  539. *
  540. * @throws MalformedURIException if URI does not have a conformant
  541. * scheme
  542. */
  543. private void initializeScheme(String p_uriSpec) throws MalformedURIException
  544. {
  545. int uriSpecLen = p_uriSpec.length();
  546. int index = 0;
  547. String scheme = null;
  548. char testChar = '\0';
  549. while (index < uriSpecLen)
  550. {
  551. testChar = p_uriSpec.charAt(index);
  552. if (testChar == ':' || testChar == '/' || testChar == '?'
  553. || testChar == '#')
  554. {
  555. break;
  556. }
  557. index++;
  558. }
  559. scheme = p_uriSpec.substring(0, index);
  560. if (scheme.length() == 0)
  561. {
  562. throw new MalformedURIException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_SCHEME_INURI, null)); //"No scheme found in URI.");
  563. }
  564. else
  565. {
  566. setScheme(scheme);
  567. }
  568. }
  569. /**
  570. * Initialize the authority (userinfo, host and port) for this
  571. * URI from a URI string spec.
  572. *
  573. * @param p_uriSpec the URI specification (cannot be null)
  574. *
  575. * @throws MalformedURIException if p_uriSpec violates syntax rules
  576. */
  577. private void initializeAuthority(String p_uriSpec)
  578. throws MalformedURIException
  579. {
  580. int index = 0;
  581. int start = 0;
  582. int end = p_uriSpec.length();
  583. char testChar = '\0';
  584. String userinfo = null;
  585. // userinfo is everything up @
  586. if (p_uriSpec.indexOf('@', start) != -1)
  587. {
  588. while (index < end)
  589. {
  590. testChar = p_uriSpec.charAt(index);
  591. if (testChar == '@')
  592. {
  593. break;
  594. }
  595. index++;
  596. }
  597. userinfo = p_uriSpec.substring(start, index);
  598. index++;
  599. }
  600. // host is everything up to ':'
  601. String host = null;
  602. start = index;
  603. while (index < end)
  604. {
  605. testChar = p_uriSpec.charAt(index);
  606. if (testChar == ':')
  607. {
  608. break;
  609. }
  610. index++;
  611. }
  612. host = p_uriSpec.substring(start, index);
  613. int port = -1;
  614. if (host.length() > 0)
  615. {
  616. // port
  617. if (testChar == ':')
  618. {
  619. index++;
  620. start = index;
  621. while (index < end)
  622. {
  623. index++;
  624. }
  625. String portStr = p_uriSpec.substring(start, index);
  626. if (portStr.length() > 0)
  627. {
  628. for (int i = 0; i < portStr.length(); i++)
  629. {
  630. if (!isDigit(portStr.charAt(i)))
  631. {
  632. throw new MalformedURIException(
  633. portStr + " is invalid. Port should only contain digits!");
  634. }
  635. }
  636. try
  637. {
  638. port = Integer.parseInt(portStr);
  639. }
  640. catch (NumberFormatException nfe)
  641. {
  642. // can't happen
  643. }
  644. }
  645. }
  646. }
  647. setHost(host);
  648. setPort(port);
  649. setUserinfo(userinfo);
  650. }
  651. /**
  652. * Initialize the path for this URI from a URI string spec.
  653. *
  654. * @param p_uriSpec the URI specification (cannot be null)
  655. *
  656. * @throws MalformedURIException if p_uriSpec violates syntax rules
  657. */
  658. private void initializePath(String p_uriSpec) throws MalformedURIException
  659. {
  660. if (p_uriSpec == null)
  661. {
  662. throw new MalformedURIException(
  663. "Cannot initialize path from null string!");
  664. }
  665. int index = 0;
  666. int start = 0;
  667. int end = p_uriSpec.length();
  668. char testChar = '\0';
  669. // path - everything up to query string or fragment
  670. while (index < end)
  671. {
  672. testChar = p_uriSpec.charAt(index);
  673. if (testChar == '?' || testChar == '#')
  674. {
  675. break;
  676. }
  677. // check for valid escape sequence
  678. if (testChar == '%')
  679. {
  680. if (index + 2 >= end ||!isHex(p_uriSpec.charAt(index + 1))
  681. ||!isHex(p_uriSpec.charAt(index + 2)))
  682. {
  683. throw new MalformedURIException(
  684. XSLMessages.createMessage(XSLTErrorResources.ER_PATH_CONTAINS_INVALID_ESCAPE_SEQUENCE, null)); //"Path contains invalid escape sequence!");
  685. }
  686. }
  687. else if (!isReservedCharacter(testChar)
  688. &&!isUnreservedCharacter(testChar))
  689. {
  690. if ('\\' != testChar)
  691. throw new MalformedURIException(XSLMessages.createMessage(XSLTErrorResources.ER_PATH_INVALID_CHAR, new Object[]{String.valueOf(testChar)})); //"Path contains invalid character: "
  692. //+ testChar);
  693. }
  694. index++;
  695. }
  696. m_path = p_uriSpec.substring(start, index);
  697. // query - starts with ? and up to fragment or end
  698. if (testChar == '?')
  699. {
  700. index++;
  701. start = index;
  702. while (index < end)
  703. {
  704. testChar = p_uriSpec.charAt(index);
  705. if (testChar == '#')
  706. {
  707. break;
  708. }
  709. if (testChar == '%')
  710. {
  711. if (index + 2 >= end ||!isHex(p_uriSpec.charAt(index + 1))
  712. ||!isHex(p_uriSpec.charAt(index + 2)))
  713. {
  714. throw new MalformedURIException(
  715. "Query string contains invalid escape sequence!");
  716. }
  717. }
  718. else if (!isReservedCharacter(testChar)
  719. &&!isUnreservedCharacter(testChar))
  720. {
  721. throw new MalformedURIException(
  722. "Query string contains invalid character:" + testChar);
  723. }
  724. index++;
  725. }
  726. m_queryString = p_uriSpec.substring(start, index);
  727. }
  728. // fragment - starts with #
  729. if (testChar == '#')
  730. {
  731. index++;
  732. start = index;
  733. while (index < end)
  734. {
  735. testChar = p_uriSpec.charAt(index);
  736. if (testChar == '%')
  737. {
  738. if (index + 2 >= end ||!isHex(p_uriSpec.charAt(index + 1))
  739. ||!isHex(p_uriSpec.charAt(index + 2)))
  740. {
  741. throw new MalformedURIException(
  742. "Fragment contains invalid escape sequence!");
  743. }
  744. }
  745. else if (!isReservedCharacter(testChar)
  746. &&!isUnreservedCharacter(testChar))
  747. {
  748. throw new MalformedURIException(
  749. "Fragment contains invalid character:" + testChar);
  750. }
  751. index++;
  752. }
  753. m_fragment = p_uriSpec.substring(start, index);
  754. }
  755. }
  756. /**
  757. * Get the scheme for this URI.
  758. *
  759. * @return the scheme for this URI
  760. */
  761. public String getScheme()
  762. {
  763. return m_scheme;
  764. }
  765. /**
  766. * Get the scheme-specific part for this URI (everything following the
  767. * scheme and the first colon). See RFC 2396 Section 5.2 for spec.
  768. *
  769. * @return the scheme-specific part for this URI
  770. */
  771. public String getSchemeSpecificPart()
  772. {
  773. StringBuffer schemespec = new StringBuffer();
  774. if (m_userinfo != null || m_host != null || m_port != -1)
  775. {
  776. schemespec.append("//");
  777. }
  778. if (m_userinfo != null)
  779. {
  780. schemespec.append(m_userinfo);
  781. schemespec.append('@');
  782. }
  783. if (m_host != null)
  784. {
  785. schemespec.append(m_host);
  786. }
  787. if (m_port != -1)
  788. {
  789. schemespec.append(':');
  790. schemespec.append(m_port);
  791. }
  792. if (m_path != null)
  793. {
  794. schemespec.append((m_path));
  795. }
  796. if (m_queryString != null)
  797. {
  798. schemespec.append('?');
  799. schemespec.append(m_queryString);
  800. }
  801. if (m_fragment != null)
  802. {
  803. schemespec.append('#');
  804. schemespec.append(m_fragment);
  805. }
  806. return schemespec.toString();
  807. }
  808. /**
  809. * Get the userinfo for this URI.
  810. *
  811. * @return the userinfo for this URI (null if not specified).
  812. */
  813. public String getUserinfo()
  814. {
  815. return m_userinfo;
  816. }
  817. /**
  818. * Get the host for this URI.
  819. *
  820. * @return the host for this URI (null if not specified).
  821. */
  822. public String getHost()
  823. {
  824. return m_host;
  825. }
  826. /**
  827. * Get the port for this URI.
  828. *
  829. * @return the port for this URI (-1 if not specified).
  830. */
  831. public int getPort()
  832. {
  833. return m_port;
  834. }
  835. /**
  836. * Get the path for this URI (optionally with the query string and
  837. * fragment).
  838. *
  839. * @param p_includeQueryString if true (and query string is not null),
  840. * then a "?" followed by the query string
  841. * will be appended
  842. * @param p_includeFragment if true (and fragment is not null),
  843. * then a "#" followed by the fragment
  844. * will be appended
  845. *
  846. * @return the path for this URI possibly including the query string
  847. * and fragment
  848. */
  849. public String getPath(boolean p_includeQueryString,
  850. boolean p_includeFragment)
  851. {
  852. StringBuffer pathString = new StringBuffer(m_path);
  853. if (p_includeQueryString && m_queryString != null)
  854. {
  855. pathString.append('?');
  856. pathString.append(m_queryString);
  857. }
  858. if (p_includeFragment && m_fragment != null)
  859. {
  860. pathString.append('#');
  861. pathString.append(m_fragment);
  862. }
  863. return pathString.toString();
  864. }
  865. /**
  866. * Get the path for this URI. Note that the value returned is the path
  867. * only and does not include the query string or fragment.
  868. *
  869. * @return the path for this URI.
  870. */
  871. public String getPath()
  872. {
  873. return m_path;
  874. }
  875. /**
  876. * Get the query string for this URI.
  877. *
  878. * @return the query string for this URI. Null is returned if there
  879. * was no "?" in the URI spec, empty string if there was a
  880. * "?" but no query string following it.
  881. */
  882. public String getQueryString()
  883. {
  884. return m_queryString;
  885. }
  886. /**
  887. * Get the fragment for this URI.
  888. *
  889. * @return the fragment for this URI. Null is returned if there
  890. * was no "#" in the URI spec, empty string if there was a
  891. * "#" but no fragment following it.
  892. */
  893. public String getFragment()
  894. {
  895. return m_fragment;
  896. }
  897. /**
  898. * Set the scheme for this URI. The scheme is converted to lowercase
  899. * before it is set.
  900. *
  901. * @param p_scheme the scheme for this URI (cannot be null)
  902. *
  903. * @throws MalformedURIException if p_scheme is not a conformant
  904. * scheme name
  905. */
  906. public void setScheme(String p_scheme) throws MalformedURIException
  907. {
  908. if (p_scheme == null)
  909. {
  910. throw new MalformedURIException(XSLMessages.createMessage(XSLTErrorResources.ER_SCHEME_FROM_NULL_STRING, null)); //"Cannot set scheme from null string!");
  911. }
  912. if (!isConformantSchemeName(p_scheme))
  913. {
  914. throw new MalformedURIException(XSLMessages.createMessage(XSLTErrorResources.ER_SCHEME_NOT_CONFORMANT, null)); //"The scheme is not conformant.");
  915. }
  916. m_scheme = p_scheme.toLowerCase();
  917. }
  918. /**
  919. * Set the userinfo for this URI. If a non-null value is passed in and
  920. * the host value is null, then an exception is thrown.
  921. *
  922. * @param p_userinfo the userinfo for this URI
  923. *
  924. * @throws MalformedURIException if p_userinfo contains invalid
  925. * characters
  926. */
  927. public void setUserinfo(String p_userinfo) throws MalformedURIException
  928. {
  929. if (p_userinfo == null)
  930. {
  931. m_userinfo = null;
  932. }
  933. else
  934. {
  935. if (m_host == null)
  936. {
  937. throw new MalformedURIException(
  938. "Userinfo cannot be set when host is null!");
  939. }
  940. // userinfo can contain alphanumerics, mark characters, escaped
  941. // and ';',':','&','=','+','$',','
  942. int index = 0;
  943. int end = p_userinfo.length();
  944. char testChar = '\0';
  945. while (index < end)
  946. {
  947. testChar = p_userinfo.charAt(index);
  948. if (testChar == '%')
  949. {
  950. if (index + 2 >= end ||!isHex(p_userinfo.charAt(index + 1))
  951. ||!isHex(p_userinfo.charAt(index + 2)))
  952. {
  953. throw new MalformedURIException(
  954. "Userinfo contains invalid escape sequence!");
  955. }
  956. }
  957. else if (!isUnreservedCharacter(testChar)
  958. && USERINFO_CHARACTERS.indexOf(testChar) == -1)
  959. {
  960. throw new MalformedURIException(
  961. "Userinfo contains invalid character:" + testChar);
  962. }
  963. index++;
  964. }
  965. }
  966. m_userinfo = p_userinfo;
  967. }
  968. /**
  969. * Set the host for this URI. If null is passed in, the userinfo
  970. * field is also set to null and the port is set to -1.
  971. *
  972. * @param p_host the host for this URI
  973. *
  974. * @throws MalformedURIException if p_host is not a valid IP
  975. * address or DNS hostname.
  976. */
  977. public void setHost(String p_host) throws MalformedURIException
  978. {
  979. if (p_host == null || p_host.trim().length() == 0)
  980. {
  981. m_host = p_host;
  982. m_userinfo = null;
  983. m_port = -1;
  984. }
  985. else if (!isWellFormedAddress(p_host))
  986. {
  987. throw new MalformedURIException(XSLMessages.createMessage(XSLTErrorResources.ER_HOST_ADDRESS_NOT_WELLFORMED, null)); //"Host is not a well formed address!");
  988. }
  989. m_host = p_host;
  990. }
  991. /**
  992. * Set the port for this URI. -1 is used to indicate that the port is
  993. * not specified, otherwise valid port numbers are between 0 and 65535.
  994. * If a valid port number is passed in and the host field is null,
  995. * an exception is thrown.
  996. *
  997. * @param p_port the port number for this URI
  998. *
  999. * @throws MalformedURIException if p_port is not -1 and not a
  1000. * valid port number
  1001. */
  1002. public void setPort(int p_port) throws MalformedURIException
  1003. {
  1004. if (p_port >= 0 && p_port <= 65535)
  1005. {
  1006. if (m_host == null)
  1007. {
  1008. throw new MalformedURIException(
  1009. XSLMessages.createMessage(XSLTErrorResources.ER_PORT_WHEN_HOST_NULL, null)); //"Port cannot be set when host is null!");
  1010. }
  1011. }
  1012. else if (p_port != -1)
  1013. {
  1014. throw new MalformedURIException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_PORT, null)); //"Invalid port number!");
  1015. }
  1016. m_port = p_port;
  1017. }
  1018. /**
  1019. * Set the path for this URI. If the supplied path is null, then the
  1020. * query string and fragment are set to null as well. If the supplied
  1021. * path includes a query string and/or fragment, these fields will be
  1022. * parsed and set as well. Note that, for URIs following the "generic
  1023. * URI" syntax, the path specified should start with a slash.
  1024. * For URIs that do not follow the generic URI syntax, this method
  1025. * sets the scheme-specific part.
  1026. *
  1027. * @param p_path the path for this URI (may be null)
  1028. *
  1029. * @throws MalformedURIException if p_path contains invalid
  1030. * characters
  1031. */
  1032. public void setPath(String p_path) throws MalformedURIException
  1033. {
  1034. if (p_path == null)
  1035. {
  1036. m_path = null;
  1037. m_queryString = null;
  1038. m_fragment = null;
  1039. }
  1040. else
  1041. {
  1042. initializePath(p_path);
  1043. }
  1044. }
  1045. /**
  1046. * Append to the end of the path of this URI. If the current path does
  1047. * not end in a slash and the path to be appended does not begin with
  1048. * a slash, a slash will be appended to the current path before the
  1049. * new segment is added. Also, if the current path ends in a slash
  1050. * and the new segment begins with a slash, the extra slash will be
  1051. * removed before the new segment is appended.
  1052. *
  1053. * @param p_addToPath the new segment to be added to the current path
  1054. *
  1055. * @throws MalformedURIException if p_addToPath contains syntax
  1056. * errors
  1057. */
  1058. public void appendPath(String p_addToPath) throws MalformedURIException
  1059. {
  1060. if (p_addToPath == null || p_addToPath.trim().length() == 0)
  1061. {
  1062. return;
  1063. }
  1064. if (!isURIString(p_addToPath))
  1065. {
  1066. throw new MalformedURIException(XSLMessages.createMessage(XSLTErrorResources.ER_PATH_INVALID_CHAR, new Object[]{p_addToPath})); //"Path contains invalid character!");
  1067. }
  1068. if (m_path == null || m_path.trim().length() == 0)
  1069. {
  1070. if (p_addToPath.startsWith("/"))
  1071. {
  1072. m_path = p_addToPath;
  1073. }
  1074. else
  1075. {
  1076. m_path = "/" + p_addToPath;
  1077. }
  1078. }
  1079. else if (m_path.endsWith("/"))
  1080. {
  1081. if (p_addToPath.startsWith("/"))
  1082. {
  1083. m_path = m_path.concat(p_addToPath.substring(1));
  1084. }
  1085. else
  1086. {
  1087. m_path = m_path.concat(p_addToPath);
  1088. }
  1089. }
  1090. else
  1091. {
  1092. if (p_addToPath.startsWith("/"))
  1093. {
  1094. m_path = m_path.concat(p_addToPath);
  1095. }
  1096. else
  1097. {
  1098. m_path = m_path.concat("/" + p_addToPath);
  1099. }
  1100. }
  1101. }
  1102. /**
  1103. * Set the query string for this URI. A non-null value is valid only
  1104. * if this is an URI conforming to the generic URI syntax and
  1105. * the path value is not null.
  1106. *
  1107. * @param p_queryString the query string for this URI
  1108. *
  1109. * @throws MalformedURIException if p_queryString is not null and this
  1110. * URI does not conform to the generic
  1111. * URI syntax or if the path is null
  1112. */
  1113. public void setQueryString(String p_queryString)
  1114. throws MalformedURIException
  1115. {
  1116. if (p_queryString == null)
  1117. {
  1118. m_queryString = null;
  1119. }
  1120. else if (!isGenericURI())
  1121. {
  1122. throw new MalformedURIException(
  1123. "Query string can only be set for a generic URI!");
  1124. }
  1125. else if (getPath() == null)
  1126. {
  1127. throw new MalformedURIException(
  1128. "Query string cannot be set when path is null!");
  1129. }
  1130. else if (!isURIString(p_queryString))
  1131. {
  1132. throw new MalformedURIException(
  1133. "Query string contains invalid character!");
  1134. }
  1135. else
  1136. {
  1137. m_queryString = p_queryString;
  1138. }
  1139. }
  1140. /**
  1141. * Set the fragment for this URI. A non-null value is valid only
  1142. * if this is a URI conforming to the generic URI syntax and
  1143. * the path value is not null.
  1144. *
  1145. * @param p_fragment the fragment for this URI
  1146. *
  1147. * @throws MalformedURIException if p_fragment is not null and this
  1148. * URI does not conform to the generic
  1149. * URI syntax or if the path is null
  1150. */
  1151. public void setFragment(String p_fragment) throws MalformedURIException
  1152. {
  1153. if (p_fragment == null)
  1154. {
  1155. m_fragment = null;
  1156. }
  1157. else if (!isGenericURI())
  1158. {
  1159. throw new MalformedURIException(
  1160. XSLMessages.createMessage(XSLTErrorResources.ER_FRAG_FOR_GENERIC_URI, null)); //"Fragment can only be set for a generic URI!");
  1161. }
  1162. else if (getPath() == null)
  1163. {
  1164. throw new MalformedURIException(
  1165. XSLMessages.createMessage(XSLTErrorResources.ER_FRAG_WHEN_PATH_NULL, null)); //"Fragment cannot be set when path is null!");
  1166. }
  1167. else if (!isURIString(p_fragment))
  1168. {
  1169. throw new MalformedURIException(XSLMessages.createMessage(XSLTErrorResources.ER_FRAG_INVALID_CHAR, null)); //"Fragment contains invalid character!");
  1170. }
  1171. else
  1172. {
  1173. m_fragment = p_fragment;
  1174. }
  1175. }
  1176. /**
  1177. * Determines if the passed-in Object is equivalent to this URI.
  1178. *
  1179. * @param p_test the Object to test for equality.
  1180. *
  1181. * @return true if p_test is a URI with all values equal to this
  1182. * URI, false otherwise
  1183. */
  1184. public boolean equals(Object p_test)
  1185. {
  1186. if (p_test instanceof URI)
  1187. {
  1188. URI testURI = (URI) p_test;
  1189. if (((m_scheme == null && testURI.m_scheme == null) || (m_scheme != null && testURI.m_scheme != null && m_scheme.equals(
  1190. testURI.m_scheme))) && ((m_userinfo == null && testURI.m_userinfo == null) || (m_userinfo != null && testURI.m_userinfo != null && m_userinfo.equals(
  1191. testURI.m_userinfo))) && ((m_host == null && testURI.m_host == null) || (m_host != null && testURI.m_host != null && m_host.equals(
  1192. testURI.m_host))) && m_port == testURI.m_port && ((m_path == null && testURI.m_path == null) || (m_path != null && testURI.m_path != null && m_path.equals(
  1193. testURI.m_path))) && ((m_queryString == null && testURI.m_queryString == null) || (m_queryString != null && testURI.m_queryString != null && m_queryString.equals(
  1194. testURI.m_queryString))) && ((m_fragment == null && testURI.m_fragment == null) || (m_fragment != null && testURI.m_fragment != null && m_fragment.equals(
  1195. testURI.m_fragment))))
  1196. {
  1197. return true;
  1198. }
  1199. }
  1200. return false;
  1201. }
  1202. /**
  1203. * Get the URI as a string specification. See RFC 2396 Section 5.2.
  1204. *
  1205. * @return the URI string specification
  1206. */
  1207. public String toString()
  1208. {
  1209. StringBuffer uriSpecString = new StringBuffer();
  1210. if (m_scheme != null)
  1211. {
  1212. uriSpecString.append(m_scheme);
  1213. uriSpecString.append(':');
  1214. }
  1215. uriSpecString.append(getSchemeSpecificPart());
  1216. return uriSpecString.toString();
  1217. }
  1218. /**
  1219. * Get the indicator as to whether this URI uses the "generic URI"
  1220. * syntax.
  1221. *
  1222. * @return true if this URI uses the "generic URI" syntax, false
  1223. * otherwise
  1224. */
  1225. public boolean isGenericURI()
  1226. {
  1227. // presence of the host (whether valid or empty) means
  1228. // double-slashes which means generic uri
  1229. return (m_host != null);
  1230. }
  1231. /**
  1232. * Determine whether a scheme conforms to the rules for a scheme name.
  1233. * A scheme is conformant if it starts with an alphanumeric, and
  1234. * contains only alphanumerics, '+','-' and '.'.
  1235. *
  1236. *
  1237. * @param p_scheme The sheme name to check
  1238. * @return true if the scheme is conformant, false otherwise
  1239. */
  1240. public static boolean isConformantSchemeName(String p_scheme)
  1241. {
  1242. if (p_scheme == null || p_scheme.trim().length() == 0)
  1243. {
  1244. return false;
  1245. }
  1246. if (!isAlpha(p_scheme.charAt(0)))
  1247. {
  1248. return false;
  1249. }
  1250. char testChar;
  1251. for (int i = 1; i < p_scheme.length(); i++)
  1252. {
  1253. testChar = p_scheme.charAt(i);
  1254. if (!isAlphanum(testChar) && SCHEME_CHARACTERS.indexOf(testChar) == -1)
  1255. {
  1256. return false;
  1257. }
  1258. }
  1259. return true;
  1260. }
  1261. /**
  1262. * Determine whether a string is syntactically capable of representing
  1263. * a valid IPv4 address or the domain name of a network host. A valid
  1264. * IPv4 address consists of four decimal digit groups separated by a
  1265. * '.'. A hostname consists of domain labels (each of which must
  1266. * begin and end with an alphanumeric but may contain '-') separated
  1267. * & by a '.'. See RFC 2396 Section 3.2.2.
  1268. *
  1269. *
  1270. * @param p_address The address string to check
  1271. * @return true if the string is a syntactically valid IPv4 address
  1272. * or hostname
  1273. */
  1274. public static boolean isWellFormedAddress(String p_address)
  1275. {
  1276. if (p_address == null)
  1277. {
  1278. return false;
  1279. }
  1280. String address = p_address.trim();
  1281. int addrLength = address.length();
  1282. if (addrLength == 0 || addrLength > 255)
  1283. {
  1284. return false;
  1285. }
  1286. if (address.startsWith(".") || address.startsWith("-"))
  1287. {
  1288. return false;
  1289. }
  1290. // rightmost domain label starting with digit indicates IP address
  1291. // since top level domain label can only start with an alpha
  1292. // see RFC 2396 Section 3.2.2
  1293. int index = address.lastIndexOf('.');
  1294. if (address.endsWith("."))
  1295. {
  1296. index = address.substring(0, index).lastIndexOf('.');
  1297. }
  1298. if (index + 1 < addrLength && isDigit(p_address.charAt(index + 1)))
  1299. {
  1300. char testChar;
  1301. int numDots = 0;
  1302. // make sure that 1) we see only digits and dot separators, 2) that
  1303. // any dot separator is preceded and followed by a digit and
  1304. // 3) that we find 3 dots
  1305. for (int i = 0; i < addrLength; i++)
  1306. {
  1307. testChar = address.charAt(i);
  1308. if (testChar == '.')
  1309. {
  1310. if (!isDigit(address.charAt(i - 1))
  1311. || (i + 1 < addrLength &&!isDigit(address.charAt(i + 1))))
  1312. {
  1313. return false;
  1314. }
  1315. numDots++;
  1316. }
  1317. else if (!isDigit(testChar))
  1318. {
  1319. return false;
  1320. }
  1321. }
  1322. if (numDots != 3)
  1323. {
  1324. return false;
  1325. }
  1326. }
  1327. else
  1328. {
  1329. // domain labels can contain alphanumerics and '-"
  1330. // but must start and end with an alphanumeric
  1331. char testChar;
  1332. for (int i = 0; i < addrLength; i++)
  1333. {
  1334. testChar = address.charAt(i);
  1335. if (testChar == '.')
  1336. {
  1337. if (!isAlphanum(address.charAt(i - 1)))
  1338. {
  1339. return false;
  1340. }
  1341. if (i + 1 < addrLength &&!isAlphanum(address.charAt(i + 1)))
  1342. {
  1343. return false;
  1344. }
  1345. }
  1346. else if (!isAlphanum(testChar) && testChar != '-')
  1347. {
  1348. return false;
  1349. }
  1350. }
  1351. }
  1352. return true;
  1353. }
  1354. /**
  1355. * Determine whether a char is a digit.
  1356. *
  1357. *
  1358. * @param p_char the character to check
  1359. * @return true if the char is betweeen '0' and '9', false otherwise
  1360. */
  1361. private static boolean isDigit(char p_char)
  1362. {
  1363. return p_char >= '0' && p_char <= '9';
  1364. }
  1365. /**
  1366. * Determine whether a character is a hexadecimal character.
  1367. *
  1368. *
  1369. * @param p_char the character to check
  1370. * @return true if the char is betweeen '0' and '9', 'a' and 'f'
  1371. * or 'A' and 'F', false otherwise
  1372. */
  1373. private static boolean isHex(char p_char)
  1374. {
  1375. return (isDigit(p_char) || (p_char >= 'a' && p_char <= 'f')
  1376. || (p_char >= 'A' && p_char <= 'F'));
  1377. }
  1378. /**
  1379. * Determine whether a char is an alphabetic character: a-z or A-Z
  1380. *
  1381. *
  1382. * @param p_char the character to check
  1383. * @return true if the char is alphabetic, false otherwise
  1384. */
  1385. private static boolean isAlpha(char p_char)
  1386. {
  1387. return ((p_char >= 'a' && p_char <= 'z')
  1388. || (p_char >= 'A' && p_char <= 'Z'));
  1389. }
  1390. /**
  1391. * Determine whether a char is an alphanumeric: 0-9, a-z or A-Z
  1392. *
  1393. *
  1394. * @param p_char the character to check
  1395. * @return true if the char is alphanumeric, false otherwise
  1396. */
  1397. private static boolean isAlphanum(char p_char)
  1398. {
  1399. return (isAlpha(p_char) || isDigit(p_char));
  1400. }
  1401. /**
  1402. * Determine whether a character is a reserved character:
  1403. * ';', '/', '?', ':', '@', '&', '=', '+', '$' or ','
  1404. *
  1405. *
  1406. * @param p_char the character to check
  1407. * @return true if the string contains any reserved characters
  1408. */
  1409. private static boolean isReservedCharacter(char p_char)
  1410. {
  1411. return RESERVED_CHARACTERS.indexOf(p_char) != -1;
  1412. }
  1413. /**
  1414. * Determine whether a char is an unreserved character.
  1415. *
  1416. *
  1417. * @param p_char the character to check
  1418. * @return true if the char is unreserved, false otherwise
  1419. */
  1420. private static boolean isUnreservedCharacter(char p_char)
  1421. {
  1422. return (isAlphanum(p_char) || MARK_CHARACTERS.indexOf(p_char) != -1);
  1423. }
  1424. /**
  1425. * Determine whether a given string contains only URI characters (also
  1426. * called "uric" in RFC 2396). uric consist of all reserved
  1427. * characters, unreserved characters and escaped characters.
  1428. *
  1429. *
  1430. * @param p_uric URI string
  1431. * @return true if the string is comprised of uric, false otherwise
  1432. */
  1433. private static boolean isURIString(String p_uric)
  1434. {
  1435. if (p_uric == null)
  1436. {
  1437. return false;
  1438. }
  1439. int end = p_uric.length();
  1440. char testChar = '\0';
  1441. for (int i = 0; i < end; i++)
  1442. {
  1443. testChar = p_uric.charAt(i);
  1444. if (testChar == '%')
  1445. {
  1446. if (i + 2 >= end ||!isHex(p_uric.charAt(i + 1))
  1447. ||!isHex(p_uric.charAt(i + 2)))
  1448. {
  1449. return false;
  1450. }
  1451. else
  1452. {
  1453. i += 2;
  1454. continue;
  1455. }
  1456. }
  1457. if (isReservedCharacter(testChar) || isUnreservedCharacter(testChar))
  1458. {
  1459. continue;
  1460. }
  1461. else
  1462. {
  1463. return false;
  1464. }
  1465. }
  1466. return true;
  1467. }
  1468. }