SAX Serializer
Wednesday, 6 February 2013 · 4 messages
M
Michael.Hedenus-EXT6 Feb 2013 10:37 #
Dear Christian,
it's very great that you've incorporated the SAXSerializer. But you've overdone it a bit. The setItem() method and the parse() methods are required. I've written a JUnit-Test to demonstrate the application of the SAXSerializer with JAXB.
In line 49 there should be the method setItem, which you have removed. The JAXB unmarshaller is going to invoke parse(String) which now throws an exception with an 'unimplemented' note.
So please re-enable the parse()-methods and re-introduce the setItem()-method.
package basex.test.sax;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;import junit.framework.Assert;
import org.basex.core.Context;
import org.basex.core.cmd.CreateDB;
import org.basex.io.serial.SAXSerializer;
import org.basex.io.serial.Serializer;
import org.basex.query.QueryProcessor;
import org.basex.query.value.item.Item;
import org.junit.Test;
public class SAXSerializerTest
{
@Test
public void unmarshallJAXBObjectWithSAXSerializer() throws
Exception
{
File temp = getTemp();
JAXBContext jaxbContext = JAXBContext.newInstance
(DomainObject.class);
// create XML
File input = new File(temp, "input.xml");
jaxbContext.createMarshaller().marshal(new DomainObject(
"Object1", 42), input);
// create DB
System.setProperty("org.basex.path",
temp.getAbsolutePath());
Context context = new Context();
new CreateDB("test1",
input.getAbsolutePath()).execute(context);
// get object from DB
QueryProcessor queryProcessor = new QueryProcessor(
"//domain-object[@name='Object1']", context);
Item item1 = queryProcessor.iter().next();
// write out to see that item is here
Serializer serializer1 =
queryProcessor.getSerializer(System.out);
serializer1.serialize(item1);
serializer1.close();
SAXSerializer saxSerializer = new SAXSerializer();
SAXSource saxSource = new SAXSource(saxSerializer, null);
// this is needed!!!
saxSerializer.setItem(item1);
DomainObject domainObject1 =
jaxbContext.createUnmarshaller().unmarshal(saxSource, DomainObject.class
).getValue(); queryProcessor.close();
Assert.assertEquals(42, domainObject1.getValue());
}
private File getTemp()
{
File temp = new File("d:/temp/basex");
//System.getProperty("java.io.tmpdir");
temp.mkdirs();
return temp;
}
}The code is attached to this mail.
CG
Christian Grün6 Feb 2013 12:50 #
Quoted text · Michael.Hedenus-EXT · 4 lines
> it's very great that you've incorporated the SAXSerializer. But you've > overdone it a bit. The setItem() method and the parse() methods are > required. I've written a JUnit-Test to demonstrate the application of the > SAXSerializer with JAXB.
Fine; I’ve moved the initialization of the item to be serialized in the constructor in order to avoid illegal states. I hope that works for you; feel free to check out the latest commit.
Christan
PS: if the getItem/setItem would need to be called more than once per instance, we could extend the constructor and allows Value instead of Item instances.
Quoted text · Michael.Hedenus-EXT · 88 lines
> In line 49 there should be the method setItem, which you have removed. The > JAXB unmarshaller is going to invoke parse(String) which now throws an > exception with an 'unimplemented' note. > > So please re-enable the parse()-methods and re-introduce the > setItem()-method. > > package basex.test.sax; > > import java.io.File; > > import javax.xml.bind.JAXBContext; > import javax.xml.transform.Source; > import javax.xml.transform.sax.SAXSource; > > import junit.framework.Assert; > > import org.basex.core.Context; > import org.basex.core.cmd.CreateDB; > import org.basex.io.serial.SAXSerializer; > import org.basex.io.serial.Serializer; > import org.basex.query.QueryProcessor; > import org.basex.query.value.item.Item; > import org.junit.Test; > > public class SAXSerializerTest > { > @Test > public void unmarshallJAXBObjectWithSAXSerializer() throws Exception > { > File temp = getTemp(); > JAXBContext jaxbContext = > JAXBContext.newInstance(DomainObject.class); > > // create XML > File input = new File(temp, "input.xml"); > jaxbContext.createMarshaller().marshal(new > DomainObject("Object1", 42), input); > > // create DB > System.setProperty("org.basex.path", > temp.getAbsolutePath()); > Context context = new Context(); > new CreateDB("test1", > input.getAbsolutePath()).execute(context); > > // get object from DB > QueryProcessor queryProcessor = new > QueryProcessor("//domain-object[@name='Object1']", context); > Item item1 = queryProcessor.iter().next(); > > // write out to see that item is here > Serializer serializer1 = > queryProcessor.getSerializer(System.out); > serializer1.serialize(item1); > serializer1.close(); > > SAXSerializer saxSerializer = new SAXSerializer(); > SAXSource saxSource = new SAXSource(saxSerializer, null); > > // this is needed!!! > saxSerializer.setItem(item1); > DomainObject domainObject1 = > jaxbContext.createUnmarshaller().unmarshal(saxSource, > DomainObject.class).getValue(); > > queryProcessor.close(); > > Assert.assertEquals(42, domainObject1.getValue()); > } > > private File getTemp() > { > File temp = new File("d:/temp/basex"); > //System.getProperty("java.io.tmpdir"); > temp.mkdirs(); > return temp; > } > } > > The code is attached to this mail. > > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
M
Michael.Hedenus-EXT6 Feb 2013 13:46 #
Dear Christian,
thanx! That's fine for me. I wondered how expensive it might be to instantiate a Serializer.
One problem remains: In SAXSerializer line 253 the initializiation of Attr is missing. This leads to NPE. Fix:
private Atts decls = new Atts();If there are more problems I'll be happy to report them to you :D
Mit freundlichen Grüßen/Best regards
Dr. Michael Hedenus
Software Developer
Powertrain Engine Systems CSW ETV RGB T2
PIXEL GmbH
Im Gewerbepark C15, 93059 Regensburg, Germany
Telefon/Phone: +49 941 790-6362
Telefax: +49 941 79013-90771
E-Mail: michael.hedenus-ext at continental-corporation.com
www.continental-corporation.com
______________________________________________________________________ Continental Automotive GmbH, Siemensstr. 12, 93055 Regensburg, Germany Geschäftsführer/Managing Directors: Helmut Matschi, Gérard Cordonnier, Harald Stuhlmann
Sitz der Gesellschaft/Registered office: Hannover
Registergericht/Commercial registry: Hannover, HRB 59424, USt-Id. Nr. DE814950663
_______________________________________________________________________ Proprietary and confidential. Distribution only by express authority of Continental AG or its
subsidiaries.
CG
Christian Grün6 Feb 2013 14:11 #
..should be fixed, thanks.
Quoted text · Michael.Hedenus-EXT · 43 lines
On Wed, Feb 6, 2013 at 1:46 PM, <Michael.Hedenus-EXT at continental-corporation.com> wrote: > Dear Christian, > > thanx! That's fine for me. I wondered how expensive it might be to > instantiate a Serializer. > > One problem remains: In SAXSerializer line 253 the initializiation of Attr > is missing. This leads to NPE. Fix: > > private Atts decls = new Atts(); > > If there are more problems I'll be happy to report them to you :D > > > Mit freundlichen Grüßen/Best regards > Dr. Michael Hedenus > Software Developer > Powertrain Engine Systems CSW ETV RGB T2 > PIXEL GmbH > Im Gewerbepark C15, 93059 Regensburg, Germany > > Telefon/Phone: +49 941 790-6362 > Telefax: +49 941 79013-90771 > E-Mail: michael.hedenus-ext at continental-corporation.com > www.continental-corporation.com > ______________________________________________________________________ > Continental Automotive GmbH, Siemensstr. 12, 93055 Regensburg, Germany > Geschäftsführer/Managing Directors: Helmut Matschi, Gérard Cordonnier, > Harald Stuhlmann > Sitz der Gesellschaft/Registered office: Hannover > Registergericht/Commercial registry: Hannover, HRB 59424, USt-Id. Nr. > DE814950663 > _______________________________________________________________________ > Proprietary and confidential. Distribution only by express authority of > Continental AG or its > subsidiaries. > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
Written for BaseX 7.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery