| org.andromda.translation.ocl.BaseTranslator
All known Subclasses: org.andromda.translation.ocl.testsuite.TraceTranslator, org.andromda.translation.ocl.validation.ValidationJavaTranslator, org.andromda.translation.ocl.query.QueryTranslator, org.andromda.translation.ocl.testsuite.ContextElementFinder,
BaseTranslator | abstract public class BaseTranslator extends DepthFirstAdapter implements Translator(Code) | | The "base" translator which all Translator's should extend, provides some basic functionality, such as the retrieveal
of translation fragments from the translation template file, pre-processing, post-processing, etc.
The primary methods (in addition to methods you'll extend to handle expression parsing) to take note of when
extending this class are:
author: Chad Brandon |
Field Summary | |
protected Logger | logger The logger instance that can be used by all decendant classes. |
Method Summary | |
public Object | getContextElement() Returns the context element for the expression being translated. | public Expression | getExpression() Returns the current value of the Expression. | protected OperationDeclaration | getOperation() Gets the operation declaration of the constraint (if the context is an operation), otherwise returns null. | protected String | getTranslationFragment(String fragmentName) Finds the "fragment" with the specified fragmentName from the library translation file.
IMPORTANT: as a best practice, it is recommended that you use handleTranslationFragment(Node
node) if at all possible (instead of this method), it will help your code be cleaner and the methods smaller
and more maintainable. | protected void | handleTranslationFragment(Object node) Calls the handlerMethod defined on the <fragment/> element if fragmentName matches one the
fragments defined within the current translation file.
A handlerMethod must have two arguments: - The first argument must be a
java.lang.String which will be the body of the corresponding kind element for the
matching fragment. | public void | inAClassifierContextDeclaration(AClassifierContextDeclaration declaration) Sets the element type which represents the context of the expression for expressions having classifiers as their
context. | public void | inADefClassifierExpressionBody(ADefClassifierExpressionBody expressionBody) Sets the kind and name of the expression for def expressions. | public void | inAInvClassifierExpressionBody(AInvClassifierExpressionBody expressionBody) Sets the kind and name of the expression for inv expressions. | public void | inAOperationContextDeclaration(AOperationContextDeclaration declaration) Sets the element type which represents the context of the expression for expressions having operations as their
context. | public void | inAOperationExpressionBody(AOperationExpressionBody operationExpressionBody) Sets the kind and name of the expression for operation contexts. | protected boolean | isOperationArgument(String argument) Indicates if the given argument is an operation argument (if the context declaration is an
operation)
Parameters: argument - the argument to check. | public void | postProcess() Performs any post processing. | public void | preProcess() Performs any initlization. | protected void | process(String expression) Parses the expression and applies this Translator to it. | public Expression | translate(String translationName, String expression, Object contextElement)
NOTE: null is allowed for contextElement (even though it isn't within
ExpressionTranslator.translate() since the TraceTranslator doesn't need a contextElement and we
don't want to slow down the trace by having to read and load a model each time. |
logger | protected Logger logger(Code) | | The logger instance that can be used by all decendant classes.
|
getContextElement | public Object getContextElement()(Code) | | Returns the context element for the expression being translated.
the context element. |
getExpression | public Expression getExpression()(Code) | | Returns the current value of the Expression. This stores the translation being translated.
Expression stores the the translation. |
getOperation | protected OperationDeclaration getOperation()(Code) | | Gets the operation declaration of the constraint (if the context is an operation), otherwise returns null.
the operation declaration or null. |
getTranslationFragment | protected String getTranslationFragment(String fragmentName)(Code) | | Finds the "fragment" with the specified fragmentName from the library translation file.
IMPORTANT: as a best practice, it is recommended that you use handleTranslationFragment(Node
node) if at all possible (instead of this method), it will help your code be cleaner and the methods smaller
and more maintainable.
Will retrieve the contents of the fragment kind that corresponds to the kind of expression currently
being translated. (i.e. if 'context LegalAgreement inv: allInstances -> isUnique(documentTitle') ' is
being translated, then the body of the element <kind name="inv"> would be returned).
NOTE: You would use this method instead of handleTranslationFragment(Node
node) if you just want to retrieve the value of the fragment and don't want to have a handlerMethod which actually handles the processing of the output. For example you may
want to add a fragment called 'constraintTail' which would always be added to your translation at the end of the
constraint, the 'tail'. There isn't any part of the expression that matches this, but you still want to store it
in a translation template since it could be different between translations within your Translation-Library.
Parameters: fragmentName - the name of the fragment to retrieve from the translation String the output String from the translated fragment. See Also: BaseTranslator.handleTranslationFragment(Node node) |
handleTranslationFragment | protected void handleTranslationFragment(Object node)(Code) | | Calls the handlerMethod defined on the <fragment/> element if fragmentName matches one the
fragments defined within the current translation file.
A handlerMethod must have two arguments: - The first argument must be a
java.lang.String which will be the body of the corresponding kind element for the
matching fragment. (i.e. if 'context LegalAgreement inv: allInstances -> isUnique(documentTitle')'
is being translated, then the body of the element <kind name="inv"/> would be returned) - The second
argument is of type
java.lang.Object and is the node that is currently being parsed at the time the
matching of the fragmentName occurred.
For example this handlerMethod might be defined within your translation file to handle the 'allInstances'
expression:
<fragment name="(\s*${elementName}\s*\.)?\s*allInstances.*"
handlerMethod="handleAllInstances">
<kind name="body">
from $completeElementName as $lowerCaseElementName
</kind>
</fragment>
And the implementation of the handleAllInstances method would be:
public void handleAllInstances(String translation, Object node)
{
//some handling code
}
Parameters: node - the node being parsed, the toString value of this node is what is matched against the translationfragment. We also need to pass the node to our handlerMethod so that itcan be used it for additional processing (if we need it). See Also: getTranslationFragment(java.lang.String) |
inAClassifierContextDeclaration | public void inAClassifierContextDeclaration(AClassifierContextDeclaration declaration)(Code) | | Sets the element type which represents the context of the expression for expressions having classifiers as their
context. If subclasses override this method, they MUST call this method before their own
implementation.
Parameters: declaration - the AClassifierContextDeclaration instance from which we retrieve the element type. |
inADefClassifierExpressionBody | public void inADefClassifierExpressionBody(ADefClassifierExpressionBody expressionBody)(Code) | | Sets the kind and name of the expression for def expressions. If subclasses override this method,
they MUST call this method before their own implementation.
Parameters: expressionBody - |
inAInvClassifierExpressionBody | public void inAInvClassifierExpressionBody(AInvClassifierExpressionBody expressionBody)(Code) | | Sets the kind and name of the expression for inv expressions. If subclasses override this method,
they MUST call this method before their own implementation.
Parameters: expressionBody - |
inAOperationContextDeclaration | public void inAOperationContextDeclaration(AOperationContextDeclaration declaration)(Code) | | Sets the element type which represents the context of the expression for expressions having operations as their
context. If subclasses override this method, they MUST call this method before their own
implementation.
Parameters: declaration - the AOperationContextDeclaration instance from which we retrieve the element type. |
inAOperationExpressionBody | public void inAOperationExpressionBody(AOperationExpressionBody operationExpressionBody)(Code) | | Sets the kind and name of the expression for operation contexts. If subclasses override this method, they
MUST call this method before their own implementation.
Parameters: operationExpressionBody - |
isOperationArgument | protected boolean isOperationArgument(String argument)(Code) | | Indicates if the given argument is an operation argument (if the context declaration is an
operation)
Parameters: argument - the argument to check. true/false |
postProcess | public void postProcess()(Code) | | Performs any post processing. Subclasses should override to perform any final cleanup/processing.
|
preProcess | public void preProcess()(Code) | | Performs any initlization. Subclasses should override this method if they want to provide any initilization
before translation begins.
|
process | protected void process(String expression) throws IOException(Code) | | Parses the expression and applies this Translator to it.
Parameters: expression - the expression to process. throws: IOException - if an IO error occurs during processing. |
|
|