You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

URIish.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. * Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
  3. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  6. * Copyright (C) 2013, Robin Stocker <robin@nibor.org>
  7. * and other copyright owners as documented in the project's IP log.
  8. *
  9. * This program and the accompanying materials are made available
  10. * under the terms of the Eclipse Distribution License v1.0 which
  11. * accompanies this distribution, is reproduced below, and is
  12. * available at http://www.eclipse.org/org/documents/edl-v10.php
  13. *
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or
  17. * without modification, are permitted provided that the following
  18. * conditions are met:
  19. *
  20. * - Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. *
  23. * - Redistributions in binary form must reproduce the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer in the documentation and/or other materials provided
  26. * with the distribution.
  27. *
  28. * - Neither the name of the Eclipse Foundation, Inc. nor the
  29. * names of its contributors may be used to endorse or promote
  30. * products derived from this software without specific prior
  31. * written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  34. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  36. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  38. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  40. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  41. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  43. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  44. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  45. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. */
  47. package org.eclipse.jgit.transport;
  48. import java.io.ByteArrayOutputStream;
  49. import java.io.File;
  50. import java.io.Serializable;
  51. import java.io.UnsupportedEncodingException;
  52. import java.net.URISyntaxException;
  53. import java.net.URL;
  54. import java.util.BitSet;
  55. import java.util.regex.Matcher;
  56. import java.util.regex.Pattern;
  57. import org.eclipse.jgit.internal.JGitText;
  58. import org.eclipse.jgit.lib.Constants;
  59. import org.eclipse.jgit.util.RawParseUtils;
  60. import org.eclipse.jgit.util.StringUtils;
  61. /**
  62. * This URI like construct used for referencing Git archives over the net, as
  63. * well as locally stored archives. It is similar to RFC 2396 URI's, but also
  64. * support SCP and the malformed file://&lt;path&gt; syntax (as opposed to the correct
  65. * file:&lt;path&gt; syntax.
  66. */
  67. public class URIish implements Serializable {
  68. /**
  69. * Part of a pattern which matches the scheme part (git, http, ...) of an
  70. * URI. Defines one capturing group containing the scheme without the
  71. * trailing colon and slashes
  72. */
  73. private static final String SCHEME_P = "([a-z][a-z0-9+-]+)://"; //$NON-NLS-1$
  74. /**
  75. * Part of a pattern which matches the optional user/password part (e.g.
  76. * root:pwd@ in git://root:pwd@host.xyz/a.git) of URIs. Defines two
  77. * capturing groups: the first containing the user and the second containing
  78. * the password
  79. */
  80. private static final String OPT_USER_PWD_P = "(?:([^/:@]+)(?::([^\\\\/]+))?@)?"; //$NON-NLS-1$
  81. /**
  82. * Part of a pattern which matches the host part of URIs. Defines one
  83. * capturing group containing the host name.
  84. */
  85. private static final String HOST_P = "((?:[^\\\\/:]+)|(?:\\[[0-9a-f:]+\\]))"; //$NON-NLS-1$
  86. /**
  87. * Part of a pattern which matches the optional port part of URIs. Defines
  88. * one capturing group containing the port without the preceding colon.
  89. */
  90. private static final String OPT_PORT_P = "(?::(\\d+))?"; //$NON-NLS-1$
  91. /**
  92. * Part of a pattern which matches the ~username part (e.g. /~root in
  93. * git://host.xyz/~root/a.git) of URIs. Defines no capturing group.
  94. */
  95. private static final String USER_HOME_P = "(?:/~(?:[^\\\\/]+))"; //$NON-NLS-1$
  96. /**
  97. * Part of a pattern which matches the optional drive letter in paths (e.g.
  98. * D: in file:///D:/a.txt). Defines no capturing group.
  99. */
  100. private static final String OPT_DRIVE_LETTER_P = "(?:[A-Za-z]:)?"; //$NON-NLS-1$
  101. /**
  102. * Part of a pattern which matches a relative path. Relative paths don't
  103. * start with slash or drive letters. Defines no capturing group.
  104. */
  105. private static final String RELATIVE_PATH_P = "(?:(?:[^\\\\/]+[\\\\/]+)*[^\\\\/]+[\\\\/]*)"; //$NON-NLS-1$
  106. /**
  107. * Part of a pattern which matches a relative or absolute path. Defines no
  108. * capturing group.
  109. */
  110. private static final String PATH_P = "(" + OPT_DRIVE_LETTER_P + "[\\\\/]?" //$NON-NLS-1$ //$NON-NLS-2$
  111. + RELATIVE_PATH_P + ")"; //$NON-NLS-1$
  112. private static final long serialVersionUID = 1L;
  113. /**
  114. * A pattern matching standard URI: </br>
  115. * <code>scheme "://" user_password? hostname? portnumber? path</code>
  116. */
  117. private static final Pattern FULL_URI = Pattern.compile("^" // //$NON-NLS-1$
  118. + SCHEME_P //
  119. + "(?:" // start a group containing hostname and all options only //$NON-NLS-1$
  120. // availabe when a hostname is there
  121. + OPT_USER_PWD_P //
  122. + HOST_P //
  123. + OPT_PORT_P //
  124. + "(" // open a catpuring group the the user-home-dir part //$NON-NLS-1$
  125. + (USER_HOME_P + "?") // //$NON-NLS-1$
  126. + "[\\\\/])" // //$NON-NLS-1$
  127. + ")?" // close the optional group containing hostname //$NON-NLS-1$
  128. + "(.+)?" // //$NON-NLS-1$
  129. + "$"); //$NON-NLS-1$
  130. /**
  131. * A pattern matching the reference to a local file. This may be an absolute
  132. * path (maybe even containing windows drive-letters) or a relative path.
  133. */
  134. private static final Pattern LOCAL_FILE = Pattern.compile("^" // //$NON-NLS-1$
  135. + "([\\\\/]?" + PATH_P + ")" // //$NON-NLS-1$ //$NON-NLS-2$
  136. + "$"); //$NON-NLS-1$
  137. /**
  138. * A pattern matching a URI for the scheme 'file' which has only ':/' as
  139. * separator between scheme and path. Standard file URIs have '://' as
  140. * separator, but java.io.File.toURI() constructs those URIs.
  141. */
  142. private static final Pattern SINGLE_SLASH_FILE_URI = Pattern.compile("^" // //$NON-NLS-1$
  143. + "(file):([\\\\/](?![\\\\/])" // //$NON-NLS-1$
  144. + PATH_P //
  145. + ")$"); //$NON-NLS-1$
  146. /**
  147. * A pattern matching a SCP URI's of the form user@host:path/to/repo.git
  148. */
  149. private static final Pattern RELATIVE_SCP_URI = Pattern.compile("^" // //$NON-NLS-1$
  150. + OPT_USER_PWD_P //
  151. + HOST_P //
  152. + ":(" // //$NON-NLS-1$
  153. + ("(?:" + USER_HOME_P + "[\\\\/])?") // //$NON-NLS-1$ //$NON-NLS-2$
  154. + RELATIVE_PATH_P //
  155. + ")$"); //$NON-NLS-1$
  156. /**
  157. * A pattern matching a SCP URI's of the form user@host:/path/to/repo.git
  158. */
  159. private static final Pattern ABSOLUTE_SCP_URI = Pattern.compile("^" // //$NON-NLS-1$
  160. + OPT_USER_PWD_P //
  161. + "([^\\\\/:]{2,})" // //$NON-NLS-1$
  162. + ":(" // //$NON-NLS-1$
  163. + "[\\\\/]" + RELATIVE_PATH_P // //$NON-NLS-1$
  164. + ")$"); //$NON-NLS-1$
  165. private String scheme;
  166. private String path;
  167. private String rawPath;
  168. private String user;
  169. private String pass;
  170. private int port = -1;
  171. private String host;
  172. /**
  173. * Parse and construct an {@link URIish} from a string
  174. *
  175. * @param s
  176. * @throws URISyntaxException
  177. */
  178. public URIish(String s) throws URISyntaxException {
  179. if (StringUtils.isEmptyOrNull(s)) {
  180. throw new URISyntaxException("The uri was empty or null", //$NON-NLS-1$
  181. JGitText.get().cannotParseGitURIish);
  182. }
  183. Matcher matcher = SINGLE_SLASH_FILE_URI.matcher(s);
  184. if (matcher.matches()) {
  185. scheme = matcher.group(1);
  186. rawPath = cleanLeadingSlashes(matcher.group(2), scheme);
  187. path = unescape(rawPath);
  188. return;
  189. }
  190. matcher = FULL_URI.matcher(s);
  191. if (matcher.matches()) {
  192. scheme = matcher.group(1);
  193. user = unescape(matcher.group(2));
  194. pass = unescape(matcher.group(3));
  195. host = unescape(matcher.group(4));
  196. if (matcher.group(5) != null)
  197. port = Integer.parseInt(matcher.group(5));
  198. rawPath = cleanLeadingSlashes(
  199. n2e(matcher.group(6)) + n2e(matcher.group(7)), scheme);
  200. path = unescape(rawPath);
  201. return;
  202. }
  203. matcher = RELATIVE_SCP_URI.matcher(s);
  204. if (matcher.matches()) {
  205. user = matcher.group(1);
  206. pass = matcher.group(2);
  207. host = matcher.group(3);
  208. rawPath = matcher.group(4);
  209. path = rawPath;
  210. return;
  211. }
  212. matcher = ABSOLUTE_SCP_URI.matcher(s);
  213. if (matcher.matches()) {
  214. user = matcher.group(1);
  215. pass = matcher.group(2);
  216. host = matcher.group(3);
  217. rawPath = matcher.group(4);
  218. path = rawPath;
  219. return;
  220. }
  221. matcher = LOCAL_FILE.matcher(s);
  222. if (matcher.matches()) {
  223. rawPath = matcher.group(1);
  224. path = rawPath;
  225. return;
  226. }
  227. throw new URISyntaxException(s, JGitText.get().cannotParseGitURIish);
  228. }
  229. private static int parseHexByte(byte c1, byte c2) {
  230. return ((RawParseUtils.parseHexInt4(c1) << 4)
  231. | RawParseUtils.parseHexInt4(c2));
  232. }
  233. private static String unescape(String s) throws URISyntaxException {
  234. if (s == null)
  235. return null;
  236. if (s.indexOf('%') < 0)
  237. return s;
  238. byte[] bytes;
  239. try {
  240. bytes = s.getBytes(Constants.CHARACTER_ENCODING);
  241. } catch (UnsupportedEncodingException e) {
  242. throw new RuntimeException(e); // can't happen
  243. }
  244. byte[] os = new byte[bytes.length];
  245. int j = 0;
  246. for (int i = 0; i < bytes.length; ++i) {
  247. byte c = bytes[i];
  248. if (c == '%') {
  249. if (i + 2 >= bytes.length)
  250. throw new URISyntaxException(s, JGitText.get().cannotParseGitURIish);
  251. byte c1 = bytes[i + 1];
  252. byte c2 = bytes[i + 2];
  253. int val;
  254. try {
  255. val = parseHexByte(c1, c2);
  256. } catch (ArrayIndexOutOfBoundsException e) {
  257. throw new URISyntaxException(s, JGitText.get().cannotParseGitURIish);
  258. }
  259. os[j++] = (byte) val;
  260. i += 2;
  261. } else
  262. os[j++] = c;
  263. }
  264. return RawParseUtils.decode(os, 0, j);
  265. }
  266. private static final BitSet reservedChars = new BitSet(127);
  267. static {
  268. for (byte b : Constants.encodeASCII("!*'();:@&=+$,/?#[]")) //$NON-NLS-1$
  269. reservedChars.set(b);
  270. }
  271. /**
  272. * Escape unprintable characters optionally URI-reserved characters
  273. *
  274. * @param s
  275. * The Java String to encode (may contain any character)
  276. * @param escapeReservedChars
  277. * true to escape URI reserved characters
  278. * @param encodeNonAscii
  279. * encode any non-ASCII characters
  280. * @return a URI-encoded string
  281. */
  282. private static String escape(String s, boolean escapeReservedChars,
  283. boolean encodeNonAscii) {
  284. if (s == null)
  285. return null;
  286. ByteArrayOutputStream os = new ByteArrayOutputStream(s.length());
  287. byte[] bytes;
  288. try {
  289. bytes = s.getBytes(Constants.CHARACTER_ENCODING);
  290. } catch (UnsupportedEncodingException e) {
  291. throw new RuntimeException(e); // cannot happen
  292. }
  293. for (int i = 0; i < bytes.length; ++i) {
  294. int b = bytes[i] & 0xFF;
  295. if (b <= 32 || (encodeNonAscii && b > 127) || b == '%'
  296. || (escapeReservedChars && reservedChars.get(b))) {
  297. os.write('%');
  298. byte[] tmp = Constants.encodeASCII(String.format("%02x", //$NON-NLS-1$
  299. Integer.valueOf(b)));
  300. os.write(tmp[0]);
  301. os.write(tmp[1]);
  302. } else {
  303. os.write(b);
  304. }
  305. }
  306. byte[] buf = os.toByteArray();
  307. return RawParseUtils.decode(buf, 0, buf.length);
  308. }
  309. private String n2e(String s) {
  310. if (s == null)
  311. return ""; //$NON-NLS-1$
  312. else
  313. return s;
  314. }
  315. // takes care to cut of a leading slash if a windows drive letter or a
  316. // user-home-dir specifications are
  317. private String cleanLeadingSlashes(String p, String s) {
  318. if (p.length() >= 3
  319. && p.charAt(0) == '/'
  320. && p.charAt(2) == ':'
  321. && (p.charAt(1) >= 'A' && p.charAt(1) <= 'Z' || p.charAt(1) >= 'a'
  322. && p.charAt(1) <= 'z'))
  323. return p.substring(1);
  324. else if (s != null && p.length() >= 2 && p.charAt(0) == '/'
  325. && p.charAt(1) == '~')
  326. return p.substring(1);
  327. else
  328. return p;
  329. }
  330. /**
  331. * Construct a URIish from a standard URL.
  332. *
  333. * @param u
  334. * the source URL to convert from.
  335. */
  336. public URIish(final URL u) {
  337. scheme = u.getProtocol();
  338. path = u.getPath();
  339. try {
  340. rawPath = u.toURI().getRawPath();
  341. } catch (URISyntaxException e) {
  342. throw new RuntimeException(e); // Impossible
  343. }
  344. final String ui = u.getUserInfo();
  345. if (ui != null) {
  346. final int d = ui.indexOf(':');
  347. user = d < 0 ? ui : ui.substring(0, d);
  348. pass = d < 0 ? null : ui.substring(d + 1);
  349. }
  350. port = u.getPort();
  351. host = u.getHost();
  352. }
  353. /** Create an empty, non-configured URI. */
  354. public URIish() {
  355. // Configure nothing.
  356. }
  357. private URIish(final URIish u) {
  358. this.scheme = u.scheme;
  359. this.rawPath = u.rawPath;
  360. this.path = u.path;
  361. this.user = u.user;
  362. this.pass = u.pass;
  363. this.port = u.port;
  364. this.host = u.host;
  365. }
  366. /**
  367. * @return true if this URI references a repository on another system.
  368. */
  369. public boolean isRemote() {
  370. return getHost() != null;
  371. }
  372. /**
  373. * @return host name part or null
  374. */
  375. public String getHost() {
  376. return host;
  377. }
  378. /**
  379. * Return a new URI matching this one, but with a different host.
  380. *
  381. * @param n
  382. * the new value for host.
  383. * @return a new URI with the updated value.
  384. */
  385. public URIish setHost(final String n) {
  386. final URIish r = new URIish(this);
  387. r.host = n;
  388. return r;
  389. }
  390. /**
  391. * @return protocol name or null for local references
  392. */
  393. public String getScheme() {
  394. return scheme;
  395. }
  396. /**
  397. * Return a new URI matching this one, but with a different scheme.
  398. *
  399. * @param n
  400. * the new value for scheme.
  401. * @return a new URI with the updated value.
  402. */
  403. public URIish setScheme(final String n) {
  404. final URIish r = new URIish(this);
  405. r.scheme = n;
  406. return r;
  407. }
  408. /**
  409. * @return path name component
  410. */
  411. public String getPath() {
  412. return path;
  413. }
  414. /**
  415. * @return path name component
  416. */
  417. public String getRawPath() {
  418. return rawPath;
  419. }
  420. /**
  421. * Return a new URI matching this one, but with a different path.
  422. *
  423. * @param n
  424. * the new value for path.
  425. * @return a new URI with the updated value.
  426. */
  427. public URIish setPath(final String n) {
  428. final URIish r = new URIish(this);
  429. r.path = n;
  430. r.rawPath = n;
  431. return r;
  432. }
  433. /**
  434. * Return a new URI matching this one, but with a different (raw) path.
  435. *
  436. * @param n
  437. * the new value for path.
  438. * @return a new URI with the updated value.
  439. * @throws URISyntaxException
  440. */
  441. public URIish setRawPath(final String n) throws URISyntaxException {
  442. final URIish r = new URIish(this);
  443. r.path = unescape(n);
  444. r.rawPath = n;
  445. return r;
  446. }
  447. /**
  448. * @return user name requested for transfer or null
  449. */
  450. public String getUser() {
  451. return user;
  452. }
  453. /**
  454. * Return a new URI matching this one, but with a different user.
  455. *
  456. * @param n
  457. * the new value for user.
  458. * @return a new URI with the updated value.
  459. */
  460. public URIish setUser(final String n) {
  461. final URIish r = new URIish(this);
  462. r.user = n;
  463. return r;
  464. }
  465. /**
  466. * @return password requested for transfer or null
  467. */
  468. public String getPass() {
  469. return pass;
  470. }
  471. /**
  472. * Return a new URI matching this one, but with a different password.
  473. *
  474. * @param n
  475. * the new value for password.
  476. * @return a new URI with the updated value.
  477. */
  478. public URIish setPass(final String n) {
  479. final URIish r = new URIish(this);
  480. r.pass = n;
  481. return r;
  482. }
  483. /**
  484. * @return port number requested for transfer or -1 if not explicit
  485. */
  486. public int getPort() {
  487. return port;
  488. }
  489. /**
  490. * Return a new URI matching this one, but with a different port.
  491. *
  492. * @param n
  493. * the new value for port.
  494. * @return a new URI with the updated value.
  495. */
  496. public URIish setPort(final int n) {
  497. final URIish r = new URIish(this);
  498. r.port = n > 0 ? n : -1;
  499. return r;
  500. }
  501. public int hashCode() {
  502. int hc = 0;
  503. if (getScheme() != null)
  504. hc = hc * 31 + getScheme().hashCode();
  505. if (getUser() != null)
  506. hc = hc * 31 + getUser().hashCode();
  507. if (getPass() != null)
  508. hc = hc * 31 + getPass().hashCode();
  509. if (getHost() != null)
  510. hc = hc * 31 + getHost().hashCode();
  511. if (getPort() > 0)
  512. hc = hc * 31 + getPort();
  513. if (getPath() != null)
  514. hc = hc * 31 + getPath().hashCode();
  515. return hc;
  516. }
  517. public boolean equals(final Object obj) {
  518. if (!(obj instanceof URIish))
  519. return false;
  520. final URIish b = (URIish) obj;
  521. if (!eq(getScheme(), b.getScheme()))
  522. return false;
  523. if (!eq(getUser(), b.getUser()))
  524. return false;
  525. if (!eq(getPass(), b.getPass()))
  526. return false;
  527. if (!eq(getHost(), b.getHost()))
  528. return false;
  529. if (getPort() != b.getPort())
  530. return false;
  531. if (!eq(getPath(), b.getPath()))
  532. return false;
  533. return true;
  534. }
  535. private static boolean eq(final String a, final String b) {
  536. if (a == b)
  537. return true;
  538. if (a == null || b == null)
  539. return false;
  540. return a.equals(b);
  541. }
  542. /**
  543. * Obtain the string form of the URI, with the password included.
  544. *
  545. * @return the URI, including its password field, if any.
  546. */
  547. public String toPrivateString() {
  548. return format(true, false);
  549. }
  550. public String toString() {
  551. return format(false, false);
  552. }
  553. private String format(final boolean includePassword, boolean escapeNonAscii) {
  554. final StringBuilder r = new StringBuilder();
  555. if (getScheme() != null) {
  556. r.append(getScheme());
  557. r.append("://"); //$NON-NLS-1$
  558. }
  559. if (getUser() != null) {
  560. r.append(escape(getUser(), true, escapeNonAscii));
  561. if (includePassword && getPass() != null) {
  562. r.append(':');
  563. r.append(escape(getPass(), true, escapeNonAscii));
  564. }
  565. }
  566. if (getHost() != null) {
  567. if (getUser() != null && getUser().length() > 0)
  568. r.append('@');
  569. r.append(escape(getHost(), false, escapeNonAscii));
  570. if (getScheme() != null && getPort() > 0) {
  571. r.append(':');
  572. r.append(getPort());
  573. }
  574. }
  575. if (getPath() != null) {
  576. if (getScheme() != null) {
  577. if (!getPath().startsWith("/")) //$NON-NLS-1$
  578. r.append('/');
  579. } else if (getHost() != null)
  580. r.append(':');
  581. if (getScheme() != null)
  582. if (escapeNonAscii)
  583. r.append(escape(getPath(), false, escapeNonAscii));
  584. else
  585. r.append(getRawPath());
  586. else
  587. r.append(getPath());
  588. }
  589. return r.toString();
  590. }
  591. /**
  592. * @return the URI as an ASCII string. Password is not included.
  593. */
  594. public String toASCIIString() {
  595. return format(false, true);
  596. }
  597. /**
  598. * @return the URI including password, formatted with only ASCII characters
  599. * such that it will be valid for use over the network.
  600. */
  601. public String toPrivateASCIIString() {
  602. return format(true, true);
  603. }
  604. /**
  605. * Get the "humanish" part of the path. Some examples of a 'humanish' part
  606. * for a full path:
  607. * <table summary="path vs humanish path" border="1">
  608. * <tr>
  609. * <th>Path</th>
  610. * <th>Humanish part</th>
  611. * </tr>
  612. * <tr>
  613. * <td><code>/path/to/repo.git</code></td>
  614. * <td rowspan="4"><code>repo</code></td>
  615. * </tr>
  616. * <tr>
  617. * <td><code>/path/to/repo.git/</code></td>
  618. * </tr>
  619. * <tr>
  620. * <td><code>/path/to/repo/.git</code></td>
  621. * </tr>
  622. * <tr>
  623. * <td><code>/path/to/repo/</code></td>
  624. * </tr>
  625. * <tr>
  626. * <td><code>/path//to</code></td>
  627. * <td>an empty string</td>
  628. * </tr>
  629. * </table>
  630. *
  631. * @return the "humanish" part of the path. May be an empty string. Never
  632. * {@code null}.
  633. * @throws IllegalArgumentException
  634. * if it's impossible to determine a humanish part, or path is
  635. * {@code null} or empty
  636. * @see #getPath
  637. */
  638. public String getHumanishName() throws IllegalArgumentException {
  639. if ("".equals(getPath()) || getPath() == null) //$NON-NLS-1$
  640. throw new IllegalArgumentException();
  641. String s = getPath();
  642. String[] elements;
  643. if ("file".equals(scheme) || LOCAL_FILE.matcher(s).matches()) //$NON-NLS-1$
  644. elements = s.split("[\\" + File.separatorChar + "/]"); //$NON-NLS-1$ //$NON-NLS-2$
  645. else
  646. elements = s.split("/+"); //$NON-NLS-1$
  647. if (elements.length == 0)
  648. throw new IllegalArgumentException();
  649. String result = elements[elements.length - 1];
  650. if (Constants.DOT_GIT.equals(result))
  651. result = elements[elements.length - 2];
  652. else if (result.endsWith(Constants.DOT_GIT_EXT))
  653. result = result.substring(0, result.length()
  654. - Constants.DOT_GIT_EXT.length());
  655. return result;
  656. }
  657. }