-
parsing text
I'm working on a syntax highlighter. The problem I'm having is that some words aren't detected properly. Given these keywords: "sync" and "sync rate". I'm guessing that I'll have to restructure my whole method, so any suggestions?
"sync" is detected first and so "on" doesn't get highlighted.
Code:
import java.util.Iterator;
import java.awt.event.*;
import java.io.*;
import java.awt.*;
public class MyEditor extends DefaultStyledDocument
{
private DefaultStyledDocument doc;
private Element rootElement;
private MutableAttributeSet normal;
private MutableAttributeSet keyword;
private MutableAttributeSet comment;
private MutableAttributeSet quote;
private HashSet keywords;
private String filename = "Editor\\Keywords\\keywords105.ini";
public MyEditor()
{
doc = this;
rootElement = doc.getDefaultRootElement();
normal = new SimpleAttributeSet();
StyleConstants.setForeground(normal, Color.black);
keyword = new SimpleAttributeSet();
StyleConstants.setForeground(keyword, Color.blue);
comment = new SimpleAttributeSet();
StyleConstants.setForeground(comment, Color.gray);
StyleConstants.setItalic(comment, true);
quote = new SimpleAttributeSet();
StyleConstants.setForeground(quote, Color.red);
keywords = new HashSet();
KeywordLoader.getKeywords(keywords,filename);
}
public void insertString(int offset, String str, AttributeSet a) throws BadLocationException
{
super.insertString(offset, str, a);
processChangedLines(offset, str.length());
}
public void remove(int offset, int length) throws BadLocationException
{
super.remove(offset, length);
processChangedLines(offset, 0);
}
public void processChangedLines(int offset, int length) throws BadLocationException
{
String content = doc.getText(0, doc.getLength());
int startLine = rootElement.getElementIndex(offset);
int endLine = rootElement.getElementIndex(offset+length);
for(int i=startLine; i<= endLine; i++)
applyHighlighting(content.toLowerCase(), i);
}
public void applyHighlighting(String content, int line)
{
int startOffset = rootElement.getElement(line).getStartOffset();
int endOffset = rootElement.getElement(line).getEndOffset();
int lineLength = endOffset - startOffset;
int contentLength = content.length();
if (endOffset >= contentLength)
endOffset = contentLength - 1;
doc.setCharacterAttributes(startOffset, lineLength, normal, true);
try{
if (content.substring(startOffset, startOffset+1).equals(getSingleLineDelimiter()))
{
doc.setCharacterAttributes(startOffset, lineLength, comment, false);
return;
}
}
catch(Exception eE){System.out.println(startOffset);}
int subOffset = startOffset;
for (int i=startOffset; i<= endOffset; i++)
{
String sub = content.substring(subOffset, i);
Iterator it = keywords.iterator();
while(it.hasNext())
{
String kw = (String)it.next();
int kwIndex = sub.indexOf(kw);
if (kwIndex >= 0)
{
doc.setCharacterAttributes(subOffset+kwIndex, kw.length(), keyword, false);
subOffset = i;
break;
}
}
}
}
protected boolean isKeyword(String str)
{
return keywords.contains(str);
}
protected boolean isQuoteDelimiter(String character)
{
String quoteDelimiters = "\"";
if (quoteDelimiters.indexOf(character) < 0)
return false;
else
return true;
}
protected String getSingleLineDelimiter()
{
return "`";
}
protected String getStartDelimiter()
{
return "remstart";
}
protected String getEndDelimiter()
{
return "remend";
}
public static void main()
{
EditorKit editorKit = new StyledEditorKit()
{
public Document createDefaultDocument()
{
return new MyEditor();
}
};
final JEditorPane editor = new JEditorPane();
JButton button = new JButton("Load example");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String path = "C:/Dark Basic Software/Dark Basic Professional/Projects/rpg talk box.dba";
FileInputStream f = new FileInputStream(path);
editor.read(f, null);
editor.requestFocus();
}
catch(Exception ee){}
}
});
JFrame f = new JFrame("YET - Yet Another Dark Editor");
editor.setEditorKitForContentType("text/DB",editorKit);
editor.setContentType("text/DB");
f.getContentPane().add(new JScrollPane(editor));
f.getContentPane().add(button, BorderLayout.SOUTH);
f.setSize(800,300);
f.setVisible(true);
}
}
-
I guess you mean "rate" is not hilighted also... ?
If the match for "synch" cancels the search for a "sync rate" match
starting at the same position I would suggest that you have your
keywords in a sorted list, and for every match on one keyword (sentence)
you attempt to do a match for the next in the list on the same text position,
and if that gives a match also, you try the next.. and so on. At the first miss
you break and select the longest match you found.
ps: Why aren't you just using four HiliLighters for this ? They are much easier
to keep track of.
Last edited by sjalle; 09-04-2005 at 07:06 PM.
eschew obfuscation
-
I don't really know anything about HighLighters. Been searching around online, and so far I've only come across 1 useful example. Know any resources on the subject?
-
No, not any resources...
..but this is an example I have that adds two different hilites to a
textComponent, and removes them one by one by clicking a button.
I don't think there is anything more to it.
Code:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// A subclass of the default highlight painter
class CustomHiliter
extends DefaultHighlighter.DefaultHighlightPainter {
public CustomHiliter(Color color) {
super(color);
}
}
public class HiliteExample
extends JFrame
implements ActionListener {
ArrayList hpList = new ArrayList();
JTextField tF = new JTextField();
FlowLayout flowLayout1 = new FlowLayout();
// make a red and a green hiliter
Highlighter.HighlightPainter hpRed = new CustomHiliter(Color.red);
Highlighter.HighlightPainter hpGreen = new CustomHiliter(Color.green);
JButton remHiLiteBtn = new JButton();
public HiliteExample() {
try {
jbInit();
remHiLiteBtn.addActionListener(this);
setBounds(10, 10, 300, 100);
setVisible(true);
addHilites();
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* Add two hilites and store their reference
*/
private void addHilites() {
try {
Highlighter hL = tF.getHighlighter();
Object ref = hL.addHighlight(2, 5, hpRed);
hpList.add(ref);
ref = ref = hL.addHighlight(8, 10, hpGreen);
hpList.add(ref);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Remove a hilite, if any left
*/
private void removeAHilite() {
if (hpList.size() > 0) {
Highlighter hL = tF.getHighlighter();
Object ref=hpList.get(0);
hL.removeHighlight(ref);
hpList.remove(0);
}
}
public void actionPerformed(ActionEvent e) {
removeAHilite();
}
private void jbInit() throws Exception {
tF.setText("This is some text");
tF.setColumns(15);
this.getContentPane().setLayout(flowLayout1);
remHiLiteBtn.setText("Remove");
this.getContentPane().add(tF, null);
this.getContentPane().add(remHiLiteBtn, null);
}
public static void main(String[] args) {
HiliteExample hiEx = new HiliteExample();
}
}
Last edited by sjalle; 09-05-2005 at 02:01 PM.
eschew obfuscation
-
Hmm. come to think of it, this is not hiliting the text, it makes a "selected" rectangle
around the text....
eschew obfuscation
Similar Threads
-
By Kevin in forum VB Classic
Replies: 3
Last Post: 12-05-2005, 06:25 PM
-
By dartmanx in forum Java
Replies: 1
Last Post: 06-07-2005, 07:22 PM
-
By J Michael in forum Database
Replies: 1
Last Post: 03-15-2002, 12:55 PM
-
Replies: 3
Last Post: 08-30-2001, 11:45 AM
-
By George Gilbert in forum vb.announcements
Replies: 0
Last Post: 08-19-2001, 11:34 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks