[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:
parent
23cbca6f89
commit
fed2bffa36
|
@ -78,7 +78,11 @@ public abstract class AbstractXmlPersistenceTest {
|
||||||
* if something goes wrong
|
* if something goes wrong
|
||||||
*/
|
*/
|
||||||
public static void init(Properties props) throws Exception {
|
public static void init(Properties props) throws Exception {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
cleanUpDb();
|
||||||
|
|
||||||
String userDir = System.getProperty("user.dir");
|
String userDir = System.getProperty("user.dir");
|
||||||
String basePath = userDir + "/target/testdb";
|
String basePath = userDir + "/target/testdb";
|
||||||
File basePathF = new File(basePath);
|
File basePathF = new File(basePath);
|
||||||
|
@ -90,14 +94,14 @@ public abstract class AbstractXmlPersistenceTest {
|
||||||
AbstractXmlPersistenceTest.logger.info("Initialized persistence handler.");
|
AbstractXmlPersistenceTest.logger.info("Initialized persistence handler.");
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
AbstractXmlPersistenceTest.logger.error(e.getMessage(), e);
|
|
||||||
|
|
||||||
|
AbstractXmlPersistenceTest.logger.error(e.getMessage(), e);
|
||||||
throw new RuntimeException("Initialization failed: " + e.getLocalizedMessage(), e);
|
throw new RuntimeException("Initialization failed: " + e.getLocalizedMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
public static void afterClass() {
|
public static void cleanUpDb() {
|
||||||
String userDir = System.getProperty("user.dir");
|
String userDir = System.getProperty("user.dir");
|
||||||
String basePath = userDir + "/target/testdb";
|
String basePath = userDir + "/target/testdb";
|
||||||
File basePathF = new File(basePath);
|
File basePathF = new File(basePath);
|
||||||
|
|
|
@ -30,7 +30,6 @@ import ch.eitchnet.xmlpers.test.impl.TestModelDaoFactory;
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*/
|
*/
|
||||||
@Ignore
|
|
||||||
public class XmlPersistenceDomTest extends AbstractXmlPersistenceTest {
|
public class XmlPersistenceDomTest extends AbstractXmlPersistenceTest {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -59,9 +59,9 @@ import ch.eitchnet.xmlpers.test.model.Resource;
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @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;
|
private static Resource res;
|
||||||
|
|
|
@ -63,7 +63,7 @@ public class BookDomDao extends BookDao {
|
||||||
public Element serializeToDom(Book book, Document document) {
|
public Element serializeToDom(Book book, Document document) {
|
||||||
|
|
||||||
Element element = document.createElement("Book");
|
Element element = document.createElement("Book");
|
||||||
|
document.appendChild(element);
|
||||||
element.setAttribute("id", Long.toString(book.getId()));
|
element.setAttribute("id", Long.toString(book.getId()));
|
||||||
element.setAttribute("title", book.getTitle());
|
element.setAttribute("title", book.getTitle());
|
||||||
element.setAttribute("author", book.getAuthor());
|
element.setAttribute("author", book.getAuthor());
|
||||||
|
|
|
@ -25,6 +25,7 @@ import javax.xml.parsers.DocumentBuilder;
|
||||||
|
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
import org.w3c.dom.NodeList;
|
import org.w3c.dom.NodeList;
|
||||||
|
|
||||||
import ch.eitchnet.xmlpers.api.DomUtil;
|
import ch.eitchnet.xmlpers.api.DomUtil;
|
||||||
|
@ -71,6 +72,7 @@ public class ResourceDomDao extends ResourceDao {
|
||||||
public Element serializeToDom(Resource resource, Document document) {
|
public Element serializeToDom(Resource resource, Document document) {
|
||||||
|
|
||||||
Element element = document.createElement("Resource");
|
Element element = document.createElement("Resource");
|
||||||
|
document.appendChild(element);
|
||||||
|
|
||||||
element.setAttribute("id", resource.getId());
|
element.setAttribute("id", resource.getId());
|
||||||
element.setAttribute("name", resource.getName());
|
element.setAttribute("name", resource.getName());
|
||||||
|
@ -79,11 +81,12 @@ public class ResourceDomDao extends ResourceDao {
|
||||||
for (String paramId : resource.getParameterKeySet()) {
|
for (String paramId : resource.getParameterKeySet()) {
|
||||||
Parameter param = resource.getParameterBy(paramId);
|
Parameter param = resource.getParameterBy(paramId);
|
||||||
Element paramElement = document.createElement("Parameter");
|
Element paramElement = document.createElement("Parameter");
|
||||||
|
element.appendChild(paramElement);
|
||||||
|
|
||||||
paramElement.setAttribute("id", param.getId());
|
paramElement.setAttribute("id", param.getId());
|
||||||
paramElement.setAttribute("name", param.getName());
|
paramElement.setAttribute("name", param.getName());
|
||||||
paramElement.setAttribute("type", param.getType());
|
paramElement.setAttribute("type", param.getType());
|
||||||
paramElement.setAttribute("value", param.getType());
|
paramElement.setAttribute("value", param.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
return element;
|
return element;
|
||||||
|
@ -97,12 +100,17 @@ public class ResourceDomDao extends ResourceDao {
|
||||||
|
|
||||||
Resource resource = new Resource(id, name, type);
|
Resource resource = new Resource(id, name, type);
|
||||||
|
|
||||||
NodeList paramElements = element.getElementsByTagName("Parameter");
|
NodeList children = element.getChildNodes();
|
||||||
for (int i = 0; i < paramElements.getLength(); i++) {
|
for (int i = 0; i < children.getLength(); i++) {
|
||||||
String paramId = element.getAttribute("id");
|
Node item = children.item(i);
|
||||||
String paramName = element.getAttribute("name");
|
if (!item.getNodeName().equals("Parameter"))
|
||||||
String paramType = element.getAttribute("type");
|
continue;
|
||||||
String paramValue = element.getAttribute("value");
|
|
||||||
|
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);
|
Parameter param = new Parameter(paramId, paramName, paramType, paramValue);
|
||||||
resource.addParameter(param);
|
resource.addParameter(param);
|
||||||
|
|
Loading…
Reference in New Issue