我为基本上是Bèzier路径的javafx.scene.shape.Path
创建了一些Converter
。为此,我转换了类型为MoveTo
和CubicCurveTo
的路径元素,为此我还有转换器:
public class MoveToConverter implements Converter {
@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
writer.startNode("move-to");
MoveTo start = (MoveTo) source;
writer.addAttribute("startX", String.valueOf(start.getX()));
writer.addAttribute("startY", String.valueOf(start.getY()));
writer.endNode();
}
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
reader.moveDown();
MoveTo moveTo = new MoveTo();
moveTo.setX(Double.valueOf(reader.getAttribute("startX")));
moveTo.setY(Double.valueOf(reader.getAttribute("startY")));
reader.moveUp();
return moveTo;
}
@Override
public boolean canConvert(Class type) {
return MoveTo.class.equals(type);
}
}
public class CubicCurveToConverter implements Converter {
@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
CubicCurveTo curveTo = (CubicCurveTo) source;
writer.startNode("cubic-curve-to");
writer.addAttribute("controlX1", String.valueOf(curveTo.getControlX1()));
writer.addAttribute("controlY1", String.valueOf(curveTo.getControlY1()));
writer.addAttribute("controlX2", String.valueOf(curveTo.getControlX2()));
writer.addAttribute("controlY2", String.valueOf(curveTo.getControlY2()));
writer.addAttribute("x", String.valueOf(curveTo.getX()));
writer.addAttribute("y", String.valueOf(curveTo.getY()));
writer.endNode();
}
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
reader.moveDown();
CubicCurveTo curveTo = new CubicCurveTo();
curveTo.setX(Double.valueOf(reader.getAttribute("x")));
curveTo.setY(Double.valueOf(reader.getAttribute("y")));
curveTo.setControlX1(Double.valueOf(reader.getAttribute("controlX1")));
curveTo.setControlY1(Double.valueOf(reader.getAttribute("controlY1")));
curveTo.setControlX2(Double.valueOf(reader.getAttribute("controlX2")));
curveTo.setControlY2(Double.valueOf(reader.getAttribute("controlY2")));
reader.moveUp();
return curveTo;
}
@Override
public boolean canConvert(Class type) {
return CubicCurveTo.class.equals(type);
}
}
public class PathConverter implements Converter {
@Override
public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext marshallingContext) {
Path path = (Path) o;
List<PathElement> elements = path.getElements();
writer.startNode("bezier-path");
writer.addAttribute("count", String.valueOf(elements.size()));
MoveTo start = (MoveTo) elements.get(0);
marshallingContext.convertAnother(start);
// serialize start
for (int i = 1; i < elements.size(); i++) {
CubicCurveTo curveTo = (CubicCurveTo) elements.get(i);
marshallingContext.convertAnother(curveTo);
}
writer.endNode();
}
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
reader.moveDown();
Path path = new Path();
path.setStrokeWidth(2);
path.setStroke(Color.RED);
int nbElements = Integer.parseInt(reader.getAttribute("count"));
MoveTo moveTo = (MoveTo) context.convertAnother(path, MoveTo.class);
path.getElements().add(moveTo);
for (int i = 1; i < nbElements; i++) {
CubicCurveTo curveTo = (CubicCurveTo) context.convertAnother(path, CubicCurveTo.class);
path.getElements().add(curveTo);
System.out.println("Added curve to: "+curveTo);
}
reader.moveUp();
return path;
}
@Override
public boolean canConvert(Class aClass) {
return Path.class.equals(aClass);
}
}
我有一个测试代码:
Path path = new Path();
path.getElements().add(new MoveTo(2, 1));
path.getElements().add(new CubicCurveTo(1, 2, 3, 4, 5, 6));
path.getElements().add(new CubicCurveTo(11, 12, 13, 14, 15, 16));
path.getElements().add(new CubicCurveTo(21, 22, 23, 24, 25, 26));
path.getElements().add(new CubicCurveTo(31, 32, 33, 34, 35, 36));
path.getElements().add(new CubicCurveTo(41, 42, 43, 44, 45, 46));
XStream xStream = new XStream();
xStream.registerConverter(converter);
xStream.registerConverter(new MoveToConverter());
xStream.registerConverter(new CubicCurveToConverter());
File f = File.createTempFile(getClass().getName(), ".xml");
OutputStream os = new FileOutputStream(f);
xStream.toXML(path, os);
Object result = xStream.fromXML(f);
assertEquals(path.toString(), result.toString());
f.delete();
混搭路径时创建的文件如下所示,这是我基本上想要的:
<javafx.scene.shape.Path>
<bezier-path count="6">
<move-to startX="2.0" startY="1.0"/>
<cubic-curve-to controlX1="1.0" controlY1="2.0" controlX2="3.0" controlY2="4.0" x="5.0" y="6.0"/>
<cubic-curve-to controlX1="11.0" controlY1="12.0" controlX2="13.0" controlY2="14.0" x="15.0" y="16.0"/>
<cubic-curve-to controlX1="21.0" controlY1="22.0" controlX2="23.0" controlY2="24.0" x="25.0" y="26.0"/>
<cubic-curve-to controlX1="31.0" controlY1="32.0" controlX2="33.0" controlY2="34.0" x="35.0" y="36.0"/>
<cubic-curve-to controlX1="41.0" controlY1="42.0" controlX2="43.0" controlY2="44.0" x="45.0" y="46.0"/>
</bezier-path>
</javafx.scene.shape.Path>
但是,在解组XML时,我遇到了这个异常:
com.thoughtworks.xstream.converters.ConversionException: only START_TAG can have attributes END_TAG seen ...<bezier-path count="6">\n <move-to startX="2.0" startY="1.0"/>... @3:41 : only START_TAG can have attributes END_TAG seen ...<bezier-path count="6">\n <move-to startX="2.0" startY="1.0"/>... @3:41
---- Debugging information ----
message : only START_TAG can have attributes END_TAG seen ...<bezier-path count="6">\n <move-to startX="2.0" startY="1.0"/>... @3:41
cause-exception : java.lang.IndexOutOfBoundsException
cause-message : only START_TAG can have attributes END_TAG seen ...<bezier-path count="6">\n <move-to startX="2.0" startY="1.0"/>... @3:41
class : javafx.scene.shape.Path
required-type : javafx.scene.shape.Path
converter-type : ch.sahits.game.openpatrician.persistence.converter.PathConverter
path : /javafx.scene.shape.Path/bezier-path
line number : 3
version : 1.4.8
-------------------------------
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1206)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1190)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1154)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1096)
at ch.sahits.game.openpatrician.persistence.converter.PathConverterTest.shouldConvertObjectToXMLAndBack(PathConverterTest.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.IndexOutOfBoundsException: only START_TAG can have attributes END_TAG seen ...<bezier-path count="6">\n <move-to startX="2.0" startY="1.0"/>... @3:41
at org.xmlpull.mxp1.MXParser.getAttributeValue(MXParser.java:927)
at com.thoughtworks.xstream.io.xml.XppReader.getAttribute(XppReader.java:139)
at com.thoughtworks.xstream.io.ReaderWrapper.getAttribute(ReaderWrapper.java:52)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:53)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
at ch.sahits.game.openpatrician.persistence.converter.PathConverter.unmarshal(PathConverter.java:52)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
... 36 more
PathConverter中的第52行指向该行(在for循环中的第一次调用):
CubicCurveTo curveTo = (CubicCurveTo) context.convertAnother(path, CubicCurveTo.class);
我的猜测是,这个问题与散集
方法上的moveDown
和moveUp
方法有关,这样读者仍然处于它期望move-to
的结束节点的状态。对于其他两个转换器,我也有测试,运行良好,所以问题似乎在于组合和对的调用context.convert另一个
。
我想知道我在创建转换器时的想法哪里出了问题,以及如何解决这个问题。
你在一条小溪上作业。如果下一步没有读取标记的值。可能导致错误
尝试:
reader.moveDown();
Path path = new Path();
path.setStrokeWidth(2);
path.setStroke(Color.RED);
int nbElements = Integer.parseInt(reader.getAttribute("count"));
MoveTo moveTo = (MoveTo) context.convertAnother(path, MoveTo.class);
path.getElements().add(moveTo);
reader.moveUp();
for (int i = 1; i < nbElements; i++) {
reader.moveDown();
CubicCurveTo curveTo = (CubicCurveTo) context.convertAnother(path, CubicCurveTo.class);
path.getElements().add(curveTo);
System.out.println("Added curve to: "+curveTo);
reader.moveUp();
}
return path;
我目前正试图使用JAXB将一些XML解组到一个java对象中,我得到了一个奇怪的空指针异常。这只是解组时的一个问题。我可以很好地处理这些类。以下是相关的代码片段(不相关的部分用“…”表示): > JAXB根元素: 根元素的Images子元素: 解组XML的逻辑: 最后,我试图散集的XML(知道这个xml文件实际上是使用JAXB封送器生成的,运行起来没有任何问题可能会有所帮助): 好的,这就是所有相
我正在尝试使用XStream将XML转换为对象树。我希望基于属性创建一个特定的子类。 我该怎么做呢? 当我简单地使用带有别名和一个事件类的XStream时,它工作得很好。 但是,我希望XStream为每个Event类型创建不同的类。我有从抽象Event扩展的类EventAAA和EventBBB。如何告诉XStream在解组时考虑到这一点?XStream目前总是尝试实例化Event并失败,因为它是抽
我使用以下代码通过 JAXB 取消绑定 XML。responseXML 包含从 Web 服务调用返回的 XML 字符串。 以下是取消编组时发生的异常。 有人帮忙解决这个问题。 下面是从XSD自动生成的TestACK类 下面是示例XML
如何编写java类来读取此XMl文件 我试过这样 但我得到了这个错误 请帮我解决这个问题。。。 非常感谢。
我正在尝试使用JAXB从对象构建XML。 但是我错过了一些东西,因为我得到了一个例外: javax.xml.bind.MarshalExc0019-具有链接异常:[com.sun.istack.internal.SAXException2:类雇员及其任何超级类在此上下文中都是已知的。javax.xml.bind.JAXBExc0019:类雇员及其任何超级类在此上下文中都是已知的。]
我正在开发一个小程序,将一个非常大的XML文件(超过2Gb)分成小块。 在研究了许多库之后,我选择了VTD-XML(对大文件使用VTDGenHuge),并开始开发一些代码测试。但我在读取文件的段字节时遇到了一个问题。 我得到抵消和长度: 然后我得到结果信息: 最后,我尝试提取字节段以将其写入另一个文件: 但我正在学习java。lang.IndexOutOfBoundsException 此外,当我