use of com.jsql.view.swing.sql.lexer.syntax.Token in project jsql-injection by ron190.
the class Colorer method processEvent.
private void processEvent(int position, int adjustment) {
HighlightedDocument doc = this.document.get();
if (doc == null) {
return;
}
// slurp everything up into local variables in case another
// thread changes them during coloring process
AttributeSet globalStyle = doc.getGlobalStyle();
Lexer syntaxLexer = doc.getSyntaxLexer();
DocumentReader documentReader = doc.getDocumentReader();
Object docLock = doc.getDocumentLock();
if (globalStyle != null) {
int start = Math.min(position, position + adjustment);
int stop = Math.max(position, position + adjustment);
synchronized (docLock) {
doc.setCharacterAttributes(start, stop - start, globalStyle, true);
}
return;
}
SortedSet<DocPosition> workingSet;
Iterator<DocPosition> workingIt;
DocPosition startRequest = new DocPosition(position);
DocPosition endRequest = new DocPosition(position + Math.abs(adjustment));
DocPosition dp;
DocPosition dpStart = null;
DocPosition dpEnd;
// token before the current position
try {
// all the good positions before
workingSet = this.iniPositions.headSet(startRequest);
// the last of the stuff before
dpStart = workingSet.last();
} catch (NoSuchElementException e) {
// if there were no good positions before the requested
// start,
// we can always start at the very beginning.
dpStart = new DocPosition(0);
// Ignore
IgnoreMessageException exceptionIgnored = new IgnoreMessageException(e);
LOGGER.trace(exceptionIgnored, exceptionIgnored);
}
// list.
if (adjustment < 0) {
workingSet = this.iniPositions.subSet(startRequest, endRequest);
workingIt = workingSet.iterator();
while (workingIt.hasNext()) {
workingIt.next();
workingIt.remove();
}
}
// adjust the positions of everything after the
// insertion/removal.
workingSet = this.iniPositions.tailSet(startRequest);
workingIt = workingSet.iterator();
while (workingIt.hasNext()) {
workingIt.next().adjustPosition(adjustment);
}
// now go through and highlight as much as needed
workingSet = this.iniPositions.tailSet(dpStart);
workingIt = workingSet.iterator();
dp = null;
if (workingIt.hasNext()) {
dp = workingIt.next();
}
try {
Token t;
boolean done = false;
dpEnd = dpStart;
synchronized (docLock) {
// we are playing some games with the lexer for
// efficiency.
// we could just create a new lexer each time here,
// but instead,
// we will just reset it so that it thinks it is
// starting at the
// beginning of the document but reporting a funny
// start position.
// Reseting the lexer causes the close() method on
// the reader
// to be called but because the close() method has
// no effect on the
// DocumentReader, we can do this.
syntaxLexer.reset(documentReader, 0, dpStart.getPosition(), 0);
// After the lexer has been set up, scroll the
// reader so that it
// is in the correct spot as well.
documentReader.seek(dpStart.getPosition());
// we will highlight tokens until we reach a good
// stopping place.
// the first obvious stopping place is the end of
// the document.
// the lexer will return null at the end of the
// document and wee
// need to stop there.
t = syntaxLexer.getNextToken();
}
this.newPositions.add(dpStart);
while (!done && t != null) {
// stored in tokenStyles.
if (t.getCharEnd() <= doc.getLength()) {
doc.setCharacterAttributes(t.getCharBegin() + this.change, t.getCharEnd() - t.getCharBegin(), TokenStyles.getStyle(t.getDescription()), true);
// record the position of the last bit of
// text that we colored
dpEnd = new DocPosition(t.getCharEnd());
}
this.lastPosition = t.getCharEnd() + this.change;
// from there on is fine already.
if (t.getState() == Token.INITIAL_STATE) {
// equal to the current position
while (dp != null && dp.getPosition() <= t.getCharEnd()) {
if (dp.getPosition() == t.getCharEnd() && dp.getPosition() >= endRequest.getPosition()) {
// we have found a state that is the
// same
done = true;
dp = null;
} else if (workingIt.hasNext()) {
// didn't find it, try again.
dp = workingIt.next();
} else {
// didn't find it, and there is no more
// info from last
// time. This means that we will just
// continue
// until the end of the document.
dp = null;
}
}
// so that we can do this check next time,
// record all the
// initial states from this time.
this.newPositions.add(dpEnd);
}
synchronized (docLock) {
t = syntaxLexer.getNextToken();
}
}
// remove all the old initial positions from the place
// where
// we started doing the highlighting right up through
// the last
// bit of text we touched.
workingIt = this.iniPositions.subSet(dpStart, dpEnd).iterator();
while (workingIt.hasNext()) {
workingIt.next();
workingIt.remove();
}
// Remove all the positions that are after the end of
// the file.:
workingIt = this.iniPositions.tailSet(new DocPosition(doc.getLength())).iterator();
while (workingIt.hasNext()) {
workingIt.next();
workingIt.remove();
}
// and put the new initial positions that we have found
// on the list.
this.iniPositions.addAll(this.newPositions);
this.newPositions.clear();
} catch (IOException e) {
// Ignore
IgnoreMessageException exceptionIgnored = new IgnoreMessageException(e);
LOGGER.trace(exceptionIgnored, exceptionIgnored);
}
synchronized (docLock) {
this.lastPosition = -1;
this.change = 0;
}
}
Aggregations