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: berkley $'
11
 *     '$Date: 2000-09-26 15:06:52 -0700 (Tue, 26 Sep 2000) $'
12
 * '$Revision: 465 $'
13
 */
14

    
15
package edu.ucsb.nceas.metacat;
16

    
17
import java.net.MalformedURLException;
18

    
19
public class metacatURL
20
{
21
  private String[][] params = new String[200][2];
22
  private String urlType = null;
23
  private String url;
24
  
25
  /**
26
   * This constructor takes a string url and parses it according to the  
27
   * following rules.  
28
   * 1) The name of the url is the text before the "://" symbol.
29
   * 2) Parameter names are written first and are terminated with the # symbol
30
   * 3) Parameter values come 2nd and are terminated with an & except for the
31
   *    last value
32
   * The form of the url looks like: 
33
   * urltype://name1#value1&name2#value2&nameN#valueN
34
   * notice there is no & after the last param.  If one is there it is ignored.
35
   *
36
   * @param url the string to parse
37
   */
38
  public metacatURL(String url) throws MalformedURLException
39
  {
40
    this.url = url;
41
    parseURL(url);
42
  }
43
  
44
  /**
45
   * This method takes a string url and parses it according to the following 
46
   * rules.  
47
   * 1) The name of the url is the text before the "://" symbol.
48
   * 2) Parameter names are written first and are terminated with the # symbol
49
   * 3) Parameter values come 2nd and are terminated with an & except for the
50
   *    last value
51
   * The form of the url looks like: 
52
   * urltype://name1#value1&name2#value2&nameN#valueN
53
   * notice there is no & after the last param.  If one is there it is ignored.
54
   */
55
  private void parseURL(String url) throws MalformedURLException
56
  {
57
    String pname = null;
58
    String param = null;
59
    String temp = "";
60
    boolean ampflag = true;
61
    boolean poundflag = false;
62
    int arrcount = 0;
63
    
64
    int urlTypeIndex = url.indexOf("://"); //anything before this is the urltype
65
    this.urlType = url.substring(0, urlTypeIndex);
66
    if(this.urlType.equals("http"))
67
    {//if this is an http url
68
      params[0][0] = "httpurl";
69
      params[0][1] = url.substring(0, url.length());
70
      for(int i=url.length()-1; i>0; i--)
71
      {
72
        if(url.charAt(i) == '/')
73
        {
74
          i=0;
75
        }
76
        else
77
        {
78
          String exchange = temp;
79
          temp = "";
80
          temp += url.charAt(i);
81
          temp += exchange;
82
        }
83
      }
84
      params[1][0] = "filename";
85
      params[1][1] = temp;
86
    }
87
    else
88
    {//other urls that meet the metacat type url structure.
89
      for(int i=urlTypeIndex + 3; i<url.length(); i++)
90
      { //go throught the remainder of the url one character at a time.
91
        if(url.charAt(i) == '#')
92
        { //if the current char is a # then the preceding should be a parametet
93
          //name
94
          if(!poundflag && ampflag)
95
          {
96
            params[arrcount][0] = temp.trim();
97
            temp = "";
98
          }
99
          else
100
          { //if there are two #s or &s in a row throw an exception.
101
            throw new MalformedURLException("metacatURL: Two parameter names " +
102
                                            "not allowed in sequence");
103
          }
104
          poundflag = true;
105
          ampflag = false;
106
        }
107
        else if(url.charAt(i) == '&' || i == url.length()-1)
108
        { //the text preceding the & should be the param value.
109
          if(i == url.length() - 1)
110
          { //if we are at the end of the string grab the last value and append it.
111
            if(url.charAt(i) != '&')
112
            { //ignore an extra & on the end of the string
113
              temp += url.charAt(i);
114
            }
115
          }
116
        
117
          if(!ampflag && poundflag)
118
          {
119
            params[arrcount][1] = temp.trim();
120
            temp = "";
121
            arrcount++; //increment the array to the next row.
122
          }
123
          else
124
          { //if there are two #s or &s in a row through an exception
125
            throw new MalformedURLException("metacatURL: Two parameter values " +
126
                                            "not allowed in sequence");
127
          }
128
          poundflag = false;
129
          ampflag = true;
130
        }
131
        else
132
        { //get the next character in the string
133
          temp += url.charAt(i); 
134
        }
135
      }
136
    }
137
  }
138
  
139
  /**
140
   * Returns the type of the url. This is defined by the text before the "://" 
141
   * symbol in the url.
142
   */
143
  public String getURLType()
144
  {
145
    return this.urlType; 
146
  }
147
  
148
  /**
149
   * Returns the parameters as a 2D string array.
150
   */
151
  public String[][] getParams()
152
  {
153
    return this.params;
154
  }
155
  
156
  /**
157
   * returns a string representation of this metacatURL
158
   */
159
  public String toString()
160
  {
161
    return this.url;
162
  }
163
  
164
  /**
165
   * Prints the parameters neatly to system.out
166
   */
167
  public void printParams()
168
  {
169
    String[][] p = null;
170
    System.out.println("url type: " + this.getURLType());
171
    System.out.println("parameters: ");
172
    p = this.getParams();
173
    System.out.println("name          value");
174
    System.out.println("-------------------");
175
    for(int i=0; i<p.length; i++)
176
    {
177
      if(p[i][0] != null)
178
      {
179
        System.out.print(p[i][0]);
180
        System.out.print("          ");
181
        System.out.print(p[i][1]);
182
        System.out.println();
183
      }
184
    }
185
  }
186
  
187
  /**
188
   * Returns a single parameter and value as a 1D string array.
189
   *
190
   * @param index the index of the parameter, value array that you want.
191
   */
192
  public String[] getParam(int index)
193
  {
194
    String[] s = new String[2];
195
    s[0] = this.params[index][0].trim();
196
    s[1] = this.params[index][1].trim();
197
    //System.out.println("0: " + s[0]);
198
    //System.out.println("1: " + s[1]);
199
    return s;
200
  }
201
  
202
  /**
203
   * Test method for this class.
204
   */
205
  public static void main(String args[])
206
  {
207
    String testurl =  "metacat://docid#NCEAS:2&docid#NCEAS:5&username#chad";
208
    try
209
    {
210
      metacatURL murl = new metacatURL(testurl);
211
      String[][] p = null;
212
      System.out.println("url type: " + murl.getURLType());
213
      System.out.println("parameters: ");
214
      p = murl.getParams();
215
      System.out.println("name          value");
216
      System.out.println("-------------------");
217
      for(int i=0; i<p.length; i++)
218
      {
219
        if(p[i][0] != null)
220
        {
221
          System.out.print(p[i][0]);
222
          System.out.print("          ");
223
          System.out.print(p[i][1]);
224
          System.out.println();
225
        }
226
      }
227
    }
228
    catch(MalformedURLException murle)
229
    {
230
      System.out.println("bad url " + murle.getMessage()); 
231
    }
232
  }
233
  
234
}
(27-27/29)