java - Extend a JDOM Document -
for project @ university, need parse gml file. gml files xml based use jdom2 parse it. fit purposes, extended org.jdom2.document so:
package datenbank; import java.io.file; // more imports public class gmldatei extends org.jdom2.document { public void saveasfile() { // ... } public gmlknoten getrootelement(){ return (gmlknoten) this.getdocument().getrootelement(); } public void setrootelement(gmlknoten root){ this.getdocument().setrootelement(root); } }
i extended org.jdom2.element , named subclass gmlknoten not matter question.
when testing, try load gml file. when using native document , element classes, loads fine, when using subclasses, following scenario:
i load file using:
saxbuilder saxbuilder = new saxbuilder(); file inputfile = new file("gml/roads_munich_route_lines.gml"); gmldatei document = null; arraylist<string> types = new arraylist<string>(); try { document = (gmldatei) saxbuilder.build(inputfile); } catch (jdomexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); }
in line
document = (gmldatei) saxbuilder.build(inputfile);
i cast-exception:
exception in thread "main" java.lang.classcastexception: org.jdom2.document cannot cast datenbank.gmldatei @ datenbank.gmltest.main(gmltest.java:27)
i thought casting schould no problem subclassing org.jdom2.document. missing?
vat
in general want "challenge" requirement extend document - value custom classes not part of native implementation? ask 2 reasons:
- as maintainer of jdom, should adding new feature?
- i curious.....
jdom has system in place allowing extend it's core classes , have different implementation of them when parsing document. done extending jdomfactory
.
consider code here: jdomfactory interface. when saxparser
parses document uses methods build document.
there default, overridable implementation in defaultjdomfactory can extend, and, example, in implementation, must override non-final "element" methods like:
@override public element element(final int line, final int col, final string name, string prefix, string uri) { return new element(name, prefix, uri); }
and instead have:
@override public element element(final int line, final int col, final string name, string prefix, string uri) { return new gmlknoten (name, prefix, uri); }
note have override methods non-final , return content customised (for example, have override 4 element
methods count.
with own gmljdomfactory
can use saxbuilder either using full constructor new saxbuilder(null, null, new gmpjdomfactory())
or setting jdomfactory
after have constructred setjdomfactory(...)
Comments
Post a Comment