From 765b040cf932efd9e3b4f283f5fd0f93bef3bed7 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 19 Nov 2014 07:52:42 +0100 Subject: [PATCH] [Major] Added writing of TimedStates in StrolchElementToSaxVisitor --- .../model/xml/StrolchElementToSaxVisitor.java | 50 +++++++++++++ .../java/li/strolch/model/XmlToDomTest.java | 1 - .../java/li/strolch/model/XmlToSaxTest.java | 75 +++++++++++++++++++ 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 li.strolch.model/src/test/java/li/strolch/model/XmlToSaxTest.java diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToSaxVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToSaxVisitor.java index 6f14e598b..78c468c26 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToSaxVisitor.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToSaxVisitor.java @@ -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> 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> 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 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 stateKeySet = resource.getTimedStateKeySet(); + for (String stateKey : stateKeySet) { + StrolchTimedState> state = resource.getTimedState(stateKey); + this.contentHandler.startElement(null, null, Tags.TIMED_STATE, attributesFor(state)); + + SortedSet>> values = state.getTimeEvolution().getValues(); + for (ITimeValue> 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); + } + } } diff --git a/li.strolch.model/src/test/java/li/strolch/model/XmlToDomTest.java b/li.strolch.model/src/test/java/li/strolch/model/XmlToDomTest.java index ecbd00fa8..84f3438f0 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/XmlToDomTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/XmlToDomTest.java @@ -27,7 +27,6 @@ import org.w3c.dom.Element; /** * @author Robert von Burg - * */ @SuppressWarnings("nls") public class XmlToDomTest extends ModelTest { diff --git a/li.strolch.model/src/test/java/li/strolch/model/XmlToSaxTest.java b/li.strolch.model/src/test/java/li/strolch/model/XmlToSaxTest.java new file mode 100644 index 000000000..d37569c63 --- /dev/null +++ b/li.strolch.model/src/test/java/li/strolch/model/XmlToSaxTest.java @@ -0,0 +1,75 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 + */ +@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()); + } +}