|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.saxonica.codegen.CompilerService
public class CompilerService
The class contains the generic logic for generating Java code to evaluate an XQuery expression.
The class contains methods to generate the boilerplate code of the Java module, and many service methods used as callbacks by the specific compiler classes for each kind of expression. It also handles code layout, detecting curly braces in the generated code and using them to trigger indentation changes.
Field Summary | |
---|---|
static int |
RETURN
|
Constructor Summary | |
---|---|
CompilerService()
|
Method Summary | |
---|---|
void |
assign(String variable,
String expression)
Generate an assignment to a variable |
String |
cast(String variable,
Class target)
Generate a Java cast unless it is known to be unnecessary. |
String |
collationVariable(String uri)
Given a URI representing a collation, register the collation and return the name of a Java variable that can be used to reference the collation in the generated Java code |
void |
compileAsLoop(Expression exp,
LoopAction action)
Compile Java code whose effect is to loop over the results of an expression, calling the supplied callback function to generate the code that processes each item in the sequence. |
String |
compileAtomicComparer(AtomicComparer comp)
Generate code to declare an AtomicComparer for a collation known statically |
void |
compileFunction(XQueryFunction fn)
Compile an XQuery function |
void |
compileGlobalVariable(GlobalVariable decl)
Compile code to declare and initialize a global variable, that is, a variable declared in the query prolog |
void |
compileImportedSchemata(Map<String,HashSet<String>> schemata)
Compile a method to import all the schema modules that were imported statically into the query |
String |
compileItemType(ItemType type)
Compile the declaration of an XPath item typs |
void |
compileKeyDefinitionClasses()
Compile class definitions corresponding to all the requested key definitions |
void |
compileKeyDefinitionSetters()
Compile initialization code to establish the key definitions |
void |
compileMainExpression(Expression exp)
Compile the main expression in the query module |
String |
compileNamespaceContext(NamespaceResolver resolver)
Compile a namespace context |
String |
compileNodeTest(NodeTest test)
Compile code for a node test used in a path expression |
void |
compilePush(Expression exp)
Compile Java code whose effect is to send the result of the given expression to the current output Receiver |
String |
compileRegularExpression(String translated,
int flagBits)
Generate an instance variable representing a compiled regular expression, and return its name |
String |
compileSchemaType(SchemaType stype)
Compile a schema type |
String |
compileSequenceType(SequenceType type)
Compile the declaration of an XPath sequence typs |
String |
compileStringCollator(CollatingFunction exp,
int collationArg)
Compile code to create/get a StringCollator for a CollatingFunction |
String |
compileStructuredQName(StructuredQName qName)
Compile a StructuredQName |
String |
compileToCharSequence(Expression exp)
Compile Java code whose effect is to compute the string value of the expression; the generated code returns a CharSequence |
String |
compileToEffectiveBooleanValue(Expression exp,
ReturnAction action)
Compile Java code whose effect is to compute the effective boolean value of the expression; the generated code returns a boolean |
String |
compileToItem(Expression exp)
Compile Java code whose effect is to evaluate the expression, which must have a cardinality of zero-or-one; the generated code returns either an Item or null (representing an empty sequence) |
String |
compileToIterator(Expression exp)
Compile Java code whose effect is to create an iterator over the result of the given expression |
String |
compileToValueRepresentation(Expression exp)
Compile an expression to place its results in an object of class ValueRepresentation |
void |
declare(Class declaredType,
String name,
String initializer,
boolean isFinal)
Declare a variable locally within a method. |
String |
declareNameCode(int nc)
Declare a Java variable holding an integer namecode representing a QName in the run-time name pool |
String |
declareNamespaceCode(int nsc)
Declare a Java variable holding an integer namespace code representing a prefix=uri binding in the run-time name pool |
void |
emit(String s)
Output a line of Java code. |
void |
emitComment(String s)
Output a Java comment |
void |
emitConditional(String condition,
String thenStatement)
Output a simple conditional statement |
void |
emitDynamicError(String message,
String code)
Output Java code to signal a dynamic error. |
void |
emitDynamicError(String message,
StructuredQName code)
Output Java code to signal a dynamic error. |
String |
emitInstanceVariable(Class type,
String name,
String initializer,
int priority,
boolean isStatic,
boolean isFinal,
String messageIfNull,
String errorCodeIfNull)
Generate a declaration of a non-local variable. |
void |
endMethod()
Register the end of a "method" |
void |
epilogue()
Output the epilogue, that is, the material at the end of the Java code module |
String |
generateLocalVariableDeclaration(Class declaredType,
String name,
String initializer,
boolean isFinal)
Generate a local variable declaration |
String |
getClassName(Class target)
Get the name of a Java class for use in the generated code. |
Configuration |
getConfiguration()
Get the Saxon configuration |
String |
getContextItemVariableName()
Get the name of the Java variable currently bound to the context item |
String |
getContextPositionVariableName()
Get the name of the Java variable currently bound to the context position |
String |
getContextSizeVariableName()
Get the name of the Java variable currently bound to the context size (last()) |
String |
getContextVariableName()
Get the name of the Java variable currently bound to the dynamic XPathContext object |
int |
getFunctionEvaluationMode(UserFunction function)
Determine what mode a function should be evaluated in: push, evaluateItem, or ValueRepresentation |
Class |
getJavaClassForFunctionParameter(UserFunction fn,
int i)
Get the Java class to use for a given function argument |
String |
getMethodName(UserFunction fn)
Get the Java method name to use for a given user-defined function |
NamePool |
getNamePool()
Get the name pool used by the Saxon configuration |
String |
getOutputterVariableName()
Get the name of the Java variable currently bound to current output destination |
TypeHierarchy |
getTypeHierarchy()
Get the TypeHierarchy which holds a cache of type information relevant to the query being compiled |
int |
getUniqueNumber()
Allocate a unique number within the module, for use in variable names |
ValueCompiler |
getValueCompiler(Value val)
Get a ValueCompiler for a particular kind of Value (usually an AtomicValue) appearing as a literal in a query |
String |
getXPathVariableName(int slotNumber)
Get the name of the Java variable used to represent the XPath variable occupying a given slot |
void |
indent()
Add indentation to the output Java code. |
boolean |
isConvertUntypedAtomicParameters()
|
boolean |
isInServletMode()
Test whether compilation is in servlet mode |
boolean |
isInstance(String variable,
Class target)
Test whether a given variable is known to be an instance of a particular Java class |
boolean |
isXSLT()
Determine whether we are compiling XSLT rather than XQuery |
static String |
javaEscape(CharSequence in)
Generate a Java string literal with appropriate escaping of special characters |
ExpressionCompiler |
makeExpressionCompiler(Expression exp)
Get a compiler for a particular kind of expression. |
void |
makeKeyDefinitionFunction(StructuredQName keyName)
Compile a function that implements a given key definition |
String |
makeValidJavaName(String name)
Given a candidate Java name, make it into a valid Java name |
void |
outdent()
Reduce the indentation level of the output Java code. |
void |
popContextItemVariable()
Deregister the variable name currently used for the context item, reverting to the previously used variable name |
void |
popContextPositionVariable()
Deregister the variable name currently used for the context position, reverting to the previously used variable name |
void |
popContextSizeVariable()
Deregister the variable name currently used for the context size, reverting to the previously used variable name |
void |
popContextVariable()
Deregister the variable name currently used for the dynamic XPathContext object, reverting to the previously used variable name |
void |
popOutputterVariable()
Deregister the variable name currently used for the current output destination, reverting to the previously used variable name |
void |
prologue(boolean mainModule)
Output the prologue, that is, the material at the start of the Java code module |
void |
pushContextItemVariable(String variableName)
Register the variable name currently used for the context item, adding it to a stack |
void |
pushContextPositionVariable(String variableName)
Register the variable name currently used for the context position, adding it to a stack |
void |
pushContextSizeVariable(String variableName)
Register the variable name currently used for the context size, adding it to a stack |
void |
pushContextVariable(String variableName)
Register the variable name currently used for the dynamic XPathContext object, adding it to a stack |
void |
pushOutputterVariable(String variableName)
Register the variable name currently used for the current output destination, adding it to a stack |
void |
registerLocalVariable(Class declaredType,
String name)
Register a local variable that has been declared (e.g. |
void |
setBaseURI(String baseURI)
Set the base URI of the query to be compiled |
void |
setClassName(String packageName,
String className)
Set the package name and class name of the generated Java code |
void |
setConfiguration(Configuration config)
Set the Saxon configuration used by this query compiler |
void |
setConvertUntypedAtomicParameters(boolean un)
|
void |
setExecutable(Executable exec)
Set the Saxon executable containing the query to be compiled |
void |
setPrintWriter(PrintWriter writer)
Set the destination for the generated Java code |
void |
setServletMode(boolean onOrOff)
Set servlet mode for compilation |
void |
setSuperClassName(String className)
Set the name of the Java class to be used as the superclass the compiled query. |
void |
setXPathVariableName(int slotNumber,
String name)
Register the name of the Java variable used to represent the XPath variable occupying a given slot |
void |
startMethod()
Register the start of a new "method" (also covers similar top-level constructs such as static initializers) |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int RETURN
Constructor Detail |
---|
public CompilerService()
Method Detail |
---|
public void setServletMode(boolean onOrOff)
onOrOff
- true to set servlet mode on, false to set it offpublic void setConvertUntypedAtomicParameters(boolean un)
public boolean isConvertUntypedAtomicParameters()
public boolean isInServletMode()
public void setSuperClassName(String className)
CompiledUserQuery
or
CompiledQueryServlet
, or potentially any other class that contains
the methods called by the generated Java code.
className
- the fully qualified name of the superclasspublic ExpressionCompiler makeExpressionCompiler(Expression exp)
CompilerClassMapper
exp
- the expression to compile
public ValueCompiler getValueCompiler(Value val)
val
- the value to be compiled
public void setConfiguration(Configuration config)
config
- the Saxon configurationpublic void setExecutable(Executable exec)
exec
- the Saxon Executablepublic Configuration getConfiguration()
public NamePool getNamePool()
public TypeHierarchy getTypeHierarchy()
public boolean isXSLT()
public void setPrintWriter(PrintWriter writer)
writer
- a PrintWriter with which the generated code should be writtenpublic void setClassName(String packageName, String className)
packageName
- the hierarchic package nameclassName
- the class name, excluding any package namepublic void setBaseURI(String baseURI)
baseURI
- the static base URI of the querypublic int getUniqueNumber()
public void startMethod()
public void endMethod()
public void declare(Class declaredType, String name, String initializer, boolean isFinal)
declaredType
- the Java class of the variable to be declaredname
- the name of the variable to be declaredinitializer
- Java expression to provide an initial valueisFinal
- true if the variable should be declared as finalpublic void emitConditional(String condition, String thenStatement)
condition
- the conditionthenStatement
- the statement on the "then" branchpublic void setXPathVariableName(int slotNumber, String name)
slotNumber
- the slot allocated to the XPath variable on the XPath execution stackname
- the name of the Java variable used to represent this XPath variablepublic String getXPathVariableName(int slotNumber)
slotNumber
- the slot allocated to the variable on the XPath execution stack
public void assign(String variable, String expression)
variable
- the name of the variableexpression
- the Java expression to appear on the rhs of the assignmentpublic String generateLocalVariableDeclaration(Class declaredType, String name, String initializer, boolean isFinal)
declaredType
- the Java class of the variablename
- the proposed Java name for the variableinitializer
- the expression used to initialize the variableisFinal
- true if the variable is to be declared as "final".
public void registerLocalVariable(Class declaredType, String name)
declaredType
- the Java class of the variablename
- the Java name of the variablepublic String cast(String variable, Class target)
cast
in interface CodeGeneratorService
variable
- the name of the variable that possibly needs to be casttarget
- the required type for the expression where the variable is being used
public boolean isInstance(String variable, Class target)
variable
- the name of the Java variabletarget
- the Java class
public void indent()
public void outdent()
public void pushContextVariable(String variableName)
variableName
- the name of the variable currently bound to the XPathContextpublic void popContextVariable()
public void pushContextItemVariable(String variableName)
variableName
- the name of the variable currently bound to the XPathContext. By
convention, if the variable name begins with "node" then we can rely on the fact that
the context item exists and is a node (though it may need to be cast)public void popContextItemVariable()
public void pushContextPositionVariable(String variableName)
variableName
- the name of the variable currently bound to the context position.
It is normally a boxed value (IntegerValue) unless the name ends with ".position()", in which
case it is unboxed.public void popContextPositionVariable()
public void pushContextSizeVariable(String variableName)
variableName
- the name of the variable currently bound to the XPathContextpublic void popContextSizeVariable()
public String getContextVariableName()
getContextVariableName
in interface CodeGeneratorService
public String getContextItemVariableName()
public String getContextPositionVariableName()
public String getContextSizeVariableName()
public void pushOutputterVariable(String variableName)
variableName
- the name of the variable currently bound to the XPathContextpublic void popOutputterVariable()
public String getOutputterVariableName()
public void emit(String s)
s
- the line of Java codepublic void emitComment(String s)
s
- the text of the commentpublic void emitDynamicError(String message, String code)
message
- the error messagecode
- the error codepublic void emitDynamicError(String message, StructuredQName code)
message
- the error messagecode
- the error codepublic String declareNameCode(int nc)
nc
- the compile time name code of the QName. This will not necessarily be the same as the run-time
name code.
public String declareNamespaceCode(int nsc)
nsc
- the compile time namespace code. This will not necessarily be the same as the run-time
namespace code.
public String makeValidJavaName(String name)
name
- a candidate Java name
public String emitInstanceVariable(Class type, String name, String initializer, int priority, boolean isStatic, boolean isFinal, String messageIfNull, String errorCodeIfNull)
type
- The Java class of the variablename
- The name to be given to the variable. If null is supplied, a name will be allocated
and returned as the result of the functioninitializer
- The initial value of the expressionpriority
- Integer from one to five indicating the order of outputisStatic
- True if the declaration is to be static.isFinal
- True if the declaration is to be final. This implies that the initializer can be
output directly, it doesn't require a separate assignment during the initialization procedure.messageIfNull
- Error message to be produced at run-time if the value is null (if any)errorCodeIfNull
- Error code to be produced at run-time if the value is null (if any)
public String collationVariable(String uri)
uri
- a URI representing a collation
public void prologue(boolean mainModule)
mainModule
- true if this is the main modulepublic void compileImportedSchemata(Map<String,HashSet<String>> schemata)
schemata
- a Map whose key is a target namespace and whose value is a set of location hints
for schema documents relating to that target namespacepublic void compileGlobalVariable(GlobalVariable decl)
decl
- the global variable declaration to be compiledpublic String getClassName(Class target)
target
- the class whose name is required
public void compileFunction(XQueryFunction fn)
fn
- the function to be compiledpublic Class getJavaClassForFunctionParameter(UserFunction fn, int i)
fn
- the user-defined functioni
- index of the relevant argument
public String getMethodName(UserFunction fn)
fn
- the user-defined function
public int getFunctionEvaluationMode(UserFunction function)
function
- the user-defined function
public void compileMainExpression(Expression exp)
exp
- the main expressionpublic void epilogue()
public void compilePush(Expression exp)
exp
- the expression to be compiledpublic String compileToIterator(Expression exp)
exp
- the expression to be compiled
public void compileAsLoop(Expression exp, LoopAction action)
exp
- the expression being compiledaction
- callback action to generate code for the body of the looppublic String compileToItem(Expression exp)
exp
- The expression to be compiled, which must have a static cardinality
of zero or one.
public String compileToEffectiveBooleanValue(Expression exp, ReturnAction action)
exp
- The expression to be compiledaction
- Represents the way in which the result of the boolean expression is to be
returned
public String compileToCharSequence(Expression exp)
exp
- The expression to be compiled
public String compileToValueRepresentation(Expression exp)
exp
- the expression to be compiled
public String compileItemType(ItemType type)
type
- the item type
public String compileSequenceType(SequenceType type)
type
- the item type
public String compileSchemaType(SchemaType stype)
public String compileNodeTest(NodeTest test)
test
- the node test
public String compileRegularExpression(String translated, int flagBits)
translated
- the regular expression, translated to Java regex syntaxflagBits
- the flag bits as used by the Java regex engine
public String compileStringCollator(CollatingFunction exp, int collationArg)
exp
- the CollatingFunction in question
public String compileAtomicComparer(AtomicComparer comp)
comp
- the AtomicComparer to be reproduced in the compiled code
public void makeKeyDefinitionFunction(StructuredQName keyName)
keyName
- the QName of the key definitionpublic void compileKeyDefinitionSetters()
public String compileStructuredQName(StructuredQName qName)
qName
- the StructuredQName
public void compileKeyDefinitionClasses()
public String compileNamespaceContext(NamespaceResolver resolver)
resolver
- a NamespaceResolver representing a namespace context
public static String javaEscape(CharSequence in)
in
- the value of the string literal
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |