-
Notifications
You must be signed in to change notification settings - Fork 1
XML Processing
Bio Objects can be exported to XML by just calling toXml()
method.
@BioObj
public class Car extends BioObject {
@BioTag(type="String", isInheritable=false)
public static final String PRODUCER = "producer" ;
@BioTag(type="Engine")
public static final String ENGINE = "engine" ;
@BioTag(type="Integer", initial="2007")
public static final String YEAR_OF_PRODUCTION = "year_of_production" ;
@BioTag(type="Boolean", expression="year_of_production > 2016")
public static final String IS_NEW_CAR = "is_new_car" ;
@BioTag(type="String", isExportable=false)
public static final String KEY_PASSWORD = "key_password" ;
@BioTag(type="Long", isEncodable=false)
public static final String MILEAGE = "mileage" ;
}
@BioObj
public class Engine extends BioObject {
@BioTag(type="Integer", initial="3200")
public static final String CAPACITY = "capacity" ;
@BioTag(type="Integer", initial="250")
public static final String HORSE_POWER = "horse_power" ;
@BioTag(type="Integer", initial="6")
public static final String NUMBER_OF_CYLINDERS = "number_of_cylinders" ;
@BioTag(type="EngineType", initial="vee")
public static final String ENGINE_TYPE = "engine_type" ;
}
Note that key_password tag is isExportable=false
which means we will not find it inside XML or JSON.
Engine e = new Engine() ;
e.set(Engine.CAPACITY, 4000);
Car c = new Car() ;
c.set(Car.ENGINE, e) ;
c.set(Car.PRODUCER, "Toyota") ;
c.set(Car.KEY_PASSWORD, "12345") ;
c.set("undefined tag", "Hello World") ;
System.out.println(c.toXml()) ;
It will generate following XML:
<?xml version="1.0" encoding="UTF-8"?>
<car type="Car" code="28201">
<engine type="Engine" code="20998">
<capacity type="Integer">4000</capacity>
<engine-type type="EngineType">vee</engine-type>
<horse-power type="Integer">250</horse-power>
<number-of-cylinders type="Integer">6</number-of-cylinders>
</engine>
<is-new-car type="Boolean">false</is-new-car>
<producer type="String">Toyota</producer>
<undefined-tag type="String">Hello World</undefined-tag>
<year-of-production type="Integer">2007</year-of-production>
</car>
Note that undefined_tag is also exported but we can't find key_password.
Same generated XML can be parsed to same Bio Object and its inner instances. You need to use BioObjectXmlParser
class for this purpose.
BioObjectXmlParser parser = new BioObjectXmlParser() ;
Car c = (Car) parser.parse(new FileInputStream("car.xml")) ;
Another way of parsing from XML is to use a static method fromXml(String xml)
inside BioObject
class. For example:
Car c = (Car) BioObject.fromXml(xml) ;
Sometimes Bio Objects can contain dynamic expression values which can be filled afterwards using other Bio Objects.
<?xml version="1.0" encoding="UTF-8"?>
<car type="Car" code="1">
<fuel-efficiency type="Double">17.8</fuel-efficiency>
<producer type="String">Ford</producer>
<year-of-production type="Integer">2019</year-of-production>
<horse-power type="Dynamic">calculateHP(car.engine.capacity, car.engine.cylinders)</horse-power>
<is-driveable type="Dynamic">weather.celcius > -10 and !weather.is_windy</is-driveable>
</car>
Last two tags are parsed as Dynamic which means that currently they contain definition of data but not actual one. In order to fill them with actual values you have to call fill()
of Bio Object and provide input Bio Objects.
Weather w = new Weather() ;
w.setCelcius(11) ;
w.setWindy(false) ;
Car c = (Car) BioObject.fromXml(xml) ;
c.fill(c, w) ;
System.out.println(v) ;
Now we will get
<?xml version="1.0" encoding="UTF-8"?>
<car type="Car" code="1">
<fuel-efficiency type="Double">17.8</fuel-efficiency>
<producer type="String">Ford</producer>
<year-of-production type="Integer">2019</year-of-production>
<horse-power type="Double">220.0</horse-power>
<is-driveable type="Boolean">true</is-driveable>
</car>
Conditional values are evaluated based on condition. If it is satisfied then <value/>
is used else <else-value/>
is used. <else-value/>
is optional if not provided and condition fails Bio Object will not contain any value.
<?xml version="1.0" encoding="UTF-8"?>
<car type="Car" code="1">
<fuel-efficiency type="Double">17.8</fuel-efficiency>
<producer type="String">Ford</producer>
<year-of-production type="Integer">2019</year-of-production>
<horse-power type="Dynamic">calculateHP(car.engine.capacity, car.engine.cylinders)</horse-power>
<is-driveable type="Dynamic">weather.celcius > -10 and !weather.is_windy</is-driveable>
<base-price type="Double">20000</base-price>
<price type="Conditional">
<condition>?seller and seller.is_distributer</condition>
<value type="Dynamic">car.base_price * 1.25</value>
<else-value type="Dynamic">car.base_price</else-value>
<price>
</car>
Car c = (Car) BioObject.fromXml(xml) ;
c.fill(c) ;
System.out.println(c.toXml()) ;
will return
<?xml version="1.0" encoding="UTF-8"?>
<car type="Car" code="28201">
<base-price type="Double">20000.0</base-price>
<fuel-efficiency type="Double">17.8</fuel-efficiency>
<horse-power type="Double">220.0</horse-power>
<is-driveable type="Boolean">false</is-driveable>
<price type="Double">20000.0</price>
<producer type="String">Ford</producer>
<year-of-production type="Integer">2019</year-of-production>
</car>
price is same as base_price because we have not provided seller
object. Let's try again.
Seller s = new Seller() ;
s.set(Seller.IS_DISTRIBUTER, true) ;
Car c = (Car) BioObject.fromXml(xml) ;
c.fill(c, s) ;
System.out.println(c.toXml()) ;
Now we will get
<?xml version="1.0" encoding="UTF-8"?>
<car type="Car" code="28201">
<base-price type="Double">20000.0</base-price>
<fuel-efficiency type="Double">17.8</fuel-efficiency>
<horse-power type="Double">220.0</horse-power>
<is-driveable type="Boolean">false</is-driveable>
<price type="Double">25000.0</price>
<producer type="String">Ford</producer>
<year-of-production type="Integer">2019</year-of-production>
</car>