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