001: /*
002: * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF
003: * NETSCAPE COMMUNICATIONS CORPORATION
004: *
005: * Copyright (c) 1996 Netscape Communications Corporation.
006: * All Rights Reserved.
007: * Use of this Source Code is subject to the terms of the applicable
008: * license agreement from Netscape Communications Corporation.
009: */
010:
011: package soif;
012:
013: import util.AltSTokenizer;
014: import util.ReportError;
015:
016: import java.applet.Applet;
017:
018: /**
019: Encapsulate Compass Server ID.
020: *
021: */
022: public class CSID {
023: private int compass;
024: private String host;
025: private String port;
026: private String name;
027:
028: private final static int XCAT = 100;
029: /**
030: * Compass.
031: */
032: public final static int XCOMPASS = XCAT + 1;
033: /**
034: * Secure compass.
035: */
036: public final static int XCOMPASSS = XCAT + 2;
037:
038: /**
039: * Constructor.
040: * @param compass only x-catalog or x-catalogs are valid
041: * @param host blank not valid
042: * @param port port number, if blank, defaults to 80
043: * @param name target
044: */
045: public CSID(int compass, String host, String port, String name) {
046: if (host == null) {
047: ReportError.reportError(ReportError.USER, "CSID", "CSID",
048: Messages.CSIDBADHOSTPARAM);
049: } else if (host.length() == 0) {
050: ReportError.reportError(ReportError.USER, "CSID", "CSID",
051: Messages.CSIDBADHOSTPARAM);
052: }
053:
054: if (port == null) {
055: ReportError.reportError(ReportError.USER, "CSID", "CSID",
056: Messages.CSIDBADPORTPARAM);
057: } else if (port.length() == 0) {
058: port = "80";
059: }
060:
061: if (name == null) {
062: ReportError.reportError(ReportError.USER, "CSID", "CSID",
063: Messages.CSIDBADNAMEPARAM);
064: } else if (name.length() == 0) {
065: ReportError.reportError(ReportError.USER, "CSID", "CSID",
066: Messages.CSIDBADNAMEPARAM);
067: }
068:
069: this .compass = compass;
070: this .host = host;
071: this .port = port;
072: this .name = name;
073:
074: }
075:
076: /**
077: * Constructor.
078: * @param urlStr send url string, examined to determine protocol
079: * @param host blank not valid
080: * @param port port number, if blank, defaults to 80
081: * @param name target
082: */
083: public CSID(String urlStr, String host, String port, String name) {
084: this (XCAT, host, port, name);
085: setCompass(urlStr);
086: }
087:
088: /**
089: * Constructor. Parse a string version, protected so
090: * we don't have to write a smart parse.
091: * <b>Untested!</b>
092: * @param s CSID in string form
093: */
094: public CSID(String csids) {
095: String s;
096:
097: AltSTokenizer ast = new AltSTokenizer(csids, ":/");
098:
099: s = ast.nextToken();
100: this .compass = stringToCompass(s);
101: this .host = ast.nextToken();
102: this .port = ast.nextToken();
103: ast.nextChar();
104: this .name = ast.nextToken("");
105: }
106:
107: /**
108: * Set the compass type based on the URL.
109: * @param urlStr send url string, examined to determine protocol
110: */
111: private void setCompass(String urlStr) {
112:
113: if (urlStr.substring(0, 6).compareTo((String) "https:") == 0) {
114: int ndx;
115:
116: this .compass = XCOMPASSS;
117: ndx = urlStr.indexOf("/http:");
118: if (ndx > 0) {
119: this .compass = XCOMPASS;
120: }
121: } else if (urlStr.substring(0, 5).compareTo((String) "http:") == 0) {
122: this .compass = XCOMPASS;
123: }
124: }
125:
126: /**
127: * Translate constant to string.
128: * @param i constant
129: */
130: public static String compassToString(int i) {
131: switch (i) {
132: case XCOMPASS:
133: return "x-catalog";
134: case XCOMPASSS:
135: return "x-catalogs";
136: default:
137: return "Unknown Compass";
138: }
139: }
140:
141: /**
142: * Translate string to constant.
143: * @param s string
144: */
145: public static int stringToCompass(String s) {
146: if (s.compareTo((String) "x-catalog") == 0) {
147: return XCOMPASS;
148: }
149:
150: if (s.compareTo((String) "x-catalogs") == 0) {
151: return XCOMPASSS;
152: }
153:
154: return XCAT;
155: }
156:
157: /**
158: * Is the instance valid?
159: */
160: public boolean isValid() {
161: if ((compass != XCOMPASS) && (compass != XCOMPASSS)) {
162: return false;
163: }
164:
165: if (host == null) {
166: return false;
167: }
168: if (port == null) {
169: return false;
170: }
171: if (name == null) {
172: return false;
173: }
174:
175: if (host.length() == 0) {
176: return false;
177: }
178: if (port.length() == 0) {
179: return false;
180: }
181: if (name.length() == 0) {
182: return false;
183: }
184:
185: return true;
186: }
187:
188: /**
189: * Get the compass.
190: */
191: public int getCompass() {
192: return compass;
193: }
194:
195: /**
196: * Get the host.
197: */
198: public String getHost() {
199: return host;
200: }
201:
202: /**
203: * Get the port.
204: */
205: public String getPort() {
206: return port;
207: }
208:
209: /**
210: * Get the name.
211: */
212: public String getName() {
213: return name;
214: }
215:
216: /**
217: * Secure?
218: */
219: public boolean isSecure() {
220: return compass == XCOMPASSS;
221: }
222:
223: /**
224: * Return valid CSID string.
225: */
226: public String toString() {
227: return compassToString(compass) + "://" + host + ":" + port
228: + "/" + name;
229: }
230: }
|