Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements a URL for use in identifying metacat
4
 *             files. It also handles http urls
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Chad Berkley
8
 *    Release: @release@
9
 *
10
 *   '$Author: jones $'
11
 *     '$Date: 2000-11-27 13:55:08 -0800 (Mon, 27 Nov 2000) $'
12
 * '$Revision: 566 $'
13
 */
14

    
15
package edu.ucsb.nceas.metacat;
16

    
17
import java.net.MalformedURLException;
18
import java.util.Hashtable;
19
import java.util.Enumeration;
20

    
21
public class MetacatURL
22
{
23
  private String[][] params = new String[200][2];
24
  private Hashtable paramsHash = new Hashtable();
25
  private String protocol = null;
26
  private String host = null;
27
  private String url;
28
  
29
  /**
30
   * This constructor takes a string url and parses it according to the  
31
   * following rules.  
32
   * 1) The protocol of the url is the text before the "://" symbol.
33
   * 2) Parameter names are written first and are terminated with the = symbol
34
   * 3) Parameter values come 2nd and are terminated with an & except for the
35
   *    last value
36
   * The form of the url looks like: 
37
   * protocol://server.domain.com/servlet/?name1=val1&name2=val2&nameN=valN
38
   * notice there is no & after the last param.  If one is there it is ignored.
39
   *
40
   * @param url the string to parse
41
   */
42
  public MetacatURL(String url) throws MalformedURLException
43
  {
44
    this.url = url;
45
    parseURL(url);
46
  }
47
  
48
  /**
49
   * This method takes a string url and parses it according to the following 
50
   * rules.  
51
   * 1) The protocol of the url is the text before the "://" symbol.
52
   * 2) Parameter names are written first and are terminated with the = symbol
53
   * 3) Parameter values come 2nd and are terminated with an & except for the
54
   *    last value
55
   * The form of the url looks like: 
56
   * protocol://server.domain.com/servlet/?name1=val1&name2=val2&nameN=valN
57
   * notice there is no & after the last param.  If one is there it is ignored.
58
   */
59
  private void parseURL(String url) throws MalformedURLException
60
  {
61
    String pname = null;
62
    String param = null;
63
    String temp = "";
64
    boolean ampflag = true;
65
    boolean poundflag = false;
66
    int arrcount = 0;
67
    
68
    //anything before this is the protocol
69
    int protocolIndex = url.indexOf("://"); 
70
    
71
    if (protocolIndex == -1) {
72
      // URL badly formed, no protocol
73
      throw new MalformedURLException("Invalid URL format: " +
74
                                      "no protocol provided.");
75
    }
76
    this.protocol = url.substring(0, protocolIndex);
77
    paramsHash.put("protocol", this.protocol);
78
    
79
    if(this.protocol.equals("http"))
80
    {//if this is an http url
81
      params[0][0] = "httpurl";
82
      params[0][1] = url.substring(0, url.length());
83
      paramsHash.put(params[0][0], params[0][1]);
84
      for(int i=url.length()-1; i>0; i--)
85
      {
86
        if(url.charAt(i) == '/')
87
        {
88
          i=0;
89
        }
90
        else
91
        {
92
          String exchange = temp;
93
          temp = "";
94
          temp += url.charAt(i);
95
          temp += exchange;
96
        }
97
      }
98
      params[1][0] = "filename";
99
      params[1][1] = temp;
100
      paramsHash.put(params[1][0], params[1][1]);
101
    }
102
    else
103
    {//other urls that meet the metacat type url structure.
104
      int hostIndex = url.indexOf("?");
105
      this.host = url.substring(protocolIndex + 3, hostIndex);
106
      paramsHash.put("host", this.host);
107
      for(int i=hostIndex + 1; i<url.length(); i++)
108
      { //go throught the remainder of the url one character at a time.
109
        if(url.charAt(i) == '=')
110
        { //if the current char is a # then the preceding should be a parametet
111
          //name
112
          if(!poundflag && ampflag)
113
          {
114
            params[arrcount][0] = temp.trim();
115
            temp = "";
116
          }
117
          else
118
          { //if there are two #s or &s in a row throw an exception.
119
            throw new MalformedURLException("metacatURL: Two parameter names " +
120
                                            "not allowed in sequence");
121
          }
122
          poundflag = true;
123
          ampflag = false;
124
        }
125
        else if(url.charAt(i) == '&' || i == url.length()-1)
126
        { //the text preceding the & should be the param value.
127
          if(i == url.length() - 1)
128
          { //if we are at the end of the string grab the last value and append it.
129
            if(url.charAt(i) != '=')
130
            { //ignore an extra & on the end of the string
131
              temp += url.charAt(i);
132
            }
133
          }
134
        
135
          if(!ampflag && poundflag)
136
          {
137
            params[arrcount][1] = temp.trim();
138
            paramsHash.put(params[arrcount][0], params[arrcount][1]);
139
            temp = "";
140
            arrcount++; //increment the array to the next row.
141
          }
142
          else
143
          { //if there are two =s or &s in a row through an exception
144
            throw new MalformedURLException("metacatURL: Two parameter values " +
145
                                            "not allowed in sequence");
146
          }
147
          poundflag = false;
148
          ampflag = true;
149
        }
150
        else
151
        { //get the next character in the string
152
          temp += url.charAt(i); 
153
        }
154
      }
155
    }
156
  }
157
  
158
  /**
159
   * Returns the type of the url. This is defined by the text before the "://" 
160
   * symbol in the url.
161
   */
162
  public String getProtocol()
163
  {
164
    return this.protocol; 
165
  }
166
  
167
  /**
168
   * Returns the parameters as a 2D string array.
169
   */
170
  public String[][] getParams()
171
  {
172
    return this.params;
173
  }
174
  
175
  /** 
176
   * Returns the parameters in a hashtable.
177
   */
178
  public Hashtable getHashParams()
179
  {
180
    return this.paramsHash;
181
  }
182
  
183
  /**
184
   * returns a single parameter from the hash by name
185
   * @param paramname the name of the parameter to return.
186
   */
187
  public Object getHashParam(String paramname)
188
  {
189
    return this.paramsHash.get(paramname);
190
  }
191
  
192
  /**
193
   * returns a string representation of this metacatURL
194
   */
195
  public String toString()
196
  {
197
    return this.url;
198
  }
199
  
200
  public void printHashParams()
201
  {
202
    Enumeration e = this.paramsHash.keys();
203
    System.out.println("name          value");
204
    System.out.println("-------------------");
205
    while(e.hasMoreElements())
206
    {
207
      String key = (String)e.nextElement();
208
      System.out.print(key);
209
      System.out.print("          ");
210
      System.out.println((String)this.paramsHash.get(key));
211
    }
212
  }
213
  
214
  /**
215
   * Prints the parameters neatly to system.out
216
   */
217
  public void printParams()
218
  {
219
    String[][] p = null;
220
    System.out.println("protocol: " + this.getProtocol());
221
    System.out.println("parameters: ");
222
    p = this.getParams();
223
    System.out.println("name          value");
224
    System.out.println("-------------------");
225
    for(int i=0; i<p.length; i++)
226
    {
227
      if(p[i][0] != null)
228
      {
229
        System.out.print(p[i][0]);
230
        System.out.print("          ");
231
        System.out.print(p[i][1]);
232
        System.out.println();
233
      }
234
    }
235
  }
236
  
237
  /**
238
   * Returns a single parameter and value as a 1D string array.
239
   *
240
   * @param index the index of the parameter, value array that you want.
241
   */
242
  public String[] getParam(int index)
243
  {
244
    String[] s = new String[2];
245
    s[0] = this.params[index][0].trim();
246
    s[1] = this.params[index][1].trim();
247
    //System.out.println("0: " + s[0]);
248
    //System.out.println("1: " + s[1]);
249
    return s;
250
  }
251
  
252
  /**
253
   * Test method for this class.
254
   */
255
  public static void main(String args[])
256
  {
257
    String testurl =  "metacat://dev.nceas.ucsb.edu?docid=NCEAS:10&username=chad&pasword=xyz";
258
    String testurl2 = "http://dev.nceas.ucsb.edu/berkley/testdata.dat";
259
    String testurl3 = "NCEAS.1287873498.32";
260
    try
261
    {
262
      System.out.println("*********************************************");
263
      MetacatURL murl = new MetacatURL(testurl);
264
      //String[][] p = null;
265
      System.out.println("protocol: " + murl.getProtocol());
266
      System.out.println("parameters: ");
267
      //p = murl.getParams();
268
      //Hashtable h = murl.getHashParams();
269
      murl.printParams();
270
      murl.printHashParams();
271
      System.out.println("*********************************************");
272

    
273
      MetacatURL murl2 = new MetacatURL(testurl2);
274
      System.out.println("protocol: " + murl2.getProtocol());
275
      System.out.println("parameters: ");
276
      murl2.printParams();
277
      murl2.printHashParams();
278
      System.out.println("*********************************************");
279

    
280
      MetacatURL murl3 = new MetacatURL(testurl3);
281
      System.out.println("protocol: " + murl3.getProtocol());
282
      System.out.println("parameters: ");
283
      murl3.printParams();
284
      System.out.println("*********************************************");
285
    }
286
    catch(MalformedURLException murle)
287
    {
288
      System.out.println("bad url " + murle.getMessage()); 
289
    }
290
  }
291
  
292
}
(30-30/38)