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