001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.util;
012:
013: import com.versant.core.common.BindingSupportImpl;
014:
015: import java.net.MalformedURLException;
016:
017: /**
018: * A URL parsed into its different parts. This differs from the URL class
019: * in that there does not have to be a handler for the protocol.
020: */
021: public class ParsedURL {
022:
023: private String orginal;
024: private String protocol;
025: private String host;
026: private int port = -1;
027: private String path;
028: private String query;
029: private String ref;
030:
031: /**
032: * Parse url and throw an user exception if invalid.
033: */
034: public ParsedURL(String url) {
035: this .orginal = url;
036: int i = url.indexOf(':');
037: if (i <= 0) {
038: throw BindingSupportImpl.getInstance().invalidOperation(
039: "Missing protocol: " + orginal);
040: }
041: protocol = url.substring(0, i);
042: url = url.substring(i + 1);
043: if (url.startsWith("//")) {
044: url = url.substring(2);
045: }
046: i = url.indexOf(':');
047: if (i >= 0) {
048: host = url.substring(0, i);
049: url = url.substring(i + 1);
050: i = url.indexOf('/');
051: String s;
052: if (i >= 0) {
053: s = url.substring(0, i);
054: url = url.substring(i);
055: } else {
056: s = url;
057: url = null;
058: }
059: try {
060: port = Integer.parseInt(s);
061: } catch (NumberFormatException e) {
062: throw BindingSupportImpl.getInstance()
063: .invalidOperation(
064: "Invalid port '" + s + "' in "
065: + orginal);
066: }
067: } else {
068: i = url.indexOf('/');
069: if (i >= 0) {
070: host = url.substring(0, i);
071: url = url.substring(i);
072: } else {
073: host = url;
074: url = null;
075: }
076: }
077: if (url != null) {
078: int ind = url.indexOf('#');
079: ref = ind < 0 ? null : url.substring(ind + 1);
080: url = ind < 0 ? url : url.substring(0, ind);
081: int q = url.lastIndexOf('?');
082: if (q != -1) {
083: query = url.substring(q + 1);
084: path = url.substring(0, q);
085: } else {
086: path = url;
087: }
088: }
089: }
090:
091: /**
092: * Just extract the protocol from url.
093: */
094: public static String getProtocol(String url) {
095: int i = url.indexOf(':');
096: return i < 0 ? null : url.substring(0, i);
097: }
098:
099: public String getUrl() {
100: return orginal;
101: }
102:
103: public String getProtocol() {
104: return protocol;
105: }
106:
107: public String getHost() {
108: return host;
109: }
110:
111: /**
112: * Get the port or -1 if none set.
113: */
114: public int getPort() {
115: return port;
116: }
117:
118: public String getPath() {
119: return path;
120: }
121:
122: public String getQuery() {
123: return query;
124: }
125:
126: public String getRef() {
127: return ref;
128: }
129:
130: }
|