Class JrpcgenConst

All Implemented Interfaces:
JrpcgenItem
Direct Known Subclasses:
JrpcgenEnum.Element

public class JrpcgenConst extends JrpcgenXdrDefinition
The JrpcgenConst class represents a single constant defined in an rpcgen "x"-file.
Version:
$Revision: 1.1 $ $Date: 2003/08/13 12:03:45 $ $State: Exp $ $Locker: $
Author:
Harald Albrecht
  • Constructor Details

    • JrpcgenConst

      public JrpcgenConst(JrpcgenContext context, String identifier, String value)
      Constructs a JrpcgenConst and sets the identifier and the associated value.
      Parameters:
      context - The context the new contant belongs to.
      identifier - Constant identifier to define.
      value - Value assigned to constant.
    • JrpcgenConst

      public JrpcgenConst(JrpcgenContext context, String identifier, String value, String enclosure)
      Constructs a JrpcgenConst and sets the identifier and the associated value of an enumeration etc.
      Parameters:
      context - The context the new contant belongs to.
      identifier - Constant identifier to define.
      value - Value assigned to constant.
      enclosure - Name of enclosing enumeration, etc.
  • Method Details

    • getAsRValue

      public static String getAsRValue(String constantExpression, JrpcgenContext context)
      Delivers the passed constant expression as an r-value. The passed constant expression may contain a constant value or an identifier of a constant known to the global table of constants.

      Passing for example the string literal "42", the method returns the passed string "42".

      Passing for example the string literal "VERSION", the method may return the string "demo.VERSION" in case the global table of constants contains a constant definition with the identifier VERSION nested in the enclosure (namespace) demo. Otherwise the passed string "VERSION" is returned.

      Passing for example the string literal "ENUM_FIRST", the method may return the string "EnumerationValues.ENUM_FIRST.getEncoding()" in case the global table of constants contains a constant definition with the identifier ENUM_FIRST nested in the enclosure (namespace) EnumerationValues originating from an enumeration definition provided that XDR ernumerations are mapped by Java enumerations.

      Parameters:
      constantExpression - A string containing a constant value or the identifier of a constant definition known to the global table of constant definitions.
      context - The context of the rpcgen run, providing the table of global constant definitions collected so far.
      Returns:
      The passed constant expression or the string representation of the reference to the constant identified by the passed constant expression.
    • getAsRValue

      public static String getAsRValue(String constantExpression, String forEnclosure, JrpcgenContext context)
      Delivers the passed constant expression as an r-value. The passed constant expression may contain a constant value or an identifier of a constant known to the global table of constants.

      Passing for example the string literal "42", the method returns the passed string "42".

      Given a target enclosure (namespace) element and a global constant definition with the identifier VERSION nested in the enclosure (namespace) demo, passing the string literals "VERSION" and "element" results in returning the string "demo.VERSION".

      Given a target enclosure (namespace) demo and a global constant definition with the identifier VERSION nested in the enclosure (namespace) demo, passing the string literals "VERSION" and "demo" results in returning the string "VERSION".

      Given a target enclosure (namespace) element and a global constant definition with the identifier ENUM_FIRST nested in the enclosure (namespace) EnumerationValues originating from an enumeration definition, passing the string literals "ENUM_FIRST" and "element" results in returning the string "EnumerationValues.ENUM_FIRST.getEncoding()" provided that XDR enumerations are mapped by Java enumerations (see JrpcgenEnum.Element).

      Parameters:
      constantExpression - A string containing a constant value or the identifier of a constant definition known to the global table of constant definitions.
      forEnclosure - The target enclosure (namespace), where the r-value is intended to be used.
      context - The context of the rpcgen run, providing the table of global constant definitions collected so far.
      Returns:
      The passed constant expression or the string representation of the reference to the constant identified by the passed constant expression.
    • getAsCaseValue

      public static String getAsCaseValue(String constantExpression, String forEnclosure, JrpcgenContext context)
      Delivers the passed constant expression as a case value, which is intended to be used within a switch-case-statement. The passed constant expression may contain a constant value or an identifier of a constant known to the global table of constants.

      Passing for example the string literal "42", the method returns the passed string "42".

      Given a target enclosure (namespace) element and a global constant definition with the identifier VERSION nested in the enclosure (namespace) demo, passing the string literals "VERSION" and "element" result in returning the string "demo.VERSION".

      Given a target enclosure (namespace) element and a global constant definition with the identifier ENUM_FIRST nested in the enclosure (namespace) EnumerationValues originating from an enumeration definition, passing the string literals "ENUM_FIRST" and "element" results in returning the string "ENUM_FIRST" provided that XDR enumerations are mapped by Java enumerations (see JrpcgenEnum.Element).

      Parameters:
      constantExpression - A string containing a constant value or the identifier of a constant definition known to the global table of constant definitions.
      forEnclosure - The target enclosure (namespace), where the r-value is intended to be used.
      context - The context of the rpcgen run, providing the table of global constant definitions collected so far.
      Returns:
      The passed constant expression or the string representation of the reference to the constant identified by the passed constant expression.
    • getContext

      public final JrpcgenContext getContext()
      Delivers the context the constant definition belongs to.
      Returns:
      The context teh constant definition belongs to.
    • getValue

      public final String getValue()
      Delivers the value defined for the constant.
      Returns:
      The value of the constant.
    • setValue

      public final void setValue(String value)
      Sets the value represented by the constant.
      Parameters:
      value - The value of the constant in string representation.
    • getEnclosure

      public final String getEnclosure()
      Returns the name of the enclosure the constant lives in.
      Returns:
      The name of the enclosure.
    • setEnclosure

      public final void setEnclosure(String enclosure)
      Sets the name of the enclosure the constant lives in.
      Parameters:
      enclosure - The name of the enclosure.
    • getForEnclosure

      public String getForEnclosure(String forEnclosure)
      Delivers the representation of the constant with respect to the passed enclosure name.

      The passed enclosure name identifies the target enclosure (namespace), where the requested constant representation is intended to be used.

      Parameters:
      forEnclosure - The name of the target enclosure.
      Returns:
      One of the following representations will be returned:
      • The value of the constant, if the constant is a global constant.
      • The identifier of the constant, if the constant lives in the target enclosure.
      • The joined version of the enclosure the constant lives in and the identifier of the constant, delimited by a dot.
    • getAsRValue

      public String getAsRValue(String forEnclosure)
      Delivers an r-value representation of the constant.

      The passed enclosure name identifies the target enclosure (namespace), where the requested constant representation is intended to be used.

      Parameters:
      forEnclosure - The name of the target enclosure.
      Returns:
      One of the following representations will be returned:
      • The value of the constant, if the constant is a global constant.
      • The identifier of the constant, if the constant lives in the target enclosure.
      • The joined version of the enclosure the constant lives in and the identifier of the constant, delimited by a dot.
    • getAsCaseValue

      public String getAsCaseValue(String forEnclosure)
      Delivers a case value representation of the constant.

      The passed enclosure name identifies the target enclosure (namespace), where the requested constant representation is intended to be used.

      Parameters:
      forEnclosure - The name of the target enclosure.
      Returns:
      One of the following representations will be returned:
      • The value of the constant, if the constant is a global constant.
      • The identifier of the constant, if the constant lives in the target enclosure.
      • The joined version of the enclosure the constant lives in and the identifier of the constant, delimited by a dot.
    • getValueAsRValue

      public String getValueAsRValue(String forEnclosure)
      Delivers an r-value representation of the value defined by the constant. This is useful for example writing the declaration of an enumeration element.

      The passed eclosure name identifies the target enclosure (namespace), where the requested value representation is intended to be used.

      Parameters:
      forEnclosure - The name of the target enclosure.
      Returns:
      One of the following representations will be returned:
      • The value of the constant, if the constant is a global constant or independent of another constant.
      • If the value is dependent on another constant, the r-value representation of that constant with respect to the target enclosure is taken and where applicable joined with the trailing part of the value following the identifier of the other constant.
    • resolveValue

      public String resolveValue()
      Returns the value as integer literal (and thus resolving identifiers recursively, if necessary). This is only possible for simple subsitutions, that is A is defined as B, B as C, and C as 42, thus A is eventually defined as 42.

      This simple kind of resolving is necessary when defining a particular version of an ONC/RPC protocol. We need to be able to resolve the version to an integer literal because we need to append the version number to any remote procedure defined to avoid identifier clashes if the same remote procedure is defined for several versions.

      Returns:
      integer literal as String or null, if the identifier could not be resolved to an integer literal.
    • writeDeclaration

      public void writeDeclaration(JrpcgenJavaFile javaFile)
    • dump

      public void dump()
      Dumps the constant as well as its value to System.out.
    • dump

      public <T extends Appendable> T dump(T appendable)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • detectIdentifier

      public static String detectIdentifier(String expression)
      Detects a character sequence at the beginning of the passed expression, which holds the definition of an identifier as described in RFC 4506: An identifier is a letter followed by an optional sequence of letters, digits, or underbar ('_'). The case of identifiers is not ignored.

      Letters are defined by the character set ['a'-'z','A'-'Z'] within this implementation.

      Digits are defined by the character set ['0'-'9'] within this implementation.

      Parameters:
      expression - An expression possibly starting with an identifier like character sequence.
      Returns:
      The detected identifier at the beginning of the passed expression or null in case the passed expression does not start with a letter.