PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/jre-1.6.0/src/com/sun/org/apache/xml/internal/utils/URI.java

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