|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectverinec.adaptation.Translator
public class Translator
Main class to translate a configuration.
If you have a nodes document including the global definitions, it can be
translated automagically by translate(Element). Of course, it must
contain the necessary definitions of the translation namespace for node
types (service translators) and target.
You can also use translateNode(Element) to translate one node. In this
case you will have to expand the variables (i.e. using the
DataUtil.expandVariables and resolve type information using
resolveNodeType(Element,Element).
Use Distribute to interpret the configuration XML and distribute
the configuration onto the machines.
| Field Summary | |
|---|---|
private Logger |
logger
|
private ITranslationRepository |
rep
|
| Constructor Summary | |
|---|---|
Translator(ITranslationRepository repo)
Instantiate the translator, using repository repo |
|
| Method Summary | |
|---|---|
private XSLTransformer |
getResolver()
Get the resolver xslt translator to resolve node types. |
private String |
getServiceType(String servicename,
Element node)
Determine the service type from that service element and the node type. |
private void |
process(Element node,
Element parent,
boolean doTranslation)
Common procedure for both translation and restriction of a configuration. |
private Element |
processService(Element node,
String service,
boolean doTranslation)
Translate or restrict a single service. |
private void |
resolveMacros(Element node)
Resolve macros within attribute values. |
Element |
resolveNodeType(Element node,
Element globals)
Resolve the type- and target-id's to types and targets inside the node configuration information. |
Document |
restrict(Element nodes)
Creates the restriction information for a set of network nodes. |
Document |
restrictNode(Element node)
Extract the restriction information of an expanded and resolved node. |
Document |
translate(Element nodes)
Translates a set of network nodes. |
Document |
translateNode(Element node)
Translate a single expanded and resolved node. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
private Logger logger
private ITranslationRepository rep
| Constructor Detail |
|---|
public Translator(ITranslationRepository repo)
repo - The repository this instance will use for loading xsl stylesheets| Method Detail |
|---|
public Document translate(Element nodes)
throws VerinecException
This method will expand variables, resolve the type and then do the actual translation.
nodes - A <nodes> element containing tr:typedef and node children
VerinecException - if an error occurs during translation
public Document restrict(Element nodes)
throws VerinecException
Only services that use something restricted will create a
service tag containing warnings within the node tag.
nodes - A <nodes> element containing tr:typedef and node children
VerinecException - if processing fails.
public Document translateNode(Element node)
throws VerinecException
translate(Element)
because you won't need to expand the node yourselves.
The node Element has to be expanded and resolved for the translation
to find the correct type information for the translation.
verinec.util.DataUtil.expandVariables(Document) and
resolveNodeType(Element,Element).
The result is a configuration document containing a set of service tags. The service tags contain meta information and data for configuration files in result-file tags.
Code Example:
//nodes_with_vars is a <nodes> element, containing variable definitions and at least one node element
//first, expand variables
Element expanded = DataUtil.expandVariables(nodes_with_vars);
//we test if it is valid (not mandatory)
SchemaValidator val = new SchemaValidator();
if (! val.validate(expanded)) {
throw new Exception("Expanding produced invalid xml");
}
//then resolve types
List nodes = expanded.getChildren("node", VerinecNamespaces.NS_NODE);
Element node1=(Element)nodes.get(0);
...
Translator translator = new Translator(TranslationRepFactory.createRepository());
node1=translator.resolveNodeType(node1, expanded);
XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
System.out.println("Unvalidated output of resolving type and target");
out.output(node1, System.out);
Only services with a translator specifycation in the nodetype tag are
processed. It is an error to use translateNode with a node that has no
nodetype.
node - The node definition to translate (expanded and resolved)
VerinecException - if processing fails.
public Document restrictNode(Element node)
throws VerinecException
The result is a restriction Document with information about all
restrictions that would apply if this node is translated with its type
information. Only services that use something restricted will create a
service tag containing warnings within the node tag.
Node must fullfill the same requirements as for
translateNode(Element).
node - The node definition to translate (expanded and resolved).
VerinecException - if processing fails.
private void process(Element node,
Element parent,
boolean doTranslation)
throws VerinecException
node - The configuration to process.parent - The element to attach resulting data to.doTranslation - Whether we do translation or restriction.
VerinecException - if processing fails.
private Element processService(Element node,
String service,
boolean doTranslation)
throws VerinecException
node - The configuration data node.service - Name of the service to process.doTranslation - Whether we do translation or restriction.
VerinecException - if processing fails.
private String getServiceType(String servicename,
Element node)
throws VerinecException
servicename - Name of the service.node - The node itselves (for the tr:nodetype local translator selection).
VerinecException - if there is no definition in the nodetype and no local servicetype tag
public Element resolveNodeType(Element node,
Element globals)
throws VerinecException
The node type declaration in tr:nodetype can specify a global node type and can specify translations for individual services in the tr:service children. Locally defined translators overwrite the ones of the global type.
This method will attach the correct type information to each nodetype, for the translateNode and restrictNode methods to find the correct translator and for the translator xsl to find the target for a service.
Additionally it does resolve the {!macro} references in attribute values. This is not done during variable expanding, because the macros are relative to their location within the document. (You will see the difference when you use a macro in a standard target, e.g. to use the hostname of the node the config file is for in an scp target.) To use {! literally in an attribute, you have to escape it with a backslash, that is \{! (note you do not need to escape a parenthesis { if it is not followed by an exclamation mark).
Currently available macros are
todo: We should consider allowing i.e. XPath expressions as macro instead of explicitely coding expressions. A plugin system for macros would be nice.
node - A node to translate.globals - All global values as provided by the repository. (that is, enclosed in the nodes tag)
VerinecException - if processing fails.
private void resolveMacros(Element node)
throws VerinecException
resolveNodeType(Element,Element) for more information about
the macro mechanism.
node - The node element maybe containing macros.
VerinecException - if the macro can not be resolved.
private XSLTransformer getResolver()
throws VerinecException
VerinecException - if XSL resource can not be loaded.
|
Copyright © 2005 Verinec, DIUF | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||