Project

General

Profile

« Previous | Next » 

Revision 524

Added by berkley over 23 years ago

changed naming scheme

View differences:

src/edu/ucsb/nceas/metacat/MetacatURL.java
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$'
11
 *     '$Date$'
12
 * '$Revision$'
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 urlType = 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 name 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
   * urltype://name1#value1&name2#value2&nameN#valueN
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 name 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
   * urltype://name1#value1&name2#value2&nameN#valueN
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
    int urlTypeIndex = url.indexOf("://"); //anything before this is the urltype
69
    this.urlType = url.substring(0, urlTypeIndex);
70
    paramsHash.put("urlType", this.urlType);
71
    
72
    if(this.urlType.equals("http"))
73
    {//if this is an http url
74
      params[0][0] = "httpurl";
75
      params[0][1] = url.substring(0, url.length());
76
      paramsHash.put(params[0][0], params[0][1]);
77
      for(int i=url.length()-1; i>0; i--)
78
      {
79
        if(url.charAt(i) == '/')
80
        {
81
          i=0;
82
        }
83
        else
84
        {
85
          String exchange = temp;
86
          temp = "";
87
          temp += url.charAt(i);
88
          temp += exchange;
89
        }
90
      }
91
      params[1][0] = "filename";
92
      params[1][1] = temp;
93
      paramsHash.put(params[1][0], params[1][1]);
94
    }
95
    else
96
    {//other urls that meet the metacat type url structure.
97
      int hostIndex = url.indexOf("?");
98
      this.host = url.substring(urlTypeIndex + 3, hostIndex);
99
      paramsHash.put("host", this.host);
100
      for(int i=hostIndex + 1; i<url.length(); i++)
101
      { //go throught the remainder of the url one character at a time.
102
        if(url.charAt(i) == '=')
103
        { //if the current char is a # then the preceding should be a parametet
104
          //name
105
          if(!poundflag && ampflag)
106
          {
107
            params[arrcount][0] = temp.trim();
108
            temp = "";
109
          }
110
          else
111
          { //if there are two #s or &s in a row throw an exception.
112
            throw new MalformedURLException("metacatURL: Two parameter names " +
113
                                            "not allowed in sequence");
114
          }
115
          poundflag = true;
116
          ampflag = false;
117
        }
118
        else if(url.charAt(i) == '&' || i == url.length()-1)
119
        { //the text preceding the & should be the param value.
120
          if(i == url.length() - 1)
121
          { //if we are at the end of the string grab the last value and append it.
122
            if(url.charAt(i) != '=')
123
            { //ignore an extra & on the end of the string
124
              temp += url.charAt(i);
125
            }
126
          }
127
        
128
          if(!ampflag && poundflag)
129
          {
130
            params[arrcount][1] = temp.trim();
131
            paramsHash.put(params[arrcount][0], params[arrcount][1]);
132
            temp = "";
133
            arrcount++; //increment the array to the next row.
134
          }
135
          else
136
          { //if there are two #s or &s in a row through an exception
137
            throw new MalformedURLException("metacatURL: Two parameter values " +
138
                                            "not allowed in sequence");
139
          }
140
          poundflag = false;
141
          ampflag = true;
142
        }
143
        else
144
        { //get the next character in the string
145
          temp += url.charAt(i); 
146
        }
147
      }
148
    }
149
  }
150
  
151
  /**
152
   * Returns the type of the url. This is defined by the text before the "://" 
153
   * symbol in the url.
154
   */
155
  public String getURLType()
156
  {
157
    return this.urlType; 
158
  }
159
  
160
  /**
161
   * Returns the parameters as a 2D string array.
162
   */
163
  public String[][] getParams()
164
  {
165
    return this.params;
166
  }
167
  
168
  /** 
169
   * Returns the parameters in a hashtable.
170
   */
171
  public Hashtable getHashParams()
172
  {
173
    return this.paramsHash;
174
  }
175
  
176
  /**
177
   * returns a single parameter from the hash by name
178
   * @param paramname the name of the parameter to return.
179
   */
180
  public Object getHashParam(String paramname)
181
  {
182
    return this.paramsHash.get(paramname);
183
  }
184
  
185
  /**
186
   * returns a string representation of this metacatURL
187
   */
188
  public String toString()
189
  {
190
    return this.url;
191
  }
192
  
193
  public void printHashParams()
194
  {
195
    Enumeration e = this.paramsHash.keys();
196
    System.out.println("name          value");
197
    System.out.println("-------------------");
198
    while(e.hasMoreElements())
199
    {
200
      String key = (String)e.nextElement();
201
      System.out.print(key);
202
      System.out.print("          ");
203
      System.out.println((String)this.paramsHash.get(key));
204
    }
205
  }
206
  
207
  /**
208
   * Prints the parameters neatly to system.out
209
   */
210
  public void printParams()
211
  {
212
    String[][] p = null;
213
    System.out.println("url type: " + this.getURLType());
214
    System.out.println("parameters: ");
215
    p = this.getParams();
216
    System.out.println("name          value");
217
    System.out.println("-------------------");
218
    for(int i=0; i<p.length; i++)
219
    {
220
      if(p[i][0] != null)
221
      {
222
        System.out.print(p[i][0]);
223
        System.out.print("          ");
224
        System.out.print(p[i][1]);
225
        System.out.println();
226
      }
227
    }
228
  }
229
  
230
  /**
231
   * Returns a single parameter and value as a 1D string array.
232
   *
233
   * @param index the index of the parameter, value array that you want.
234
   */
235
  public String[] getParam(int index)
236
  {
237
    String[] s = new String[2];
238
    s[0] = this.params[index][0].trim();
239
    s[1] = this.params[index][1].trim();
240
    //System.out.println("0: " + s[0]);
241
    //System.out.println("1: " + s[1]);
242
    return s;
243
  }
244
  
245
  /**
246
   * Test method for this class.
247
   */
248
  public static void main(String args[])
249
  {
250
    String testurl =  "metacat://dev.nceas.ucsb.edu?docid=NCEAS:10&username=chad&pasword=xyz";
251
    String testurl2 = "http://dev.nceas.ucsb.edu/berkley/testdata.dat";
252
    try
253
    {
254
      MetacatURL murl = new MetacatURL(testurl);
255
      MetacatURL murl2 = new MetacatURL(testurl2);
256
      String[][] p = null;
257
      System.out.println("url type: " + murl.getURLType());
258
      System.out.println("parameters: ");
259
      p = murl.getParams();
260
      Hashtable h = murl.getHashParams();
261
      murl.printParams();
262
      murl2.printParams();
263
      System.out.println("hash params: " );
264
      murl.printHashParams();
265
      murl2.printHashParams();
266
    }
267
    catch(MalformedURLException murle)
268
    {
269
      System.out.println("bad url " + murle.getMessage()); 
270
    }
271
  }
272
  
273
}
0 274

  
src/edu/ucsb/nceas/metacat/DBQuery.java
173 173
          doctitle = rs.getString(4);
174 174
          createDate = rs.getString(5);
175 175
          updateDate = rs.getString(6);
176
          
176
//System.out.println("vec.size = " + returndocVec.size());
177 177
          if(returndocVec.size() != 0 && !returndocVec.contains(doctype))
178 178
          { //there are returndocs to match (backtracking can now be performed). 
179
            //System.out.println("olddoctype: " + doctype);
179
//System.out.println("olddoctype: " + doctype);
180 180
            StringBuffer btBuf = new StringBuffer();
181 181
            btBuf.append("select object from xml_relation where ");
182 182
            btBuf.append("objdoctype in (");
......
201 201
            { //there was a backtrackable document found
202 202
              DocumentImpl xmldoc = null;
203 203
              //System.out.println("document found is: " + btrs.getString(1));
204
              metacatURL objURL = new metacatURL(btrs.getString(1));
204
              MetacatURL objURL = new MetacatURL(btrs.getString(1));
205 205
              try
206 206
              {
207 207
                xmldoc = new DocumentImpl(conn, objURL.getParam(0)[1]);
......
318 318
            String subDT = rs.getString(4);
319 319
            String objDT = rs.getString(5);
320 320
            
321
            metacatURL murl = new metacatURL(sub);
321
            MetacatURL murl = new MetacatURL(sub);
322 322
            if(murl.getURLType().equals("metacat"))
323 323
            {//we only want to process metacat urls here.
324 324
              String[] tempparam = murl.getParam(0);
......
722 722

  
723 723
/**
724 724
 * '$Log$
725
 * 'Revision 1.24  2000/10/03 22:48:41  berkley
726
 * 'added functionality to return the doctype of a relation in a relationdoctype tag.  This information is now returned automatically in the resultset under the path resultset/relation/relationdoctype.
727
 * '
725 728
 * 'Revision 1.23  2000/09/27 21:37:05  berkley
726 729
 * 'removed system.out.printlns
727 730
 * '
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
756 756
      try
757 757
      {
758 758
        DocumentImpl xmldoc=null;
759
        metacatURL murl = new metacatURL(((String[])params.get("url"))[0]);
759
        MetacatURL murl = new MetacatURL(((String[])params.get("url"))[0]);
760 760
        if(murl.getURLType().equals("metacat"))
761 761
        {//get the document from the database if it is the right type of url
762 762
          Hashtable murlParams = murl.getHashParams();
......
1118 1118
    //zip up the related documents
1119 1119
    for(int i=0; i<reldocs.length; i++)
1120 1120
    {
1121
      metacatURL murl = new metacatURL(((String)reldocs[i]));
1121
      MetacatURL murl = new MetacatURL(((String)reldocs[i]));
1122 1122
      if(murl.getURLType().equals("metacat"))
1123 1123
      {
1124 1124
        //get the document from the database
src/edu/ucsb/nceas/metacat/RelationHandler.java
66 66
      while(hasmorerows)
67 67
      {
68 68
        String subject = rs.getString(1);
69
        metacatURL subjectMurl = new metacatURL(subject);
69
        MetacatURL subjectMurl = new MetacatURL(subject);
70 70
        String subjectDoctype = null;
71 71
        if(subjectMurl.getURLType().equals("metacat"))
72 72
        {
......
125 125
        }
126 126
        
127 127
        //get the current relations information
128
        metacatURL subMurl = new metacatURL(subject);
129
        metacatURL objMurl = new metacatURL(object);
128
        MetacatURL subMurl = new MetacatURL(subject);
129
        MetacatURL objMurl = new MetacatURL(object);
130 130
        String subDoctype = null;
131 131
        String objDoctype = null;
132 132
        if(subMurl.getURLType().equals("metacat"))

Also available in: Unified diff