Doxygen的Search功能的前端是使用search.php实现的。我使用java代替php,读取search.idx
用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.ArrayList;
import java.util.StringTokenizer;
/**
* @author tyrone
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Search {
/**读取search.idx*/
public Search(File fp){
String content="";
try{
BufferedInputStreamIn = new BufferedInputStream(new 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;
}
private int getIndex(){
return this.Index;
}
private byte[] getContent(){
return this.Content;
}
private String getScontent(){
return this.Scontent;
}
/**
* 查询
* @param word
* @param statsList
* @return
*/
public ArrayList Searching(String word,ArrayList statsList){
this.setIndex(computeIndex(word));
TreeMap stat=new TreeMap();
int start=0;
int count=0;
byte[] buf=new byte[4];
if (this.getIndex()!=-1) // found a valid index
{
int totalHi=0;
int totalFreqHi=0;
int totalFreqLo=0;
//read 4 bytes skip header
int index=readInt(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<statsList.size();count++)
{
TreeMap statInfo = (TreeMap)statsList.get(count);
int multiplier = 1;
// whole word matches have a double weight
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];
// read docs info + occurrence frequency of the word
for (int i=0;i<numDocs;i++)
{
int idx=readInt(this.getIndex());
int freq=readInt(this.getIndex());
docInfo[i]=new TreeMap();
docInfo[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) // word occurs in high priority doc
{
totalHi++;
totalFreqHi+=freq*multiplier;
}
else // word occurs in low priority doc
{
totalFreqLo+=freq*multiplier;
}
}
// read name and url info for the doc
for (int i=0;i<numDocs;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 (count=start;count<statsList.size();count++)
{
TreeMap statInfo =(TreeMap)statsList.get(count);
int multiplier = 1;
// whole word matches have a double weight
String full=(String)statInfo.get("full");
if (full.compareTo("true")==0) multiplier=2;
TreeMap[] docInfo=(TreeMap[])statInfo.get("docs");
for (int i=0;i<docInfo.length;i++)
{
// compute frequency rank of the word in each 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));
}
}
}
}
}
return statsList;
}
private int readInt(int index){
byte[] buf1;
int b1,b2,b3,b4;
try{
b1=this.getContent()[index];
b2=this.getContent()[index+1];
b3=this.getContent()[index+2];
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);
return (b1<<24)|(b2<<16)|(b3<<8)|(b4);
}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=result+new String(re);
i=0;
}else{
i++;
}
index++;
}
result=result+new String(re,0,i);
this.setIndex(++index);
return result;
}
/**
*
* @param word
* @return
*/
private int computeIndex(String word)
{
int hi;
int lo;
if (word.length()<2) return -1;
// high char of the index
hi =word.charAt(0);
if (hi==0) return -1;
// low char of the index
lo =word.charAt(1);
if (lo==0) return -1;
// return index
return hi*256+lo;
}
/**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<result.size();i++){
TreeMap tm=(TreeMap)result.get(i);
TreeMap[] docs=(TreeMap[])tm.get("docs");
for (int l=0;l<docs.length;l++){
System.out.println((String)docs[l].get("name"));
System.out.println((String)docs[l].get("url"));
}
}
}
}
分享到:
相关推荐
在运行Doxygen后,它会扫描指定的源代码目录,读取源码中的注释,并依据`Doxyfile`的配置生成文档。生成的HTML文档通常包括类索引、类层次结构、命名空间、文件、类成员、函数等详细信息。此外,Doxygen还能生成源...
1. **源码解析**:Doxygen能够读取多种编程语言的源代码,包括C、C++、C#、Java、Python、Objective-C等,识别出类、函数、变量等元素,并解析它们之间的关系。 2. **注释支持**:Doxygen支持特殊的注释语法,如`/*...
1. **源代码解析**:doxygen能够读取多种编程语言(如C++, C#, Java, Python等)的源代码,并从中提取出函数、类、变量、枚举、宏等元素的详细信息。 2. **文档注释解析**:doxygen支持特定的注释格式,如JavaDoc、...
配置完成后,运行`doxygen Doxyfile`命令,Doxygen会读取配置文件并生成文档。生成的文档通常包含类索引、文件索引、命名空间、函数等部分,结构清晰,便于浏览。 5. **自定义模板和布局** 如果需要更个性化的...
Doxygen是一款强大的、开源的文档生成工具,广泛用于C++、C、Objective-C、Java、Python等编程语言的文档编写工作。它能够自动从源代码中抽取类结构、函数、变量、枚举、宏定义等信息,并生成易于阅读的HTML文档或者...
2. **文件索引**:展示库中的源文件,有助于追踪具体功能的实现位置。 3. **命名空间**:OGR库可能使用命名空间来组织相关类和函数,便于管理代码。 4. **模块**:可能包含对特定功能集的详细描述,如投影转换模块...