Skip to content

Latest commit

 

History

History
713 lines (542 loc) · 16.4 KB

README.md

File metadata and controls

713 lines (542 loc) · 16.4 KB

Xml DSL

Xml DSL, as you can guess, is designed to use Xml syntax for building your application. The main advantage is that you can use the same syntax/files at runtime and at compiletime as shown below.

  • To compile DSL (and generate code for targeted platform at compiletime), use BasicXmlCompiler class.
  • To read DSL at runtime, use BasicXmlReader class.

Use the basic Xml compiler

Defining context
<root name="myContextName">
    <test id="myString" value="hello world"/>
</root>
File compilation
var assembler = BasicXmlCompiler.compile( "context/xml/testBuildingString.xml" );
Locate ID
factory = assembler.getApplicationContext( "myContextName", ApplicationContext ).getCoreFactory();
var myString = factory.locate( 'myString' );

Use the basic Xml reader

Defining context
<root name="myContextName">
    <test id="myString" value="hello world"/>
</root>
File reading
var assembler = BasicXmlReader.read( "context/xml/testBuildingString.xml" );
Locate ID
factory = assembler.getApplicationContext( "myContextName", ApplicationContext ).getCoreFactory();
var myString = factory.locate( 'myString' );

More Xml examples

Primitive value assignment

Null value assignment to an ID
<root name="applicationContext">
    <test id="value" type="null"/>
</root>
Boolean value assignment to an ID
<root name="applicationContext">
    <test id="b" type="Bool" value="true"/>
</root>
String value assignment to an ID
<root name="applicationContext">
    <test id="s" value="hello"/>
</root>
Int value assignment to an ID
<root name="applicationContext">
    <test id="i" type="Int" value="-3"/>
</root>
UInt value assignment to an ID
<root name="applicationContext">
    <test id="i" type="UInt" value="3"/>
</root>
Hexadecimal value assignment to an ID
<root name="applicationContext">
    <test id="i" type="Int" value="0xFFFFFF"/>
</root>

Instanciation and references

Anonymous object
<root name="applicationContext">
    <test id="obj" type="Object">
        <property name="name" value="Francis"/>
        <property name="age" type="Int" value="44"/>
        <property name="height" type="Float" value="1.75"/>
        <property name="isWorking" type="Bool" value="true"/>
        <property name="isSleeping" type="Bool" value="false"/>
    </test>
</root>
Simple class instance
<root name="applicationContext">
    <bean id="instance" type="hex.mock.MockClassWithoutArgument"/>
</root>
Simple class instance with primitive arguments passed to the constructor
<root name="applicationContext">
    <bean id="size" type="hex.structures.Size">
        <argument type="Int" value="10"/>
        <argument type="Int" value="20"/>
    </bean>
</root>
Building an instance with primitive references passed to its constructor
<root name="applicationContext">
	
    <x id="x" type="Int" value="1"/>
    <y id="y" type="Int" value="2"/>

    <position id="position" type="hex.structures.Point">
        <argument ref="x" />
        <argument ref="y" />
    </position>
	
</root>
Building multiple instances and pass some of them as constructor arguments
<root name="applicationContext">
	
    <rectangle id="rect" type="hex.mock.MockRectangle">
        <argument ref="rectPosition.x"/>
        <argument ref="rectPosition.y"/>
        <property name="size" ref="rectSize" />
    </rectangle>

    <size id="rectSize" type="hex.structures.Point">
        <argument type="Int" value="30"/>
        <argument type="Int" value="40"/>
    </size>

    <position id="rectPosition" type="hex.structures.Point">
        <property type="Int" name="x" value="10"/>
        <property type="Int" name="y" value="20"/>
    </position>
	
</root>
Building instances with multiple references passed to the constructor
<root name="applicationContext">
	
	<chat id="chat" type="hex.mock.MockChat"/>
    <receiver id="receiver" type="hex.mock.MockReceiver"/>
	
	<proxy id="proxyChat" type="hex.mock.MockProxy">
        <argument ref="chat" />
        <argument ref="chat.onTranslation"/>
    </proxy>

    <proxy id="proxyReceiver" type="hex.mock.MockProxy">
        <argument ref="receiver" />
        <argument ref="receiver.onMessage"/>
    </proxy>
	
</root>
Array filled with references
<root name="applicationContext">

    <collection id="fruits" type="Array<hex.mock.MockFruitVO>">
        <argument ref="fruit0" />
        <argument ref="fruit1" />
        <argument ref="fruit2" />
    </collection>
	
	<collection id="empty" type="Array<Dynamic>"/>
	
	<collection id="text" type="Array<String>">
        <argument value="hello" />
        <argument value="world" />
    </collection>

    <fruit id="fruit0" type="hex.mock.MockFruitVO"><argument value="orange"/></fruit>
    <fruit id="fruit1" type="hex.mock.MockFruitVO"><argument value="apple"/></fruit>
    <fruit id="fruit2" type="hex.mock.MockFruitVO"><argument value="banana"/></fruit>

</root>
Assign class reference to an ID
<root name="applicationContext">

    <RectangleClass id="RectangleClass" type="Class" value="hex.mock.MockRectangle"/>

    <test id="classContainer" type="Object">
        <property name="AnotherRectangleClass" ref="RectangleClass"/>
    </test>

</root>
Hashmap filled with references
<root name="applicationContext">
	
    <collection id="fruits" type="hex.collection.HashMap<Dynamic, hex.mock.MockFruitVO>">
        <item> <key value="0"/> <value ref="fruit0"/></item>
        <item> <key type="Int" value="1"/> <value ref="fruit1"/></item>
        <item> <key ref="stubKey"/> <value ref="fruit2"/></item>
    </collection>
	
    <fruit id="fruit0" type="hex.mock.MockFruitVO"><argument value="orange"/></fruit>
    <fruit id="fruit1" type="hex.mock.MockFruitVO"><argument value="apple"/></fruit>
    <fruit id="fruit2" type="hex.mock.MockFruitVO"><argument value="banana"/></fruit>
    <point id="stubKey" type="hex.structures.Point"/>
	
</root>
Get instance from static method
<root name="applicationContext">
	
    <gateway id="gateway" value="http://localhost/amfphp/gateway.php"/>

    <service id="service" type="hex.mock.MockServiceProvider" static-call="getInstance">
        <method-call name="setGateway">
            <argument ref="gateway" />
        </method-call>
    </service>
	
</root>
Get instance from static method with arguments
<root name="applicationContext">
	
    <rectangle id="rect" type="hex.mock.MockRectangleFactory" static-call="getRectangle">
        <argument type="Int" value="10"/><argument type="Int" value="20"/>
        <argument type="Int" value="30"/><argument type="Int" value="40"/>
    </rectangle>
	
</root>
Get instance from object's method call returned by static method
<root name="applicationContext">
	
    <point id="point" type="hex.mock.MockPointFactory" static-call="getInstance" factory-method="getPoint">
        <argument type="Int" value="10"/>
        <argument type="Int" value="20"/>
    </point>
	
</root>
Building multiple instances with arguments
<root name="applicationContext">
	
	<rectangle id="rect" type="hex.mock.MockRectangle">
		<argument type="Int" value="10"/>
        <argument type="Int" value="20"/>
		<argument type="Int" value="30"/>
        <argument type="Int" value="40"/>
    </rectangle>

    <bean id="size" type="hex.structures.Size">
        <argument type="Int" value="15"/>
        <argument type="Int" value="25"/>
    </bean>
	
	<bean id="position" type="hex.structures.Point">
        <argument type="Int" value="35"/>
        <argument type="Int" value="45"/>
    </bean>
	
</root>

Injection and mapping

Inject into an instance
<root name="applicationContext">
    <instance id="instance" type="hex.mock.MockClassWithInjectedProperty" inject-into="true"/>
</root>
Class instance with its abstract type mapped to context's injector
<root name="applicationContext">

    <module id="instance" type="hex.mock.MockClass" map-type="hex.mock.IMockInterface"/>

</root>
Class instance mapped to 2 abstract types in context's injector
<root name="applicationContext">
    <module id="instance" type="hex.mock.MockClass" map-type="hex.mock.IMockInterface; hex.mock.IAnotherMockInterface"/>
</root>
HashMap with mapped type
<root name="applicationContext">
	
    <collection id="fruits" type="hex.collection.HashMap" map-type="hex.collection.HashMap<String, hex.mock.MockFruitVO>">
        <item> <key value="0"/> <value ref="fruit0"/></item>
        <item> <key value="1"/> <value ref="fruit1"/></item>
    </collection>
	
    <fruit id="fruit0" type="hex.mock.MockFruitVO"><argument value="orange"/></fruit>
    <fruit id="fruit1" type="hex.mock.MockFruitVO"><argument value="apple"/></fruit>
	
</root>
Array instanciation mapped to abstract types thorugh context's injector
<root name="applicationContext">
    <test id="intCollection" type="Array<Int>" map-type="Array<Int>; Array<UInt>"/>
    <test id="stringCollection" type="Array<String>" map-type="Array<String>"/>
</root>
Instances mapped to abstract types with type params
<root name="applicationContext">
   
    <i id="i"  type="Int"  value="3"/>
	<intInstance id="intInstance" type="hex.mock.MockClassWithIntGeneric" map-type="hex.mock.IMockInterfaceWithGeneric<Int>; hex.mock.IMockInterfaceWithGeneric<UInt>">
		<argument ref="i"/>
	</intInstance>
	
	<s id="s"  value="test"/>
	<stringInstance id="stringInstance" type="hex.mock.MockClassWithStringGeneric" map-type="hex.mock.IMockInterfaceWithGeneric<String>">
		<argument ref="s"/>
	</stringInstance>
	
</root>

Properties

Properties assignment
<root name="applicationContext">
	
	<rectangle id="rect" type="hex.mock.MockRectangle">
        <property name="size" ref="size" />
    </rectangle>
	
    <size id="size" type="hex.structures.Point">
        <property name="x" ref="width" />
        <property name="y" ref="height" />
    </size>
	
	<bean id="width" type="Int" value="10"/>
	<bean id="height" type="Int" value="20"/>

</root>
Assign class reference and static variable as object's property
<root name="applicationContext">

    <object id="object" type="Object">
        <property name="property" static-ref="hex.mock.MockClass.MESSAGE_TYPE"/>
    </object>
	
	<object id="object2" type="Object">
        <property name="property" type="Class" value="hex.mock.MockClass"/>
    </object>

    <instance id="instance" type="hex.mock.ClassWithConstantConstantArgument">
        <argument static-ref="hex.mock.MockClass.MESSAGE_TYPE"/>
    </instance>

</root>

Method call

Simple method call on an instance
<root name="applicationContext">

    <caller id="caller" type="hex.mock.MockCaller">
        <method-call name="call">
            <argument value="hello"/>
            <argument value="world"/>
        </method-call>
    </caller>

</root>
Method call with argument typed from class with type paramemeters
<root name="applicationContext">

    <caller id="caller" type="hex.mock.MockCaller">
        <method-call name="callArray">
            <argument ref="fruitsInterfaces"/>
        </method-call>
    </caller>

    <collection id="fruitsInterfaces" type="Array<hex.mock.IMockFruit>">
        <argument ref="fruit0" />
        <argument ref="fruit1" />
        <argument ref="fruit2" />
    </collection>
	
    <fruit id="fruit0" type="hex.mock.MockFruitVO"><argument value="orange"/></fruit>
    <fruit id="fruit1" type="hex.mock.MockFruitVO"><argument value="apple"/></fruit>
    <fruit id="fruit2" type="hex.mock.MockFruitVO"><argument value="banana"/></fruit>
	
</root>
Building multiple instances and call methods on them
<root name="applicationContext">
	
    <rectangle id="rect" type="hex.mock.MockRectangle">
        <property name="size" ref="rectSize" />
        <method-call name="offsetPoint">
            <argument ref="rectPosition"/>
        </method-call>
	</rectangle>

    <size id="rectSize" type="hex.structures.Point">
        <argument type="Int" value="30"/>
        <argument type="Int" value="40"/>
    </size>

    <position id="rectPosition" type="hex.structures.Point">
        <property type="Int" name="x" value="10"/>
        <property type="Int" name="y" value="20"/>
    </position>

    <rectangle id="anotherRect" type="hex.mock.MockRectangle">
        <property name="size" ref="rectSize" />
        <method-call name="reset"/>
    </rectangle>
	
</root>

Static variable

Assign static variable to an ID
<root name="applicationContext">
    <constant id="constant" static-ref="hex.mock.MockClass.MESSAGE_TYPE"/>
</root>
Pass static variable as a constructor argument
<root name="applicationContext">

    <instance id="instance" type="hex.mock.ClassWithConstantConstantArgument">
        <argument static-ref="hex.mock.MockClass.MESSAGE_TYPE"/>
    </instance>

</root>
Pass a static variable as a method call argument
<root name="applicationContext">

    <instance id="instance" type="hex.mock.MockMethodCaller">
		<method-call name="call">
			<argument static-ref="hex.mock.MockMethodCaller.staticVar"/>
		</method-call>
    </instance>

</root>

Misc

Example with DSL preprocessing
<root ${context}>

    ${node}

</root>
Parse and make Xml object
<root name="applicationContext">

    <data id="fruits" type="XML">
        <root>
            <node>orange</node>
            <node>apple</node>
            <node>banana</node>
        </root>
    </data>

</root>
Parse Xml with custom parser and make custom instance
<root name="applicationContext">

    <data id="fruits" type="XML" parser-class="hex.mock.MockXmlParser">
        <root>
            <node>orange</node>
            <node>apple</node>
            <node>banana</node>
        </root>
    </data>

</root>
Conditional parsing
<root name="applicationContext">

    <msg id="message" value="hello debug" if="test,release"/>
    <msg id="message" value="hello production" if="production"/>

</root>
Use a custom application context class
<root name="applicationContext" type="hex.ioc.parser.xml.context.mock.MockApplicationContext">
	<test id="test" value="Hola Mundo"/>
</root>
Instantiate mapping configuration
<root name="applicationContext">

    <config id="config" type="hex.di.mapping.MappingConfiguration">
		
        <item> 
			<key type="Class" value="hex.mock.IMockInterface"/> 
			<value type="Class" value="hex.mock.MockClass"/>
		</item>
		
        <item> 
			<key type="Class" value="hex.mock.IAnotherMockInterface"/> 
			<value ref="instance"/>
		</item>
		
    </config>

    <facebookService id="instance" type="hex.mock.AnotherMockClass"/>

</root>