1 2 12 package com.versant.core.util; 13 14 import com.versant.core.common.BindingSupportImpl; 15 16 import java.net.MalformedURLException ; 17 18 22 public class ParsedURL { 23 24 private String orginal; 25 private String protocol; 26 private String host; 27 private int port = -1; 28 private String path; 29 private String query; 30 private String ref; 31 32 35 public ParsedURL(String url) { 36 this.orginal = url; 37 int i = url.indexOf(':'); 38 if (i <= 0) { 39 throw BindingSupportImpl.getInstance().invalidOperation( 40 "Missing protocol: " + orginal); 41 } 42 protocol = url.substring(0, i); 43 url = url.substring(i + 1); 44 if (url.startsWith("//")) { 45 url = url.substring(2); 46 } 47 i = url.indexOf(':'); 48 if (i >= 0) { 49 host = url.substring(0, i); 50 url = url.substring(i + 1); 51 i = url.indexOf('/'); 52 String s; 53 if (i >= 0) { 54 s = url.substring(0, i); 55 url = url.substring(i); 56 } else { 57 s = url; 58 url = null; 59 } 60 try { 61 port = Integer.parseInt(s); 62 } catch (NumberFormatException e) { 63 throw BindingSupportImpl.getInstance().invalidOperation( 64 "Invalid port '" + s + "' in " + orginal); 65 } 66 } else { 67 i = url.indexOf('/'); 68 if (i >= 0) { 69 host = url.substring(0, i); 70 url = url.substring(i); 71 } else { 72 host = url; 73 url = null; 74 } 75 } 76 if (url != null) { 77 int ind = url.indexOf('#'); 78 ref = ind < 0 ? null: url.substring(ind + 1); 79 url = ind < 0 ? url: url.substring(0, ind); 80 int q = url.lastIndexOf('?'); 81 if (q != -1) { 82 query = url.substring(q + 1); 83 path = url.substring(0, q); 84 } else { 85 path = url; 86 } 87 } 88 } 89 90 93 public static String getProtocol(String url) { 94 int i = url.indexOf(':'); 95 return i < 0 ? null : url.substring(0, i); 96 } 97 98 public String getUrl() { 99 return orginal; 100 } 101 102 public String getProtocol() { 103 return protocol; 104 } 105 106 public String getHost() { 107 return host; 108 } 109 110 113 public int getPort() { 114 return port; 115 } 116 117 public String getPath() { 118 return path; 119 } 120 121 public String getQuery() { 122 return query; 123 } 124 125 public String getRef() { 126 return ref; 127 } 128 129 } 130 131 | Popular Tags |