Class ParseSession

  • Direct Known Subclasses:
    CliSession

    public class ParseSession
    extends Session
    A Session with support for parsing Java expressions.
    • Field Detail

      • PARSE_FUNCTIONS_DESCRIPTOR_RESOURCE

        public static final String PARSE_FUNCTIONS_DESCRIPTOR_RESOURCE
        Classpath XML file resource describing available Functions: "META-INF/permazen/parse-function-implementations.xml".

        Example:

          <parse-function-implementations>
              <parse-function-implementation class="com.example.MyFunction"/>
          </parse-function-implementations>
         

        Instances must have a public constructor taking either zero parameters or one ParseSession parameter.

        See Also:
        loadFunctionsFromClasspath(), Constant Field Values
    • Method Detail

      • getImports

        public Set<String> getImports()
        Get currently configured Java imports.

        Each entry should of the form foo.bar.Name or foo.bar.*.

        Returns:
        configured imports
      • getFunctions

        public SortedMap<String,​Function> getFunctions()
        Get the Functions registered with this instance.
        Returns:
        registered functions indexed by name
      • getVars

        public SortedMap<String,​Value> getVars()
        Get all variables set on this instance.
        Returns:
        variables indexed by name
      • registerFunction

        public void registerFunction​(Function function)
        Register the given Function.

        Any existing Function with the same name will be replaced.

        Parameters:
        function - new function
        Throws:
        IllegalArgumentException - if function is null
      • getIdentifierParser

        public Parser<? extends Node> getIdentifierParser()
        Get the current standalone identifier parser.
        Returns:
        current identifier parser, if any, otherwise null
      • setIdentifierParser

        public void setIdentifierParser​(Parser<? extends Node> identifierParser)
        Set the standalone identifier parser.

        The configured identifier parser, if any, is invoked on standalone identifiers. This allows for configurable behavior with respect to resolving such identifiers.

        Typically when installing a new parser, the previous parser (if any) is set as its delegate.

        Parameters:
        identifierParser - parser for standalone identifiers, or null for none
      • resolveClass

        public Class<?> resolveClass​(String name,
                                     boolean allowPrimitive)
        Resolve a class name against this instance's currently configured class imports.
        Parameters:
        name - class name as it would appear in the Java language
        allowPrimitive - whether to allow primitive types like int
        Returns:
        resolved class
        Throws:
        IllegalArgumentException - if name cannot be resolved
      • relativizeClassName

        public String relativizeClassName​(Class<?> klass)
        Relativize the given class's name, so that it is as short as possible given the configured imports. For example, for class String this will return String, but for class ArrayList this will return java.util.ArrayList unless java.util.* has been imported.
        Parameters:
        klass - class whose name to relativize
        Returns:
        relativized class name
        Throws:
        IllegalArgumentException - if klass is null
      • performParseSessionAction

        public boolean performParseSessionAction​(ParseSession.Action action)
        Perform the given action in the context of this session.

        This is a ParseSession-specific overload of Session.performSessionAction(); see that method for details.

        Parameters:
        action - action to perform, possibly within a transaction
        Returns:
        true if action completed successfully, false if a transaction could not be created or action threw an exception
        Throws:
        IllegalArgumentException - if action is null