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 19KB

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