Class IndexScanner

java.lang.Object
org.eclipse.lemminx.extensions.index.scanner.IndexScanner
All Implemented Interfaces:
AutoCloseable

public final class IndexScanner extends Object implements AutoCloseable
Lightweight streaming XML scanner for workspace Bloom-index collection.

Design goals

  • Reader-based — operates on a Reader; never loads the whole file into memory. Peak heap per file = the BufferedReader internal buffer (~8 KB) plus the small strings captured per token.
  • Forward-only — no backtracking beyond a single character; a 16-char PushbackReader buffer is enough for all look-ahead.
  • No DOM — produces only the IndexTokenTypes needed to build a BloomFilter and locate GrammarReferences. All other XML constructs are skipped without allocation.
  • Inline position tracking — line and col are updated on every read() call; no positionAt(offset) look-up against a full-document line index is needed.

Usage

 try (IndexScanner scanner = new IndexScanner(Files.newBufferedReader(path))) {
     IndexTokenType token;
     while ((token = scanner.next()) != IndexTokenType.EOS) {
         switch (token) {
             case START_TAG      -> System.out.println("element: " + scanner.getValue());
             case ATTRIBUTE_NAME -> System.out.println("attr: "    + scanner.getValue());
             case ATTRIBUTE_VALUE-> System.out.println("value: "   + scanner.getValue());
             // ...
         }
     }
 }
 

Position: getLine() and getStartChar() / getEndChar() are 0-indexed, matching LSP conventions. They are captured at the start of each token's value content (quotes stripped for attribute values and DOCTYPE identifiers).

Thread safety: not thread-safe; use one instance per thread.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a scanner that reads from reader with no character limit.
    IndexScanner(Reader reader, int maxChars)
    Creates a scanner that reads from reader and stops after maxChars characters.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    int
    0-indexed exclusive end column of the token value.
    int
    0-indexed line number of the token start.
    int
    Absolute character offset of the token start in the source stream.
    int
    0-indexed start column of the token value (LSP character).
    Returns the string value of the last token (quotes stripped for values).
    boolean
    Returns true if scanning stopped because the maxChars limit was reached before the end of the stream.
    Advances to the next significant token and returns its type.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • IndexScanner

      public IndexScanner(Reader reader)
      Creates a scanner that reads from reader with no character limit. The reader is wrapped in a BufferedReader (8 KB) unless it already is one.
      Parameters:
      reader - source of XML content; the scanner takes ownership and closes it.
    • IndexScanner

      public IndexScanner(Reader reader, int maxChars)
      Creates a scanner that reads from reader and stops after maxChars characters. Use isPartial() after scanning to check whether the limit was hit.
      Parameters:
      reader - source of XML content; the scanner takes ownership and closes it.
      maxChars - maximum characters to consume; Integer.MAX_VALUE = no limit.
  • Method Details

    • next

      public IndexTokenType next() throws IOException
      Advances to the next significant token and returns its type.
      Returns:
      the next token, or IndexTokenType.EOS at end of stream.
      Throws:
      IOException - if the underlying reader fails.
    • getValue

      public String getValue()
      Returns the string value of the last token (quotes stripped for values).
    • getOffset

      public int getOffset()
      Absolute character offset of the token start in the source stream.
    • getLine

      public int getLine()
      0-indexed line number of the token start.
    • getStartChar

      public int getStartChar()
      0-indexed start column of the token value (LSP character).
    • getEndChar

      public int getEndChar()
      0-indexed exclusive end column of the token value.
    • isPartial

      public boolean isPartial()
      Returns true if scanning stopped because the maxChars limit was reached before the end of the stream. When true, callers should treat the indexed file as partially scanned and conservatively include it as a candidate in all lookups.
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException