Modified XmlDao to have a Document as input to create the DOM
representation of the object.
This commit is contained in:
parent
e5c23a8924
commit
33df39c03c
|
@ -19,7 +19,6 @@
|
|||
*/
|
||||
package ch.eitchnet.xmlpers;
|
||||
|
||||
import org.w3c.dom.DOMImplementation;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.xml.sax.ContentHandler;
|
||||
|
@ -51,11 +50,10 @@ public interface XmlDao<T> {
|
|||
/**
|
||||
*
|
||||
* @param object
|
||||
* @param domImplementation
|
||||
* @param document
|
||||
* @return
|
||||
*/
|
||||
// XXX think about returning a document, instead of an element, or use document as input
|
||||
public Document serializeToDom(T object, DOMImplementation domImplementation);
|
||||
public Element serializeToDom(T object, Document document);
|
||||
|
||||
/**
|
||||
* @param element
|
||||
|
|
|
@ -19,9 +19,7 @@
|
|||
*/
|
||||
package ch.eitchnet.xmlpers;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
@ -38,19 +36,14 @@ import org.w3c.dom.Element;
|
|||
import org.xml.sax.SAXException;
|
||||
|
||||
import ch.eitchnet.utils.helper.FileHelper;
|
||||
|
||||
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
|
||||
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
|
||||
import ch.eitchnet.utils.helper.XmlHelper;
|
||||
|
||||
/**
|
||||
*@author Robert von Burg <eitch@eitchnet.ch>
|
||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
*/
|
||||
public class XmlFilePersister {
|
||||
|
||||
//
|
||||
private static final String XML_DEFAULT_ENCODING = "UTF-8";
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(XmlFilePersister.class);
|
||||
|
||||
private boolean verbose;
|
||||
|
@ -89,34 +82,16 @@ public class XmlFilePersister {
|
|||
}
|
||||
|
||||
if (this.verbose)
|
||||
XmlFilePersister.logger.info("Persisting " + type + " / " + subType + " / " + id + " to " + pathF.getAbsolutePath() + "...");
|
||||
XmlFilePersister.logger.info("Persisting " + type + " / " + subType + " / " + id + " to "
|
||||
+ pathF.getAbsolutePath() + "...");
|
||||
|
||||
BufferedOutputStream outStream = null;
|
||||
try {
|
||||
|
||||
outStream = new BufferedOutputStream(new FileOutputStream(pathF));
|
||||
|
||||
OutputFormat outputFormat = new OutputFormat("XML", XmlFilePersister.XML_DEFAULT_ENCODING, true);
|
||||
outputFormat.setIndent(1);
|
||||
outputFormat.setIndenting(true);
|
||||
//of.setDoctype(null, null);
|
||||
|
||||
XMLSerializer serializer = new XMLSerializer(outStream, outputFormat);
|
||||
serializer.asDOMSerializer();
|
||||
serializer.serialize(document);
|
||||
outStream.flush();
|
||||
XmlHelper.writeDocument(document, pathF);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new XmlPersistenceExecption("Could not persist object " + type + " / " + subType + " / " + id
|
||||
+ " to " + pathF.getAbsolutePath(), e);
|
||||
} finally {
|
||||
if (outStream != null) {
|
||||
try {
|
||||
outStream.close();
|
||||
} catch (IOException e) {
|
||||
XmlFilePersister.logger.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.verbose)
|
||||
|
@ -137,12 +112,12 @@ public class XmlFilePersister {
|
|||
pathF = this.xmlPathHelper.getPathF(type, id);
|
||||
|
||||
if (this.verbose)
|
||||
XmlFilePersister.logger.info("Remove persistence file for " + type + " / " + subType + " / " + id + " from "
|
||||
+ pathF.getAbsolutePath() + "...");
|
||||
XmlFilePersister.logger.info("Remove persistence file for " + type + " / " + subType + " / " + id
|
||||
+ " from " + pathF.getAbsolutePath() + "...");
|
||||
|
||||
if (!pathF.exists()) {
|
||||
XmlFilePersister.logger.error("Persistence file for " + type + " / " + subType + " / " + id + " does not exist at "
|
||||
+ pathF.getAbsolutePath());
|
||||
XmlFilePersister.logger.error("Persistence file for " + type + " / " + subType + " / " + id
|
||||
+ " does not exist at " + pathF.getAbsolutePath());
|
||||
} else if (!pathF.delete()) {
|
||||
throw new XmlPersistenceExecption("Could not delete persistence file for " + type + " / " + subType + " / "
|
||||
+ id + " at " + pathF.getAbsolutePath());
|
||||
|
@ -421,8 +396,8 @@ public class XmlFilePersister {
|
|||
|
||||
File pathF = this.xmlPathHelper.getPathF(type, subType, id);
|
||||
if (!pathF.exists()) {
|
||||
XmlFilePersister.logger.error("Path for " + type + " / " + subType + " / " + id + " at " + pathF.getAbsolutePath()
|
||||
+ " does not exist, so object does not exist!");
|
||||
XmlFilePersister.logger.error("Path for " + type + " / " + subType + " / " + id + " at "
|
||||
+ pathF.getAbsolutePath() + " does not exist, so object does not exist!");
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@ import org.w3c.dom.DOMImplementation;
|
|||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import ch.eitchnet.utils.helper.XmlHelper;
|
||||
import ch.eitchnet.utils.objectfilter.ITransactionObject;
|
||||
import ch.eitchnet.utils.objectfilter.ObjectFilter;
|
||||
|
||||
|
@ -279,8 +280,10 @@ public class XmlPersistenceTransaction {
|
|||
String subType = dao.getSubType(object);
|
||||
String id = dao.getId(object);
|
||||
|
||||
Document asDom = dao.serializeToDom(object, getDomImpl());
|
||||
this.persister.saveOrUpdate(type, subType, id, asDom);
|
||||
Document doc = XmlHelper.createDocument();
|
||||
Element asDom = dao.serializeToDom(object, doc);
|
||||
doc.appendChild(asDom);
|
||||
this.persister.saveOrUpdate(type, subType, id, doc);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -298,8 +301,10 @@ public class XmlPersistenceTransaction {
|
|||
String subType = dao.getSubType(object);
|
||||
String id = dao.getId(object);
|
||||
|
||||
Document asDom = dao.serializeToDom(object, getDomImpl());
|
||||
this.persister.saveOrUpdate(type, subType, id, asDom);
|
||||
Document doc = XmlHelper.createDocument();
|
||||
Element asDom = dao.serializeToDom(object, doc);
|
||||
doc.appendChild(asDom);
|
||||
this.persister.saveOrUpdate(type, subType, id, doc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,12 +54,12 @@ public class XmlPersistenceTest {
|
|||
public static void init() throws Exception {
|
||||
try {
|
||||
String userDir = System.getProperty("user.dir");
|
||||
String basePath = userDir + "/tmp/testdb";
|
||||
String basePath = userDir + "/target/testdb";
|
||||
File basePathF = new File(basePath);
|
||||
if (!basePathF.exists() && !basePathF.mkdirs())
|
||||
Assert.fail("Could not create temporaray database store in " + basePathF.getAbsolutePath());
|
||||
|
||||
System.setProperty(XmlPersistenceHandler.CONFIG_BASEPATH, "tmp/testdb");
|
||||
System.setProperty(XmlPersistenceHandler.CONFIG_BASEPATH, "target/testdb");
|
||||
System.setProperty(XmlPersistenceHandler.CONFIG_VERBOSE, "true");
|
||||
System.setProperty(XmlPersistenceHandler.CONFIG_DAO_FACTORY_CLASS, MyDaoFactory.class.getName());
|
||||
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
*/
|
||||
package ch.eitchnet.xmlpers.test.impl;
|
||||
|
||||
import org.w3c.dom.DOMImplementation;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Text;
|
||||
|
@ -35,39 +34,25 @@ import ch.eitchnet.xmlpers.XmlDao;
|
|||
*/
|
||||
public class MyClassDao implements XmlDao<MyClass> {
|
||||
|
||||
/**
|
||||
* @see ch.eitchnet.xmlpers.XmlDao#getType(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public String getType(MyClass object) {
|
||||
return MyClass.class.getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ch.eitchnet.xmlpers.XmlDao#getSubType(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public String getSubType(MyClass object) {
|
||||
return object.getType();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ch.eitchnet.xmlpers.XmlDao#getId(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public String getId(MyClass object) {
|
||||
return object.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ch.eitchnet.xmlpers.XmlDao#serializeToDom(java.lang.Object, org.w3c.dom.DOMImplementation)
|
||||
*/
|
||||
@Override
|
||||
public Document serializeToDom(MyClass object, DOMImplementation domImplementation) {
|
||||
public Element serializeToDom(MyClass object, Document document) {
|
||||
|
||||
Document document = domImplementation.createDocument(null, null, null);
|
||||
Element element = document.createElement("MyClass");
|
||||
document.appendChild(element);
|
||||
|
||||
element.setAttribute("id", object.getId());
|
||||
element.setAttribute("type", object.getType());
|
||||
|
@ -77,7 +62,7 @@ public class MyClassDao implements XmlDao<MyClass> {
|
|||
Text textNode = document.createTextNode(object.getName());
|
||||
nameElement.appendChild(textNode);
|
||||
|
||||
return document;
|
||||
return element;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -131,5 +116,4 @@ public class MyClassDao implements XmlDao<MyClass> {
|
|||
throw new RuntimeException("Failed to serialize " + object + " to SAX", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue