Highlighting StyledDocument为文件的每一行向indexof添加额外计数
我有一个奇怪的问题(至少看起来是这样),当在文本窗格中搜索字符串时,我会为每个行号获取一个额外的索引,这些行号是在使用StyledDoc时搜索并返回的,而这些行号只是从文本窗格中获取文本。我从同一个窗格中得到相同的文本,只是一个来自纯文本,另一个来自样式化文档。我是不是遗漏了什么。我将尝试列出我正在使用的两个版本之间的尽可能多的更改 纯文本版本:Highlighting StyledDocument为文件的每一行向indexof添加额外计数,highlighting,indexof,styleddocument,Highlighting,Indexof,Styleddocument,我有一个奇怪的问题(至少看起来是这样),当在文本窗格中搜索字符串时,我会为每个行号获取一个额外的索引,这些行号是在使用StyledDoc时搜索并返回的,而这些行号只是从文本窗格中获取文本。我从同一个窗格中得到相同的文本,只是一个来自纯文本,另一个来自样式化文档。我是不是遗漏了什么。我将尝试列出我正在使用的两个版本之间的尽可能多的更改 纯文本版本: public int displayXMLFile(String path, int target){ InputStreamReader i
public int displayXMLFile(String path, int target){
InputStreamReader inputStream;
FileInputStream fileStream;
BufferedReader buffReader;
if(target == 1){
try{
File file = new File(path);
fileStream = new FileInputStream(file);
inputStream = new InputStreamReader(fileStream,"UTF-8");
buffReader = new BufferedReader(inputStream);
StringBuffer content = new StringBuffer("");
String line = "";
while((line = buffReader.readLine())!=null){
content.append(line+"\n");
}
buffReader.close();
xhw.txtDisplay_1.setText(content.toString());
}
catch(Exception e){
e.printStackTrace();
return -1;
}
}
}
与样式化文档相对应(不应用样式)
以下是我的搜索方式:
public int searchText(int sPos, int target) throws BadLocationException{
String search = xhw.textSearch.getText();
String contents;
JTextPane searchPane;
if(target == 1){
searchPane = xhw.txtDisplay_1;
} else {
searchPane = xhw.txtDisplay_2;
}
if(xhw.textSearch.getText().isEmpty()){
xhw.displayDialog("Nothing to search for");
highlight(searchPane, null, 0,0);
} else {
contents = searchPane.getText();
// Search for the desired string starting at cursor position
int newPos = contents.indexOf( search, sPos );
// cycle cursor to beginning of doc window
if (newPos == -1 && sPos > 0){
sPos = 0;
newPos = contents.indexOf( search, sPos );
}
if ( newPos >= 0 ) {
// Select occurrence if found
highlight(searchPane, contents, newPos, target);
sPos = newPos + search.length()+1;
} else {
xhw.displayDialog("\"" + search + "\"" + " was not found in File " + target);
}
}
return sPos;
}
示例文件:
<?xml version="1.0" encoding="UTF-8"?>
<AlternateDepartureRoutes>
<AlternateDepartureRoute>
<AdrName>BOIRR</AdrName>
<AdrRouteAlpha>..BROPH..</AdrRouteAlpha>
<TransitionFix>
<FixName>BROPH</FixName>
</TransitionFix>
</AlternateDepartureRoute>
<AlternateDepartureRoute>
</AlternateDepartureRoutes>
当我用indexof()搜索Alt时,纯文本得到40(这是它应该返回的),用样式化文档搜索得到41。对于在I get上显示的每一行和额外的索引(这样indexof()调用将返回比第3行所需的多2行)。它找到的每一行都会发生这种情况。我错过了什么明显的东西吗?(如果我需要把它推到一个较小的类中,以便更容易检查,我可以在以后有更多时间时这样做)
提前感谢…好的,我已经找到了解决方案(基本上)。我是从两种不同的方式从同一个文本组合中获取文本的角度来处理这个问题的
String search = xw.textSearch.getText();
String contents;
String contentsS;
JTextPane searchPane;
StyledDocument sSearchPane;
searchPane = xw.txtDisplay_left;
sSearchPane = xw.txtDisplay_left.getStyledDocument();
contents = searchPane.getText();
contentsS = sSearchPane.getText(0, sSearchPane.getLength());
// Search for the desired string starting at cursor position
int newPos = contents.indexOf( search, sPos );
int newPosS = contentsS.indexOf(search, sPos);
因此,当比较两个变量“newPos”和“newPosS”时,newPos为搜索字符串所在的每一行多运行1个newPosS。因此,当查看示例文件并搜索“Alt”时,第一个实例出现在第2行。“newPos”返回41,“newPosS”返回40(然后突出显示正确的文本)。下一个实例(在第3行中找到)“newPos”返回71和“newPosS”“返回69。正如您所看到的,每一个新行都会根据发生的行号增加计数。我怀疑,对于textPane中的每一行,都会添加一个额外的字符,而StyledDoc中不存在这个字符
我确信有一个合理的解释,但我现在没有。如果您在Windows上,那么TextComponent文本(searchPane.getText())可以包含回车+换行符(\r\n),但是TextComponent的样式化文档(sSearchPane.getText(0,sSearchPane.getLength())只包含换行符(\n)。这就是为什么你的newPos总是比newPos的换行数要多的原因。要解决此问题,您可以在搜索功能中更改:
contents = searchPane.getText();
致:
这样,搜索将与样式化文档使用的索引相同
String search = xw.textSearch.getText();
String contents;
String contentsS;
JTextPane searchPane;
StyledDocument sSearchPane;
searchPane = xw.txtDisplay_left;
sSearchPane = xw.txtDisplay_left.getStyledDocument();
contents = searchPane.getText();
contentsS = sSearchPane.getText(0, sSearchPane.getLength());
// Search for the desired string starting at cursor position
int newPos = contents.indexOf( search, sPos );
int newPosS = contentsS.indexOf(search, sPos);
contents = searchPane.getText();
contents = searchPane.getText().replaceAll("\r\n","\n");