|
Here we show that the same program written using
JPEL can handle files in different formats, showing how XML can be
used, and also how you can write your own parsers to integration
within JPEL.
|
2.1 XML Format |
|
Beside the specific JPEL format given at section
1, we can use XML to describe profiles applications bellow we can
see the same cache example written in a XML syntax.
|
cache.xml |
util.xml |
<?xml version="1.0" encoding="ISO-8859-1"?>
<configuration>
<description><![CDATA[
cache.xml
Arquivo de parametrização de uma cache.
]]></description>
<include id="util.xml" as="MATH">
<description><![CDATA[
inclui o arquivo util.xml
]]></description>
</include>
<module id="CACHE">
<description><![CDATA[
Define um módulo que armazena os
parâmetros de uma cache de objetos.
]]></description>
<function id="MAX">
<description><![CDATA[
Tamanho máximo permitido para
qualquer cache de objetos. 1K objetos
]]></description>
<value><![CDATA[
MATH.pow(2,10)
]]></value>
</function>
<function id="SIZE">
<description><![CDATA[
Tamanho da cache de objetos.
10% do valor máximo.
]]></description>
<value><![CDATA[
MATH.div (CACHE.MAX,10)
]]></value>
</function>
<function id="FLUSH">
<description><![CDATA[
Numero de objeto que deve ser removido
quando a cache atinge o limite de
objetos. 20% do tamanho da cache.
]]></description>
<value><![CDATA[
CACHE.SIZE * 0.2
]]></value>
</function>
</module>
</configuration>
|
<?xml version="1.0" encoding="ISO-8859-1"?>
<configuration>
<description><![CDATA[
util.xml
Arquivo biblioteca de funções
]]></description>
<function id="div" args="(x,y)">
<description><![CDATA[
Define uma divisão entre inteiros,
x é o dividendo y é o divisor.
]]></description>
<value><![CDATA[
x / y
]]></value>
</function>
<native id="pow" args="(base,expoente)">
<description><![CDATA[
Adiciona o operador de potenciação
]]></description>
<value><![CDATA[
jpel.language.operators.ExpressionPow
]]></value>
</native>
</configuration>
|
You can simply XML sintax using an XML tags file
indicator, or using the XML reader constructor which receives a
XMLConstants instance. Above you will see an XML file tag definition,
used to generate the new XML file. To run applications using another
XML tag file definition use the JVM parameter -Dxml.constants
indicating the appropriate file, o create a specific XML reader with
XMLConstants set up. If you want to convert an XML
file to another one, create an XML reader instance with the
appropriate XMLConstants and an XML writer with the target
XMLConstants, use reader to read old XML and the writer to write the
new format.
|
source_constants.lib |
target_constants.lib |
CONFIGURATION :: configuration
INCLUDE :: include
MODULE :: module
FUNCTION :: function
NATIVE :: native
NAME :: id
FORMAL :: args
DESCRIPTION :: description
VALUE :: value
AS :: as
|
CONFIGURATION :: config
INCLUDE :: inc
MODULE :: group
FUNCTION :: par
NATIVE :: nat
NAME :: id
FORMAL :: arg
DESCRIPTION :: des
VALUE :: val
AS :: as
|
Converting the formats we have got:
|
cache.xml |
util.xml |
<?xml version="1.0" encoding="ISO-8859-1"?>
<config>
<des><![CDATA[
cache.xml
Arquivo de parametrização de uma cache.
]]></des>
<inc id="util.xml" as="MATH">
<des><![CDATA[
inclui o arquivo util.xml
]]></des>
</inc>
<group id="CACHE">
<des><![CDATA[
Define um módulo que armazena os
parâmetros de uma cache de objetos.
]]></des>
<par id="MAX">
<des><![CDATA[
Tamanho máximo permitido para
qualquer cache de objetos. 1K objetos
]]></des>
<val><![CDATA[
MATH.pow(2,10)
]]></val>
</par>
<par id="SIZE">
<des><![CDATA[
Tamanho da cache de objetos.
10% do valor máximo.
]]></des>
<val><![CDATA[
MATH.div (CACHE.MAX,10)
]]></val>
</par>
<par id="FLUSH">
<des><![CDATA[
Numero de objeto que deve ser removido
quando a cache atinge o limite de
objetos. 20% do tamanho da cache.
]]></des>
<val><![CDATA[
CACHE.SIZE * 0.2
]]></val>
</par>
</group>
</config>
|
<?xml version="1.0" encoding="ISO-8859-1"?>
<config>
<des><![CDATA[
util.xml
Arquivo biblioteca de funções
]]></des>
<par id="div" arg="(x,y)">
<des><![CDATA[
Define uma divisão entre inteiros,
x é o dividendo y é o divisor.
]]></des>
<val><![CDATA[
x / y
]]></val>
</par>
<nat id="pow" arg="(base,expoente)">
<des><![CDATA[
Adiciona o operador de potenciação
]]></des>
<val><![CDATA[
jpel.language.operators.ExpressionPow
]]></val>
</nat>
</config>
|
|
|
2.2 Combining
multiple formats |
|
Another option into JPEL is the possibility of
using diferent file formats together. i.e The cache profile could be
a the compositions of JPEL syntax and XML.
|
cache.jpel |
util.xml |
# cache.jpel
# Arquivo de parametrização de uma cache.
# inclui o arquivo util.jpel
include "util.jpel" as MATH
# Define um módulo que armazena os
# parâmetros de uma cache de objetos.
module CACHE
{
#Tamanho máximo permitido para
# qualquer cache de objetos. 1K objetos
MAX = MATH.pow(2,10);
#Tamanho da cache de objetos.
# 10% do valor máximo.
SIZE = MATH.div(CACHE.MAX,10);
#Numero de objeto que deve ser removido
# quando a cache atinge o limite de
# objetos. 20% do tamanho da cache.
FLUSH = CACHE.SIZE * 0.2;
}
|
<?xml version="1.0" encoding="ISO-8859-1"?>
<configuration>
<description><![CDATA[
util.xml
Arquivo biblioteca de funções
]]></description>
<function id="div" args="(x,y)">
<description><![CDATA[
Define uma divisão entre inteiros,
x é o dividendo y é o divisor.
]]></description>
<value><![CDATA[
x / y
]]></value>
</function>
<native id="pow" args="(base,expoente)">
<description><![CDATA[
Adiciona o operador de potenciação
]]></description>
<value><![CDATA[
jpel.language.operators.ExpressionPow
]]></value>
</native>
</configuration>
|
|
|
2.3 Adding new
formats |
|
To add new file formats your must:
|
2.3.1 Implement
interfaces jpel.tree.NodeReader/ jpel.tree.NodeWriter |
The basic idea of the format independency is the existence of a
declaration tree model:
|
cache.jpel |
util.xml |
+ DeclarationInclude(cache.jpel)
|
+ DeclarationInclude(util.jpel)
|
+ DeclarationModule(CACHE)
|
+ DeclarationFunction(MAX,(),MATH.pow(2,10))
|
+ DeclarationFunction(SIZE,(),MATH.div (CACHE.MAX,10) )
|
+ DeclarationFunction(FLUSH,(),CACHE.SIZE * 0.2)
|
+ DeclarationInclude(util.xml)
|
+ DeclarationFunction(div,(x,y),x/y)
|
+ DeclarationNative(pow,(base,expoente),
jpel.language.operators.ExpressionPow)
|
The objetive of the NodeReader is to extract from the given file
this tree of declarations objects. When the tree is ok, we have
build a JPEL reader. The writer must be able to write this tree in
the same input format. Thus we have brought a new format file to
JPEL framework. To undertand better this process you can see the
NodeReaderJpel.jj JavaCC file and NodeReaderXML.java, as well as
NodeWriterJpel.java and NodeWriterXML.java files. |
|
2.3.2 Add
information to framework file |
|
The final step to add the new Reader/Writer into the framework is to
edit the files readers.lib and writers.lib inside de
language module jar file to add an entry with the names of the
reader/writer Java classes.
Now add the implementations classes to your classpath and use the
format files you have defined. Important: there is no need to
modify the applications using JPEL. |
|
|