搜索
首页Javajava教程Java实现读取Doxygen查询功能的索引文件

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{
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{
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{
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{
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{
// 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;iTreeMap tm=(TreeMap)result.get(i);
TreeMap[] docs=(TreeMap[])tm.get("docs");
for (int l=0;lSystem.out.println((String)docs[l].get("name"));
System.out.println((String)docs[l].get("url"));
}
}
}
}


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Java开发的哪些方面取决于平台?Java开发的哪些方面取决于平台?Apr 26, 2025 am 12:19 AM

JavadevelovermentIrelyPlatForm-DeTueTososeVeralFactors.1)JVMVariationsAffectPerformanceNandBehaviorAcroSsdifferentos.2)Nativelibrariesviajnijniiniininiinniinindrododerplatefform.3)

在不同平台上运行Java代码时是否存在性能差异?为什么?在不同平台上运行Java代码时是否存在性能差异?为什么?Apr 26, 2025 am 12:15 AM

Java代码在不同平台上运行时会有性能差异。1)JVM的实现和优化策略不同,如OracleJDK和OpenJDK。2)操作系统的特性,如内存管理和线程调度,也会影响性能。3)可以通过选择合适的JVM、调整JVM参数和代码优化来提升性能。

Java平台独立性有什么局限性?Java平台独立性有什么局限性?Apr 26, 2025 am 12:10 AM

Java'splatFormentenceHaslimitations不包括PerformanceOverhead,versionCompatibilityIsissues,挑战WithnativelibraryIntegration,Platform-SpecificFeatures,andjvminstallation/jvminstallation/jvmintenance/jeartenance.therefactorscomplicatorscomplicatethe“ writeOnce”

解释平台独立性和跨平台发展之间的差异。解释平台独立性和跨平台发展之间的差异。Apr 26, 2025 am 12:08 AM

PlatformIndependendecealLowsProgramStormonanyPlograwsStormanyPlatFormWithOutModification,而LileCross-PlatFormDevelopmentRequiredquiresMomePlatform-specificAdjustments.platFormIndependence,EneblesuniveByjava,EnablesuniversUniversAleversalexecutionbutmayCotutionButMayComproMisePerformance.cross.cross.cross-platformd

即时(JIT)汇编如何影响Java的性能和平台独立性?即时(JIT)汇编如何影响Java的性能和平台独立性?Apr 26, 2025 am 12:02 AM

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

为什么Java是开发跨平台桌面应用程序的流行选择?为什么Java是开发跨平台桌面应用程序的流行选择?Apr 25, 2025 am 12:23 AM

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runanywhere”哲学。1)itusesbytbytybytecebytecodethatrunsonanyjvm-platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

讨论可能需要在Java中编写平台特定代码的情况。讨论可能需要在Java中编写平台特定代码的情况。Apr 25, 2025 am 12:22 AM

在Java中编写平台特定代码的原因包括访问特定操作系统功能、与特定硬件交互和优化性能。1)使用JNA或JNI访问Windows注册表;2)通过JNI与Linux特定硬件驱动程序交互;3)通过JNI使用Metal优化macOS上的游戏性能。尽管如此,编写平台特定代码会影响代码的可移植性、增加复杂性、可能带来性能开销和安全风险。

与平台独立性相关的Java开发的未来趋势是什么?与平台独立性相关的Java开发的未来趋势是什么?Apr 25, 2025 am 12:12 AM

Java将通过云原生应用、多平台部署和跨语言互操作进一步提升平台独立性。1)云原生应用将使用GraalVM和Quarkus提升启动速度。2)Java将扩展到嵌入式设备、移动设备和量子计算机。3)通过GraalVM,Java将与Python、JavaScript等语言无缝集成,增强跨语言互操作性。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具