java.lang.Object
org.eclipse.lemminx.extensions.relaxng.grammar.rnc.RNCNode
org.eclipse.lemminx.extensions.relaxng.grammar.rnc.RNCDocument
All Implemented Interfaces:
RNCRange

public class RNCDocument extends RNCNode
Represents a parsed RNC (RELAX NG Compact Syntax) document.

This is the root node of the RNC abstract syntax tree, holding the full source text, the document URI, and all top-level constructs (namespace declarations, datatypes declarations, start pattern, defines, includes, divs, elements, and attributes) as child nodes. It also collects parser errors.

Use case: produced by RNCParser.parse(CharSequence, String) and used by all language features (completion, hover, definition, etc.) to access the document structure.

Example: parsing the RNC source:

 namespace a = "http://example.com"
 start = root
 root = element root {
   attribute id { text },
   name+
 }
 name = element name { text }
 

produces an RNCDocument with children: RNCNamespaceDecl, RNCStart, RNCDefine("root"), RNCDefine("name").

  • Constructor Details

    • RNCDocument

      public RNCDocument(CharSequence source, String uri)
      Creates a new RNC document.
      Parameters:
      source - the full source text.
      uri - the document URI.
  • Method Details

    • getNodeType

      public RNCNodeType getNodeType()
      Specified by:
      getNodeType in class RNCNode
    • getTextDocument

      public TextDocument getTextDocument()
    • setTextDocument

      public void setTextDocument(TextDocument textDocument)
    • addError

      public void addError(RNCErrorCode errorCode, String message, int start, int end)
      Records a parser error in this document.
      Parameters:
      errorCode - the error code identifying the type of error.
      message - the human-readable error message.
      start - the inclusive start offset of the error range.
      end - the exclusive end offset of the error range.
    • getErrors

      public List<RNCDocument.RNCError> getErrors()
      Returns the list of parser errors collected during parsing.
      Returns:
      the errors (may be empty, never null).
    • getUri

      public String getUri()
      Returns the document URI.
      Returns:
      the URI string (e.g. "file:///path/to/schema.rnc").
    • getSource

      public CharSequence getSource()
      Returns the full source text of the document.
      Returns:
      the source text.
    • getText

      public String getText(int start, int end)
      Extracts a substring from the source text.
      Parameters:
      start - the inclusive start offset.
      end - the exclusive end offset.
      Returns:
      the extracted text.
    • getNamespaceDecls

      public List<RNCNamespaceDecl> getNamespaceDecls()
      Returns all top-level namespace declarations in this document.

      Example RNC: namespace a = "http://example.com"

      Returns:
      the namespace declarations.
    • getDatatypesDecls

      public List<RNCDatatypesDecl> getDatatypesDecls()
      Returns all top-level datatypes declarations in this document.

      Example RNC: datatypes xsd = "http://www.w3.org/2001/XMLSchema-datatypes"

      Returns:
      the datatypes declarations.
    • getDefines

      public List<RNCDefine> getDefines()
      Returns all define declarations in this document, including those nested inside div or include blocks.

      Use case: used by completion (to suggest define references), code lens (to count references), highlighting, and definition (to find declarations).

      Example RNC: root = element root \{ text \} declares a define named "root".

      Returns:
      all defines in the document (searches recursively through children).
    • getStartDecl

      public RNCStart getStartDecl()
      Returns the start declaration, or null if none exists.

      Example RNC: start = root

      Returns:
      the RNCStart node, or null.
    • hasTopLevelPattern

      public boolean hasTopLevelPattern()
      Returns whether this document has a top-level pattern (e.g., a standalone element root { text } without a start = declaration).

      In RNC, a schema can be either a grammar form (start = ...) or a pattern form (a bare pattern at the top level).

      Returns:
      true if a top-level element or attribute pattern exists.
    • hasDefaultNamespace

      public boolean hasDefaultNamespace()
      Returns whether a default namespace declaration exists.
      Returns:
      true if a default namespace is declared.
    • getIncludes

      public List<RNCInclude> getIncludes()
      Returns all top-level include declarations in this document.

      Example RNC: include "other.rnc"

      Returns:
      the include declarations.
    • getExternals

      public List<RNCExternal> getExternals()
      Returns all external references in this document, including nested ones.

      Example RNC: start = external "other.rnc"

    • getFileReferences

      public List<RNCFileReferenceNode> getFileReferences()
      Returns all file reference nodes (includes and externals) in this document.
    • isOnElementOrAttributeName

      public boolean isOnElementOrAttributeName(int offset)
      Returns true if the given offset falls within the name range of any element or attribute pattern node in the document.

      Use case: rename must refuse to operate when the cursor is on an element or attribute name (e.g. element tune \{ \}), because those names are XML node names, not define references, and renaming them would be semantically wrong.

      Parameters:
      offset - the character offset to test.
      Returns:
      true if the offset is inside an element or attribute name.
    • getAllVisibleDefines

      public List<RNCDefine> getAllVisibleDefines(RNCInclude excludeInclude, IRNCDocumentProvider documentProvider)
      Returns all defines visible from this document: local defines plus defines from all included files. Duplicates (by name) are excluded — local defines take precedence.
      Parameters:
      excludeInclude - optional include to skip (e.g. the enclosing include when completing inside an include body), or null.
      documentProvider - provides access to included documents (checks open documents first, falls back to disk).
      Returns:
      the list of all visible defines.
    • findDefine

      public RNCDefine findDefine(String name)
      Finds a define by name.

      Use case: used by hover (to show the define text), definition (to navigate to the declaration), and highlighting (to locate the Write highlight).

      Parameters:
      name - the define name to search for (e.g. "root").
      Returns:
      the matching RNCDefine, or null if not found.
    • hasNoContent

      public boolean hasNoContent()
      Returns true if this document has no meaningful content — only whitespace and comments, no declarations or patterns.

      Use case: when the document has no content, RNC completion offers new-file snippets (full schema templates) instead of incremental declarations.

      Returns:
      true if the document has no content (no declarations, no non-whitespace/non-comment text).