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
 *
9
 *   '$Author: jones $'
10
 *     '$Date: 2006-11-10 10:25:38 -0800 (Fri, 10 Nov 2006) $'
11
 * '$Revision: 3077 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

    
28
package edu.ucsb.nceas.metacat;
29

    
30
import java.net.MalformedURLException;
31
import java.util.Hashtable;
32
import java.util.Enumeration;
33

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

    
286
      MetacatURL murl2 = new MetacatURL(testurl2);
287
      System.out.println("protocol: " + murl2.getProtocol());
288
      System.out.println("parameters: ");
289
      murl2.printParams();
290
      murl2.printHashParams();
291
      System.out.println("*********************************************");
292

    
293
      MetacatURL murl3 = new MetacatURL(testurl3);
294
      System.out.println("protocol: " + murl3.getProtocol());
295
      System.out.println("parameters: ");
296
      murl3.printParams();
297
      System.out.println("*********************************************");
298
    }
299
    catch(MalformedURLException murle)
300
    {
301
      System.out.println("bad url " + murle.getMessage()); 
302
    }
303
  }
304
  
305
}
(46-46/64)