Skip to content

Package: URLName

URLName

nameinstructionbranchcomplexitylinemethod
URLName(String)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
URLName(String, String, int, String, String, String)
M: 66 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%
URLName(URL)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
_encode(String)
M: 117 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 29 C: 0
0%
M: 1 C: 0
0%
decode(String)
M: 89 C: 0
0%
M: 9 C: 0
0%
M: 6 C: 0
0%
M: 25 C: 0
0%
M: 1 C: 0
0%
encode(String)
M: 28 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
equals(Object)
M: 108 C: 0
0%
M: 36 C: 0
0%
M: 19 C: 0
0%
M: 25 C: 0
0%
M: 1 C: 0
0%
getFile()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getHost()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getHostAddress()
M: 27 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
getPassword()
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getPort()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getProtocol()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getRef()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getURL()
M: 28 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getUsername()
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hashCode()
M: 75 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%
indexOfAny(String, String)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
indexOfAny(String, String, int)
M: 23 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
parseString(String)
M: 221 C: 0
0%
M: 26 C: 0
0%
M: 14 C: 0
0%
M: 44 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 62 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
toString()
M: 109 C: 0
0%
M: 22 C: 0
0%
M: 12 C: 0
0%
M: 27 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package jakarta.mail;
18:
19: import java.net.*;
20:
21: import java.io.ByteArrayOutputStream;
22: import java.io.OutputStreamWriter;
23: import java.io.IOException;
24: import java.io.UnsupportedEncodingException;
25: import java.util.BitSet;
26: import java.util.Locale;
27:
28:
29: /**
30: * The name of a URL. This class represents a URL name and also
31: * provides the basic parsing functionality to parse most internet
32: * standard URL schemes. <p>
33: *
34: * Note that this class differs from <code>java.net.URL</code>
35: * in that this class just represents the name of a URL, it does
36: * not model the connection to a URL.
37: *
38: * @author        Christopher Cotton
39: * @author        Bill Shannon
40: */
41:
42: public class URLName {
43:
44: /**
45: * The full version of the URL
46: */
47: protected String fullURL;
48:
49: /**
50: * The protocol to use (ftp, http, nntp, imap, pop3 ... etc.) .
51: */
52: private String protocol;
53:
54: /**
55: * The username to use when connecting
56: */
57: private String username;
58:
59: /**
60: * The password to use when connecting.
61: */
62: private String password;
63:
64: /**
65: * The host name to which to connect.
66: */
67: private String host;
68:
69: /**
70: * The host's IP address, used in equals and hashCode.
71: * Computed on demand.
72: */
73: private InetAddress hostAddress;
74: private boolean hostAddressKnown = false;
75:
76: /**
77: * The protocol port to connect to.
78: */
79: private int port = -1;
80:
81: /**
82: * The specified file name on that host.
83: */
84: private String file;
85:
86: /**
87: * # reference.
88: */
89: private String ref;
90:
91: /**
92: * Our hash code.
93: */
94: private int hashCode = 0;
95:
96: /**
97: * A way to turn off encoding, just in case...
98: */
99: private static boolean doEncode = true;
100:
101: static {
102:         try {
103:•         doEncode = !Boolean.getBoolean("mail.URLName.dontencode");
104:         } catch (Exception ex) {
105:          // ignore any errors
106:         }
107: }
108:
109: /**
110: * Creates a URLName object from the specified protocol,
111: * host, port number, file, username, and password. Specifying a port
112: * number of -1 indicates that the URL should use the default port for
113: * the protocol.
114: *
115: * @param        protocol        the protocol
116: * @param        host                the host name
117: * @param        port                the port number
118: * @param        file                the file
119: * @param        username        the user name
120: * @param        password        the password
121: */
122: public URLName(
123:         String protocol,
124:         String host,
125:         int port,
126:         String file,
127:         String username,
128:         String password
129:         )
130: {
131:         this.protocol = protocol;
132:         this.host = host;
133:         this.port = port;
134:         int refStart;
135:•        if (file != null && (refStart = file.indexOf('#')) != -1) {
136:          this.file = file.substring(0, refStart);
137:          this.ref = file.substring(refStart + 1);
138:         } else {
139:          this.file = file;
140:          this.ref = null;
141:         }
142:•        this.username = doEncode ? encode(username) : username;
143:•        this.password = doEncode ? encode(password) : password;
144: }
145:
146: /**
147: * Construct a URLName from a java.net.URL object.
148: *
149: * @param        url        the URL
150: */
151: public URLName(URL url) {
152:         this(url.toString());
153: }
154:
155: /**
156: * Construct a URLName from the string. Parses out all the possible
157: * information (protocol, host, port, file, username, password).
158: *
159: * @param        url        the URL string
160: */
161: public URLName(String url) {
162:         parseString(url);
163: }
164:
165: /**
166: * Constructs a string representation of this URLName.
167: */
168: @Override
169: public String toString() {
170:•        if (fullURL == null) {
171:          // add the "protocol:"
172:          StringBuilder tempURL = new StringBuilder();
173:•         if (protocol != null) {
174:                 tempURL.append(protocol);
175:                 tempURL.append(":");
176:          }
177:
178:•         if (username != null || host != null) {
179:                 // add the "//"
180:                 tempURL.append("//");
181:                 
182:                 // add the user:password@
183:                 // XXX - can you just have a password? without a username?
184:•                if (username != null) {
185:                  tempURL.append(username);
186:                 
187:•                 if (password != null){
188:                         tempURL.append(":");
189:                         tempURL.append(password);
190:                  }
191:                 
192:                  tempURL.append("@");
193:                 }
194:         
195:                 // add host
196:•                if (host != null) {
197:                  tempURL.append(host);
198:                 }
199:         
200:                 // add port (if needed)
201:•                if (port != -1) {
202:                  tempURL.append(":");
203:                  tempURL.append(Integer.toString(port));
204:                 }
205:•                if (file != null)
206:                  tempURL.append("/");
207:          }
208:         
209:          // add the file
210:•         if (file != null) {
211:                 tempURL.append(file);
212:          }
213:         
214:          // add the ref
215:•         if (ref != null) {
216:                 tempURL.append("#");
217:                 tempURL.append(ref);
218:          }
219:
220:          // create the fullURL now
221:          fullURL = tempURL.toString();
222:         }
223:
224:         return fullURL;
225: }
226:
227: /**
228: * Method which does all of the work of parsing the string.
229: *
230: * @param        url        the URL string to parse
231: */
232: protected void parseString(String url) {
233:         // initialize everything in case called from subclass
234:         // (URLName really should be a final class)
235:         protocol = file = ref = host = username = password = null;
236:         port = -1;
237:
238:         int len = url.length();
239:
240:         // find the protocol
241:         // XXX - should check for only legal characters before the colon
242:         // (legal: a-z, A-Z, 0-9, "+", ".", "-")
243:         int protocolEnd = url.indexOf(':');
244:• if (protocolEnd != -1)
245:          protocol = url.substring(0, protocolEnd);
246:
247:         // is this an Internet standard URL that contains a host name?
248:•        if (url.regionMatches(protocolEnd + 1, "//", 0, 2)) {
249:          // find where the file starts
250:          String fullhost = null;
251:          int fileStart = url.indexOf('/', protocolEnd + 3);
252:•         if (fileStart != -1) {
253:                 fullhost = url.substring(protocolEnd + 3, fileStart);
254:•                if (fileStart + 1 < len)
255:                  file = url.substring(fileStart + 1);
256:                 else
257:                  file = "";
258:          } else
259:                 fullhost = url.substring(protocolEnd + 3);
260:
261:          // examine the fullhost, for username password etc.
262:          int i = fullhost.indexOf('@');
263:•         if (i != -1) {
264:                 String fulluserpass = fullhost.substring(0, i);
265:                 fullhost = fullhost.substring(i + 1);
266:
267:                 // get user and password
268:                 int passindex = fulluserpass.indexOf(':');
269:•                if (passindex != -1) {
270:                  username = fulluserpass.substring(0, passindex);
271:                  password = fulluserpass.substring(passindex + 1);
272:                 } else {
273:                  username = fulluserpass;
274:                 }
275:          }
276:         
277:          // get the port (if there)
278:          int portindex;
279:•         if (fullhost.length() > 0 && fullhost.charAt(0) == '[') {
280:                 // an IPv6 address?
281:                 portindex = fullhost.indexOf(':', fullhost.indexOf(']'));
282:          } else {
283:                 portindex = fullhost.indexOf(':');
284:          }
285:•         if (portindex != -1) {
286:                 String portstring = fullhost.substring(portindex + 1);
287:•                if (portstring.length() > 0) {
288:                  try {
289:                         port = Integer.parseInt(portstring);
290:                  } catch (NumberFormatException nfex) {
291:                         port = -1;
292:                  }
293:                 }
294:                 
295:                 host = fullhost.substring(0, portindex);
296:          } else {
297:                 host = fullhost;
298:          }
299:         } else {
300:•         if (protocolEnd + 1 < len)
301:                 file = url.substring(protocolEnd + 1);
302:         }
303:
304:         // extract the reference from the file name, if any
305:         int refStart;
306:•        if (file != null && (refStart = file.indexOf('#')) != -1) {
307:          ref = file.substring(refStart + 1);
308:          file = file.substring(0, refStart);
309:         }
310: }
311:
312: /**
313: * Returns the port number of this URLName.
314: * Returns -1 if the port is not set.
315: *
316: * @return        the port number
317: */
318: public int getPort() {
319:         return port;
320: }
321:
322: /**
323: * Returns the protocol of this URLName.
324: * Returns null if this URLName has no protocol.
325: *
326: * @return        the protocol
327: */
328: public String getProtocol() {
329:         return protocol;
330: }
331:
332: /**
333: * Returns the file name of this URLName.
334: * Returns null if this URLName has no file name.
335: *
336: * @return        the file name of this URLName
337: */
338: public String getFile() {
339:         return file;
340: }
341:
342: /**
343: * Returns the reference of this URLName.
344: * Returns null if this URLName has no reference.
345: *
346: * @return        the reference part of the URLName
347: */
348: public String getRef() {
349:         return ref;
350: }
351:
352: /**
353: * Returns the host of this URLName.
354: * Returns null if this URLName has no host.
355: *
356: * @return        the host name
357: */
358: public String getHost() {
359:         return host;
360: }
361:
362: /**
363: * Returns the user name of this URLName.
364: * Returns null if this URLName has no user name.
365: *
366: * @return        the user name
367: */
368: public String getUsername() {
369:•        return doEncode ? decode(username) : username;
370: }
371:
372: /**
373: * Returns the password of this URLName.
374: * Returns null if this URLName has no password.
375: *
376: * @return        the password
377: */
378: public String getPassword() {
379:•        return doEncode ? decode(password) : password;
380: }
381:
382: /**
383: * Constructs a URL from the URLName.
384: *
385: * @return        the URL
386: * @exception        MalformedURLException if the URL is malformed
387: */
388: public URL getURL() throws MalformedURLException {
389:         // URL expects the file to include the separating "/"
390:         String f = getFile();
391:•        if (f == null)
392:          f = "";
393:         else
394:          f = "/" + f;
395: return new URL(getProtocol(), getHost(), getPort(), f);
396: }
397:
398: /**
399: * Compares two URLNames. The result is true if and only if the
400: * argument is not null and is a URLName object that represents the
401: * same URLName as this object. Two URLName objects are equal if
402: * they have the same protocol and the same host,
403: * the same port number on the host, the same username,
404: * and the same file on the host. The fields (host, username,
405: * file) are also considered the same if they are both
406: * null. <p>
407: *
408: * Hosts are considered equal if the names are equal (case independent)
409: * or if host name lookups for them both succeed and they both reference
410: * the same IP address. <p>
411: *
412: * Note that URLName has no knowledge of default port numbers for
413: * particular protocols, so "imap://host" and "imap://host:143"
414: * would not compare as equal. <p>
415: *
416: * Note also that the password field is not included in the comparison,
417: * nor is any reference field appended to the filename.
418: */
419: @Override
420: public boolean equals(Object obj) {
421:• if (!(obj instanceof URLName))
422:          return false;
423:         URLName u2 = (URLName)obj;
424:
425:         // compare protocols
426:•        if (!(protocol == u2.protocol ||
427:•                (protocol != null && protocol.equals(u2.protocol))))
428:          return false;
429:
430:         // compare hosts
431:         InetAddress a1 = getHostAddress(), a2 = u2.getHostAddress();
432:         // if we have internet address for both, and they're not the same, fail
433:•        if (a1 != null && a2 != null) {
434:•         if (!a1.equals(a2))
435:                 return false;
436:         // else, if we have host names for both, and they're not the same, fail
437:•        } else if (host != null && u2.host != null) {
438:•         if (!host.equalsIgnoreCase(u2.host))
439:                 return false;
440:         // else, if not both null
441:•        } else if (host != u2.host) {
442:          return false;
443:         }
444:         // at this point, hosts match
445:
446:         // compare usernames
447:•        if (!(username == u2.username ||
448:•                (username != null && username.equals(u2.username))))
449:          return false;
450:
451:         // Forget about password since it doesn't
452:         // really denote a different store.
453:
454:         // compare files
455:•        String f1 = file == null ? "" : file;
456:•        String f2 = u2.file == null ? "" : u2.file;
457:
458:•        if (!f1.equals(f2))
459:          return false;
460:
461:         // compare ports
462:•        if (port != u2.port)
463:          return false;
464:
465:         // all comparisons succeeded, they're equal
466: return true;
467: }
468:
469: /**
470: * Compute the hash code for this URLName.
471: */
472: @Override
473: public int hashCode() {
474:•        if (hashCode != 0)
475:          return hashCode;
476:•        if (protocol != null)
477:          hashCode += protocol.hashCode();
478:         InetAddress addr = getHostAddress();
479:•        if (addr != null)
480:          hashCode += addr.hashCode();
481:•        else if (host != null)
482:          hashCode += host.toLowerCase(Locale.ENGLISH).hashCode();
483:•        if (username != null)
484:          hashCode += username.hashCode();
485:•        if (file != null)
486:          hashCode += file.hashCode();
487:         hashCode += port;
488:         return hashCode;
489: }
490:
491: /**
492: * Get the IP address of our host. Look up the
493: * name the first time and remember that we've done
494: * so, whether the lookup fails or not.
495: */
496: private synchronized InetAddress getHostAddress() {
497:•        if (hostAddressKnown)
498:          return hostAddress;
499:•        if (host == null)
500:          return null;
501:         try {
502:          hostAddress = InetAddress.getByName(host);
503:         } catch (UnknownHostException ex) {
504:          hostAddress = null;
505:         }
506:         hostAddressKnown = true;
507:         return hostAddress;
508: }
509:
510: /**
511: * The class contains a utility method for converting a
512: * <code>String</code> into a MIME format called
513: * "<code>x-www-form-urlencoded</code>" format.
514: * <p>
515: * To convert a <code>String</code>, each character is examined in turn:
516: * <ul>
517: * <li>The ASCII characters '<code>a</code>' through '<code>z</code>',
518: * '<code>A</code>' through '<code>Z</code>', '<code>0</code>'
519: * through '<code>9</code>', and ".", "-",
520: * "*", "_" remain the same.
521: * <li>The space character '<code> </code>' is converted into a
522: * plus sign '<code>+</code>'.
523: * <li>All other characters are converted into the 3-character string
524: * "<code>%<i>xy</i></code>", where <i>xy</i> is the two-digit
525: * hexadecimal representation of the lower 8-bits of the character.
526: * </ul>
527: *
528: * @author Herb Jellinek
529: * @since JDK1.0
530: */
531: static BitSet dontNeedEncoding;
532: static final int caseDiff = ('a' - 'A');
533:
534: /* The list of characters that are not encoded have been determined by
535: referencing O'Reilly's "HTML: The Definitive Guide" (page 164). */
536:
537: static {
538:         dontNeedEncoding = new BitSet(256);
539:         int i;
540:•        for (i = 'a'; i <= 'z'; i++) {
541:          dontNeedEncoding.set(i);
542:         }
543:•        for (i = 'A'; i <= 'Z'; i++) {
544:          dontNeedEncoding.set(i);
545:         }
546:•        for (i = '0'; i <= '9'; i++) {
547:          dontNeedEncoding.set(i);
548:         }
549:         /* encoding a space to a + is done in the encode() method */
550:         dontNeedEncoding.set(' ');
551:         dontNeedEncoding.set('-');
552:         dontNeedEncoding.set('_');
553:         dontNeedEncoding.set('.');
554:         dontNeedEncoding.set('*');
555: }
556:
557: /**
558: * Translates a string into <code>x-www-form-urlencoded</code> format.
559: *
560: * @param s <code>String</code> to be translated.
561: * @return the translated <code>String</code>.
562: */
563: static String encode(String s) {
564:•        if (s == null)
565:          return null;
566:         // the common case is no encoding is needed
567:•        for (int i = 0; i < s.length(); i++) {
568:          int c = (int)s.charAt(i);
569:•         if (c == ' ' || !dontNeedEncoding.get(c))
570:                 return _encode(s);
571:         }
572:         return s;
573: }
574:
575: private static String _encode(String s) {
576:         int maxBytesPerChar = 10;
577: StringBuilder out = new StringBuilder(s.length());
578:         ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
579:         OutputStreamWriter writer = new OutputStreamWriter(buf);
580:
581:•        for (int i = 0; i < s.length(); i++) {
582:          int c = (int)s.charAt(i);
583:•         if (dontNeedEncoding.get(c)) {
584:•                if (c == ' ') {
585:                  c = '+';
586:                 }
587:                 out.append((char)c);
588:          } else {
589:                 // convert to external encoding before hex conversion
590:                 try {
591:                  writer.write(c);
592:                  writer.flush();
593:                 } catch(IOException e) {
594:                  buf.reset();
595:                  continue;
596:                 }
597:                 byte[] ba = buf.toByteArray();
598:•                for (int j = 0; j < ba.length; j++) {
599:                  out.append('%');
600:                  char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
601:                  // converting to use uppercase letter as part of
602:                  // the hex value if ch is a letter.
603:•                 if (Character.isLetter(ch)) {
604:                         ch -= caseDiff;
605:                  }
606:                  out.append(ch);
607:                  ch = Character.forDigit(ba[j] & 0xF, 16);
608:•                 if (Character.isLetter(ch)) {
609:                         ch -= caseDiff;
610:                  }
611:                  out.append(ch);
612:                 }
613:                 buf.reset();
614:          }
615:         }
616:
617:         return out.toString();
618: }
619:
620:
621: /**
622: * The class contains a utility method for converting from
623: * a MIME format called "<code>x-www-form-urlencoded</code>"
624: * to a <code>String</code>
625: * <p>
626: * To convert to a <code>String</code>, each character is examined in turn:
627: * <ul>
628: * <li>The ASCII characters '<code>a</code>' through '<code>z</code>',
629: * '<code>A</code>' through '<code>Z</code>', and '<code>0</code>'
630: * through '<code>9</code>' remain the same.
631: * <li>The plus sign '<code>+</code>'is converted into a
632: * space character '<code> </code>'.
633: * <li>The remaining characters are represented by 3-character
634: * strings which begin with the percent sign,
635: * "<code>%<i>xy</i></code>", where <i>xy</i> is the two-digit
636: * hexadecimal representation of the lower 8-bits of the character.
637: * </ul>
638: *
639: * @author Mark Chamness
640: * @author Michael McCloskey
641: * @since 1.2
642: */
643:
644: /**
645: * Decodes a "x-www-form-urlencoded"
646: * to a <code>String</code>.
647: * @param s the <code>String</code> to decode
648: * @return the newly decoded <code>String</code>
649: */
650: static String decode(String s) {
651:•        if (s == null)
652:          return null;
653:•        if (indexOfAny(s, "+%") == -1)
654:          return s;                // the common case
655:
656: StringBuilder sb = new StringBuilder();
657:• for (int i = 0; i < s.length(); i++) {
658: char c = s.charAt(i);
659:• switch (c) {
660: case '+':
661: sb.append(' ');
662: break;
663: case '%':
664: try {
665: sb.append((char)Integer.parseInt(
666: s.substring(i+1,i+3),16));
667: } catch (NumberFormatException e) {
668: throw new IllegalArgumentException(
669:                          "Illegal URL encoded value: " +
670:                          s.substring(i,i+3));
671: }
672: i += 2;
673: break;
674: default:
675: sb.append(c);
676: break;
677: }
678: }
679: // Undo conversion to external encoding
680: String result = sb.toString();
681: try {
682: byte[] inputBytes = result.getBytes("8859_1");
683: result = new String(inputBytes);
684: } catch (UnsupportedEncodingException e) {
685: // The system should always have 8859_1
686: }
687: return result;
688: }
689:
690: /**
691: * Return the first index of any of the characters in "any" in "s",
692: * or -1 if none are found.
693: *
694: * This should be a method on String.
695: */
696: private static int indexOfAny(String s, String any) {
697:         return indexOfAny(s, any, 0);
698: }
699:
700: private static int indexOfAny(String s, String any, int start) {
701:         try {
702:          int len = s.length();
703:•         for (int i = start; i < len; i++) {
704:•                if (any.indexOf(s.charAt(i)) >= 0)
705:                  return i;
706:          }
707:          return -1;
708:         } catch (StringIndexOutOfBoundsException e) {
709:          return -1;
710:         }
711: }
712:
713: /*
714: // Do not remove, this is needed when testing new URL cases
715: public static void main(String[] argv) {
716:         String [] testURLNames = {
717:          "protocol://userid:password@host:119/file",
718:          "http://funny/folder/file.html",
719:          "http://funny/folder/file.html#ref",
720:          "http://funny/folder/file.html#",
721:          "http://funny/#ref",
722:          "imap://jmr:secret@labyrinth//var/mail/jmr",
723:          "nntp://fred@labyrinth:143/save/it/now.mbox",
724:          "imap://jmr@labyrinth/INBOX",
725:          "imap://labryrinth",
726:          "imap://labryrinth/",
727:          "file:",
728:          "file:INBOX",
729:          "file:/home/shannon/mail/foo",
730:          "/tmp/foo",
731:          "//host/tmp/foo",
732:          ":/tmp/foo",
733:          "/really/weird:/tmp/foo#bar",
734:          ""
735:         };
736:
737:         URLName url =
738:          new URLName("protocol", "host", 119, "file", "userid", "password");
739:         System.out.println("Test URL: " + url.toString());
740:         if (argv.length == 0) {
741:          for (int i = 0; i < testURLNames.length; i++) {
742:                 print(testURLNames[i]);
743:                 System.out.println();
744:          }
745:         } else {
746:          for (int i = 0; i < argv.length; i++) {
747:                 print(argv[i]);
748:                 System.out.println();
749:          }
750:          if (argv.length == 2) {
751:                 URLName u1 = new URLName(argv[0]);
752:                 URLName u2 = new URLName(argv[1]);
753:                 System.out.println("URL1 hash code: " + u1.hashCode());
754:                 System.out.println("URL2 hash code: " + u2.hashCode());
755:                 if (u1.equals(u2))
756:                  System.out.println("success, equal");
757:                 else
758:                  System.out.println("fail, not equal");
759:                 if (u2.equals(u1))
760:                  System.out.println("success, equal");
761:                 else
762:                  System.out.println("fail, not equal");
763:                 if (u1.hashCode() == u2.hashCode())
764:                  System.out.println("success, hashCodes equal");
765:                 else
766:                  System.out.println("fail, hashCodes not equal");
767:          }
768:         }
769: }
770:
771: private static void print(String name) {
772:         URLName url = new URLName(name);
773:         System.out.println("Original URL: " + name);
774:         System.out.println("The fullUrl : " + url.toString());
775:         if (!name.equals(url.toString()))
776:          System.out.println(" : NOT EQUAL!");
777:         System.out.println("The protocol is: " + url.getProtocol());
778:         System.out.println("The host is: " + url.getHost());
779:         System.out.println("The port is: " + url.getPort());
780:         System.out.println("The user is: " + url.getUsername());
781:         System.out.println("The password is: " + url.getPassword());
782:         System.out.println("The file is: " + url.getFile());
783:         System.out.println("The ref is: " + url.getRef());
784: }
785: */
786: }