⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yellowpagelsp.java

📁 数字图书馆的互操作接口
💻 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.*;

public class YellowpageLSP implements Metadata, Searcher, ResultAccess {
  static final String COLL_NAME = "中国黄页";
  public static final String Namespace = "http://www.yellowpage.com.cn/#";
  static int SIDGenerator = 0;
  Hashtable sessions = new Hashtable();
  String namet, addrt, phonet, cityt; // temporary storage of parsed arguments

  class Session implements ActionListener {

    int SID;
    Yellowpage yp;
    Timer timer;
    YellowpageLSP container;
    boolean done;
    long endTime;

    Session(YellowpageLSP yplsp, String queryName, String queryAddr, String queryPhone, String queryCity) throws DLI2FEException {
      container = yplsp;
      SID = container.getNextSID();
      yp = new Yellowpage(queryName, queryAddr, queryPhone, queryCity);
      endTime = System.currentTimeMillis() + DLI2FE.DEFAULT_TIMEOUT*1000;
      timer = new Timer(DLI2FE.DEFAULT_TIMEOUT*1000, this);
      timer.setRepeats(false);
      timer.start();
      done = false;
      yp.searchYellowpage();
      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 String createResult(String selection) throws DLI2FEException {
      RangeEnumerator re = new RangeEnumerator(selection);
      String result = "<SearchResult xmlns='" + DLI2FE.Namespace +"' xmlns:dc='" +
      DublinCore.Namespace + "' xmlns:yp='" + Namespace + "'>\n";
      while (re.next()) {
        result += yp.getResultString(re.get() - 1) + "\n";
      }
      result += "</SearchResult>";
      return result;
    }

    public Yellowpage getYP() {
      return yp;
    }

    public int getSessionID() {
      return SID;
    }

    public String toString() {
      return "[ SID : " + SID + "]";
    }
  }

  public int getNextSID() {
    return ++SIDGenerator;
  }

  public YellowpageLSP() {
  }

  public String toString() {
    return "YellowpageLSP";
  }

  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;
  }

  // parse query XML String and stores arguments to temporary storage.
  // XML query String example:
  /*
  dli2fens:http://localhost:9524/ search 1 "<basicsearch xmlns='DAV:' xmlns:dc='http://purl.org/metadata/dublin_core#' xmlns:yp='http://www.yellowpage.com.cn/#'> <where> <and> <like> <prop><yp:Address/></prop> <literal>天山西路</literal> </like> <eq> <prop><yp:City/></prop> <literal>上海</literal> </eq> </and> </where> </basicsearch>" 20
  <basicsearch xmlns='DAV:' xmlns:dc='http://purl.org/metadata/dublin_core#'
      xmlns:yp='http://www.yellowpage.com.cn/#'>
    <where>
      <and>
        <like>
          <prop><yp:Address/></prop>
          <literal>天山西路</literal>
        </like>
        <eq>
          <prop><yp:City/></prop>
          <literal>上海</literal>
        </eq>
      </and>
    </where>
  </basicsearch>
  */
  void parseQuery(XMLObject query) throws DLI2FEException {
    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);
    for (int i = 0; i < list.size(); 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.");
      String 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.");
      String value = l.item(0).getFirstChild().getNodeValue().trim();
      if ("Title".equals(local))
        namet = value;
      else if ("Address".equals(local))
        addrt = value;
      else if ("Phone".equals(local))
        phonet = value;
      else if ("City".equals(local))
        cityt = value;
      else
        throw new DLI2FEException(DLI2FEException.MALFORMED_XML_EXC, "Invalid property " + local +".");
    }
  }

  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/yellowpage</baseURI> <ctgrName>Yellowpage</ctgrName>\n" +
      "<contentConstraint/> <boolOp>and</boolOp> <language>zh_CN</language>\n</SiteMetadata>");
  }

  public void getAttributeInfo (String ctgrName,
                                XMLObject attributeInfo) throws DLI2FEException {
    if ("Yellowpage".equals(ctgrName)) {
      attributeInfo.setString("<AttributeInfo collectionName='" + COLL_NAME + "' xmlns:dc='" +
      DublinCore.Namespace + "' xmlns:yp='" + Namespace + "'> \n" +
      "<attr attrName='dc:Title'><searchable/><retrievable/><operator>like</operator></attr>\n" +
      "<attr attrName='dc:Identifier'><retrievable/><operator/></attr>\n" +
      "<attr attrName='yp:Address'><searchable/><operator>like</operator></attr>\n" +
      "<attr attrName='yp:Phone'><searchable/><operator>like</operator></attr>\n" +
      "<attr attrName='yp:City'><searchable/><operator>eq</operator></attr>\n" +
      "</AttributeInfo>");
    }
    else
      throw new DLI2FEException(DLI2FEException.INVALID_REQUEST_EXC, "Category " + notNull(ctgrName) + " not supported.");
  }

  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 {
    System.out.println("YellowpageLSP:" + query.getString());
    expectedTotal.value = Searcher.UNKNOWABLE;
    parseQuery(query);
    // ignores clientSID, subcols, docPropList and queryOptions arguments
    Session session = new Session(this, namet, addrt, phonet, cityt);
    sessions.put(new Integer(session.getSessionID()), session);
    serverSID.value = session.getSessionID();
    if ((expectedTotal.value = session.getYP().getNumDocs()) == 0) {
      return;
    }
    String selection = "1-";
    if ( numDocs == Searcher.RETURN_ALL_DOCS || numDocs > expectedTotal.value)
      selection += String.valueOf(expectedTotal.value);
    else
      selection += String.valueOf(numDocs);
    String dbg = session.createResult(selection);
    System.out.println(dbg);
    result.setString(dbg);
  }

  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.getYP().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.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 {
    YellowpageLSP yellowpageLSP = new YellowpageLSP();
    ServerCORBATransport t = new ServerCORBATransport();
    t.addLSP(yellowpageLSP);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -