aspose file tools*
The moose likes XML and Related Technologies and the fly likes XML mapping rules Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML mapping rules" Watch "XML mapping rules" New topic
Author

XML mapping rules

Ahmed Basheer
Ranch Hand

Joined: Apr 15, 2004
Posts: 77
I am trying to think of the XML rules engine which will take a input in the form of the Byte Stream( from socket) and create a logical structure (Java object out of it). I would like to specify the rules in XML as to how to traverse through the byte array and which bytes in the byte array transform to which attributes of the java object.
The input us the byte array and output is structured Java object. I need to specify the conversion functions e.g integer stored in the byte array from 0 bit position to 25 bit position will map to string field in the Java object.
Does any one know any examples or model which can give me start or idea of how to structure these mapping rules.I appreciate your help.

Thanks,
Basheer
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
The byte offset approach is a bit difficult when dealing with variable-length strings. For example, if you specify that from an Email object the "subject", "recipient" and "body" fields need to be serialized to byte offsets 0-199, 200-299 and 300-1299, respectively, and the values for "subject", "recipient" and "body" aren't exactly 200, 100 and 1000 bytes long, you'll have to fill in the blanks somehow. Similarly, what do you do when one email has a subject over 200 bytes long?

Would it be possible to just specify the order in which each field is presented in the stream? E.g. something like this:

...and then use java.io.DataOutputStream and java.io.DataInputStream to read/write them in the correct order (and using the Reflection API to read/write them from/to an instance of the associated class, of course).


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Ahmed Basheer
Ranch Hand

Joined: Apr 15, 2004
Posts: 77
Thanks for replying. I don't have control of the byte array that is written. Some other server writes the bytes to the socket. I am client and have mapping like this(this is just an example)

byte - 1 - code ( maps to particular java object)
byte - 2 - loginid
byte - 3 and 4 - SC#
byte - 5 - 4 MSB - size of bytes for name - let us say that is 4
bytes - 5 - 4 LSB to byte 9 MSB - name ( 4 bytes )
bytes - 9 - 4LSB - unused

so size of the message is 9 bytes
LSB - least significant bits
MSB - Most significant bits.

Since message size is not know they are variable, so I would like to create these mapping rules and calculating size in XML. some thing like this.



<rulesstore>

<rule id = code classname = targetclass>

<field1>
<type source=int8 target=int>
<blen>8<blen>
<name>code</loginid>
</field1>

<field2>
<type source=int8 target=int>
<blen>8<blen>
<name>loginid</loginid>
</field2>

<filed3>
<type source=int16 target=int>
<blen>16<blen>
<name>SS#</loginid>
<apply>changeEndian</apply>
</field3>

<filed4>
<type source=int8 target=int>
<blen>4<blen>
<name>length_of_name_field</loginid>
<apply>somefunction( e,g & 0xF) >> 4)</apply>
</field4>

<filed4>
<type source=int32 target=int>
<blen>32<blen>
<name>arraytype</loginid>
<count>length_of_name_field>/count>
<apply>somefunction_which_will_parse_the_btes_to_array</apply>
</field4>

<filed4>
<type source=int8 target=int>
<blen>4<blen>
<name>not_used</loginid>
</field4>


</rule>

</rulesstore>

Do you think, this is the right approach?

Thanks,
Basheer
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Yes, that looks like it could work. I noticed there weren't any strings involved -- is that the case?

Do you need to go the other way, i.e. serialize Java objects to that "custom" byte stream?

By the way, how were you thinking of implementing this?

My first thought was to use a scripting engine like BeanShell or Jython.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML mapping rules