Revision 699
Added by bojilova about 23 years ago
DBUtil.java | ||
---|---|---|
2 | 2 |
* '$RCSfile$' |
3 | 3 |
* Purpose: A Class that implements utility methods like: |
4 | 4 |
* 1/ Reding all doctypes from db connection |
5 |
* 2/ Reading Lore type Data Guide from db connection |
|
5 |
* 2/ Reading DTD or Schema file from Metacat catalog system |
|
6 |
* 3/ Reading Lore type Data Guide from db connection |
|
6 | 7 |
* Copyright: 2000 Regents of the University of California and the |
7 | 8 |
* National Center for Ecological Analysis and Synthesis |
8 | 9 |
* Authors: Jivka Bojilova |
... | ... | |
33 | 34 |
import java.sql.SQLException; |
34 | 35 |
import java.sql.PreparedStatement; |
35 | 36 |
import java.sql.ResultSet; |
37 |
|
|
38 |
import java.io.BufferedInputStream; |
|
39 |
import java.io.InputStream; |
|
40 |
import java.io.IOException; |
|
41 |
import java.net.URL; |
|
42 |
import java.net.URLConnection; |
|
43 |
import java.net.MalformedURLException; |
|
44 |
|
|
36 | 45 |
import java.util.Enumeration; |
37 | 46 |
import java.util.Vector; |
38 | 47 |
import java.util.Stack; |
... | ... | |
57 | 66 |
if (args.length < 1) |
58 | 67 |
{ |
59 | 68 |
System.err.println("Wrong number of arguments!!!"); |
60 |
System.err.println("USAGE: java DBUtil <-dt | -dg [doctype]>"); |
|
69 |
System.err.println( |
|
70 |
"USAGE: java DBUtil <-dt | -ds [doctype] | -dg [doctype]>"); |
|
61 | 71 |
return; |
62 | 72 |
} else { |
63 | 73 |
try { |
... | ... | |
76 | 86 |
if ( args.length == 2 ) { doctype = args[1]; } |
77 | 87 |
String dataguide = dbutil.readDataGuide(doctype); |
78 | 88 |
System.out.println(dataguide); |
89 |
} else if ( args[0].equals("-ds") ) { |
|
90 |
String doctype = null; |
|
91 |
if ( args.length == 2 ) { doctype = args[1]; } |
|
92 |
String dtdschema = dbutil.readDTDSchema(doctype); |
|
93 |
System.out.println(dtdschema); |
|
79 | 94 |
} else { |
80 |
System.err.println("USAGE: java DBUtil <-dt | -dg [doctype]>"); |
|
95 |
System.err.println( |
|
96 |
"USAGE: java DBUtil <-dt | -ds [doctype] | -dg [doctype]>"); |
|
81 | 97 |
} |
82 | 98 |
|
83 | 99 |
} catch (Exception e) { |
84 |
System.err.println("error in DBUtil.main"); |
|
85 |
System.err.println(e.getMessage()); |
|
100 |
//System.err.println("error in DBUtil.main");
|
|
101 |
//System.err.println(e.getMessage());
|
|
86 | 102 |
e.printStackTrace(System.err); |
87 | 103 |
} |
88 | 104 |
} |
... | ... | |
121 | 137 |
pstmt.close(); |
122 | 138 |
|
123 | 139 |
} catch (SQLException e) { |
124 |
System.out.println("DBUtil.readDoctypes(): " + e.getMessage()); |
|
125 |
throw e; |
|
140 |
throw new SQLException("DBUtil.readDoctypes(). " + e.getMessage()); |
|
126 | 141 |
} |
127 | 142 |
|
128 | 143 |
return formatToXML(doctypeList, "doctype"); |
129 | 144 |
} |
130 | 145 |
|
131 | 146 |
/** |
147 |
* read DTD or Schema file from Metacat's XML catalog system |
|
148 |
*/ |
|
149 |
public String readDTDSchema(String doctype) |
|
150 |
throws SQLException, MalformedURLException, IOException |
|
151 |
{ |
|
152 |
String systemID = null; |
|
153 |
PreparedStatement pstmt; |
|
154 |
StringBuffer cbuff = new StringBuffer(); |
|
155 |
|
|
156 |
// get doctype's System ID from db catalog |
|
157 |
try { |
|
158 |
pstmt = conn.prepareStatement("SELECT system_id " + |
|
159 |
"FROM xml_catalog " + |
|
160 |
"WHERE entry_type in ('DTD','Schema') " + |
|
161 |
"AND public_id LIKE ?"); |
|
162 |
pstmt.setString(1, doctype); |
|
163 |
pstmt.execute(); |
|
164 |
ResultSet rs = pstmt.getResultSet(); |
|
165 |
boolean hasRow = rs.next(); |
|
166 |
if (hasRow) { |
|
167 |
systemID = rs.getString(1); |
|
168 |
} else { |
|
169 |
throw new SQLException("Non-registered doctype: " + doctype); |
|
170 |
} |
|
171 |
pstmt.close(); |
|
172 |
|
|
173 |
} catch (SQLException e) { |
|
174 |
throw new SQLException("DBUtil.readDTD(). " + e.getMessage()); |
|
175 |
} |
|
176 |
|
|
177 |
// read from URL stream as specified by the System ID. |
|
178 |
try { |
|
179 |
// open a connection to this URL and return an InputStream |
|
180 |
// for reading from that connection |
|
181 |
InputStream istream = new URL(systemID).openStream(); |
|
182 |
// create a buffering character-input stream |
|
183 |
// that uses a default-sized input buffer |
|
184 |
BufferedInputStream in = new BufferedInputStream(istream); |
|
185 |
|
|
186 |
// read the input and write into the string buffer |
|
187 |
int inputByte; |
|
188 |
while ( (inputByte = in.read()) != -1 ) { |
|
189 |
cbuff.append((char)inputByte); |
|
190 |
} |
|
191 |
|
|
192 |
// the input stream must be closed |
|
193 |
in.close(); |
|
194 |
|
|
195 |
} catch (MalformedURLException e) { |
|
196 |
throw new MalformedURLException |
|
197 |
("DBUtil.readDTD(). " + e.getMessage()); |
|
198 |
} catch (IOException e) { |
|
199 |
throw new IOException |
|
200 |
("DBUtil.readDTD(). " + e.getMessage()); |
|
201 |
} catch (SecurityException e) { |
|
202 |
throw new IOException |
|
203 |
("DBUtil.readDTD(). " + e.getMessage()); |
|
204 |
} |
|
205 |
|
|
206 |
return cbuff.toString(); |
|
207 |
} |
|
208 |
|
|
209 |
/** |
|
132 | 210 |
* read Data Guide for a given doctype from db connection in XML format |
133 | 211 |
* select all distinct absolute paths from xml_index table |
134 | 212 |
*/ |
... | ... | |
169 | 247 |
pstmt.close(); |
170 | 248 |
|
171 | 249 |
} catch (SQLException e) { |
172 |
System.out.println("DBUtil.readDataGuide(): " + e.getMessage()); |
|
173 |
throw e; |
|
250 |
throw new SQLException("DBUtil.readDataGuide(). " + e.getMessage()); |
|
174 | 251 |
} |
175 | 252 |
|
176 | 253 |
return formatToXML(dataguide); |
Also available in: Unified diff
implemented interface for download of DTD or Schema file from Metacat file system
through the params:
action="getdtdschema"
doctype