[Major] Added writing of TimedStates in StrolchElementToSaxVisitor

This commit is contained in:
Robert von Burg 2014-11-19 07:52:42 +01:00
parent 3d66c33e7f
commit 765b040cf9
3 changed files with 125 additions and 1 deletions

View File

@ -19,14 +19,20 @@ import static li.strolch.model.StrolchModelConstants.INTERPRETATION_NONE;
import static li.strolch.model.StrolchModelConstants.UOM_NONE;
import java.util.Set;
import java.util.SortedSet;
import li.strolch.model.GroupedParameterizedElement;
import li.strolch.model.Order;
import li.strolch.model.ParameterBag;
import li.strolch.model.Resource;
import li.strolch.model.StrolchElement;
import li.strolch.model.Tags;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.timedstate.StrolchTimedState;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.IValue;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
@ -80,6 +86,33 @@ public abstract class StrolchElementToSaxVisitor {
return attributes;
}
private Attributes attributesFor(StrolchTimedState<IValue<?>> state) {
AttributesImpl attributes = attributesFor((StrolchElement) state);
if (!UOM_NONE.equals(state.getUom())) {
attributes.addAttribute(null, null, Tags.UOM, Tags.CDATA, state.getUom());
}
if (!INTERPRETATION_NONE.equals(state.getInterpretation())) {
attributes.addAttribute(null, null, Tags.INTERPRETATION, Tags.CDATA, state.getInterpretation());
}
if (state.isHidden()) {
attributes.addAttribute(null, null, Tags.HIDDEN, Tags.CDATA, Boolean.toString(state.isHidden()));
}
if (state.getIndex() != 0) {
attributes.addAttribute(null, null, Tags.INDEX, Tags.CDATA, Integer.toString(state.getIndex()));
}
return attributes;
}
private Attributes attributesFor(ITimeValue<IValue<?>> value) {
AttributesImpl attributes = new AttributesImpl();
ISO8601FormatFactory df = ISO8601FormatFactory.getInstance();
attributes.addAttribute(null, null, Tags.TIME, Tags.CDATA, df.formatDate(value.getTime()));
attributes.addAttribute(null, null, Tags.VALUE, Tags.CDATA, value.getValue().getValueAsString());
return attributes;
}
protected void toSax(GroupedParameterizedElement parameterizedElement) throws SAXException {
Set<String> bagKeySet = parameterizedElement.getParameterBagKeySet();
for (String bagKey : bagKeySet) {
@ -96,4 +129,21 @@ public abstract class StrolchElementToSaxVisitor {
this.contentHandler.endElement(null, null, Tags.PARAMETER_BAG);
}
}
protected void toSax(Resource resource) throws SAXException {
toSax((GroupedParameterizedElement) resource);
Set<String> stateKeySet = resource.getTimedStateKeySet();
for (String stateKey : stateKeySet) {
StrolchTimedState<IValue<?>> state = resource.getTimedState(stateKey);
this.contentHandler.startElement(null, null, Tags.TIMED_STATE, attributesFor(state));
SortedSet<ITimeValue<IValue<?>>> values = state.getTimeEvolution().getValues();
for (ITimeValue<IValue<?>> value : values) {
this.contentHandler.startElement(null, null, Tags.VALUE, attributesFor(value));
this.contentHandler.endElement(null, null, Tags.VALUE);
}
this.contentHandler.endElement(null, null, Tags.TIMED_STATE);
}
}
}

View File

@ -27,7 +27,6 @@ import org.w3c.dom.Element;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
@SuppressWarnings("nls")
public class XmlToDomTest extends ModelTest {

View File

@ -0,0 +1,75 @@
/*
* 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.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import li.strolch.model.visitor.OrderDeepEqualsVisitor;
import li.strolch.model.visitor.ResourceDeepEqualsVisitor;
import li.strolch.model.xml.OrderToSaxVisitor;
import li.strolch.model.xml.ResourceToSaxVisitor;
import li.strolch.model.xml.SimpleStrolchElementListener;
import li.strolch.model.xml.XmlModelSaxReader;
import org.junit.Test;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@SuppressWarnings("nls")
public class XmlToSaxTest extends ModelTest {
@Test
public void shouldFormatAndParseOrder() {
Order order = ModelGenerator.createOrder("@1", "My Order 1", "MyOrder");
SimpleStrolchElementListener listener = new SimpleStrolchElementListener();
XmlModelSaxReader saxReader = new XmlModelSaxReader(listener);
OrderToSaxVisitor domVisitor = new OrderToSaxVisitor(saxReader);
domVisitor.visit(order);
assertEquals(1, listener.getOrders().size());
assertNull(listener.getResources());
Order parsedOrder = listener.getOrders().get(0);
OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(order);
visitor.visit(parsedOrder);
assertTrue("To DOM and back should equal same Order:\n" + visitor.getMismatchedLocators(), visitor.isEqual());
}
@Test
public void shouldFormatAndParseResource() {
Resource resource = ModelGenerator.createResource("@1", "My Resource 1", "MyResource");
SimpleStrolchElementListener listener = new SimpleStrolchElementListener();
XmlModelSaxReader saxReader = new XmlModelSaxReader(listener);
ResourceToSaxVisitor domVisitor = new ResourceToSaxVisitor(saxReader);
domVisitor.visit(resource);
assertEquals(1, listener.getResources().size());
assertNull(listener.getOrders());
Resource parsedResource = listener.getResources().get(0);
ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(resource);
visitor.visit(parsedResource);
assertTrue("To DOM and back should equal same Resource:\n" + visitor.getMismatchedLocators(), visitor.isEqual());
}
}