使用 XSLT 生成有趣的 Java 代码问题
我正在编写代码生成 XSL 并遇到一个棘手的问题,我需要一些专家的帮助。
XML 定义消息传递对象及其成员。下面是需要转换为适合序列化的 Java 对象的消息对象之一的小示例:
<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>
我正在努力解决如何为上面的 foo 和 bar 成员生成必要的位操作。这些“numberOfBits”成员的每组将始终是 8 位组,并且始终适合一个字节。特别是,我无法跟踪当前字节的位数以及何时开始下一个字节。
例如,如果我从头开始编写,上面的 foo 成员将如下所示:
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; }
任何让我朝着正确方向前进的指针将不胜感激。
麦克风
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议尝试 xsl:param 和/或 xsl:variable 以及嵌套模板。
顶级模板匹配第一个 foo 并调用下一个兄弟的嵌套模板,将累积的位偏移量作为 xsl:param 传递给它。
嵌套模板将自身称为下一个同级模板。
当找不到更多 foo 时,嵌套结束。
我认为应该有效。
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.