PageRenderTime 66ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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