📄 webxmlqslsp.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 javax.swing.Timer;
import java.awt.event.*;
import java.util.Hashtable;
import org.w3c.dom.*;
import dli2fe.xml.NodeListImpl;
import dli2fe.helpers.*;
/* sample parameters for dli2fe.sample.Client
dli2fens:http://localhost:9524/ search 1 "<basicsearch xmlns='DAV:' xmlns:dc='http://purl.org/metadata/dublin_core#' xmlns:webxmlqs='http://www.cs.fudan.edu.cn/WebXMLQS/#'> <where> <and> <like> <prop><webxmlqs:QueryClause/></prop> <literal>select b.name,b.date from customer b where b in pages(http://america/zb/x.xml,5,20) and b.orders.item.number>1 </literal> </like> </and> </where> </basicsearch>" 1
dli2fens:http://localhost:9524/ search 1 " <basicsearch xmlns='DAV:' xmlns:dc='http://purl.org/metadata/dublin_core#' xmlns:webxmlqs='http://www.cs.fudan.edu.cn/WebXMLQS/#'> <where> <and> <eq> <prop><webxmlqs:StartURL/></prop> <literal>C:\Inetpub\wwwroot\postinfo.html</literal> </eq> <eq> <prop><webxmlqs:Depth/></prop> <literal>1</literal> </eq> <eq> <prop><webxmlqs:KeySearch/></prop> <literal>用户</literal> </eq> <eq> <prop><webxmlqs:KeyOp/></prop> <literal>1</literal></eq></and> </where> </basicsearch>" 1
dli2fens:http://localhost:9524/ search 1 " <basicsearch xmlns='DAV:' xmlns:dc='http://purl.org/metadata/dublin_core#' xmlns:webxmlqs='http://www.cs.fudan.edu.cn/WebXMLQS/#'> <where> <and> <eq> <prop><webxmlqs:StartURL/></prop> <literal>C:\Inetpub\wwwroot\postinfo.html</literal> </eq> <eq> <prop><webxmlqs:Depth/></prop> <literal>1</literal> </eq> <eq> <prop><webxmlqs:KeySearch/></prop> <literal>用户</literal> </eq> <eq> <prop><webxmlqs:KeyOp/></prop> <literal>0</literal></eq><eq> <prop><webxmlqs:KeySearch/></prop> <literal>网页</literal> </eq> <eq> <prop><webxmlqs:KeyOp/></prop> <literal>0</literal></eq></and> </where> </basicsearch>" 1
*/
public class WebxmlqsLSP implements Metadata, Searcher, ResultAccess {
static int SIDGenerator = 0;
static final String COLL_NAME = "WebXMLQS";
public static final int keyNum = 3;
public static final String Namespace = "http://www.cs.fudan.edu.cn/DLI2FE/1.0#";
public static final String WebNamespace = "http://www.cs.fudan.edu.cn/WebXMLQS/1.0#";
public static final String simplepage = "http://10.11.10.33/cgi/simple.dll?simplesearch";
public static final String complexpage = "http://10.11.10.33/cgi/cplex.dll?cplexsearch";
public static final String xmlpage = "http://10.11.10.33/cgi/xmlsearch.dll?inputstring";
Hashtable sessions = new Hashtable();
String queryClauset,startURLt,deptht;
String[] keySearcht,keyOpt;
int keySearchNum;
int keyOpNum;
int numDocst; //需要的查询结果数
String tempResult;
class Session implements ActionListener {
int SID;
Webxmlqs wx;
Timer timer;
WebxmlqsLSP container;
boolean done;
long endTime;
Session(WebxmlqsLSP wxlsp, String queryString) throws DLI2FEException {
container = wxlsp;
SID = container.getNextSID();
wx = new Webxmlqs(queryString);
endTime = System.currentTimeMillis() + DLI2FE.DEFAULT_TIMEOUT*1000;
timer = new Timer(DLI2FE.DEFAULT_TIMEOUT*1000, this);
timer.setRepeats(false);
timer.start();
done = false;
wx.searchWebxmlqs();
done = true;
}
public void extendTimeout(int seconds) {
endTime = System.currentTimeMillis() + seconds * 1000;
timer.stop();
timer.setDelay(seconds*1000);
timer.setRepeats(false);
timer.start();
}
public int getTimeout() {
return (int)((endTime - System.currentTimeMillis())/1000);
}
// for interface ActionListener to receive timer's message
public void actionPerformed(ActionEvent e) {
if (!done) {
timer.stop();
timer.setDelay(10000);
timer.setRepeats(false);
timer.start();
} else
container.removeSession(this);
}
public int getSessionID() {
return SID;
}
public Webxmlqs getWX() {
return wx;
}
public String toString() {
return "[ SID : " + SID + "]";
}
}
public int getNextSID() {
return ++SIDGenerator;
}
public WebxmlqsLSP() {
}
public String toString() {
return "WebXMLQS";
}
public void addSession(Session session) {
sessions.put(new Integer(session.getSessionID()), session);
}
public void removeSession(Object handle) {
Session session = (Session)handle;
System.out.println("Session " + session + " timed out!");
sessions.remove(new Integer(session.getSessionID()));
}
String notNull(String str) {
return str == null ? "" : str;
}
void parseQuery(XMLObject query) throws DLI2FEException { //分析开始的xml的查询,得到各字段的值
String value,local;
keySearchNum = 0;
keyOpNum = 0;
dli2fe.xml.XMLObject xml = dli2fe.xml.XMLObject.create(query);
// Match all 'like' or 'eq' elements
NodeListImpl list = new NodeListImpl(xml.getElement().getElementsByTagName("like"));
NodeListImpl list1 = new NodeListImpl(xml.getElement().getElementsByTagName("eq"));
list.addAll(list1);
int j = list.size();
keySearcht = new String[j];
keyOpt = new String[j];
for (int i = 0; i < j; i++) {
Element el = (Element)list.item(i);
NodeList l = el.getElementsByTagName("prop");
if (l.getLength() != 1)
throw new DLI2FEException(DLI2FEException.MALFORMED_XML_EXC, "Invalid " + el.getTagName() +" element, 'prop' expected.");
Element prop = (Element)l.item(0);
l = prop.getElementsByTagName("*");
if (l.getLength() != 1)
throw new DLI2FEException(DLI2FEException.MALFORMED_XML_EXC, "Invalid " + el.getTagName() +" element, prop expected.");
local = l.item(0).getLocalName();
l = el.getElementsByTagName("literal");
if (l.getLength() != 1)
throw new DLI2FEException(DLI2FEException.MALFORMED_XML_EXC, "Invalid " + el.getTagName() +" element, 'literal' expected.");
else
value = l.item(0).getFirstChild().getNodeValue().trim();
if ("QueryClause".equals(local))
queryClauset = value;
else if ("StartURL".equals(local))
startURLt = value;
else if ("Depth".equals(local))
deptht = value;
else if ("KeySearch".equals(local))
keySearcht[keySearchNum++] = value;
else if ("KeyOp".equals(local))
keyOpt[keyOpNum++] = value;
else
throw new DLI2FEException(DLI2FEException.MALFORMED_XML_EXC, "Invalid property " + local +".");
}
if (keySearchNum != keyOpNum)
throw new DLI2FEException(DLI2FEException.MALFORMED_XML_EXC, "Invalid key. ");
}
public void getSiteMetadata (XMLObject theSiteMetadata) throws DLI2FEException{
theSiteMetadata.setString("<SiteMetadata xmlns='" + DLI2FE.Namespace +"'>\n<version>1.0</version> " +
"<collectionName>" + COLL_NAME + "</collectionName> <baseURI>dli2fens:http://localhost:9524/webxmlqs</baseURI> <ctgrName>WebXMLQS</ctgrName>\n" +
"<contentConstraint/> <boolOp>and</boolOp> <language>zh_CN</language>\n</SiteMetadata>");
}
public void getAttributeInfo (String ctgrName,
XMLObject attributeInfo) throws DLI2FEException {
if ("WebXMLQS".equals(ctgrName)) {
attributeInfo.setString("<AttributeInfo collectionName='" + COLL_NAME + "' xmlns='" + Namespace +
"' xmlns:dc='" + DublinCore.Namespace + "' xmlns:webxmlqs='" + WebNamespace + "'> \n" +
"<attr attrName='webxmlqs:QueryClause'><searchable/><operator>eq</operator></attr>\n" +
"<attr attrName='webxmlqs:StartURL'><searchable/><operator>eq</operator></attr>\n" +
"<attr attrName='webxmlqs:Depth'><searchable/><operator>eq</operator></attr>\n" +
"<attr attrName='webxmlqs:KeySearch'><searchable/><operator>eq</operator></attr>\n" +
"<attr attrName='webxmlqs:KeyOp'><searchable/><operator>eq</operator></attr>\n" +
"<attr attrName='dc:Title'><retrievable/><operator/></attr>\n" +
"<attr attrName='dc:Identifier'><retrievable/><operator/></attr>\n" +
"<attr attrName='dc:Description'><retrievable/><operator/></attr>\n" +
"<attr attrName='webxmlqs:KeyName'><retrievable/><operator/></attr>\n" +
"<attr attrName='webxmlqs:KeyValue'><retrievable/><operator/></attr>\n" +
"</AttributeInfo>");
}
else
throw new DLI2FEException(DLI2FEException.INVALID_REQUEST_EXC, "Category " + notNull(ctgrName) + " not supported.");
}
public String createHead(String contentString) throws DLI2FEException {
String result = "<SearchResult xmlns='" + DLI2FE.Namespace +"' xmlns:webxmlqs='" +
WebNamespace + "'>\n";
result += contentString + "</SearchResult>";
return result;
}
public String getqueryString() throws DLI2FEException {
String tempString = "";
if (queryClauset != null)
{tempString = xmlpage + "&Instring=" + queryClauset;}
else
{startURLt = startURLt == null ? "http://www.sina.com.cn" : startURLt.trim();
deptht = deptht == null ? "2" : deptht.trim();
//numDocst = numDocst == null ? "10" : numDocst.trim();
if (keySearchNum > 1)
{
for (int i=0; i<keyNum;i++)
{
keySearcht[i] = keySearcht[i] == null ? "" : keySearcht[i].trim();
keyOpt[i] = keyOpt[i] == null ? "0" : keyOpt[i].trim();
}
tempString = complexpage + "&Url=" + startURLt;
for (int i=0; i<keyNum;i++)
{
int j = i+1;
tempString += "&Keyword" + j + "=" + keySearcht[i] +
"&Key" + j + "=" + keyOpt[i];
}
tempString += "&Depth=" + deptht +"&Totallink=" + numDocst;
}
else
{
keySearcht[0] = keySearcht[0] == null ? "" : keySearcht[0].trim();
tempString = simplepage + "&Url=" + startURLt + "&Keyword=" + keySearcht[0]
+ "&Depth=" + deptht +"&Totallink=" + numDocst;
}
}
/*String result = createHead(tempString);
return result;*/
return tempString;
}
// XML query String example:
/*
<basicsearch xmlns='DAV:' xmlns:dc='http://purl.org/metadata/dublin_core#'
xmlns:webxmlqs='http://www.cs.fudan.edu.cn/WebXMLQS/#'>
<where>
<and>
<like>
<prop><webxmlqs:QueryClause/></prop>
<literal>select * from a where c=a;</literal>
</like>
</and>
</where>
</basicsearch>
*/
//html query String example:
/*
<basicsearch xmlns='DAV:' xmlns:dc='http://purl.org/metadata/dublin_core#'
xmlns:webxmlqs='http://www.cs.fudan.edu.cn/WebXMLQS/#'>
<where>
<and>
<eq>
<prop><webxmlqs:StartURL/></prop>
<literal>C:\Inetpub\wwwroot\postinfo.html</literal>
</eq>
<eq>
<prop><webxmlqs:Depth/></prop>
<literal>1</literal>
</eq>
<eq>
<prop><webxmlqs:KeySearch/></prop>
<literal>用户</literal>
</eq>
<eq>
<prop><webxmlqs:KeyOp/></prop>
<literal>1</literal>
</eq>
</and>
</where>
</basicsearch>
*/
public void search (int clientSID,
XMLObject subcols,
XMLObject query,
int numDocs,
XMLObject docPropList,
XMLObject queryOptions,
org.omg.CORBA.IntHolder expectedTotal,
org.omg.CORBA.IntHolder serverSID,
XMLObject result) throws DLI2FEException {
String queryString;
numDocst=numDocs;
expectedTotal.value = Searcher.UNKNOWABLE;
parseQuery(query);
if ((queryClauset == null) && (keySearchNum == 0))
throw new DLI2FEException(DLI2FEException.BAD_QUERY_EXC, "Query for nothing");
queryString = getqueryString();
Session session = new Session(this, queryString);
sessions.put(new Integer(session.getSessionID()), session);
serverSID.value = session.getSessionID();
if ((expectedTotal.value = session.getWX().getNumDocs()) == 0) {
return;
}
tempResult = session.getWX().getResultString();
result.setString(tempResult);
}
public void getSessionInfo (int serverSID,
org.omg.CORBA.IntHolder expectedTotal,
org.omg.CORBA.IntHolder stateTimeout) throws DLI2FEException {
Session session = (Session)sessions.get(new Integer(serverSID));
if (session == null)
throw new DLI2FEException(DLI2FEException.INVALID_SESSIONID_EXC, "Invalid session ID.");
expectedTotal.value = session.getWX().getNumDocs();
stateTimeout.value = session.getTimeout();
}
public void getDocs (int serverSID,
int reqID,
XMLObject docPropList,
String docsToGet,
XMLObject res) throws DLI2FEException {
// ignores reqID, docPropList
Session session = (Session)sessions.get(new Integer(serverSID));
if (session == null)
throw new DLI2FEException(DLI2FEException.INVALID_SESSIONID_EXC, "Invalid session ID.");
//res.setString(session.getWX.createResult(docsToGet));
}
public void extendStateTimeout (int serverSID,
int secondsToExtend) throws DLI2FEException {
Session session = (Session)sessions.get(new Integer(serverSID));
if (session == null)
throw new DLI2FEException(DLI2FEException.INVALID_SESSIONID_EXC, "Invalid session ID.");
session.extendTimeout(secondsToExtend);
}
// Do nothing
public void cancelRequest (int serverSID,
int reqID) throws DLI2FEException {
return;
}
public static void main(String[] args) throws Exception{
WebxmlqsLSP WebxmlqsLSP1 = new WebxmlqsLSP();
ServerCORBATransport t = new ServerCORBATransport();
t.addLSP(WebxmlqsLSP1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -