com.google.streamhtmlparser.util
Class JavascriptTokenBuffer

java.lang.Object
  extended by com.google.streamhtmlparser.util.JavascriptTokenBuffer

public class JavascriptTokenBuffer
extends Object

Implements a circular (ring) buffer of characters with specialized application logic in order to determine the context of some Javascript content that is being parsed. This is a specialized class - of no use to external code - which aims to be 100% compatible with the corresponding logic in the C-version of the HtmlParser, specifically jsparser.c. In particular:


Constructor Summary
JavascriptTokenBuffer()
          Constructs an empty javascript token buffer.
JavascriptTokenBuffer(JavascriptTokenBuffer aJavascriptTokenBuffer)
          Constructs a javascript token buffer that is identical to the one given.
 
Method Summary
 void appendChar(char input)
          Appends a character to the buffer.
 void appendString(String input)
          A simple wrapper over appendChar, it appends a string to the buffer.
 char getChar(int position)
          Returns the character at a given index in the buffer or nul ('\0') if the index is outside the range of the buffer.
 String getLastIdentifier()
          Returns the last javascript identifier/keyword in the buffer.
 char popChar()
          Returns the last character in the buffer and removes it from the buffer or the NUL character '\0' if the buffer is empty.
 boolean setChar(int position, char input)
          Sets the given input at the given position of the buffer.
 String slice(int start, int end)
          Returns a slice of the buffer delimited by the given indices.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JavascriptTokenBuffer

public JavascriptTokenBuffer()
Constructs an empty javascript token buffer. The size is fixed, see BUFFER_SIZE.


JavascriptTokenBuffer

public JavascriptTokenBuffer(JavascriptTokenBuffer aJavascriptTokenBuffer)
Constructs a javascript token buffer that is identical to the one given. In particular, it has the same size and contents.

Parameters:
aJavascriptTokenBuffer - the JavascriptTokenBuffer to copy
Method Detail

appendString

public void appendString(String input)
A simple wrapper over appendChar, it appends a string to the buffer. Sequences of whitespace and newlines are folded into one character to save space. Null strings are not allowed.

Parameters:
input - the String to append, cannot be null

appendChar

public void appendChar(char input)
Appends a character to the buffer. We fold sequences of whitespace and newlines into one to save space.

Parameters:
input - the char to append

popChar

public char popChar()
Returns the last character in the buffer and removes it from the buffer or the NUL character '\0' if the buffer is empty.

Returns:
last character in the buffer or '\0' if the buffer is empty

getChar

public char getChar(int position)
Returns the character at a given index in the buffer or nul ('\0') if the index is outside the range of the buffer. Such could happen if the buffer is not filled enough or the index is larger than the size of the buffer.

Position must be negative where -1 is the index of the last character in the buffer.

Parameters:
position - The index into the buffer
Returns:
character at the requested index

setChar

public boolean setChar(int position,
                       char input)
Sets the given input at the given position of the buffer. Returns true if we succeeded or false if we failed (i.e. the write was beyond the buffer boundary).

Index positions are negative where -1 is the index of the last character in the buffer.

Parameters:
position - The index at which to set the character
input - The character to set in the buffer
Returns:
true if we succeeded, false otherwise

getLastIdentifier

public String getLastIdentifier()
Returns the last javascript identifier/keyword in the buffer.

Returns:
the last identifier or null if none was found

slice

public String slice(int start,
                    int end)
Returns a slice of the buffer delimited by the given indices. The start and end indexes represent the start and end of the slice to copy. If the start argument extends beyond the beginning of the buffer, the slice will only contain characters starting from the beginning of the buffer.

Parameters:
start - The index of the first character the copy
end - the index of the last character to copy
Returns:
String between the given indices


Copyright © 2010-2012 Google. All Rights Reserved.