Class CodeColorizer

java.lang.Object
org.apache.sis.internal.book.CodeColorizer

public final class CodeColorizer extends Object
Apply syntax colorization on Java code. This class is different than most other colorization tools since its apply different colors depending on whether a word is known to be defined in an OGC/ISO standard, in GeoAPI or in Apache SIS.
Since:
0.7
Version:
1.3
  • Field Details

    • JAVA_KEYWORDS

      public static final Set<String> JAVA_KEYWORDS
      Lists of Java keywords.
    • identifierSpecifiers

      private final Map<String,CodeColorizer.Specifier> identifierSpecifiers
      Map of predefined identifiers and the authority who defined them.
    • document

      private final Document document
      The object to use for creating nodes.
  • Constructor Details

    • CodeColorizer

      public CodeColorizer(Document document) throws IOException, BookException
      Creates a new color colorizer.
      Parameters:
      document - the object to use for creating nodes.
      Throws:
      IOException - if an error occurred while reading the list of predefined identifiers.
      BookException - if an identifier is defined twice.
  • Method Details

    • toArray

      static Node[] toArray(NodeList nodes)
      Returns all nodes in the given list as an array. This method is used for getting a snapshot of the list before to modify it (for example before the elements are moved to another node).
    • getSpecifier

      private CodeColorizer.Specifier getSpecifier(String word)
      Returns the re-defined identifiers and authority who defined it for the given word.
      Parameters:
      word - the word for which to get a specifier.
      Returns:
      the specifier for the given word, or null if none.
    • startsWithWord

      private static boolean startsWithWord(String string, String prefix)
      Returns true if the given string starts with the given prefix, and the character following the prefix is not an identifier character.
    • isJavaIdentifier

      private static boolean isJavaIdentifier(String identifier, int i, int upper)
      Returns true if the given string from i inclusive to upper exclusive is a Java identifier. Ignore zero-width space and soft hyphen.
    • styleForSingleIdentifier

      final String styleForSingleIdentifier(String word)
      Returns the value to put inside in class attribute of a <code> element encompassing the given identifier.

      This method differs from highlight(Node, String) in that it is used for applying a single style on the whole string. By contrast, highlight(…) parses the text and may apply different styles for different words.

    • highlight

      public void highlight(Node parent, String type) throws BookException
      Applies emphasing on the words found in all text node of the given node.
      Parameters:
      parent - the root element where to put Java keywords in bold characters. This is typically a <samp> or <code> element.
      type - "xml" if the element to process is XML rather than Java code.
      Throws:
      BookException - if an element cannot be processed.