de.dante.extex.interpreter.primitives.dynamic.java
Class JavaDef

java.lang.Object
  extended byde.dante.extex.interpreter.type.AbstractCode
      extended byde.dante.extex.interpreter.type.AbstractAssignment
          extended byde.dante.extex.interpreter.primitives.dynamic.java.JavaDef
All Implemented Interfaces:
Code, Definer, Localizable, java.io.Serializable

public class JavaDef
extends AbstractAssignment
implements Definer

This primitive provides a binding of a macro or active character to Java code. This code implements the primitive \javadef.

The Primitive \javadef

The primitive \javadef attaches a definition to a macro or active character. This is done in a similar way as \def works. The difference is that the definition has to be provided in form of a Java class.

Syntax

The general form of this primitive is
   ⟨javadef⟩
       → \javadef ⟨control sequence⟩ ⟨tokens⟩ 

The ⟨control sequence⟩ is any macro or active character. If this token is missing or of the wrong type then an error is raised.

The ⟨tokens⟩ is any specification of a list of tokens like a constant list enclosed in braces or a tokens register. The value of these tokens are taken and interpreted as the name of a Java class. This class is loaded if needed and instantiated. The instance is bound as code to the ⟨control sequence⟩.

The following example illustrates the use of this primitive:

   \javadef\abc{de.dante.extex.interpreter.primitive.Relax} 

The primitive \javadef is local to the enclosing group as is \def. And similar to \def the modifier \global can be used to make the definition in all groups instead of the current group only. This is shown in the following example:

   \global\javadef\abc{de.dante.extex.interpreter.primitive.Relax}
 

The primitive \javadef also respects the count register \globaldefs to enable general global assignment.

Since the primitive is classified as assignment the value of \afterassignment is applied.

Java Implementation

Now we come to the Java side of the definition. The class given as ⟨tokens⟩ must implement the interface Code. The easiest way to achieve this is by declaring a class derived from AbstractCode.

   package my.package;

   import de.dante.extex.interpreter.type.AbstractCode;
   import de.dante.extex.interpreter.contect.Context;
   import de.dante.extex.interpreter.Flags;
   import de.dante.extex.interpreter.TokenSource;
   import de.dante.extex.typesetter.Typesetter;
   import de.dante.util.GeneralException;

   class MyPrimitive extends AbstractCode {

     public MyPrimitive(final String name) {
       super(name);
       // initialization code – if required
     }

     public boolean execute(final Flags prefix,
                            final Context context,
                            final TokenSource source,
                            final Typesetter typesetter
                           ) {
       // implement the execution behavior here
       return true;
     }
   } 

There is more to say about primitives like how to write expandable primitives or ifs. Those details can be found in section Primitives.

Version:
$Revision: 1.32 $
Author:
Gerd Neugebauer
See Also:
Serialized Form

Field Summary
protected static long serialVersionUID
          The constant serialVersionUID contains the id for serialization.
 
Constructor Summary
JavaDef()
          Creates a new object.
JavaDef(java.lang.String codeName)
          Creates a new object.
 
Method Summary
 void assign(Flags prefix, Context context, TokenSource source, Typesetter typesetter)
          The method assign is the core of the functionality of execute().
 void define(Flags prefix, Context context, TokenSource source, Typesetter typesetter)
          Perform a define operation
 
Methods inherited from class de.dante.extex.interpreter.type.AbstractAssignment
execute
 
Methods inherited from class de.dante.extex.interpreter.type.AbstractCode
enableLocalization, getLocalizer, getName, isIf, isOuter, printable, printableControlSequence, readResolve, setName, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

protected static final long serialVersionUID
The constant serialVersionUID contains the id for serialization.

See Also:
Constant Field Values
Constructor Detail

JavaDef

public JavaDef()
Creates a new object. This method is needed for the nativedef wrapper.


JavaDef

public JavaDef(java.lang.String codeName)
Creates a new object.

Parameters:
codeName - the name for debugging
Method Detail

assign

public void assign(Flags prefix,
                   Context context,
                   TokenSource source,
                   Typesetter typesetter)
            throws InterpreterException
Description copied from class: AbstractAssignment
The method assign is the core of the functionality of execute(). This method is preferable to execute() since the execute() method provided in this class takes care of \afterassignment and \globaldefs as well.

Specified by:
assign in class AbstractAssignment
Parameters:
prefix - the prefix controlling the execution
context - the interpreter context
source - the token source
typesetter - the typesetter
Throws:
InterpreterException - in case of an error
See Also:
AbstractAssignment.assign( de.dante.extex.interpreter.Flags, de.dante.extex.interpreter.context.Context, de.dante.extex.interpreter.TokenSource, de.dante.extex.typesetter.Typesetter)

define

public void define(Flags prefix,
                   Context context,
                   TokenSource source,
                   Typesetter typesetter)
            throws InterpreterException
Description copied from interface: Definer
Perform a define operation

Specified by:
define in interface Definer
Parameters:
prefix - the flags
context - the interpreter context
source - the source for new tokens
typesetter - the typesetter
Throws:
InterpreterException - in case of an error
See Also:
Definer.define( de.dante.extex.interpreter.Flags, de.dante.extex.interpreter.context.Context, de.dante.extex.interpreter.TokenSource, de.dante.extex.typesetter.Typesetter)