Revision 5752
Added by ben leinfelder over 13 years ago
DBQuery.java | ||
---|---|---|
30 | 30 |
|
31 | 31 |
package edu.ucsb.nceas.metacat; |
32 | 32 |
|
33 |
import java.io.*; |
|
34 |
import java.util.zip.*; |
|
33 |
import java.io.BufferedWriter; |
|
34 |
import java.io.File; |
|
35 |
import java.io.FileInputStream; |
|
36 |
import java.io.FileOutputStream; |
|
37 |
import java.io.IOException; |
|
38 |
import java.io.InputStream; |
|
39 |
import java.io.InputStreamReader; |
|
40 |
import java.io.OutputStreamWriter; |
|
41 |
import java.io.Reader; |
|
42 |
import java.io.StringReader; |
|
43 |
import java.io.StringWriter; |
|
44 |
import java.io.Writer; |
|
35 | 45 |
import java.sql.PreparedStatement; |
36 | 46 |
import java.sql.ResultSet; |
37 | 47 |
import java.sql.SQLException; |
38 |
import java.util.*; |
|
48 |
import java.util.Enumeration; |
|
49 |
import java.util.Hashtable; |
|
50 |
import java.util.Iterator; |
|
51 |
import java.util.StringTokenizer; |
|
52 |
import java.util.Vector; |
|
53 |
import java.util.zip.ZipEntry; |
|
54 |
import java.util.zip.ZipOutputStream; |
|
39 | 55 |
|
40 | 56 |
import javax.servlet.ServletOutputStream; |
41 | 57 |
import javax.servlet.http.HttpServletResponse; |
42 |
import javax.servlet.http.HttpSession; |
|
43 | 58 |
|
44 | 59 |
import org.apache.log4j.Logger; |
45 | 60 |
|
46 |
import org.w3c.dom.*; |
|
47 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
48 |
import org.xml.sax.InputSource; |
|
49 |
import org.w3c.dom.ls.*; |
|
50 |
|
|
51 | 61 |
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface; |
52 | 62 |
import edu.ucsb.nceas.metacat.database.DBConnection; |
53 | 63 |
import edu.ucsb.nceas.metacat.database.DBConnectionPool; |
... | ... | |
113 | 123 |
* the main routine used to test the DBQuery utility. |
114 | 124 |
* <p> |
115 | 125 |
* Usage: java DBQuery <xmlfile> |
116 |
* |
|
126 |
* NOTE: encoding should be provided for best results
|
|
117 | 127 |
* @param xmlfile the filename of the xml file containing the query |
118 | 128 |
*/ |
119 | 129 |
static public void main(String[] args) |
... | ... | |
149 | 159 |
|
150 | 160 |
// Execute the query |
151 | 161 |
DBQuery queryobj = new DBQuery(); |
152 |
FileReader xml = new FileReader(new File(xmlfile));
|
|
162 |
Reader xml = new InputStreamReader(new FileInputStream(new File(xmlfile)));
|
|
153 | 163 |
Hashtable nodelist = null; |
154 | 164 |
//nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex); |
155 | 165 |
|
... | ... | |
187 | 197 |
//write into a file "result.txt" |
188 | 198 |
if (!showRuntime) { |
189 | 199 |
File f = new File("./result.txt"); |
190 |
FileWriter fw = new FileWriter(f);
|
|
200 |
Writer fw = new OutputStreamWriter(new FileOutputStream(f));
|
|
191 | 201 |
BufferedWriter out = new BufferedWriter(fw); |
192 | 202 |
out.write(result.toString()); |
193 | 203 |
out.flush(); |
... | ... | |
288 | 298 |
* @param sessionid the sessionid |
289 | 299 |
*/ |
290 | 300 |
public void findDocuments(HttpServletResponse response, |
291 |
PrintWriter out, Hashtable params,
|
|
301 |
Writer out, Hashtable params, |
|
292 | 302 |
String user, String[] groups, |
293 | 303 |
String sessionid) throws PropertyNotFoundException |
294 | 304 |
{ |
... | ... | |
309 | 319 |
* @param sessionid the sessionid |
310 | 320 |
*/ |
311 | 321 |
public void findDocuments(HttpServletResponse response, |
312 |
PrintWriter out, Hashtable params,
|
|
322 |
Writer out, Hashtable params, |
|
313 | 323 |
String user, String[] groups, |
314 | 324 |
String sessionid, boolean useXMLIndex) |
315 | 325 |
{ |
... | ... | |
379 | 389 |
{ |
380 | 390 |
//knb format, in this case we will get whole result and sent it out |
381 | 391 |
response.setContentType("text/html"); |
382 |
PrintWriter nonout = null;
|
|
392 |
Writer nonout = null; |
|
383 | 393 |
StringBuffer xml = createResultDocument(xmlquery, qspec, nonout, user, |
384 | 394 |
groups, useXMLIndex, pagesize, |
385 | 395 |
pagestart, sessionid, qformat); |
... | ... | |
440 | 450 |
*/ |
441 | 451 |
public StringBuffer createResultDocument(String xmlquery, |
442 | 452 |
QuerySpecification qspec, |
443 |
PrintWriter out,
|
|
453 |
Writer out, |
|
444 | 454 |
String user, String[] groups, |
445 | 455 |
boolean useXMLIndex) |
446 | 456 |
{ |
... | ... | |
455 | 465 |
*/ |
456 | 466 |
public StringBuffer createResultDocument(String xmlquery, |
457 | 467 |
QuerySpecification qspec, |
458 |
PrintWriter out,
|
|
468 |
Writer out, |
|
459 | 469 |
String user, String[] groups, |
460 | 470 |
boolean useXMLIndex, int pagesize, |
461 | 471 |
int pagestart, String sessionid, |
... | ... | |
481 | 491 |
//send out a new query |
482 | 492 |
if (out != null) |
483 | 493 |
{ |
484 |
out.println(resultset.toString()); |
|
494 |
try { |
|
495 |
out.write(resultset.toString()); |
|
496 |
} catch (IOException e) { |
|
497 |
logMetacat.error(e.getMessage(), e); |
|
498 |
} |
|
485 | 499 |
} |
486 | 500 |
if (qspec != null) |
487 | 501 |
{ |
... | ... | |
543 | 557 |
resultset.append(closeRestultset); |
544 | 558 |
if (out != null) |
545 | 559 |
{ |
546 |
out.println(closeRestultset); |
|
560 |
try { |
|
561 |
out.write(closeRestultset); |
|
562 |
} catch (IOException e) { |
|
563 |
logMetacat.error(e.getMessage(), e); |
|
564 |
} |
|
547 | 565 |
} |
548 | 566 |
|
549 | 567 |
//default to returning the whole resultset |
... | ... | |
554 | 572 |
* Find the doc list which match the query |
555 | 573 |
*/ |
556 | 574 |
private StringBuffer findResultDoclist(QuerySpecification qspec, |
557 |
PrintWriter out,
|
|
575 |
Writer out, |
|
558 | 576 |
String user, String[]groups, |
559 | 577 |
DBConnection dbconn, boolean useXMLIndex, |
560 | 578 |
int pagesize, int pagestart, String sessionid, |
... | ... | |
651 | 669 |
logMetacat.info("DBQuery.findResultDoclist - result from cache !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); |
652 | 670 |
if (out != null) |
653 | 671 |
{ |
654 |
out.println(cachedResult);
|
|
672 |
out.write(cachedResult);
|
|
655 | 673 |
} |
656 | 674 |
resultsetBuffer.append(cachedResult); |
657 | 675 |
return resultsetBuffer; |
... | ... | |
793 | 811 |
resultsetBuffer.append("\n<lastpage>" + lastpage + "</lastpage>\n"); |
794 | 812 |
if (out != null) |
795 | 813 |
{ |
796 |
out.println("\n<lastpage>" + lastpage + "</lastpage>\n");
|
|
814 |
out.write("\n<lastpage>" + lastpage + "</lastpage>\n");
|
|
797 | 815 |
} |
798 | 816 |
|
799 | 817 |
// now we only cached none-paged query and user is public |
... | ... | |
814 | 832 |
*/ |
815 | 833 |
private StringBuffer handleSubsetResult(QuerySpecification qspec, |
816 | 834 |
StringBuffer resultset, |
817 |
PrintWriter out, ResultDocumentSet partOfDoclist,
|
|
835 |
Writer out, ResultDocumentSet partOfDoclist, |
|
818 | 836 |
String user, String[]groups, |
819 | 837 |
DBConnection dbconn, boolean useXMLIndex, |
820 | 838 |
String qformat) |
... | ... | |
951 | 969 |
//send single element to output |
952 | 970 |
if (out != null) |
953 | 971 |
{ |
954 |
out.println(xmlElement);
|
|
972 |
out.write(xmlElement);
|
|
955 | 973 |
} |
956 | 974 |
resultset.append(xmlElement); |
957 | 975 |
}//while |
... | ... | |
978 | 996 |
//send single element to output |
979 | 997 |
if (out != null) |
980 | 998 |
{ |
981 |
out.println(xmlElement);
|
|
999 |
out.write(xmlElement);
|
|
982 | 1000 |
} |
983 | 1001 |
resultset.append(xmlElement); |
984 | 1002 |
}//while |
Also available in: Unified diff
use detected XML encoding when reading/writing files
use UTF-8 as default when performing queries in the DB (assume DB is using UTF-8)
remove as many PrintWriters (uses system default character encoding only) as possible and construct OutputStreamWriters where explicit encoding can be given.
http://bugzilla.ecoinformatics.org/show_bug.cgi?id=2495