Revision 540
Added by berkley about 24 years ago
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 |
} |
|
274 | 0 |
src/edu/ucsb/nceas/metacat/relationHandler.java | ||
---|---|---|
1 |
/** |
|
2 |
* '$RCSfile$' |
|
3 |
* Purpose: A class to asyncronously build the package relation index |
|
4 |
* in the database table xml_relation. |
|
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.sql.*; |
|
18 |
import java.util.*; |
|
19 |
import java.lang.Thread; |
|
20 |
|
|
21 |
public class relationHandler implements Runnable |
|
22 |
{ |
|
23 |
private Thread btThread = null; |
|
24 |
private Connection conn = null; |
|
25 |
private DocumentImpl xmldoc = null; |
|
26 |
MetaCatUtil util = new MetaCatUtil(); |
|
27 |
|
|
28 |
/** |
|
29 |
* Constructor for this class. finds all of the relations to a single xml |
|
30 |
* document and writes them to the database. This includes transitive |
|
31 |
* relations. |
|
32 |
* @param xmldoc the xml document to index. |
|
33 |
*/ |
|
34 |
public relationHandler(DocumentImpl xmldoc, Connection conn) |
|
35 |
{ |
|
36 |
this.conn = conn; |
|
37 |
this.xmldoc = xmldoc; |
|
38 |
if(xmldoc.getDoctype().equals(util.getOption("packagedoctype"))) |
|
39 |
{ //make sure this doctype is a package document then run the thread |
|
40 |
btThread = new Thread(this); |
|
41 |
btThread.setPriority(Thread.MIN_PRIORITY); |
|
42 |
btThread.start(); |
|
43 |
} |
|
44 |
} |
|
45 |
|
|
46 |
/** |
|
47 |
* The thread handler |
|
48 |
*/ |
|
49 |
public void run() |
|
50 |
{ |
|
51 |
//pseudo-code algorithm |
|
52 |
//for each new relation r in xml_nodes |
|
53 |
// put r into xml_relation |
|
54 |
// compare r to each relation already in xml_relation |
|
55 |
// if r isrelatedto a row in xml_relation |
|
56 |
// add a new row to xml_relation that represents this new relation |
|
57 |
try |
|
58 |
{ |
|
59 |
String docid = xmldoc.getDocID(); |
|
60 |
PreparedStatement pstmt = conn.prepareStatement( |
|
61 |
QuerySpecification.printPackageSQL(docid)); |
|
62 |
pstmt.execute(); |
|
63 |
//get the new relations out of xml_nodes |
|
64 |
ResultSet rs = pstmt.getResultSet(); |
|
65 |
boolean hasmorerows = rs.next(); |
|
66 |
while(hasmorerows) |
|
67 |
{ |
|
68 |
String subject = rs.getString(1); |
|
69 |
metacatURL subjectMurl = new metacatURL(subject); |
|
70 |
String subjectDoctype = null; |
|
71 |
if(subjectMurl.getURLType().equals("metacat")) |
|
72 |
{ |
|
73 |
DocumentImpl subDoc = new DocumentImpl(conn, |
|
74 |
subjectMurl.getParam(0)[1]); |
|
75 |
subjectDoctype = subDoc.getDoctype(); |
|
76 |
} |
|
77 |
String relationship = rs.getString(2); |
|
78 |
String object = rs.getString(3); |
|
79 |
|
|
80 |
//compare r to each relation in xml_relation |
|
81 |
pstmt = conn.prepareStatement("select subject, subdoctype, " + |
|
82 |
"relationship, object, objdoctype " + |
|
83 |
"from xml_relation"); |
|
84 |
pstmt.execute(); |
|
85 |
//get each relation in xml_relation for comparison |
|
86 |
ResultSet relations = pstmt.getResultSet(); |
|
87 |
boolean hasmorerelations = relations.next(); |
|
88 |
while(hasmorerelations) |
|
89 |
{ |
|
90 |
String currentSub = relations.getString(1); |
|
91 |
String currentSubDoctype = relations.getString(2); |
|
92 |
String currentRelationship = relations.getString(3); |
|
93 |
String currentObj = relations.getString(4); |
|
94 |
String currentObjDoctype = relations.getString(5); |
|
95 |
|
|
96 |
if(object.equals(currentObj)) |
|
97 |
{//there is a transitive relation so add a new relation to the table |
|
98 |
StringBuffer insertTransRelation = new StringBuffer(); |
|
99 |
insertTransRelation.append("insert into xml_relation (subject, "); |
|
100 |
insertTransRelation.append("subdoctype, relationship, object, "); |
|
101 |
insertTransRelation.append("objdoctype) values ('"); |
|
102 |
insertTransRelation.append(currentSub).append("', '"); |
|
103 |
insertTransRelation.append(currentSubDoctype).append("', "); |
|
104 |
insertTransRelation.append("'hasTransitiveRelationTo', '"); |
|
105 |
insertTransRelation.append(subject).append("', '"); |
|
106 |
insertTransRelation.append(subjectDoctype).append("')"); |
|
107 |
pstmt = conn.prepareStatement(insertTransRelation.toString()); |
|
108 |
pstmt.execute(); |
|
109 |
|
|
110 |
insertTransRelation = new StringBuffer(); |
|
111 |
//put the same relation in with the subject and object switched |
|
112 |
insertTransRelation.append("insert into xml_relation (subject, "); |
|
113 |
insertTransRelation.append("subdoctype, relationship, object, "); |
|
114 |
insertTransRelation.append("objdoctype) values ('"); |
|
115 |
insertTransRelation.append(subject).append("', '"); |
|
116 |
insertTransRelation.append(subjectDoctype).append("', "); |
|
117 |
insertTransRelation.append("'hasTransitiveRelationTo', '"); |
|
118 |
insertTransRelation.append(currentSub).append("', '"); |
|
119 |
insertTransRelation.append(currentSubDoctype).append("')"); |
|
120 |
pstmt = conn.prepareStatement(insertTransRelation.toString()); |
|
121 |
pstmt.execute(); |
|
122 |
} |
|
123 |
|
|
124 |
hasmorerelations = relations.next(); |
|
125 |
} |
|
126 |
|
|
127 |
//get the current relations information |
|
128 |
metacatURL subMurl = new metacatURL(subject); |
|
129 |
metacatURL objMurl = new metacatURL(object); |
|
130 |
String subDoctype = null; |
|
131 |
String objDoctype = null; |
|
132 |
if(subMurl.getURLType().equals("metacat")) |
|
133 |
{ |
|
134 |
DocumentImpl subDoc = new DocumentImpl(conn, subMurl.getParam(0)[1]); |
|
135 |
subDoctype = subDoc.getDoctype(); |
|
136 |
} |
|
137 |
if(objMurl.getURLType().equals("metacat")) |
|
138 |
{ |
|
139 |
DocumentImpl objDoc = new DocumentImpl(conn, objMurl.getParam(0)[1]); |
|
140 |
objDoc.getDoctype(); |
|
141 |
} |
|
142 |
//now that the comparisons are done, the new relation can be put |
|
143 |
//into xml_relation |
|
144 |
StringBuffer insertStmt = new StringBuffer(); |
|
145 |
insertStmt.append("insert into xml_relation (subject, subdoctype, "); |
|
146 |
insertStmt.append("relationship, object, objdoctype) values ('"); |
|
147 |
insertStmt.append(subject).append("', '"); |
|
148 |
insertStmt.append(subDoctype).append("', '"); |
|
149 |
insertStmt.append(relationship).append("', '"); |
|
150 |
insertStmt.append(object).append("', '"); |
|
151 |
insertStmt.append(objDoctype).append("')"); |
|
152 |
pstmt = conn.prepareStatement(insertStmt.toString()); |
|
153 |
pstmt.execute(); |
|
154 |
|
|
155 |
hasmorerows = rs.next(); |
|
156 |
} |
|
157 |
|
|
158 |
conn.commit(); |
|
159 |
btThread = null; |
|
160 |
|
|
161 |
} catch(Exception e) { |
|
162 |
try { |
|
163 |
conn.rollback(); |
|
164 |
} catch (SQLException sqle) {} |
|
165 |
System.out.println("Error in relationHandler: " + e.getMessage()); |
|
166 |
util.debugMessage("Error in relationHandler: " + e.getMessage()); |
|
167 |
e.printStackTrace(System.out); |
|
168 |
btThread = null; |
|
169 |
} |
|
170 |
} |
|
171 |
} |
|
172 | 0 |
Also available in: Unified diff
no message