📄 yellowpage.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
*/
/**
* this is the internal implementation class for YellowpageLSP.
* this class will search yellowpage.com.cn for enterprise name/address/phone
* information from http://www.yellowpage.com.cn/
*
*/
import dli2fe.DLI2FE;
import dli2fe.DLI2FEException;
import java.io.*;
import dli2fe.xml.XMLObject;
import java.util.*;
import java.net.*;
import org.w3c.tools.codec.Base64Encoder;
//import HTTPClient.*;
public class Yellowpage {
public static final String Namespace = "http://www.yellowpage.com.cn/#";
public static final String NAME = Namespace + "name";
public static final String ADDRESS = Namespace + "address";
public static final String PHONE = Namespace + "phone";
// search URL and parameters
public static final String ypHOST = "www.yellowpage.com.cn";
public static final String ypURL = "http://www.yellowpage.com.cn/";
public static final String queryPATH = ypURL + "Prog/Busi.asp?";
public static final String paramNAME = "NameText";
public static final String paramADDRESS = "AddrText";
public static final String paramPHONE = "TeleText";
public static final String paramAREA = "AreaText";
public static final String paramPAGE = "p";
// patterns for search results
public static final String NOT_FOUND = "未查到符合条件的企事业单位";
public static final String FOUND1 = "共查到";
public static final String FOUND2 = "个企事业";
public static final String JS_DETAIL = "javascript:Detail";
// patterns for detail
public static final String detailNAME = "名 称:";
public static final String detailADDR = "地 址:";
public static final String detailPOST = "邮 编:";
public static final String detailPHONE = "电 话:";
public static final int resultsPerPage = 20;
public static final String detailURL = "http://www.yellowpage.com.cn/Prog/Trade/Detail.asp?";
public static boolean useProxy = true;
String name;
String addr;
String phone;
String city;
//HTTPConnection con;
Vector result;
int numResults;
public class ResultDoc {
public String name;
public String url;
}
public Yellowpage() {
//HTTPConnection.setProxyServer("202.120.224.4", 8080);
//AuthorizationInfo.addBasicAuthorization("202.120.224.4", 8080, "unspecified", "xh_xh", "xh_xh");
result = new Vector();
}
public Yellowpage(String name0, String addr0, String phone0, String city0) {
this();
name = name0 == null ? "" : name0.trim();
addr = addr0 == null ? "" : addr0.trim();
phone = phone0 == null ? "" : phone0.trim();
city = city0 == null ? "上海" : city0.trim();
}
public ResultDoc getResult(int index) throws DLI2FEException {
if (index < 0 || index >= result.size())
throw new DLI2FEException(DLI2FEException.INVALID_REQUEST_EXC, "Non existent result:" + index);
return (ResultDoc)result.elementAt(index);
}
public String getResultString(int index) throws DLI2FEException {
if (index < 0 || index >= result.size())
throw new DLI2FEException(DLI2FEException.INVALID_REQUEST_EXC, "Non existent result" + index);
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>";
}
public int getNumDocs() {
return numResults;
}
public void searchYellowpage() throws DLI2FEException {
searchYellowpage(name, addr, phone, city);
}
public void searchYellowpage(String name0, String addr0, String phone0, String city0) throws DLI2FEException {
String resultHTML = "";
int i, j;
name = name0 == null ? "" : name0.trim();
addr = addr0 == null ? "" : addr0.trim();
phone = phone0 == null ? "" : phone0.trim();
city = city0 == null ? "上海" : city0.trim();
resultHTML = getResultPage(1);
if (resultHTML.indexOf(NOT_FOUND) != -1) {
result = new Vector();
throw new DLI2FEException(DLI2FEException.NOT_FOUND_EXC, "No document found");
}
i = resultHTML.indexOf(FOUND1);
j = resultHTML.indexOf(FOUND2);
if (i == -1 || j == -1)
throw new DLI2FEException(DLI2FEException.NOT_FOUND_EXC, "Error parsing documents");
numResults = Integer.parseInt(resultHTML.substring(i + 3, j));
result = new Vector();
parsePage(resultHTML); // parse this page and get the results to result Vector
if (numResults > resultsPerPage)
for (i = 2; i <= numResults / resultsPerPage + 1; i++) {
resultHTML = getResultPage(i);
parsePage(resultHTML);
}
numResults = result.size();
}
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", "false");
System.getProperties().put("proxyHost", "202.120.224.104");
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_ns:xh_ns");
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;
}
void parsePage(String page) {
int i, j, k, did;
String detailURL, unitName;
i = page.indexOf(JS_DETAIL);
while (i != -1) {
ResultDoc doc = new ResultDoc();
String dbg;
j = page.indexOf(")", i);
doc.url = detailURL = makeDetailURL(page.substring(i, j + 1));
j = page.indexOf(">", j);
j = page.indexOf(">", j + 1);
k = page.indexOf("<", j);
doc.name = unitName = page.substring(j + 1, k);
result.addElement(doc);
i = page.indexOf(JS_DETAIL, i + 5);
}
}
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.yellowpage.com.cn/Prog/Trade/Detail.asp?r=B3100000&c=0795&u=00002199998&a=
String makeDetailURL(String detailJSString) {
String request = detailURL;
detailJSString = detailJSString.substring(detailJSString.indexOf("(") + 1, detailJSString.indexOf(")"));
StringTokenizer stk = new StringTokenizer(detailJSString, ",");
request += "r=" + trimQuotation(stk.nextToken());
request += "&c=" + trimQuotation(stk.nextToken());
request += "&u=" + trimQuotation(stk.nextToken());
request += "&a=" + trimQuotation(stk.nextToken());
return request;
}
String trimQuotation(String quotedStr) {
int first = quotedStr.indexOf("'");
int last = quotedStr.lastIndexOf("'");
if (last == first + 1)
return "";
else
return quotedStr.substring(first + 1, last);
}
String getAreaText() {
if (city == null)
return "";
if (city.equals("北京"))
return "B110000BJ-北京";
else if (city.equals("武汉"))
return "B420100WH-武汉";
return "B310000SH-上海";
}
public static void main(String[] args) {
int i;
Yellowpage yp = new Yellowpage(null, "天山", null, "上海");
try {
yp.searchYellowpage();
for (i = 0; i < yp.getNumDocs(); i++) {
System.out.println("NAME: " + yp.getResult(i).name + "\tURL: " + yp.getResult(i).url);
}
} catch (DLI2FEException dli2fe) {
System.err.println("DLI2FE exception (" + dli2fe.getCode() + "): " + dli2fe.getReason());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -