开发者

Interesting Java code generation issue using XSLT

开发者 https://www.devze.com 2023-02-22 05:16 出处:网络
I am in the process of writing a code generation XSL and have a sticky issue with which I need some expert help.

I am in the process of writing a code generation XSL and have a sticky issue with which I need some expert help.

The XML defines a messaging object and its members. Below is a small sample of one of the message objects which would need to get translated into a Java object suitable for serialization:

<message name="MyObject">
  <comment>Some comment</comment>
  <field name="a" type="byte"/>
  <field name="b" type="Int32"/>
  <field name="c" type="string"/>
  <field name="foo1" type="byte" numberOfBits="3"/>
  <field name="foo2" type="bool" numberOfBits="1" />
  <field name="foo3" type="bool" numberOfBits="1" />
  <field name="foo4" type="bool" numberOfBits="1" />
  <field name="foo5" type="bool" numberOfBits="1" />
  <field name="foo6" type="bool" numberOfBits="1"/>
  <field name="d" type="Int32"/>
  <field name="e" type="Int32"/>
  <field name="f" type="Int16"/>
  <field name="bar1" type="byte" numberOfBits="4"/>
  <field name="bar2" type="empty" numberOfBits="3"/>
  <field name="bar3" type="bool" numberOfBits="1"/>
</message>

I am struggling with how to generate the necessary bit operations for the foo and bar members above. Each set of these "numberOfBits" members will always be groups of 8 bits and will always fit within a byte. In particular I am having trouble keeping track of the number of bits into the current byte and when to start the next byte.

For example, the foo members above would look something like below if I was writing from scratch:

 byte bitset1;


 byte    getFoo1() { return (biteset1 &开发者_如何转开发; 0x07); }
 boolean getFoo2() { return (biteset1 & 0x08) == 0x08; }
 boolean getFoo3() { return (biteset1 & 0x10) == 0x10; }
 boolean getFoo4() { return (biteset1 & 0x20) == 0x20; }
 boolean getFoo5() { return (biteset1 & 0x40) == 0x40; }
 boolean getFoo6() { return (biteset1 & 0x80) == 0x80; }

Any pointers to get me going in the right direction would be greatly appreciated.

Mike


I'd suggest to try xsl:param and/or xsl:variable and a nested template.

Top templates matches first foo and calls nested template for next-sibling, passing the accumulated bit offset to it as xsl:param.

Nested template calls itself for next-sibling.

The nesting ends when no more foo is found.

I think it should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消