Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

URIish.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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 String unescape(String s) throws URISyntaxException {
  230. if (s == null)
  231. return null;
  232. if (s.indexOf('%') < 0)
  233. return s;
  234. byte[] bytes;
  235. try {
  236. bytes = s.getBytes(Constants.CHARACTER_ENCODING);
  237. } catch (UnsupportedEncodingException e) {
  238. throw new RuntimeException(e); // can't happen
  239. }
  240. byte[] os = new byte[bytes.length];
  241. int j = 0;
  242. for (int i = 0; i < bytes.length; ++i) {
  243. byte c = bytes[i];
  244. if (c == '%') {
  245. if (i + 2 >= bytes.length)
  246. throw new URISyntaxException(s, JGitText.get().cannotParseGitURIish);
  247. int val = (RawParseUtils.parseHexInt4(bytes[i + 1]) << 4)
  248. | RawParseUtils.parseHexInt4(bytes[i + 2]);
  249. os[j++] = (byte) val;
  250. i += 2;
  251. } else
  252. os[j++] = c;
  253. }
  254. return RawParseUtils.decode(os, 0, j);
  255. }
  256. private static final BitSet reservedChars = new BitSet(127);
  257. static {
  258. for (byte b : Constants.encodeASCII("!*'();:@&=+$,/?#[]")) //$NON-NLS-1$
  259. reservedChars.set(b);
  260. }
  261. /**
  262. * Escape unprintable characters optionally URI-reserved characters
  263. *
  264. * @param s
  265. * The Java String to encode (may contain any character)
  266. * @param escapeReservedChars
  267. * true to escape URI reserved characters
  268. * @param encodeNonAscii
  269. * encode any non-ASCII characters
  270. * @return a URI-encoded string
  271. */
  272. private static String escape(String s, boolean escapeReservedChars,
  273. boolean encodeNonAscii) {
  274. if (s == null)
  275. return null;
  276. ByteArrayOutputStream os = new ByteArrayOutputStream(s.length());
  277. byte[] bytes;
  278. try {
  279. bytes = s.getBytes(Constants.CHARACTER_ENCODING);
  280. } catch (UnsupportedEncodingException e) {
  281. throw new RuntimeException(e); // cannot happen
  282. }
  283. for (int i = 0; i < bytes.length; ++i) {
  284. int b = bytes[i] & 0xFF;
  285. if (b <= 32 || (encodeNonAscii && b > 127) || b == '%'
  286. || (escapeReservedChars && reservedChars.get(b))) {
  287. os.write('%');
  288. byte[] tmp = Constants.encodeASCII(String.format("%02x", //$NON-NLS-1$
  289. Integer.valueOf(b)));
  290. os.write(tmp[0]);
  291. os.write(tmp[1]);
  292. } else {
  293. os.write(b);
  294. }
  295. }
  296. byte[] buf = os.toByteArray();
  297. return RawParseUtils.decode(buf, 0, buf.length);
  298. }
  299. private String n2e(String s) {
  300. if (s == null)
  301. return ""; //$NON-NLS-1$
  302. else
  303. return s;
  304. }
  305. // takes care to cut of a leading slash if a windows drive letter or a
  306. // user-home-dir specifications are
  307. private String cleanLeadingSlashes(String p, String s) {
  308. if (p.length() >= 3
  309. && p.charAt(0) == '/'
  310. && p.charAt(2) == ':'
  311. && (p.charAt(1) >= 'A' && p.charAt(1) <= 'Z' || p.charAt(1) >= 'a'
  312. && p.charAt(1) <= 'z'))
  313. return p.substring(1);
  314. else if (s != null && p.length() >= 2 && p.charAt(0) == '/'
  315. && p.charAt(1) == '~')
  316. return p.substring(1);
  317. else
  318. return p;
  319. }
  320. /**
  321. * Construct a URIish from a standard URL.
  322. *
  323. * @param u
  324. * the source URL to convert from.
  325. */
  326. public URIish(final URL u) {
  327. scheme = u.getProtocol();
  328. path = u.getPath();
  329. try {
  330. rawPath = u.toURI().getRawPath();
  331. } catch (URISyntaxException e) {
  332. throw new RuntimeException(e); // Impossible
  333. }
  334. final String ui = u.getUserInfo();
  335. if (ui != null) {
  336. final int d = ui.indexOf(':');
  337. user = d < 0 ? ui : ui.substring(0, d);
  338. pass = d < 0 ? null : ui.substring(d + 1);
  339. }
  340. port = u.getPort();
  341. host = u.getHost();
  342. }
  343. /** Create an empty, non-configured URI. */
  344. public URIish() {
  345. // Configure nothing.
  346. }
  347. private URIish(final URIish u) {
  348. this.scheme = u.scheme;
  349. this.rawPath = u.rawPath;
  350. this.path = u.path;
  351. this.user = u.user;
  352. this.pass = u.pass;
  353. this.port = u.port;
  354. this.host = u.host;
  355. }
  356. /**
  357. * @return true if this URI references a repository on another system.
  358. */
  359. public boolean isRemote() {
  360. return getHost() != null;
  361. }
  362. /**
  363. * @return host name part or null
  364. */
  365. public String getHost() {
  366. return host;
  367. }
  368. /**
  369. * Return a new URI matching this one, but with a different host.
  370. *
  371. * @param n
  372. * the new value for host.
  373. * @return a new URI with the updated value.
  374. */
  375. public URIish setHost(final String n) {
  376. final URIish r = new URIish(this);
  377. r.host = n;
  378. return r;
  379. }
  380. /**
  381. * @return protocol name or null for local references
  382. */
  383. public String getScheme() {
  384. return scheme;
  385. }
  386. /**
  387. * Return a new URI matching this one, but with a different scheme.
  388. *
  389. * @param n
  390. * the new value for scheme.
  391. * @return a new URI with the updated value.
  392. */
  393. public URIish setScheme(final String n) {
  394. final URIish r = new URIish(this);
  395. r.scheme = n;
  396. return r;
  397. }
  398. /**
  399. * @return path name component
  400. */
  401. public String getPath() {
  402. return path;
  403. }
  404. /**
  405. * @return path name component
  406. */
  407. public String getRawPath() {
  408. return rawPath;
  409. }
  410. /**
  411. * Return a new URI matching this one, but with a different path.
  412. *
  413. * @param n
  414. * the new value for path.
  415. * @return a new URI with the updated value.
  416. */
  417. public URIish setPath(final String n) {
  418. final URIish r = new URIish(this);
  419. r.path = n;
  420. r.rawPath = n;
  421. return r;
  422. }
  423. /**
  424. * Return a new URI matching this one, but with a different (raw) path.
  425. *
  426. * @param n
  427. * the new value for path.
  428. * @return a new URI with the updated value.
  429. * @throws URISyntaxException
  430. */
  431. public URIish setRawPath(final String n) throws URISyntaxException {
  432. final URIish r = new URIish(this);
  433. r.path = unescape(n);
  434. r.rawPath = n;
  435. return r;
  436. }
  437. /**
  438. * @return user name requested for transfer or null
  439. */
  440. public String getUser() {
  441. return user;
  442. }
  443. /**
  444. * Return a new URI matching this one, but with a different user.
  445. *
  446. * @param n
  447. * the new value for user.
  448. * @return a new URI with the updated value.
  449. */
  450. public URIish setUser(final String n) {
  451. final URIish r = new URIish(this);
  452. r.user = n;
  453. return r;
  454. }
  455. /**
  456. * @return password requested for transfer or null
  457. */
  458. public String getPass() {
  459. return pass;
  460. }
  461. /**
  462. * Return a new URI matching this one, but with a different password.
  463. *
  464. * @param n
  465. * the new value for password.
  466. * @return a new URI with the updated value.
  467. */
  468. public URIish setPass(final String n) {
  469. final URIish r = new URIish(this);
  470. r.pass = n;
  471. return r;
  472. }
  473. /**
  474. * @return port number requested for transfer or -1 if not explicit
  475. */
  476. public int getPort() {
  477. return port;
  478. }
  479. /**
  480. * Return a new URI matching this one, but with a different port.
  481. *
  482. * @param n
  483. * the new value for port.
  484. * @return a new URI with the updated value.
  485. */
  486. public URIish setPort(final int n) {
  487. final URIish r = new URIish(this);
  488. r.port = n > 0 ? n : -1;
  489. return r;
  490. }
  491. public int hashCode() {
  492. int hc = 0;
  493. if (getScheme() != null)
  494. hc = hc * 31 + getScheme().hashCode();
  495. if (getUser() != null)
  496. hc = hc * 31 + getUser().hashCode();
  497. if (getPass() != null)
  498. hc = hc * 31 + getPass().hashCode();
  499. if (getHost() != null)
  500. hc = hc * 31 + getHost().hashCode();
  501. if (getPort() > 0)
  502. hc = hc * 31 + getPort();
  503. if (getPath() != null)
  504. hc = hc * 31 + getPath().hashCode();
  505. return hc;
  506. }
  507. public boolean equals(final Object obj) {
  508. if (!(obj instanceof URIish))
  509. return false;
  510. final URIish b = (URIish) obj;
  511. if (!eq(getScheme(), b.getScheme()))
  512. return false;
  513. if (!eq(getUser(), b.getUser()))
  514. return false;
  515. if (!eq(getPass(), b.getPass()))
  516. return false;
  517. if (!eq(getHost(), b.getHost()))
  518. return false;
  519. if (getPort() != b.getPort())
  520. return false;
  521. if (!eq(getPath(), b.getPath()))
  522. return false;
  523. return true;
  524. }
  525. private static boolean eq(final String a, final String b) {
  526. if (a == b)
  527. return true;
  528. if (a == null || b == null)
  529. return false;
  530. return a.equals(b);
  531. }
  532. /**
  533. * Obtain the string form of the URI, with the password included.
  534. *
  535. * @return the URI, including its password field, if any.
  536. */
  537. public String toPrivateString() {
  538. return format(true, false);
  539. }
  540. public String toString() {
  541. return format(false, false);
  542. }
  543. private String format(final boolean includePassword, boolean escapeNonAscii) {
  544. final StringBuilder r = new StringBuilder();
  545. if (getScheme() != null) {
  546. r.append(getScheme());
  547. r.append("://"); //$NON-NLS-1$
  548. }
  549. if (getUser() != null) {
  550. r.append(escape(getUser(), true, escapeNonAscii));
  551. if (includePassword && getPass() != null) {
  552. r.append(':');
  553. r.append(escape(getPass(), true, escapeNonAscii));
  554. }
  555. }
  556. if (getHost() != null) {
  557. if (getUser() != null && getUser().length() > 0)
  558. r.append('@');
  559. r.append(escape(getHost(), false, escapeNonAscii));
  560. if (getScheme() != null && getPort() > 0) {
  561. r.append(':');
  562. r.append(getPort());
  563. }
  564. }
  565. if (getPath() != null) {
  566. if (getScheme() != null) {
  567. if (!getPath().startsWith("/")) //$NON-NLS-1$
  568. r.append('/');
  569. } else if (getHost() != null)
  570. r.append(':');
  571. if (getScheme() != null)
  572. if (escapeNonAscii)
  573. r.append(escape(getPath(), false, escapeNonAscii));
  574. else
  575. r.append(getRawPath());
  576. else
  577. r.append(getPath());
  578. }
  579. return r.toString();
  580. }
  581. /**
  582. * @return the URI as an ASCII string. Password is not included.
  583. */
  584. public String toASCIIString() {
  585. return format(false, true);
  586. }
  587. /**
  588. * @return the URI including password, formatted with only ASCII characters
  589. * such that it will be valid for use over the network.
  590. */
  591. public String toPrivateASCIIString() {
  592. return format(true, true);
  593. }
  594. /**
  595. * Get the "humanish" part of the path. Some examples of a 'humanish' part
  596. * for a full path:
  597. * <table summary="path vs humanish path" border="1">
  598. * <tr>
  599. * <th>Path</th>
  600. * <th>Humanish part</th>
  601. * </tr>
  602. * <tr>
  603. * <td><code>/path/to/repo.git</code></td>
  604. * <td rowspan="4"><code>repo</code></td>
  605. * </tr>
  606. * <tr>
  607. * <td><code>/path/to/repo.git/</code></td>
  608. * </tr>
  609. * <tr>
  610. * <td><code>/path/to/repo/.git</code></td>
  611. * </tr>
  612. * <tr>
  613. * <td><code>/path/to/repo/</code></td>
  614. * </tr>
  615. * <tr>
  616. * <td><code>/path//to</code></td>
  617. * <td>an empty string</td>
  618. * </tr>
  619. * </table>
  620. *
  621. * @return the "humanish" part of the path. May be an empty string. Never
  622. * {@code null}.
  623. * @throws IllegalArgumentException
  624. * if it's impossible to determine a humanish part, or path is
  625. * {@code null} or empty
  626. * @see #getPath
  627. */
  628. public String getHumanishName() throws IllegalArgumentException {
  629. if ("".equals(getPath()) || getPath() == null) //$NON-NLS-1$
  630. throw new IllegalArgumentException();
  631. String s = getPath();
  632. String[] elements;
  633. if ("file".equals(scheme) || LOCAL_FILE.matcher(s).matches()) //$NON-NLS-1$
  634. elements = s.split("[\\" + File.separatorChar + "/]"); //$NON-NLS-1$ //$NON-NLS-2$
  635. else
  636. elements = s.split("/+"); //$NON-NLS-1$
  637. if (elements.length == 0)
  638. throw new IllegalArgumentException();
  639. String result = elements[elements.length - 1];
  640. if (Constants.DOT_GIT.equals(result))
  641. result = elements[elements.length - 2];
  642. else if (result.endsWith(Constants.DOT_GIT_EXT))
  643. result = result.substring(0, result.length()
  644. - Constants.DOT_GIT_EXT.length());
  645. return result;
  646. }
  647. }