?? webxmlqs.java
字號:
package dli2fe.sample;
/**
* Title: Digial Library Interoperable Interface Fudan Edition
* Description: This project contains all the classes required for DLI2FE interface. Developers use these classes to implement the wrapper and client side codes. The basic functions of DLI2FE is as follows:
* Search: Search a digital library source site.
* Metadata: Fetch metadata for a site.
* ResultAccess: Get results for a given query.
* DLI2FE uses Dublin Core as the basic attribute model, DAV/DASL as the general XML-based query language and CORBA as distributed object transportation mechanism.
* Copyright: Copyright (c) 2001
* Company: Fudan University
* @author Carl Tao
* @version 1.0
*/
import dli2fe.*;
import dli2fe.DLI2FE;
import dli2fe.DLI2FEException;
import java.io.*;
import dli2fe.xml.XMLObject;
import java.awt.event.*;
import org.w3c.dom.*;
import java.util.*;
import java.net.*;
import dli2fe.xml.NodeListImpl;
import org.w3c.tools.codec.Base64Encoder;
//import HTTPClient.*;
public class Webxmlqs {
public static boolean useProxy = false;
String queryString;
String result;
int numResults;
public static final String WebNamespace = "http://www.cs.fudan.edu.cn/WebXMLQS/1.0#";
public Webxmlqs() {
//HTTPConnection.setProxyServer("202.120.224.4", 8080);
//AuthorizationInfo.addBasicAuthorization("202.120.224.4", 8080, "unspecified", "xh_xh", "xh_xh");
result = "";
}
public Webxmlqs(String queryString0) {
this();
queryString = queryString0 == null ? "" : queryString0.trim();
}
/*public ResultDoc getResult(int index) throws DLI2FEException {
if (index < 0 || index >= result.size())
throw new DLI2FEException(DLI2FEException.INVALID_REQUEST_EXC, "Non existent reult");
return (ResultDoc)result.elementAt(index);
}*/
public String getResultString() throws DLI2FEException {
/*if (index < 0 || index >= result.size())
throw new DLI2FEException(DLI2FEException.INVALID_REQUEST_EXC, "Non existent reult");
ResultDoc resultDoc = (ResultDoc)result.elementAt(index);
return "<doc> <DID>" + (index + 1) + "</DID> <propList> <dc:Title>" + resultDoc.name +
"</dc:Title> <dc:Identifier>" + URLEncoder.encode(resultDoc.url) + "</dc:Identifier> </propList> </doc>";
*/
return result;
}
public int getNumDocs() {
return numResults;
}
public String changeCharInURL(String strURL) throws DLI2FEException {
String tempString;
String head,tail;
tempString = strURL;
char c;
for (int k = 0; k < tempString.length() ; k++)
{
c = tempString.charAt(k);
/*if ( c=='+')
{head = queryString.substring(0,k);
tail = queryString.substring(k+1,queryString.length());
queryString = head + "%2B" + tail;}
else*/ if ( c==' ')
{ head = tempString.substring(0,k);
tail = tempString.substring(k+1,tempString.length());
tempString = head + "%20" + tail;
k += 3;
}
}
//tempString=tempString+"%2B";
return tempString;
/*char c;
int cAsc;
int i = tempString.indexOf('?');
int j = tempString.length();
for (int k = i+1; k <= j ; k++)
{
c = tempString.charAt(k);
if (!isLetterOrDigit(c) && (c!='&'))
{
cAsc = asc(c);
c = '%' + cAsc;
}
}*/
}
public void searchWebxmlqs() throws DLI2FEException {
XMLObject resultTemp = new XMLObject();
String resultX = "";
String dbg;
int i, j;
queryString = changeCharInURL(queryString);
System.out.println("queryString:"+queryString);
try {
if (!useProxy) {
BufferedReader din = new BufferedReader(new InputStreamReader(
new URL(queryString).openStream()));
while ((dbg = din.readLine()) != null) {
resultX += dbg + "\n";
System.out.println(dbg);
}
din.close();
//調用查詢程序,得到結果,賦予resultX
} else {
//System.getProperties().put("proxySet", "false");
//System.getProperties().put("proxyHost", "202.120.224.4");
//System.getProperties().put("proxyPort", "8080" );
URL url = new URL(queryString);
//System.out.println(dbg);
URLConnection con = url.openConnection();
//Base64Encoder b = new Base64Encoder("xh_xh:xh_xh");
//con.setRequestProperty("Proxy-Authorization", "Basic " + b.processString());
con.setDoInput(true);
con.setDoOutput(false);
BufferedReader din = new BufferedReader(new InputStreamReader(con.getInputStream()));
while ((dbg = din.readLine()) != null) {
resultX += dbg + "\n";
}
din.close();
//System.getProperties().put("proxySet", "false");
//調用查詢程序,得到結果,賦予resultX
}
} catch (Exception any) {
throw new DLI2FEException(DLI2FEException.SERVICE_UNAVAILABLE_EXC, "Failed to contact server");
}
//suppose resultX' value;
/*resultX="<?xml version='1.0' encoding='GB2312' ?><SearchResult xmlns='http://www.cs.fudan.edu.cn/WebXMLQS/1.0#'> " +
"<NumDoc>2</NumDoc>" +"<QueryType>html</QueryType>"+
"<Record><Title>三川出租汽車公司</Title> <Identifier>" +
"http%3A%2F%2Fwww.yellowpage.com.cn%2FProg%2FTrade%2FDetail.asp%3Fr%3DB310000%26c%3D0189%26u%3D0000522671%26a%3D</Identifier> " +
"<Description>hsdhsyrtuyjfj</Description></Record>" +
"<Record><Title>三川出租汽車公司</Title> <Identifier>" +
"http%3A%2F%2Fwww.yellowpage.com.cn%2FProg%2FTrade%2FDetail.asp%3Fr%3DB310000%26c%3D0189%26u%3D0000522671%26a%3D</Identifier> " +
"<Description>hsdhsyrtuyjfj</Description></Record></SearchResult>";*/
resultTemp.setString(resultX);
XMLObject xmlTemp = XMLObject.create(resultTemp);
NodeList l = new NodeListImpl(xmlTemp.getElement().getElementsByTagName("NumDoc"));
if (l.getLength() != 1)
throw new DLI2FEException(DLI2FEException.MALFORMED_XML_EXC, "Invalid element, 'numDoc' expected.");
String value = l.item(0).getFirstChild().getNodeValue().trim();
numResults = Integer.parseInt(value);
if (numResults == 0){
throw new DLI2FEException(DLI2FEException.NOT_FOUND_EXC, "No document found");
}
else createResult(xmlTemp);
}
public void createResult(XMLObject xmlTemp) throws DLI2FEException {
result = "<?xml version='1.0' encoding='GB2312' ?><SearchResult xmlns='" + DLI2FE.Namespace +"' xmlns:dc='" +
DublinCore.Namespace + "' xmlns:webxmlqs='" + WebNamespace + "'>\n";
String value,local;
//XMLObject xmlTemp = XMLObject.create(resultTemp);
NodeList l = xmlTemp.getElement().getElementsByTagName("QueryType");
String xmlJudge = l.item(0).getFirstChild().getNodeValue().trim();
NodeListImpl list = new NodeListImpl(xmlTemp.getElement().getElementsByTagName("Record"));
if (xmlJudge.equalsIgnoreCase("html"))
{
for (int i = 0; i < list.size(); i++) {
result += "<doc>\n<DID>" + String.valueOf(i) + "</DID><propList>";
Element el = (Element)list.item(i);
l = el.getElementsByTagName("Title");
if (l.getLength() == 1)
//throw new DLI2FEException(DLI2FEException.MALFORMED_XML_EXC, "Invalid " + el.getTagName() +" element, 'Title' expected.");
//else
{
value = l.item(0).getFirstChild().getNodeValue().trim();
result += "<dc:Title>" + value + "</dc:Title>\n";
}
l = el.getElementsByTagName("Description");
if (l.getLength() == 1)
//throw new DLI2FEException(DLI2FEException.MALFORMED_XML_EXC, "Invalid " + el.getTagName() +" element, 'Description' expected.");
//else
{
value = l.item(0).getFirstChild().getNodeValue().trim();
result += "<dc:Description>" + value + "</dc:Description>\n";
}
l = el.getElementsByTagName("Identifier");
if (l.getLength() != 1)
throw new DLI2FEException(DLI2FEException.MALFORMED_XML_EXC, "Invalid " + el.getTagName() +" element, 'Identifier' expected.");
else {
value = l.item(0).getFirstChild().getNodeValue().trim();
result += "<dc:Identifier>" + value + "</dc:Identifier>";
}
result += "</propList></doc>\n";
}
result += "</SearchResult>";
}
else if (xmlJudge.equalsIgnoreCase("xml"))
{
for (int i = 0; i < list.size(); i++) {
result += "<doc>\n<DID>" + String.valueOf(i) + "</DID><propList>";
Element el = (Element)list.item(i);
NodeListImpl listXml = new NodeListImpl(el.getElementsByTagName("*"));
for (int j = 0; j < listXml.size(); j++)
{
Element elXml = (Element)listXml.item(j);
local = elXml.getLocalName();
value = elXml.getFirstChild().getNodeValue().trim();
if ("Title".equals(local))
result += "<dc:Title>" + value + "</dc:Title>\n";
else if ("Description".equals(local))
result += "<dc:Description>" + value + "</dc:Description>\n";
else if ("Identifier".equals(local))
result += "<dc:Identifier>" + value + "</dc:Identifier>\n";
else
{
result += "<webxmlqs:KeyName>" + local + "</webxmlqs:KeyName>\n";
result += "<webxmlqs:KeyValue>" + value + "</webxmlqs:KeyValue>\n";
}
}
result += "</propList></doc>\n";
}
result += "</SearchResult>";
}
else
throw new DLI2FEException(DLI2FEException.MALFORMED_XML_EXC, "Invalid 'QueryType' expected.");
}
/* String getResultPage(int pageNo) throws DLI2FEException {
String dbg;
String page = "";
if (name.equals("") && addr.equals("") && phone.equals(""))
throw new DLI2FEException(DLI2FEException.BAD_QUERY_EXC, "Query for nothing");
try {
if (!useProxy) {
BufferedReader din = new BufferedReader(new InputStreamReader(
new URL(dbg = makeRequestWithPage(pageNo)).openStream()));
System.out.println(dbg);
while ((dbg = din.readLine()) != null) {
page += dbg + "\n";
}
din.close();
} else {
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", "202.120.224.4");
System.getProperties().put("proxyPort", "8080" );
URL url = new URL(dbg = makeRequestWithPage(pageNo));
System.out.println(dbg);
URLConnection con = url.openConnection();
Base64Encoder b = new Base64Encoder("xh_xh:xh_xh");
con.setRequestProperty("Proxy-Authorization", "Basic " + b.processString());
con.setDoInput(true);
con.setDoOutput(false);
BufferedReader din = new BufferedReader(new InputStreamReader(con.getInputStream()));
while ((dbg = din.readLine()) != null) {
page += dbg + "\n";
}
din.close();
System.getProperties().put("proxySet", "false");
}
} catch (Exception any) {
throw new DLI2FEException(DLI2FEException.SERVICE_UNAVAILABLE_EXC, "Failed to contact server");
}
return page;
}
String makeRequestWithPage(int page) {
return queryPATH + paramNAME + "=" + name + "&" + paramADDRESS + "=" + addr + "&" + paramPHONE
+ "=" + phone + "&" + paramAREA + "=" + getAreaText() + "&" + paramPAGE + "=" + String.valueOf(page);
}
// detailJSString example: javascript:Detail ( 'B310000', '0795', '0000219998', '' )
// corresponding detailURL: http://www.Webxmlqs.com.cn/Prog/Trade/Detail.asp?r=B3100000&c=0795&u=00002199998&a=
String trimQuotation(String quotedStr) {
int first = quotedStr.indexOf("'");
int last = quotedStr.lastIndexOf("'");
if (last == first + 1)
return "";
else
return quotedStr.substring(first + 1, last);
}*/
public static void main(String[] args) {
int i;
Webxmlqs wx = new Webxmlqs("<queryType>XML</queryType>\n<queryClause>select * from w1;</queryClause>");
try {
wx.searchWebxmlqs();
System.out.println("numDocs: "+wx.getNumDocs());
System.out.println("after query: "+wx.getResultString());
} catch (DLI2FEException dli2fe) {
System.err.println("DLI2FE exception (" + dli2fe.getCode() + "): " + dli2fe.getReason());
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -