Doxygen的Search功能的前端是使用search.php實現的。我使用java取代php,
* @author tyrone*
* TODO 要更改此生成的類型註釋的模板,請轉到* 視窗 - 首選項 - Java - 程式碼樣式 - 程式碼模板
用Doxygen產生原始碼的文件需要設定文件,設定檔內有search選項:
#------------------ -------------------------------------------------- -------
# Configuration::additions related to the search engine#------------------------------ ---------------------------------------------
SEARCHENGINE = YES如果YES,則在產生文件時會產生search.idx索引檔案和search.php查詢介面。
search.php會將要查詢的字串作為輸入參數呼叫用php實現的查詢function search($file,$word,&$statsList)
我將該方法以及該方法調用的其他方法翻譯成java語言,如下:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.TreeMap;
import java.util.ArrayListnutil. StringTokenizer;
/**讀取search.idx*/
public class Search {
/**
* 查詢
* @param word
* @param statsList
* @return
*/
public Search(File fp){
String content="";
try{
BufferedInputStreamIn = new BufferedInputStream(new BufferedInputsnew FileInputStream( fp ));
int len=In.available();
this.Content=new byte[len];
In.read(this.Content);
Scontent=new String(this.Content);
} catch(Exception ex){
ex.printStackTrace();
}
this.Scontent=new String(this.Content);
//this.Content=content.getBytes();
}
private byte[] Content;
private String Scontent;
private int Index;
private void setIndex(int index){
this.Index=index;
}
花ate byte[] getContent(){
return this.Content;
}
private String getScontent(){
return this.Scontent;
}
/**&*/
public ArrayList Search {
this.setIndex(computeIndex(word));
TreeMap stat=new TreeMap();
int start=0;
int count=0;
byte[] buf=new byte[4];
if (this;
if (this;
if (this;
if .getIndex()!=-1) // found a valid index
{
int totalHi=0;
int totalFreqHi=0;
int totalFreqLo=0;
//read 4 bytes int totalFreqLo=0;
//read 4 bytes skip headerbytes skip. this.getIndex()*4+4);
//int index=readInt(8,this.getIn());
if (index>0){// found words matching the hash key
start=statsList.size ();
count=start;
String w=readString(index);
while (w.length()!=0){
int statIdx = readInt(this.getIndex());
if (w.compareTo( word)>=0){
// found word that matches (as substring)
stat.put("word",word);
stat.put("match",w);
stat.put("index" ,new Integer(statIdx));
if (w.length()==word.length())
stat.put("full","true");
else
stat.put("full"," false");
statsList.add(stat);
}
w = readString(this.getIndex());
}
for (count=start;count
TreeMap statInfo = (TreeMap)statsList.get(count);
int multiplier = 1;
//整個單字匹配有雙倍權重
int multiplier = 1;
//整個單字匹配有雙倍權重
String full=(String)statInfo.get("full");
if (full.compareTo("true")==0) multiplier=2;
Integer inte=(Integer)statInfo.get("index");
int numDocs = readInt(inte.intValue());
TreeMap[] docInfo =new TreeMap[numDocs];
//讀取文檔資訊+單字出現頻率
for (int i=0;i
int idx=readInt(this.getIndex());
int freq=readInt(this.getIndex());
docInfo[i]=new TreeMap();
doc[i].put("idx" ,new Integer(idx));
docInfo[i].put("freq",new Integer(freq>>1 ));
docInfo[i].put("rank",new Double(0.0));
docInfo[i].put("hi",new Integer(freq&1));
if ((freq&1)>0 ) // 單字出現在高優先權
{
totalHi++;
totalFreqHi+=freq*multiplierq*multiplierq*multiplierq*multiplierq*multiplierq*multiplierq*multiplierq*multiplierq*multiplierq*multiplierq*multiplierq*multiplierq*multiplierq*multiplierq*mult
else // 單詞出現在低優先級doc
{
totalFreqLo+=freq*multiplier;
}
}
// 讀取文檔的名稱和網址信息
for (int i=0;i
Integer idx=(Integer)docInfo[i].get("idx");
docInfo[i].put( "name",readString(idx.intValue()));
docInfo[i].put( "url",readString(this.getIndex()));
}
statInfo.put("docs",docInfo);
}
int TotalFreq=(totalHi+1)*totalFreqLo +totalFreqHi;
for(totalHi+1)*totalFreqLo +totalFreqHi; start;count
TreeMap statInfo =(TreeMap)statsList.get(count);
int multiplier = 1;
//全字匹配有雙倍權重
String full=(String )statInfo.get("full");
if (full.compareTo("true")==0) multiplier=2 ;
TreeMap[] docInfo=(TreeMap[])statInfo.get("docs");
for (int i=0;i
//計算每個字中的詞頻排名doc
Integer inte=(Integer)docInfo[i].get("freq");
int freq=inte.intValue();
inte=(Integer)docInfo[i].get("hi");
if (inte.intValue()>0){
docInfo[i].put("rank", new Double((freq*multiplier+totalFreqLo)/totalFreq));
}else{
docInfo[i].put( "rank",new Double((freq*multiplier)/totalFreq));
}
}
}
}
}
}
回傳statsList;
}
private int readInt(int index){
byte[] buf1;
int b1,b2,b3,b4;
];
b2=this.getContent()[index+1];
b4=this.getContent()[index+3];
/**費了好大勁,才知道java的byte轉換成ASCII碼是16位,idx存的是8位的。*/
b1=b1&0xff;
b2=b2&0xff;
b3=b3&0xff;
b4=b4&0xff;
index= index+4;
this.setIndex(index);
index= index+4;
this.setIndex(index); }catch(Exception ex){
}
return -1;
}
private String readString(int index){
String result="";
byte[] re=new byte[60];
int i=0;
byte ind;
while ((ind=this.getContent()[index])!=0){
re[i]=ind;
if (i==59){
result=結果+new String(re);
i=0 ;
}else{
i++;
}
index++;
}
result=結果+new String(re,0, i);
this.setIndex(++index);
回傳結果;
}
/**
*
* @param word
* @return
*/
private intcomputeIndex(String word)
{
int hi;
int lo;
if (word. length()//索引的高位字元
hi = word.charAt(0);
if (hi==0) return -1;
//索引的低字元
lo =word .charAt(1);
if (lo==0) return -1;
// 回傳索引
}
/**args[0]=search.idx, args[1]="word1+word2+..." ,如何顯示statsList 結果已經不重要了*/
public static void main (String[] args){
Search se=new Search(new File(args[ 0]));
StringTokenizer st = new StringTokenizer(args[1],"+");
ArrayList result=new ArrayList();
while (st.hasMoreTokens()){
result=se.Searching(st. nextToken(),result);
}
for(int i=0;i
TreeMap[] docs=(TreeMap [])tm.get("docs");
for (int l=0;l
System.out.println((String)docs[l].get("url"));
}
}
}