[Major] Added SAX XML persistence incl. DOM for Activities

This commit is contained in:
Robert von Burg 2015-07-08 08:16:30 +02:00
parent 203013c93d
commit 5822c231f0
12 changed files with 388 additions and 15 deletions

@ -1 +1 @@
Subproject commit f72ff76b406f2675cf194174aa35872d0c969ab6
Subproject commit 6659b90b83aa253e79e9a705c42e9c0e3b3fd63c

View File

@ -36,6 +36,7 @@ import li.strolch.model.timevalue.IValue;
import li.strolch.model.timevalue.impl.ValueChange;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import ch.eitchnet.utils.dbc.DBC;
@ -71,7 +72,11 @@ public class StrolchElementFromDomVisitor {
NodeList childNodes = resourceElement.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Element timedStateElem = (Element) childNodes.item(i);
Node item = childNodes.item(i);
if (!(item instanceof Element))
continue;
Element timedStateElem = (Element) item;
if (!timedStateElem.getNodeName().equals(Tags.TIMED_STATE))
continue;
@ -113,7 +118,11 @@ public class StrolchElementFromDomVisitor {
NodeList timeValueElems = timedStateElem.getChildNodes();
for (int j = 0; j < timeValueElems.getLength(); j++) {
Element timeValueElem = (Element) timeValueElems.item(j);
Node timeValueItem = timeValueElems.item(j);
if (!(timeValueItem instanceof Element))
continue;
Element timeValueElem = (Element) timeValueItem;
if (!timeValueElem.getNodeName().equals(Tags.VALUE))
continue;
@ -151,7 +160,11 @@ public class StrolchElementFromDomVisitor {
NodeList bags = element.getChildNodes();
for (int i = 0; i < bags.getLength(); i++) {
Element bagElement = (Element) bags.item(i);
Node item = bags.item(i);
if (!(item instanceof Element))
continue;
Element bagElement = (Element) item;
if (!bagElement.getNodeName().equals(Tags.PARAMETER_BAG))
continue;
@ -170,7 +183,11 @@ public class StrolchElementFromDomVisitor {
// add all the parameters
NodeList parameterElements = element.getChildNodes();
for (int i = 0; i < parameterElements.getLength(); i++) {
Element paramElement = (Element) parameterElements.item(i);
Node item = parameterElements.item(i);
if (!(item instanceof Element))
continue;
Element paramElement = (Element) item;
if (!paramElement.getNodeName().equals(Tags.PARAMETER))
continue;
@ -224,7 +241,11 @@ public class StrolchElementFromDomVisitor {
NodeList childNodes = activityElement.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Element childElem = (Element) childNodes.item(i);
Node item = childNodes.item(i);
if (!(item instanceof Element))
continue;
Element childElem = (Element) item;
switch (childElem.getNodeName()) {
case Tags.ACTIVITY:
@ -259,7 +280,11 @@ public class StrolchElementFromDomVisitor {
NodeList valueChangeNodes = element.getChildNodes();
for (int i = 0; i < valueChangeNodes.getLength(); i++) {
Element valueChangeElem = (Element) valueChangeNodes.item(i);
Node item = valueChangeNodes.item(i);
if (!(item instanceof Element))
continue;
Element valueChangeElem = (Element) item;
if (!valueChangeElem.getNodeName().equals(Tags.VALUE_CHANGE))
continue;

View File

@ -0,0 +1,42 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.xml.model;
import li.strolch.model.Tags;
import li.strolch.model.activity.Activity;
import ch.eitchnet.xmlpers.api.PersistenceContext;
import ch.eitchnet.xmlpers.api.PersistenceContextFactory;
import ch.eitchnet.xmlpers.objref.IdOfSubTypeRef;
import ch.eitchnet.xmlpers.objref.ObjectRef;
import ch.eitchnet.xmlpers.objref.ObjectReferenceCache;
public class ActivityContextFactory implements PersistenceContextFactory<Activity> {
@Override
public PersistenceContext<Activity> createCtx(ObjectRef objectRef) {
PersistenceContext<Activity> ctx = new PersistenceContext<>(objectRef);
ctx.setParserFactory(new ActivityParserFactory());
return ctx;
}
@Override
public PersistenceContext<Activity> createCtx(ObjectReferenceCache objectRefCache, Activity t) {
IdOfSubTypeRef objectRef = objectRefCache.getIdOfSubTypeRef(Tags.ACTIVITY, t.getType(), t.getId());
PersistenceContext<Activity> ctx = createCtx(objectRef);
ctx.setObject(t);
return ctx;
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.xml.model;
import li.strolch.model.activity.Activity;
import li.strolch.model.xml.ActivityFromDomVisitor;
import li.strolch.model.xml.ActivityToDomVisitor;
import org.w3c.dom.Document;
import ch.eitchnet.xmlpers.api.DomParser;
public class ActivityDomParser implements DomParser<Activity> {
private Activity activity;
@Override
public Activity getObject() {
return this.activity;
}
@Override
public void setObject(Activity activity) {
this.activity = activity;
}
@Override
public Document toDom() {
ActivityToDomVisitor activityDomVisitor = new ActivityToDomVisitor();
activityDomVisitor.visit(this.activity);
return activityDomVisitor.getDocument();
}
@Override
public void fromDom(Document document) {
Activity activity = new ActivityFromDomVisitor().visit(document);
this.activity = activity;
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.xml.model;
import li.strolch.model.activity.Activity;
import ch.eitchnet.xmlpers.api.DomParser;
import ch.eitchnet.xmlpers.api.ParserFactory;
import ch.eitchnet.xmlpers.api.SaxParser;
public class ActivityParserFactory implements ParserFactory<Activity> {
@Override
public DomParser<Activity> getDomParser() {
return new ActivityDomParser();
}
@Override
public SaxParser<Activity> getSaxParser() {
return new ActivitySaxParser();
}
}

View File

@ -0,0 +1,74 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.xml.model;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.activity.Activity;
import li.strolch.model.xml.ActivityToSaxWriterVisitor;
import li.strolch.model.xml.StrolchElementListener;
import li.strolch.model.xml.XmlModelSaxReader;
import org.xml.sax.helpers.DefaultHandler;
import ch.eitchnet.xmlpers.api.SaxParser;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ActivitySaxParser implements SaxParser<Activity> {
protected Activity activity;
@Override
public Activity getObject() {
return this.activity;
}
@Override
public void setObject(Activity activity) {
this.activity = activity;
}
@Override
public DefaultHandler getDefaultHandler() {
return new XmlModelSaxReader(new StrolchElementListener() {
@Override
public void notifyResource(Resource resource) {
throw new IllegalArgumentException("Only expect Activities!");
}
@Override
public void notifyOrder(Order order) {
throw new IllegalArgumentException("Only expect Activities!");
}
@Override
public void notifyActivity(Activity activity) {
ActivitySaxParser.this.activity = activity;
}
});
}
@Override
public void write(XMLStreamWriter xmlWriter) throws XMLStreamException {
new ActivityToSaxWriterVisitor(xmlWriter);
}
}

View File

@ -16,10 +16,10 @@
package li.strolch.persistence.xml.model;
import li.strolch.model.Order;
import li.strolch.model.xml.OrderFromDomVisitor;
import li.strolch.model.xml.OrderToDomVisitor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ch.eitchnet.xmlpers.api.DomParser;
@ -46,8 +46,7 @@ public class OrderDomParser implements DomParser<Order> {
@Override
public void fromDom(Document document) {
Element rootElement = document.getDocumentElement();
Order order = new Order(rootElement);
Order order = new OrderFromDomVisitor().visit(document);
this.order = order;
}
}

View File

@ -29,6 +29,6 @@ public class OrderParserFactory implements ParserFactory<Order> {
@Override
public SaxParser<Order> getSaxParser() {
throw new UnsupportedOperationException("Not yet implemented!"); //$NON-NLS-1$
return new OrderSaxParser();
}
}

View File

@ -0,0 +1,74 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.xml.model;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.activity.Activity;
import li.strolch.model.xml.OrderToSaxWriterVisitor;
import li.strolch.model.xml.StrolchElementListener;
import li.strolch.model.xml.XmlModelSaxReader;
import org.xml.sax.helpers.DefaultHandler;
import ch.eitchnet.xmlpers.api.SaxParser;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class OrderSaxParser implements SaxParser<Order> {
protected Order order;
@Override
public Order getObject() {
return this.order;
}
@Override
public void setObject(Order order) {
this.order = order;
}
@Override
public DefaultHandler getDefaultHandler() {
return new XmlModelSaxReader(new StrolchElementListener() {
@Override
public void notifyResource(Resource resource) {
throw new IllegalArgumentException("Only expect Orders!");
}
@Override
public void notifyOrder(Order order) {
OrderSaxParser.this.order = order;
}
@Override
public void notifyActivity(Activity activity) {
throw new IllegalArgumentException("Only expect Orders!");
}
});
}
@Override
public void write(XMLStreamWriter xmlWriter) throws XMLStreamException {
new OrderToSaxWriterVisitor(xmlWriter);
}
}

View File

@ -16,10 +16,10 @@
package li.strolch.persistence.xml.model;
import li.strolch.model.Resource;
import li.strolch.model.xml.ResourceFromDomVisitor;
import li.strolch.model.xml.ResourceToDomVisitor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ch.eitchnet.xmlpers.api.DomParser;
@ -46,8 +46,7 @@ public class ResourceDomParser implements DomParser<Resource> {
@Override
public void fromDom(Document document) {
Element rootElement = document.getDocumentElement();
Resource resource = new Resource(rootElement);
Resource resource = new ResourceFromDomVisitor().visit(document);
this.resource = resource;
}
}

View File

@ -29,6 +29,6 @@ public class ResourceParserFactory implements ParserFactory<Resource> {
@Override
public SaxParser<Resource> getSaxParser() {
throw new UnsupportedOperationException("Not yet implemented!"); //$NON-NLS-1$
return new ResourceSaxParser();
}
}

View File

@ -0,0 +1,74 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.xml.model;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.activity.Activity;
import li.strolch.model.xml.ResourceToSaxWriterVisitor;
import li.strolch.model.xml.StrolchElementListener;
import li.strolch.model.xml.XmlModelSaxReader;
import org.xml.sax.helpers.DefaultHandler;
import ch.eitchnet.xmlpers.api.SaxParser;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ResourceSaxParser implements SaxParser<Resource> {
protected Resource resource;
@Override
public Resource getObject() {
return this.resource;
}
@Override
public void setObject(Resource resource) {
this.resource = resource;
}
@Override
public DefaultHandler getDefaultHandler() {
return new XmlModelSaxReader(new StrolchElementListener() {
@Override
public void notifyResource(Resource resource) {
ResourceSaxParser.this.resource = resource;
}
@Override
public void notifyOrder(Order order) {
throw new IllegalArgumentException("Only expect Resources!");
}
@Override
public void notifyActivity(Activity activity) {
throw new IllegalArgumentException("Only expect Resources!");
}
});
}
@Override
public void write(XMLStreamWriter xmlWriter) throws XMLStreamException {
new ResourceToSaxWriterVisitor(xmlWriter);
}
}