PlainLineTokenizer.java revision c23e82b612acd5e947c164114377578116f6d298
/* The following code was generated by JFlex 1.4.1 on 2/22/06 1:19 AM */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* ident "@(#)PlainLineTokenizer.lex 1.2 06/02/22 SMI"
*/
/**
* for plain text tokenizers
*/
package org.opensolaris.opengrok.search.context;
import org.opensolaris.opengrok.web.*;
import java.util.*;
import java.io.*;
import org.apache.lucene.analysis.*;
import org.opensolaris.opengrok.search.Hit;
/**
* This class is a scanner generated by
* <a href="http://www.jflex.de/">JFlex</a> 1.4.1
* on 2/22/06 1:19 AM from the specification file
* <tt>/home/cb117521/prj/opengrok-0.3/src/org/opensolaris/opengrok/search/context/PlainLineTokenizer.lex</tt>
*/
public class PlainLineTokenizer {
/** This character denotes the end of file */
public static final int YYEOF = -1;
/** initial size of the lookahead buffer */
private static final int ZZ_BUFFERSIZE = 16384;
/** lexical states */
public static final int YYINITIAL = 0;
/**
* Translates characters to character classes
*/
private static final String ZZ_CMAP_PACKED =
"\12\0\1\12\31\0\3\11\4\0\1\11\1\0\1\11\1\3\1\0"+
"\1\4\11\2\1\11\2\0\1\11\1\0\2\11\6\10\21\1\1\6"+
"\2\1\1\5\1\0\1\7\1\11\1\1\1\0\6\10\21\1\1\6"+
"\2\1\uff85\0";
/**
* Translates characters to character classes
*/
private static final char [] ZZ_CMAP = zzUnpackCMap(ZZ_CMAP_PACKED);
/**
* Translates DFA states to action switch labels.
*/
private static final int [] ZZ_ACTION = zzUnpackAction();
private static final String ZZ_ACTION_PACKED_0 =
"\1\0\1\1\4\2\1\3\2\0\1\2\3\0\1\2";
private static int [] zzUnpackAction() {
int [] result = new int[14];
int offset = 0;
offset = zzUnpackAction(ZZ_ACTION_PACKED_0, offset, result);
return result;
}
private static int zzUnpackAction(String packed, int offset, int [] result) {
int i = 0; /* index in packed string */
int j = offset; /* index in unpacked array */
int l = packed.length();
while (i < l) {
int count = packed.charAt(i++);
int value = packed.charAt(i++);
do result[j++] = value; while (--count > 0);
}
return j;
}
/* error codes */
private static final int ZZ_UNKNOWN_ERROR = 0;
private static final int ZZ_NO_MATCH = 1;
private static final int ZZ_PUSHBACK_2BIG = 2;
/* error messages for the codes above */
private static final String ZZ_ERROR_MSG[] = {
"Unkown internal scanner error",
"Error: could not match input",
"Error: pushback value was too large"
};
/** the input device */
private java.io.Reader zzReader;
/** the current state of the DFA */
private int zzState;
/** the current lexical state */
private int zzLexicalState = YYINITIAL;
/** this buffer contains the current text to be matched and is
the source of the yytext() string */
private char zzBuffer[] = new char[ZZ_BUFFERSIZE];
/** the textposition at the last accepting state */
private int zzMarkedPos;
/** the textposition at the last state to be included in yytext */
private int zzPushbackPos;
/** the current text position in the buffer */
private int zzCurrentPos;
/** startRead marks the beginning of the yytext() string in the buffer */
private int zzStartRead;
/** endRead marks the last character in the buffer, that has been read
from input */
private int zzEndRead;
/** number of newlines encountered up to the start of the matched text */
private int yyline;
/** the number of characters up to the start of the matched text */
private int yychar;
/**
* the number of characters from the last newline up to the start of the
* matched text
*/
private int yycolumn;
/**
* zzAtBOL == true <=> the scanner is currently at the beginning of a line
*/
private boolean zzAtBOL = true;
/** zzAtEOF == true <=> the scanner is at the EOF */
private boolean zzAtEOF;
/* user code: */
int markedPos=0;
int curLinePos=0;
int matchStart=-1;
int markedLine=0;
int rest=0;
boolean wait = false;
boolean dumpRest = false;
Writer out;
String url;
TreeMap<Integer, String[]> tags;
boolean prevHi = false;
Integer prevLn = null;
List<Hit> hits;
Hit hit;
StringBuilder sb;
boolean alt;
/**
* Set the writer that should receive all output
* @param out The new writer to write to
*/
public void setWriter(Writer out) {
yyline = 1;
this.out = out;
}
/*
* This would be faster than yytext().tolowercase()
*/
public String loweryytext() {
char[] lcs = new char[zzMarkedPos-zzStartRead];
int k = 0;
for(int i = zzStartRead; i < zzMarkedPos; i++) {
char s = zzBuffer[i];
if(s >= 'A' && s <= 'Z') {
lcs[k++] =(char)(s + 32);
} else {
lcs[k++]= s;
}
}
return new String(lcs);
}
/**
* Set the name of the file we are working on (needed if we would like to
* generate a list of hits instead of generating html)
* @param filename the name of the file
*/
public void setFilename(String filename) {
this.url = filename;
hit = new Hit(filename, null, null, false, alt);
}
/**
* Set the list we should create Hit objects for
* @param hits the hits we should add Hit objects
*/
public void setHitList(List<Hit> hits) {
this.hits = hits;
}
/**
* Set the name of the file we are working on (needed if we would like to
* generate a list of hits instead of generating html)
* @param filename the name of the file
*/
public void setAlt(boolean alt) {
this.alt = alt;
}
public void reInit(char[] buf, int len, Writer out, String url, TreeMap<Integer, String[]> tags) {
yyreset((Reader) null);
zzBuffer = buf;
zzStartRead = 0;
zzEndRead = len;
zzAtEOF = true;
wait = false;
dumpRest = false;
rest = 0;
markedPos=0;
curLinePos=0;
matchStart=-1;
markedLine=0;
yyline = 1;
this.out = out;
this.url = url;
this.tags = tags;
if(this.tags == null) {
this.tags = new TreeMap<Integer, String[]>();
}
prevHi = false;
}
public void reInit(Reader in, Writer out, String url, TreeMap<Integer, String[]> tags) {
yyreset(in);
zzStartRead = 0;
wait = false;
dumpRest = false;
rest = 0;
markedPos=0;
curLinePos=0;
matchStart=-1;
markedLine=0;
yyline = 1;
this.out = out;
this.url = url;
this.tags = tags;
if(this.tags == null) {
this.tags = new TreeMap<Integer, String[]>();
}
prevHi = false;
}
public void holdOn() {
if(!wait) {
wait = true;
matchStart = zzStartRead;
}
}
public void neverMind() {
wait = false;
if(!dumpRest) {
markedPos = curLinePos;
markedLine = yyline;
}
matchStart = -1;
}
private int printWithNum(char[] buf, int start, int end, int lineNo) throws IOException {
for(int i=start;i<end; i++) {
switch(buf[i]) {
case '\n':
++lineNo;
Integer ln = new Integer(lineNo);
boolean hi = tags.containsKey(ln);
out.write("</a>");
if(prevHi){
out.write(" <i> ");
String[] desc = tags.remove(prevLn);
out.write(desc[2]);
out.write(" </i>");
}
out.write("<br/>");
prevHi = hi;
prevLn = ln;
if(hi) out.write("<spans class=\"h\">");
out.write("<a href=\"");
out.write(url);
String num = String.valueOf(lineNo);
out.write(num);
out.write("\"><span class=\"l\">");
out.write(num);
out.write("</span> ");
break;
case '<':
out.write("&lt;");
break;
case '>':
out.write("&gt;");
break;
case '&':
out.write("&amp;");
break;
default:
out.write(buf[i]);
}
}
return lineNo;
}
private int formatWithNum(char[] buf, int start, int end, int lineNo) {
for(int i=start;i<end; i++) {
switch(buf[i]) {
case '\n':
++lineNo;
Integer ln = new Integer(lineNo);
boolean hi = tags.containsKey(ln);
if(prevHi){
String[] desc = tags.remove(prevLn);
hit.setTag(desc[2]);
}
prevHi = hi;
prevLn = ln;
sb.append(' ');
break;
case '<':
sb.append("&lt;");
break;
case '>':
sb.append("&gt;");
break;
case '&':
sb.append("&amp;");
break;
default:
sb.append(buf[i]);
}
}
return lineNo;
}
public void printContext() throws IOException {
if (sb == null) {
sb = new StringBuilder();
}
if (hit == null) {
hit = new Hit(url, null, null, false, alt);
}
wait = false;
if (matchStart == -1) {
matchStart = zzStartRead;
}
if(curLinePos == markedPos) {
Integer ln = new Integer(markedLine);
prevHi = tags.containsKey(ln);
prevLn = ln;
if (prevHi) {
prevLn = ln;
}
if (out != null) {
out.write("<a class=\"s\" href=\"");
out.write(url);
String num = String.valueOf(markedLine);
out.write(num);
out.write("\"><span class=\"l\">");
out.write(num);
out.write("</span> ");
}
}
if (out != null) {
markedLine = printWithNum(zzBuffer, markedPos, matchStart, markedLine);
out.write("<b>");
markedLine = printWithNum(zzBuffer, matchStart, zzMarkedPos, markedLine);
out.write("</b>");
} else {
markedLine = formatWithNum(zzBuffer, markedPos, matchStart, markedLine);
hit.setLineno(String.valueOf(markedLine));
sb.append("<b>");
markedLine = formatWithNum(zzBuffer, matchStart, zzMarkedPos, markedLine);
sb.append("</b>");
}
markedPos = zzMarkedPos;
matchStart = -1;
dumpRest = true;
rest = zzMarkedPos;
}
public void dumpRest() throws IOException {
if(dumpRest) {
for(int i=0; i<zzEndRead && i<100; i++) {
if(zzBuffer[rest+i] == '\n') {
if (out != null) {
printWithNum(zzBuffer, rest, rest+i-1, markedLine);
//out.write(zzBuffer, rest, i);
out.write("</a>");
if (prevHi) {
out.write(" <i> ");
String[] desc = tags.remove(prevLn);
out.write(desc[2]);
out.write(" </i>");
}
out.write("<br/>");
} else {
formatWithNum(zzBuffer, rest, rest+i-1, markedLine);
hit.setLine(sb.toString());
if (prevHi) {
String[] desc = tags.remove(prevLn);
hit.setTag(desc[2]);
}
hits.add(hit);
}
break;
}
}
}
if (tags.size() > 0) {
if (out != null) {
for(Integer rem : tags.keySet()) {
String[] desc = tags.get(rem);
out.write("<a class=\"s\" href=\"");
out.write(url);
out.write(desc[1]);
out.write("\"><span class=\"l\">");
out.write(desc[1]);
out.write("</span> ");
out.write(Util.Htmlize(desc[3]).replaceAll(desc[0], "<b>" + desc[0] + "</b>"));
out.write("</a> <i> ");
out.write(desc[2]);
out.write(" </i><br/>");
}
} else {
for(Integer rem : tags.keySet()) {
String[] desc = tags.get(rem);
hit = new Hit(url, "<html>" + Util.Htmlize(desc[3]).replaceAll(desc[0], "<b>" + desc[0] + "</b>"),
desc[1], false, alt);
hit.setTag(desc[2]);
hits.add(hit);
}
}
}
}
/**
* Creates a new scanner
* There is also a java.io.InputStream version of this constructor.
*
* @param in the java.io.Reader to read input from.
*/
public PlainLineTokenizer(java.io.Reader in) {
this.zzReader = in;
}
/**
* Creates a new scanner.
* There is also java.io.Reader version of this constructor.
*
* @param in the java.io.Inputstream to read input from.
*/
public PlainLineTokenizer(java.io.InputStream in) {
this(new java.io.InputStreamReader(in));
}
/**
* Unpacks the compressed character translation table.
*
* @param packed the packed character translation table
* @return the unpacked character translation table
*/
private static char [] zzUnpackCMap(String packed) {
char [] map = new char[0x10000];
int i = 0; /* index in packed string */
int j = 0; /* index in unpacked array */
while (i < 64) {
int count = packed.charAt(i++);
char value = packed.charAt(i++);
do map[j++] = value; while (--count > 0);
}
return map;
}
/**
* Refills the input buffer.
*
* @return <code>false</code>, iff there was new input.
*
* @exception java.io.IOException if any I/O-Error occurs
*/
private boolean zzRefill() throws java.io.IOException {
/* first: make room (if you can) */
if (zzStartRead > 0) {
System.arraycopy(zzBuffer, zzStartRead,
zzBuffer, 0,
zzEndRead-zzStartRead);
/* translate stored positions */
zzEndRead-= zzStartRead;
zzCurrentPos-= zzStartRead;
zzMarkedPos-= zzStartRead;
zzPushbackPos-= zzStartRead;
zzStartRead = 0;
}
/* is the buffer big enough? */
if (zzCurrentPos >= zzBuffer.length) {
/* if not: blow it up */
char newBuffer[] = new char[zzCurrentPos*2];
System.arraycopy(zzBuffer, 0, newBuffer, 0, zzBuffer.length);
zzBuffer = newBuffer;
}
/* finally: fill the buffer with new input */
int numRead = zzReader.read(zzBuffer, zzEndRead,
zzBuffer.length-zzEndRead);
if (numRead < 0) {
return true;
}
else {
zzEndRead+= numRead;
return false;
}
}
/**
* Closes the input stream.
*/
public final void yyclose() throws java.io.IOException {
zzAtEOF = true; /* indicate end of file */
zzEndRead = zzStartRead; /* invalidate buffer */
if (zzReader != null)
zzReader.close();
}
/**
* Resets the scanner to read from a new input stream.
* Does not close the old reader.
*
* All internal variables are reset, the old input stream
* <b>cannot</b> be reused (internal buffer is discarded and lost).
* Lexical state is set to <tt>ZZ_INITIAL</tt>.
*
* @param reader the new input stream
*/
public final void yyreset(java.io.Reader reader) {
zzReader = reader;
zzAtBOL = true;
zzAtEOF = false;
zzEndRead = zzStartRead = 0;
zzCurrentPos = zzMarkedPos = zzPushbackPos = 0;
yyline = yychar = yycolumn = 0;
zzLexicalState = YYINITIAL;
}
/**
* Returns the current lexical state.
*/
public final int yystate() {
return zzLexicalState;
}
/**
* Enters a new lexical state
*
* @param newState the new lexical state
*/
public final void yybegin(int newState) {
zzLexicalState = newState;
}
/**
* Returns the text matched by the current regular expression.
*/
public final String yytext() {
return new String( zzBuffer, zzStartRead, zzMarkedPos-zzStartRead );
}
/**
* Returns the character at position <tt>pos</tt> from the
* matched text.
*
* It is equivalent to yytext().charAt(pos), but faster
*
* @param pos the position of the character to fetch.
* A value from 0 to yylength()-1.
*
* @return the character at position pos
*/
public final char yycharat(int pos) {
return zzBuffer[zzStartRead+pos];
}
/**
* Returns the length of the matched text region.
*/
public final int yylength() {
return zzMarkedPos-zzStartRead;
}
/**
* Reports an error that occured while scanning.
*
* In a wellformed scanner (no or only correct usage of
* yypushback(int) and a match-all fallback rule) this method
* will only be called with things that "Can't Possibly Happen".
* If this method is called, something is seriously wrong
* (e.g. a JFlex bug producing a faulty scanner etc.).
*
* Usual syntax/scanner level error handling should be done
* in error fallback rules.
*
* @param errorCode the code of the errormessage to display
*/
private void zzScanError(int errorCode) {
String message;
try {
message = ZZ_ERROR_MSG[errorCode];
}
catch (ArrayIndexOutOfBoundsException e) {
message = ZZ_ERROR_MSG[ZZ_UNKNOWN_ERROR];
}
throw new Error(message);
}
/**
* Pushes the specified amount of characters back into the input stream.
*
* They will be read again by then next call of the scanning method
*
* @param number the number of characters to be read again.
* This number must not be greater than yylength()!
*/
public void yypushback(int number) {
if ( number > yylength() )
zzScanError(ZZ_PUSHBACK_2BIG);
zzMarkedPos -= number;
}
/**
* Resumes scanning until the next regular expression is matched,
* the end of input is encountered or an I/O-Error occurs.
*
* @return the next token
* @exception java.io.IOException if any I/O-Error occurs
*/
public String next() throws java.io.IOException {
int zzInput;
int zzAction;
// cached fields:
int zzCurrentPosL;
int zzMarkedPosL;
int zzEndReadL = zzEndRead;
char [] zzBufferL = zzBuffer;
char [] zzCMapL = ZZ_CMAP;
while (true) {
zzMarkedPosL = zzMarkedPos;
boolean zzR = false;
for (zzCurrentPosL = zzStartRead; zzCurrentPosL < zzMarkedPosL;
zzCurrentPosL++) {
switch (zzBufferL[zzCurrentPosL]) {
case '\u000B':
case '\u000C':
case '\u0085':
case '\u2028':
case '\u2029':
yyline++;
zzR = false;
break;
case '\r':
yyline++;
zzR = true;
break;
case '\n':
if (zzR)
zzR = false;
else {
yyline++;
}
break;
default:
zzR = false;
}
}
if (zzR) {
// peek one character ahead if it is \n (if we have counted one line too much)
boolean zzPeek;
if (zzMarkedPosL < zzEndReadL)
zzPeek = zzBufferL[zzMarkedPosL] == '\n';
else if (zzAtEOF)
zzPeek = false;
else {
boolean eof = zzRefill();
zzEndReadL = zzEndRead;
zzMarkedPosL = zzMarkedPos;
zzBufferL = zzBuffer;
if (eof)
zzPeek = false;
else
zzPeek = zzBufferL[zzMarkedPosL] == '\n';
}
if (zzPeek) yyline--;
}
zzAction = -1;
zzCurrentPosL = zzCurrentPos = zzStartRead = zzMarkedPosL;
zzState = zzLexicalState;
zzForAction: {
while (true) {
if (zzCurrentPosL < zzEndReadL)
zzInput = zzBufferL[zzCurrentPosL++];
else if (zzAtEOF) {
zzInput = YYEOF;
break zzForAction;
}
else {
// store back cached positions
zzCurrentPos = zzCurrentPosL;
zzMarkedPos = zzMarkedPosL;
boolean eof = zzRefill();
// get translated positions and possibly new buffer
zzCurrentPosL = zzCurrentPos;
zzMarkedPosL = zzMarkedPos;
zzBufferL = zzBuffer;
zzEndReadL = zzEndRead;
if (eof) {
zzInput = YYEOF;
break zzForAction;
}
else {
zzInput = zzBufferL[zzCurrentPosL++];
}
}
zzInput = zzCMapL[zzInput];
boolean zzIsFinal = false;
boolean zzNoLookAhead = false;
zzForNext: { switch (zzState) {
case 0:
switch (zzInput) {
case 1:
case 6:
case 8: zzIsFinal = true; zzState = 2; break zzForNext;
case 2: zzIsFinal = true; zzState = 3; break zzForNext;
case 3:
case 9: zzIsFinal = true; zzNoLookAhead = true; zzState = 4; break zzForNext;
case 4: zzIsFinal = true; zzState = 5; break zzForNext;
case 10: zzIsFinal = true; zzNoLookAhead = true; zzState = 6; break zzForNext;
default: zzIsFinal = true; zzNoLookAhead = true; zzState = 1; break zzForNext;
}
case 2:
switch (zzInput) {
case 1:
case 2:
case 4:
case 6:
case 8: zzIsFinal = true; break zzForNext;
default: break zzForAction;
}
case 3:
switch (zzInput) {
case 2:
case 4: zzIsFinal = true; break zzForNext;
case 3: zzState = 7; break zzForNext;
default: break zzForAction;
}
case 5:
switch (zzInput) {
case 2:
case 4: zzIsFinal = true; zzState = 3; break zzForNext;
case 3: zzState = 7; break zzForNext;
case 5: zzState = 8; break zzForNext;
default: break zzForAction;
}
case 7:
switch (zzInput) {
case 2:
case 4: zzIsFinal = true; zzState = 9; break zzForNext;
default: break zzForAction;
}
case 8:
switch (zzInput) {
case 6: zzState = 10; break zzForNext;
default: break zzForAction;
}
case 9:
switch (zzInput) {
case 2:
case 4: zzIsFinal = true; break zzForNext;
default: break zzForAction;
}
case 10:
switch (zzInput) {
case 6: zzState = 11; break zzForNext;
default: break zzForAction;
}
case 11:
switch (zzInput) {
case 7: zzState = 12; break zzForNext;
default: break zzForAction;
}
case 12:
switch (zzInput) {
case 2:
case 4:
case 8: zzIsFinal = true; zzState = 13; break zzForNext;
default: break zzForAction;
}
case 13:
switch (zzInput) {
case 2:
case 4:
case 8: zzIsFinal = true; break zzForNext;
default: break zzForAction;
}
default:
// if this is ever reached, there is a serious bug in JFlex
zzScanError(ZZ_UNKNOWN_ERROR);
break;
} }
if ( zzIsFinal ) {
zzAction = zzState;
zzMarkedPosL = zzCurrentPosL;
if ( zzNoLookAhead ) break zzForAction;
}
}
}
// store back cached position
zzMarkedPos = zzMarkedPosL;
switch (zzAction < 0 ? zzAction : ZZ_ACTION[zzAction]) {
case 2:
{ return loweryytext();
}
case 4: break;
case 3:
{ if(!wait) {
markedPos = zzMarkedPos;
markedLine = yyline+1;
matchStart = -1;
curLinePos=zzMarkedPos;
}
if(dumpRest) {
if (out != null) {
printWithNum(zzBuffer, rest, zzMarkedPos-1, markedLine);
out.write("</a>");
if(prevHi){
out.write(" <i> ");
String[] desc = tags.remove(prevLn);
out.write(desc[2]);
out.write("</i> ");
}
out.write("<br/>");
} else {
formatWithNum(zzBuffer, rest, zzMarkedPos-1, markedLine);
hit.setLine(sb.toString());
if(prevHi){
String[] desc = tags.remove(prevLn);
hit.setTag(desc[2]);
}
hits.add(hit);
sb.setLength(0);
hit = new Hit(url, null, null, false, alt);
}
dumpRest = false;
}
}
case 5: break;
case 1:
{
}
case 6: break;
default:
if (zzInput == YYEOF && zzStartRead == zzCurrentPos) {
zzAtEOF = true;
{
return null;
}
}
else {
zzScanError(ZZ_NO_MATCH);
}
}
}
}
}