[Major] major rewrite, now implemented SAX and DOM methods.

Now DOM implementation is working as well with a test scenario
This commit is contained in:
Robert von Burg 2013-09-15 21:41:00 +02:00
parent 23cbca6f89
commit fed2bffa36
5 changed files with 24 additions and 13 deletions

View File

@ -78,7 +78,11 @@ public abstract class AbstractXmlPersistenceTest {
* if something goes wrong
*/
public static void init(Properties props) throws Exception {
try {
cleanUpDb();
String userDir = System.getProperty("user.dir");
String basePath = userDir + "/target/testdb";
File basePathF = new File(basePath);
@ -90,14 +94,14 @@ public abstract class AbstractXmlPersistenceTest {
AbstractXmlPersistenceTest.logger.info("Initialized persistence handler.");
} catch (Exception e) {
AbstractXmlPersistenceTest.logger.error(e.getMessage(), e);
AbstractXmlPersistenceTest.logger.error(e.getMessage(), e);
throw new RuntimeException("Initialization failed: " + e.getLocalizedMessage(), e);
}
}
@AfterClass
public static void afterClass() {
public static void cleanUpDb() {
String userDir = System.getProperty("user.dir");
String basePath = userDir + "/target/testdb";
File basePathF = new File(basePath);

View File

@ -30,7 +30,6 @@ import ch.eitchnet.xmlpers.test.impl.TestModelDaoFactory;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@Ignore
public class XmlPersistenceDomTest extends AbstractXmlPersistenceTest {
/**

View File

@ -59,9 +59,9 @@ import ch.eitchnet.xmlpers.test.model.Resource;
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class Main {
public class XmlTestMain {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
private static final Logger logger = LoggerFactory.getLogger(XmlTestMain.class);
private static Resource res;

View File

@ -63,7 +63,7 @@ public class BookDomDao extends BookDao {
public Element serializeToDom(Book book, Document document) {
Element element = document.createElement("Book");
document.appendChild(element);
element.setAttribute("id", Long.toString(book.getId()));
element.setAttribute("title", book.getTitle());
element.setAttribute("author", book.getAuthor());

View File

@ -25,6 +25,7 @@ import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import ch.eitchnet.xmlpers.api.DomUtil;
@ -71,6 +72,7 @@ public class ResourceDomDao extends ResourceDao {
public Element serializeToDom(Resource resource, Document document) {
Element element = document.createElement("Resource");
document.appendChild(element);
element.setAttribute("id", resource.getId());
element.setAttribute("name", resource.getName());
@ -79,11 +81,12 @@ public class ResourceDomDao extends ResourceDao {
for (String paramId : resource.getParameterKeySet()) {
Parameter param = resource.getParameterBy(paramId);
Element paramElement = document.createElement("Parameter");
element.appendChild(paramElement);
paramElement.setAttribute("id", param.getId());
paramElement.setAttribute("name", param.getName());
paramElement.setAttribute("type", param.getType());
paramElement.setAttribute("value", param.getType());
paramElement.setAttribute("value", param.getValue());
}
return element;
@ -97,12 +100,17 @@ public class ResourceDomDao extends ResourceDao {
Resource resource = new Resource(id, name, type);
NodeList paramElements = element.getElementsByTagName("Parameter");
for (int i = 0; i < paramElements.getLength(); i++) {
String paramId = element.getAttribute("id");
String paramName = element.getAttribute("name");
String paramType = element.getAttribute("type");
String paramValue = element.getAttribute("value");
NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node item = children.item(i);
if (!item.getNodeName().equals("Parameter"))
continue;
Element paramElement = (Element) item;
String paramId = paramElement.getAttribute("id");
String paramName = paramElement.getAttribute("name");
String paramType = paramElement.getAttribute("type");
String paramValue = paramElement.getAttribute("value");
Parameter param = new Parameter(paramId, paramName, paramType, paramValue);
resource.addParameter(param);