diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToDomVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToDomVisitor.java deleted file mode 100644 index c8b4f3d60..000000000 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToDomVisitor.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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.xml; - -import javax.xml.parsers.DocumentBuilder; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -import li.strolch.model.activity.Action; -import li.strolch.model.activity.Activity; -import li.strolch.model.visitor.ActivityVisitor; -import li.strolch.utils.helper.DomUtil; - -/** - * @author Robert von Burg - */ -public class ActivityToDomVisitor extends StrolchElementToDomVisitor implements ActivityVisitor { - - @Override - public Document visit(Activity activity) { - DocumentBuilder documentBuilder = DomUtil.createDocumentBuilder(); - this.document = documentBuilder.getDOMImplementation().createDocument(null, null, null); - - Element asDom = toDom(activity); - document.appendChild(asDom); - return this.document; - } - - @Override - public Element toDom(Action action) { - return super.toDom(action); - } - - @Override - public Element toDom(Activity activity) { - return super.toDom(activity); - } -} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToSaxVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToSaxVisitor.java deleted file mode 100644 index 898c3befc..000000000 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToSaxVisitor.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2015 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.xml; - -import java.text.MessageFormat; - -import li.strolch.model.activity.Activity; -import li.strolch.model.visitor.ActivityVisitor; - -import org.xml.sax.ContentHandler; - -/** - * @author Robert von Burg - */ -public class ActivityToSaxVisitor extends StrolchElementToSaxVisitor implements ActivityVisitor { - - public ActivityToSaxVisitor(ContentHandler contentHandler) { - super(contentHandler); - } - - @Override - public Void visit(Activity activity) { - try { - - toSax(activity); - - } catch (Exception e) { - String msg = "Failed to transform Activity {0} to XML due to {1}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, activity.getLocator(), e.getMessage()); - throw new RuntimeException(msg, e); - } - - return null; - } -} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToSaxWriterVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToSaxWriterVisitor.java deleted file mode 100644 index 74e78f127..000000000 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToSaxWriterVisitor.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2015 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.xml; - -import java.text.MessageFormat; - -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamWriter; - -import li.strolch.exception.StrolchException; -import li.strolch.model.activity.Activity; -import li.strolch.model.visitor.ActivityVisitor; - -/** - * @author Robert von Burg - */ -public class ActivityToSaxWriterVisitor extends StrolchElementToSaxWriterVisitor implements ActivityVisitor { - - public ActivityToSaxWriterVisitor(XMLStreamWriter writer) { - super(writer); - } - - @Override - public Void visit(Activity activity) { - try { - writeElement(activity); - this.writer.flush(); - } catch (XMLStreamException e) { - String msg = "Failed to write Activity {0} due to {1}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, activity.getLocator(), e.getMessage()); - throw new StrolchException(msg, e); - } - - return null; - } -} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToDomVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToDomVisitor.java deleted file mode 100644 index b48efe2dc..000000000 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToDomVisitor.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.xml; - -import javax.xml.parsers.DocumentBuilder; - -import li.strolch.model.Order; -import li.strolch.model.visitor.OrderVisitor; -import li.strolch.utils.helper.DomUtil; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -/** - * @author Robert von Burg - */ -public class OrderToDomVisitor extends StrolchElementToDomVisitor implements OrderVisitor { - - @Override - public Document visit(Order order) { - DocumentBuilder documentBuilder = DomUtil.createDocumentBuilder(); - this.document = documentBuilder.getDOMImplementation().createDocument(null, null, null); - - Element asDom = toDom(order); - document.appendChild(asDom); - return this.document; - } -} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToSaxVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToSaxVisitor.java deleted file mode 100644 index 36ef50a99..000000000 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToSaxVisitor.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.xml; - -import java.text.MessageFormat; - -import li.strolch.model.Order; -import li.strolch.model.visitor.OrderVisitor; - -import org.xml.sax.ContentHandler; -import org.xml.sax.SAXException; - -/** - * @author Robert von Burg - */ -public class OrderToSaxVisitor extends StrolchElementToSaxVisitor implements OrderVisitor { - - public OrderToSaxVisitor(ContentHandler contentHandler) { - super(contentHandler); - } - - @Override - public Void visit(Order order) { - try { - - toSax(order); - - } catch (SAXException e) { - String msg = "Failed to transform Order {0} to XML due to {1}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, order.getLocator(), e.getMessage()); - throw new RuntimeException(msg, e); - } - - return null; - } -} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToSaxWriterVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToSaxWriterVisitor.java deleted file mode 100644 index bf1392c3f..000000000 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToSaxWriterVisitor.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.xml; - -import java.text.MessageFormat; - -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamWriter; - -import li.strolch.exception.StrolchException; -import li.strolch.model.Order; -import li.strolch.model.visitor.OrderVisitor; - -/** - * @author Robert von Burg - */ -public class OrderToSaxWriterVisitor extends StrolchElementToSaxWriterVisitor implements OrderVisitor { - - public OrderToSaxWriterVisitor(XMLStreamWriter writer) { - super(writer); - } - - @Override - public Void visit(Order order) { - try { - writeElement(order); - this.writer.flush(); - } catch (XMLStreamException e) { - String msg = "Failed to write Order {0} due to {1}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, order.getLocator(), e.getMessage()); - throw new StrolchException(msg, e); - } - - return null; - } -} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToXmlStringVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToXmlStringVisitor.java deleted file mode 100644 index 62688bb5a..000000000 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToXmlStringVisitor.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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.xml; - -import java.io.StringWriter; - -import javanet.staxutils.IndentingXMLStreamWriter; - -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamWriter; - -import li.strolch.model.Order; -import li.strolch.model.StrolchModelConstants; -import li.strolch.model.visitor.OrderVisitor; -import li.strolch.utils.dbc.DBC; - -/** - * @author Robert von Burg - */ -public class OrderToXmlStringVisitor implements OrderVisitor { - - @Override - public String visit(Order element) { - DBC.PRE.assertNotNull("Order my not be null!", element); - try { - StringWriter stringWriter = new StringWriter(); - XMLOutputFactory factory = XMLOutputFactory.newInstance(); - XMLStreamWriter writer = factory.createXMLStreamWriter(stringWriter); - writer = new IndentingXMLStreamWriter(writer); - - // start document - writer.writeStartDocument(StrolchModelConstants.DEFAULT_ENCODING, StrolchModelConstants.DEFAULT_XML_VERSION); - new OrderToSaxWriterVisitor(writer).visit(element); - writer.writeEndDocument(); - - return stringWriter.toString(); - } catch (Exception e) { - throw new RuntimeException("Failed to format Element " + element.getLocator() + " to xml string due to " - + e.getMessage(), e); - } - } -} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToDomVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToDomVisitor.java deleted file mode 100644 index 12074f61f..000000000 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToDomVisitor.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.xml; - -import javax.xml.parsers.DocumentBuilder; - -import li.strolch.model.Resource; -import li.strolch.model.visitor.ResourceVisitor; -import li.strolch.utils.helper.DomUtil; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -/** - * @author Robert von Burg - */ -public class ResourceToDomVisitor extends StrolchElementToDomVisitor implements ResourceVisitor { - - @Override - public Document visit(Resource resource) { - DocumentBuilder documentBuilder = DomUtil.createDocumentBuilder(); - this.document = documentBuilder.getDOMImplementation().createDocument(null, null, null); - - Element asDom = toDom(resource); - document.appendChild(asDom); - return this.document; - } -} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToSaxVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToSaxVisitor.java deleted file mode 100644 index 29bfec648..000000000 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToSaxVisitor.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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.xml; - -import java.text.MessageFormat; - -import li.strolch.model.Resource; -import li.strolch.model.visitor.ResourceVisitor; - -import org.xml.sax.ContentHandler; - -/** - * @author Robert von Burg - */ -public class ResourceToSaxVisitor extends StrolchElementToSaxVisitor implements ResourceVisitor { - - public ResourceToSaxVisitor(ContentHandler contentHandler) { - super(contentHandler); - } - - @Override - public Void visit(Resource res) { - try { - - toSax(res); - - } catch (Exception e) { - String msg = "Failed to transform Resource {0} to XML due to {1}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, res.getLocator(), e.getMessage()); - throw new RuntimeException(msg, e); - } - - return null; - } -} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToSaxWriterVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToSaxWriterVisitor.java deleted file mode 100644 index be8c29078..000000000 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToSaxWriterVisitor.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.xml; - -import java.text.MessageFormat; - -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamWriter; - -import li.strolch.exception.StrolchException; -import li.strolch.model.Resource; -import li.strolch.model.visitor.ResourceVisitor; - -/** - * @author Robert von Burg - */ -public class ResourceToSaxWriterVisitor extends StrolchElementToSaxWriterVisitor implements ResourceVisitor { - - public ResourceToSaxWriterVisitor(XMLStreamWriter writer) { - super(writer); - } - - @Override - public Void visit(Resource resource) { - try { - writeElement(resource); - this.writer.flush(); - } catch (XMLStreamException e) { - String msg = "Failed to write Resource {0} due to {1}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, resource.getLocator(), e.getMessage()); - throw new StrolchException(msg, e); - } - - return null; - } -} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToXmlStringVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToXmlStringVisitor.java deleted file mode 100644 index 17bf53c01..000000000 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToXmlStringVisitor.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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.xml; - -import java.io.StringWriter; - -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamWriter; - -import javanet.staxutils.IndentingXMLStreamWriter; -import li.strolch.model.Resource; -import li.strolch.model.StrolchModelConstants; -import li.strolch.model.visitor.ResourceVisitor; -import li.strolch.utils.dbc.DBC; - -/** - * @author Robert von Burg - */ -public class ResourceToXmlStringVisitor implements ResourceVisitor { - - @Override - public String visit(Resource element) { - DBC.PRE.assertNotNull("Resource my not be null!", element); - try { - StringWriter stringWriter = new StringWriter(); - XMLOutputFactory factory = XMLOutputFactory.newInstance(); - XMLStreamWriter writer = factory.createXMLStreamWriter(stringWriter); - writer = new IndentingXMLStreamWriter(writer); - - // start document - writer.writeStartDocument(StrolchModelConstants.DEFAULT_ENCODING, - StrolchModelConstants.DEFAULT_XML_VERSION); - new ResourceToSaxWriterVisitor(writer).visit(element); - writer.writeEndDocument(); - - return stringWriter.toString(); - } catch (Exception e) { - throw new RuntimeException( - "Failed to format Element " + element.getLocator() + " to xml string due to " + e.getMessage(), e); - } - } -} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java index 06ba2e856..b649d52cb 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java @@ -19,6 +19,8 @@ import java.util.Iterator; import java.util.Map.Entry; import java.util.SortedSet; +import javax.xml.parsers.DocumentBuilder; + import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -42,12 +44,14 @@ import li.strolch.model.timedstate.StrolchTimedState; import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.IValue; import li.strolch.model.timevalue.IValueChange; +import li.strolch.model.visitor.StrolchRootElementVisitor; +import li.strolch.utils.helper.DomUtil; import li.strolch.utils.iso8601.ISO8601FormatFactory; /** * @author Robert von Burg */ -public class StrolchElementToDomVisitor { +public class StrolchElementToDomVisitor implements StrolchRootElementVisitor { protected Document document; @@ -55,7 +59,37 @@ public class StrolchElementToDomVisitor { return this.document; } - protected Element toDom(Order order) { + @Override + public Document visitActivity(Activity activity) { + DocumentBuilder documentBuilder = DomUtil.createDocumentBuilder(); + this.document = documentBuilder.getDOMImplementation().createDocument(null, null, null); + + Element asDom = toDom(activity); + document.appendChild(asDom); + return this.document; + } + + @Override + public Document visitOrder(Order order) { + DocumentBuilder documentBuilder = DomUtil.createDocumentBuilder(); + this.document = documentBuilder.getDOMImplementation().createDocument(null, null, null); + + Element asDom = toDom(order); + document.appendChild(asDom); + return this.document; + } + + @Override + public Document visitResource(Resource resource) { + DocumentBuilder documentBuilder = DomUtil.createDocumentBuilder(); + this.document = documentBuilder.getDOMImplementation().createDocument(null, null, null); + + Element asDom = toDom(resource); + document.appendChild(asDom); + return this.document; + } + + public Element toDom(Order order) { Element asDom = document.createElement(Tags.ORDER); asDom.setAttribute(Tags.DATE, ISO8601FormatFactory.getInstance().formatDate(order.getDate())); @@ -66,7 +100,7 @@ public class StrolchElementToDomVisitor { return asDom; } - protected Element toDom(Resource resource) { + public Element toDom(Resource resource) { Element asDom = document.createElement(Tags.RESOURCE); fillElement(asDom, (StrolchRootElement) resource); @@ -82,7 +116,7 @@ public class StrolchElementToDomVisitor { return asDom; } - protected Element toDom(Activity activity) { + public Element toDom(Activity activity) { Element element = document.createElement(Tags.ACTIVITY); element.setAttribute(Tags.TIME_ORDERING, activity.getTimeOrdering().getName()); @@ -105,7 +139,7 @@ public class StrolchElementToDomVisitor { return element; } - protected Element toDom(Action action) { + public Element toDom(Action action) { Element element = document.createElement(Tags.ACTION); fillElement(element, action); 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 608ac18d5..dd0a910a9 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 @@ -18,6 +18,7 @@ package li.strolch.model.xml; import static li.strolch.model.StrolchModelConstants.INTERPRETATION_NONE; import static li.strolch.model.StrolchModelConstants.UOM_NONE; +import java.text.MessageFormat; import java.util.Iterator; import java.util.Map.Entry; import java.util.Set; @@ -46,19 +47,65 @@ import li.strolch.model.timedstate.StrolchTimedState; import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.IValue; import li.strolch.model.timevalue.IValueChange; +import li.strolch.model.visitor.StrolchRootElementVisitor; import li.strolch.utils.iso8601.ISO8601FormatFactory; /** * @author Robert von Burg */ -public abstract class StrolchElementToSaxVisitor { +public class StrolchElementToSaxVisitor implements StrolchRootElementVisitor { protected ContentHandler contentHandler; - protected StrolchElementToSaxVisitor(ContentHandler contentHandler) { + public StrolchElementToSaxVisitor(ContentHandler contentHandler) { this.contentHandler = contentHandler; } + @Override + public Void visitActivity(Activity activity) { + try { + + toSax(activity); + + } catch (Exception e) { + String msg = "Failed to transform Activity {0} to XML due to {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, activity.getLocator(), e.getMessage()); + throw new RuntimeException(msg, e); + } + + return null; + } + + @Override + public Void visitOrder(Order order) { + try { + + toSax(order); + + } catch (SAXException e) { + String msg = "Failed to transform Order {0} to XML due to {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, order.getLocator(), e.getMessage()); + throw new RuntimeException(msg, e); + } + + return null; + } + + @Override + public Void visitResource(Resource resource) { + try { + + toSax(resource); + + } catch (Exception e) { + String msg = "Failed to transform Resource {0} to XML due to {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, resource.getLocator(), e.getMessage()); + throw new RuntimeException(msg, e); + } + + return null; + } + protected void toSax(Resource resource) throws SAXException { this.contentHandler.startElement(null, null, Tags.RESOURCE, attributesFor(resource)); diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToSaxWriterVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToSaxWriterVisitor.java index b7bb7d1eb..f646bccb0 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToSaxWriterVisitor.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToSaxWriterVisitor.java @@ -18,6 +18,7 @@ package li.strolch.model.xml; import static li.strolch.model.StrolchModelConstants.INTERPRETATION_NONE; import static li.strolch.model.StrolchModelConstants.UOM_NONE; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -30,6 +31,7 @@ import java.util.TreeSet; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; +import li.strolch.exception.StrolchException; import li.strolch.model.GroupedParameterizedElement; import li.strolch.model.Order; import li.strolch.model.ParameterBag; @@ -50,12 +52,13 @@ import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.ITimeVariable; import li.strolch.model.timevalue.IValue; import li.strolch.model.timevalue.IValueChange; +import li.strolch.model.visitor.StrolchRootElementVisitor; import li.strolch.utils.iso8601.ISO8601FormatFactory; /** * @author Robert von Burg */ -public abstract class StrolchElementToSaxWriterVisitor { +public class StrolchElementToSaxWriterVisitor implements StrolchRootElementVisitor { protected XMLStreamWriter writer; @@ -63,6 +66,51 @@ public abstract class StrolchElementToSaxWriterVisitor { this.writer = writer; } + @Override + public Void visitActivity(Activity activity) { + + try { + writeElement(activity); + this.writer.flush(); + } catch (XMLStreamException e) { + String msg = "Failed to write Activity {0} due to {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, activity.getLocator(), e.getMessage()); + throw new StrolchException(msg, e); + } + + return null; + } + + @Override + public Void visitOrder(Order order) { + + try { + writeElement(order); + this.writer.flush(); + } catch (XMLStreamException e) { + String msg = "Failed to write Order {0} due to {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, order.getLocator(), e.getMessage()); + throw new StrolchException(msg, e); + } + + return null; + } + + @Override + public Void visitResource(Resource resource) { + + try { + writeElement(resource); + this.writer.flush(); + } catch (XMLStreamException e) { + String msg = "Failed to write Resource {0} due to {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, resource.getLocator(), e.getMessage()); + throw new StrolchException(msg, e); + } + + return null; + } + protected void writeElement(Resource resource) throws XMLStreamException { boolean empty = !resource.hasParameterBags() && !resource.hasTimedStates() && !resource.hasPolicyDefs(); @@ -110,7 +158,7 @@ public abstract class StrolchElementToSaxWriterVisitor { writeStartStrolchElement(Tags.ACTIVITY, empty, activity); this.writer.writeAttribute(Tags.TIME_ORDERING, activity.getTimeOrdering().getName()); - + if (activity.hasVersion()) writeVersion(activity); diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToXmlStringVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToXmlStringVisitor.java similarity index 52% rename from li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToXmlStringVisitor.java rename to li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToXmlStringVisitor.java index 16c119e2a..a674be76a 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToXmlStringVisitor.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToXmlStringVisitor.java @@ -1,18 +1,3 @@ -/* - * 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.xml; import java.io.StringWriter; @@ -21,19 +6,18 @@ import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamWriter; import javanet.staxutils.IndentingXMLStreamWriter; +import li.strolch.model.Order; +import li.strolch.model.Resource; import li.strolch.model.StrolchModelConstants; +import li.strolch.model.StrolchRootElement; import li.strolch.model.activity.Activity; -import li.strolch.model.visitor.ActivityVisitor; +import li.strolch.model.visitor.StrolchRootElementVisitor; import li.strolch.utils.dbc.DBC; -/** - * @author Robert von Burg - */ -public class ActivityToXmlStringVisitor implements ActivityVisitor { +public class StrolchElementToXmlStringVisitor implements StrolchRootElementVisitor { + + private String visit(StrolchRootElement element) { - @Override - public String visit(Activity element) { - DBC.PRE.assertNotNull("Activity my not be null!", element); try { StringWriter stringWriter = new StringWriter(); XMLOutputFactory factory = XMLOutputFactory.newInstance(); @@ -43,13 +27,34 @@ public class ActivityToXmlStringVisitor implements ActivityVisitor { // start document writer.writeStartDocument(StrolchModelConstants.DEFAULT_ENCODING, StrolchModelConstants.DEFAULT_XML_VERSION); - new ActivityToSaxWriterVisitor(writer).visit(element); + + element.accept(new StrolchElementToSaxWriterVisitor(writer)); + writer.writeEndDocument(); return stringWriter.toString(); + } catch (Exception e) { throw new RuntimeException( "Failed to format Element " + element.getLocator() + " to xml string due to " + e.getMessage(), e); } } + + @Override + public String visitOrder(Order element) { + DBC.PRE.assertNotNull("Order my not be null!", element); + return visit(element); + } + + @Override + public String visitResource(Resource element) { + DBC.PRE.assertNotNull("Resource my not be null!", element); + return visit(element); + } + + @Override + public String visitActivity(Activity element) { + DBC.PRE.assertNotNull("Activity my not be null!", element); + return visit(element); + } } diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchXmlHelper.java b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchXmlHelper.java new file mode 100644 index 000000000..f5c132fe1 --- /dev/null +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchXmlHelper.java @@ -0,0 +1,68 @@ +package li.strolch.model.xml; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.util.List; + +import javax.xml.stream.FactoryConfigurationError; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; + +import javanet.staxutils.IndentingXMLStreamWriter; +import li.strolch.model.Order; +import li.strolch.model.Resource; +import li.strolch.model.StrolchModelConstants; +import li.strolch.model.StrolchRootElement; +import li.strolch.model.Tags; +import li.strolch.model.activity.Activity; + +public class StrolchXmlHelper { + + public static Resource parseAndReturnResource(File file, String id) { + SimpleStrolchElementListener elementListener = new SimpleStrolchElementListener(); + new XmlModelSaxFileReader(elementListener, file, false).parseFile(); + return elementListener.getResource(id); + } + + public static Order parseAndReturnOrder(File file, String id) { + SimpleStrolchElementListener elementListener = new SimpleStrolchElementListener(); + new XmlModelSaxFileReader(elementListener, file, false).parseFile(); + return elementListener.getOrder(id); + } + + public static Activity parseAndReturnActivity(File file, String id) { + SimpleStrolchElementListener elementListener = new SimpleStrolchElementListener(); + new XmlModelSaxFileReader(elementListener, file, false).parseFile(); + return elementListener.getActivity(id); + } + + public static void writeToFile(File file, List elements) { + + try (FileOutputStream out = new FileOutputStream(file)) { + + XMLStreamWriter writer = openXmlStreamWriter(out); + + for (StrolchRootElement element : elements) { + element.accept(new StrolchElementToSaxWriterVisitor(writer)); + } + + } catch (Exception e) { + throw new RuntimeException("Failed to write elements to " + file, e); + } + } + + public static XMLStreamWriter openXmlStreamWriter(OutputStream out) + throws FactoryConfigurationError, XMLStreamException { + + XMLOutputFactory factory = XMLOutputFactory.newInstance(); + XMLStreamWriter writer = factory.createXMLStreamWriter(out, StrolchModelConstants.DEFAULT_ENCODING); + writer = new IndentingXMLStreamWriter(writer); + + // start document + writer.writeStartDocument(StrolchModelConstants.DEFAULT_ENCODING, StrolchModelConstants.DEFAULT_XML_VERSION); + writer.writeStartElement(Tags.STROLCH_MODEL); + return writer; + } +} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchXmlParser.java b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchXmlParser.java deleted file mode 100644 index 9066ce178..000000000 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchXmlParser.java +++ /dev/null @@ -1,28 +0,0 @@ -package li.strolch.model.xml; - -import java.io.File; - -import li.strolch.model.Order; -import li.strolch.model.Resource; -import li.strolch.model.activity.Activity; - -public class StrolchXmlParser { - - public static Resource parseAndReturnResource(File file, String id) { - SimpleStrolchElementListener elementListener = new SimpleStrolchElementListener(); - new XmlModelSaxFileReader(elementListener, file, false).parseFile(); - return elementListener.getResource(id); - } - - public static Order parseAndReturnOrder(File file, String id) { - SimpleStrolchElementListener elementListener = new SimpleStrolchElementListener(); - new XmlModelSaxFileReader(elementListener, file, false).parseFile(); - return elementListener.getOrder(id); - } - - public static Activity parseAndReturnActivity(File file, String id) { - SimpleStrolchElementListener elementListener = new SimpleStrolchElementListener(); - new XmlModelSaxFileReader(elementListener, file, false).parseFile(); - return elementListener.getActivity(id); - } -} diff --git a/li.strolch.model/src/test/java/li/strolch/model/ModelToDomTest.java b/li.strolch.model/src/test/java/li/strolch/model/ModelToDomTest.java index e4f3dd5d2..f75ed78b0 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/ModelToDomTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/ModelToDomTest.java @@ -24,11 +24,9 @@ import li.strolch.model.visitor.ActivityDeepEqualsVisitor; import li.strolch.model.visitor.OrderDeepEqualsVisitor; import li.strolch.model.visitor.ResourceDeepEqualsVisitor; import li.strolch.model.xml.ActivityFromDomVisitor; -import li.strolch.model.xml.ActivityToDomVisitor; import li.strolch.model.xml.OrderFromDomVisitor; -import li.strolch.model.xml.OrderToDomVisitor; import li.strolch.model.xml.ResourceFromDomVisitor; -import li.strolch.model.xml.ResourceToDomVisitor; +import li.strolch.model.xml.StrolchElementToDomVisitor; /** * @author Robert von Burg @@ -38,9 +36,7 @@ public class ModelToDomTest extends ModelMarshallingTest { protected Resource formatAndParseResource(Resource resource) { - ResourceToDomVisitor domVisitor = new ResourceToDomVisitor(); - domVisitor.visit(resource); - Document document = domVisitor.getDocument(); + Document document = resource.accept(new StrolchElementToDomVisitor()); Resource parsedResource = new ResourceFromDomVisitor().visit(document); @@ -53,9 +49,8 @@ public class ModelToDomTest extends ModelMarshallingTest { } protected Order formatAndParseOrder(Order order) { - OrderToDomVisitor domVisitor = new OrderToDomVisitor(); - domVisitor.visit(order); - Document document = domVisitor.getDocument(); + + Document document = order.accept(new StrolchElementToDomVisitor()); Order parsedOrder = new OrderFromDomVisitor().visit(document); @@ -68,9 +63,7 @@ public class ModelToDomTest extends ModelMarshallingTest { protected Activity formatAndParseActivity(Activity activity) { - ActivityToDomVisitor domVisitor = new ActivityToDomVisitor(); - domVisitor.visit(activity); - Document document = domVisitor.getDocument(); + Document document = activity.accept(new StrolchElementToDomVisitor()); Activity parsedActivity = new ActivityFromDomVisitor().visit(document); diff --git a/li.strolch.model/src/test/java/li/strolch/model/ModelToSaxTest.java b/li.strolch.model/src/test/java/li/strolch/model/ModelToSaxTest.java index 189059675..9da62242b 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/ModelToSaxTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/ModelToSaxTest.java @@ -24,10 +24,8 @@ import li.strolch.model.activity.Activity; import li.strolch.model.visitor.ActivityDeepEqualsVisitor; import li.strolch.model.visitor.OrderDeepEqualsVisitor; import li.strolch.model.visitor.ResourceDeepEqualsVisitor; -import li.strolch.model.xml.ActivityToSaxVisitor; -import li.strolch.model.xml.OrderToSaxVisitor; -import li.strolch.model.xml.ResourceToSaxVisitor; import li.strolch.model.xml.SimpleStrolchElementListener; +import li.strolch.model.xml.StrolchElementToSaxVisitor; import li.strolch.model.xml.XmlModelSaxReader; /** @@ -42,8 +40,7 @@ public class ModelToSaxTest extends ModelMarshallingTest { SimpleStrolchElementListener listener = new SimpleStrolchElementListener(); XmlModelSaxReader saxReader = new XmlModelSaxReader(listener); - OrderToSaxVisitor domVisitor = new OrderToSaxVisitor(saxReader); - domVisitor.visit(order); + order.accept(new StrolchElementToSaxVisitor(saxReader)); assertEquals(1, listener.getOrders().size()); assertEquals(Collections.emptyList(), listener.getResources()); @@ -63,8 +60,7 @@ public class ModelToSaxTest extends ModelMarshallingTest { SimpleStrolchElementListener listener = new SimpleStrolchElementListener(); XmlModelSaxReader saxReader = new XmlModelSaxReader(listener); - ResourceToSaxVisitor domVisitor = new ResourceToSaxVisitor(saxReader); - domVisitor.visit(resource); + resource.accept(new StrolchElementToSaxVisitor(saxReader)); assertEquals(1, listener.getResources().size()); assertEquals(Collections.emptyList(), listener.getActivities()); @@ -85,8 +81,7 @@ public class ModelToSaxTest extends ModelMarshallingTest { SimpleStrolchElementListener listener = new SimpleStrolchElementListener(); XmlModelSaxReader saxReader = new XmlModelSaxReader(listener); - ActivityToSaxVisitor domVisitor = new ActivityToSaxVisitor(saxReader); - domVisitor.visit(activity); + activity.accept(new StrolchElementToSaxVisitor(saxReader)); assertEquals(1, listener.getActivities().size()); assertEquals(Collections.emptyList(), listener.getResources()); diff --git a/li.strolch.model/src/test/java/li/strolch/model/ModelToSaxWriterTest.java b/li.strolch.model/src/test/java/li/strolch/model/ModelToSaxWriterTest.java index dc2938353..16e3ea6b0 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/ModelToSaxWriterTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/ModelToSaxWriterTest.java @@ -22,17 +22,15 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.Collections; -import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamWriter; import li.strolch.model.activity.Activity; import li.strolch.model.visitor.ActivityDeepEqualsVisitor; import li.strolch.model.visitor.OrderDeepEqualsVisitor; import li.strolch.model.visitor.ResourceDeepEqualsVisitor; -import li.strolch.model.xml.ActivityToSaxWriterVisitor; -import li.strolch.model.xml.OrderToSaxWriterVisitor; -import li.strolch.model.xml.ResourceToSaxWriterVisitor; import li.strolch.model.xml.SimpleStrolchElementListener; +import li.strolch.model.xml.StrolchElementToSaxWriterVisitor; +import li.strolch.model.xml.StrolchXmlHelper; import li.strolch.model.xml.XmlModelSaxStreamReader; /** @@ -44,13 +42,9 @@ public class ModelToSaxWriterTest extends ModelMarshallingTest { @Override protected Order formatAndParseOrder(Order order) throws Exception { - XMLOutputFactory factory = XMLOutputFactory.newInstance(); ByteArrayOutputStream out = new ByteArrayOutputStream(); - XMLStreamWriter writer = factory.createXMLStreamWriter(out, "UTF-8"); - writer.writeStartDocument(); - writer.writeStartElement(Tags.STROLCH_MODEL); - OrderToSaxWriterVisitor toSax = new OrderToSaxWriterVisitor(writer); - toSax.visit(order); + XMLStreamWriter writer = StrolchXmlHelper.openXmlStreamWriter(out); + order.accept(new StrolchElementToSaxWriterVisitor(writer)); writer.writeEndDocument(); SimpleStrolchElementListener listener = new SimpleStrolchElementListener(); @@ -73,13 +67,9 @@ public class ModelToSaxWriterTest extends ModelMarshallingTest { @Override protected Resource formatAndParseResource(Resource resource) throws Exception { - XMLOutputFactory factory = XMLOutputFactory.newInstance(); ByteArrayOutputStream out = new ByteArrayOutputStream(); - XMLStreamWriter writer = factory.createXMLStreamWriter(out, "UTF-8"); - writer.writeStartDocument(); - writer.writeStartElement(Tags.STROLCH_MODEL); - ResourceToSaxWriterVisitor toSax = new ResourceToSaxWriterVisitor(writer); - toSax.visit(resource); + XMLStreamWriter writer = StrolchXmlHelper.openXmlStreamWriter(out); + resource.accept(new StrolchElementToSaxWriterVisitor(writer)); writer.writeEndDocument(); SimpleStrolchElementListener listener = new SimpleStrolchElementListener(); @@ -99,20 +89,15 @@ public class ModelToSaxWriterTest extends ModelMarshallingTest { return parsedResource; } - @Override protected Activity formatAndParseActivity(Activity activity) throws Exception { - XMLOutputFactory factory = XMLOutputFactory.newInstance(); ByteArrayOutputStream out = new ByteArrayOutputStream(); - XMLStreamWriter writer = factory.createXMLStreamWriter(out, "UTF-8"); - writer.writeStartDocument(); - writer.writeStartElement(Tags.STROLCH_MODEL); - ActivityToSaxWriterVisitor toSax = new ActivityToSaxWriterVisitor(writer); - toSax.visit(activity); + XMLStreamWriter writer = StrolchXmlHelper.openXmlStreamWriter(out); + activity.accept(new StrolchElementToSaxWriterVisitor(writer)); writer.writeEndDocument(); - + System.out.println(out.toString()); SimpleStrolchElementListener listener = new SimpleStrolchElementListener(); diff --git a/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java b/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java index 346a918eb..0b6f64ad5 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java @@ -24,7 +24,7 @@ import org.w3c.dom.Element; import li.strolch.model.timevalue.IValueChange; import li.strolch.model.timevalue.impl.IntegerValue; import li.strolch.model.timevalue.impl.ValueChange; -import li.strolch.model.xml.ActivityToDomVisitor; +import li.strolch.model.xml.StrolchElementToDomVisitor; public class ActionTest { @@ -77,7 +77,7 @@ public class ActionTest { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = db.newDocument(); - Element dom = new ActivityToDomVisitor().toDom(this.action); + Element dom = new StrolchElementToDomVisitor().toDom(this.action); document.appendChild(dom); Transformer transformer = TransformerFactory.newInstance().newTransformer(); diff --git a/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java b/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java index e59856b1f..b3924a440 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java @@ -34,7 +34,7 @@ import org.w3c.dom.Element; import li.strolch.exception.StrolchException; import li.strolch.model.State; -import li.strolch.model.xml.ActivityToDomVisitor; +import li.strolch.model.xml.StrolchElementToDomVisitor; public class ActivityTest { @@ -144,7 +144,7 @@ public class ActivityTest { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.newDocument(); - Element dom = new ActivityToDomVisitor().toDom(this.activity); + Element dom = new StrolchElementToDomVisitor().toDom(this.activity); document.appendChild(dom); Transformer transformer = TransformerFactory.newInstance().newTransformer(); diff --git a/li.strolch.persistence.postgresql/src/main/java/li/strolch/persistence/postgresql/PostgreSqlActivityDao.java b/li.strolch.persistence.postgresql/src/main/java/li/strolch/persistence/postgresql/PostgreSqlActivityDao.java index 2d9b30e17..0264d374d 100644 --- a/li.strolch.persistence.postgresql/src/main/java/li/strolch/persistence/postgresql/PostgreSqlActivityDao.java +++ b/li.strolch.persistence.postgresql/src/main/java/li/strolch/persistence/postgresql/PostgreSqlActivityDao.java @@ -38,8 +38,8 @@ import org.xml.sax.SAXException; import li.strolch.model.Tags; import li.strolch.model.activity.Activity; import li.strolch.model.query.ActivityQuery; -import li.strolch.model.xml.ActivityToSaxVisitor; import li.strolch.model.xml.SimpleStrolchElementListener; +import li.strolch.model.xml.StrolchElementToSaxVisitor; import li.strolch.model.xml.XmlModelSaxReader; import li.strolch.persistence.api.ActivityDao; import li.strolch.persistence.api.StrolchPersistenceException; @@ -90,7 +90,7 @@ public class PostgreSqlActivityDao extends PostgresqlDao implements Ac SAXResult saxResult = sqlxml.setResult(SAXResult.class); ContentHandler contentHandler = saxResult.getHandler(); contentHandler.startDocument(); - new ActivityToSaxVisitor(contentHandler).visit(activity); + activity.accept(new StrolchElementToSaxVisitor(contentHandler)); contentHandler.endDocument(); return sqlxml; } diff --git a/li.strolch.persistence.postgresql/src/main/java/li/strolch/persistence/postgresql/PostgreSqlOrderDao.java b/li.strolch.persistence.postgresql/src/main/java/li/strolch/persistence/postgresql/PostgreSqlOrderDao.java index d9ca102eb..7753b3151 100644 --- a/li.strolch.persistence.postgresql/src/main/java/li/strolch/persistence/postgresql/PostgreSqlOrderDao.java +++ b/li.strolch.persistence.postgresql/src/main/java/li/strolch/persistence/postgresql/PostgreSqlOrderDao.java @@ -38,8 +38,8 @@ import org.xml.sax.SAXException; import li.strolch.model.Order; import li.strolch.model.Tags; import li.strolch.model.query.OrderQuery; -import li.strolch.model.xml.OrderToSaxVisitor; import li.strolch.model.xml.SimpleStrolchElementListener; +import li.strolch.model.xml.StrolchElementToSaxVisitor; import li.strolch.model.xml.XmlModelSaxReader; import li.strolch.persistence.api.OrderDao; import li.strolch.persistence.api.StrolchPersistenceException; @@ -89,7 +89,7 @@ public class PostgreSqlOrderDao extends PostgresqlDao implements OrderDao SAXResult saxResult = sqlxml.setResult(SAXResult.class); ContentHandler contentHandler = saxResult.getHandler(); contentHandler.startDocument(); - new OrderToSaxVisitor(contentHandler).visit(order); + order.accept(new StrolchElementToSaxVisitor(contentHandler)); contentHandler.endDocument(); return sqlxml; } diff --git a/li.strolch.persistence.postgresql/src/main/java/li/strolch/persistence/postgresql/PostgreSqlResourceDao.java b/li.strolch.persistence.postgresql/src/main/java/li/strolch/persistence/postgresql/PostgreSqlResourceDao.java index c2c7c40fe..b0f1c1519 100644 --- a/li.strolch.persistence.postgresql/src/main/java/li/strolch/persistence/postgresql/PostgreSqlResourceDao.java +++ b/li.strolch.persistence.postgresql/src/main/java/li/strolch/persistence/postgresql/PostgreSqlResourceDao.java @@ -38,8 +38,8 @@ import org.xml.sax.SAXException; import li.strolch.model.Resource; import li.strolch.model.Tags; import li.strolch.model.query.ResourceQuery; -import li.strolch.model.xml.ResourceToSaxVisitor; import li.strolch.model.xml.SimpleStrolchElementListener; +import li.strolch.model.xml.StrolchElementToSaxVisitor; import li.strolch.model.xml.XmlModelSaxReader; import li.strolch.persistence.api.ResourceDao; import li.strolch.persistence.api.StrolchPersistenceException; @@ -89,7 +89,7 @@ public class PostgreSqlResourceDao extends PostgresqlDao implements Re SAXResult saxResult = sqlxml.setResult(SAXResult.class); ContentHandler contentHandler = saxResult.getHandler(); contentHandler.startDocument(); - new ResourceToSaxVisitor(contentHandler).visit(res); + res.accept(new StrolchElementToSaxVisitor(contentHandler)); contentHandler.endDocument(); return sqlxml; } diff --git a/li.strolch.rest/src/main/java/li/strolch/rest/endpoint/Inspector.java b/li.strolch.rest/src/main/java/li/strolch/rest/endpoint/Inspector.java index ebad50364..7d538ce1b 100644 --- a/li.strolch.rest/src/main/java/li/strolch/rest/endpoint/Inspector.java +++ b/li.strolch.rest/src/main/java/li/strolch/rest/endpoint/Inspector.java @@ -61,10 +61,8 @@ import li.strolch.model.query.OrderQuery; import li.strolch.model.query.ResourceQuery; import li.strolch.model.query.StrolchTypeNavigation; import li.strolch.model.query.parser.QueryParser; -import li.strolch.model.xml.ActivityToXmlStringVisitor; -import li.strolch.model.xml.OrderToXmlStringVisitor; -import li.strolch.model.xml.ResourceToXmlStringVisitor; import li.strolch.model.xml.SimpleStrolchElementListener; +import li.strolch.model.xml.StrolchElementToXmlStringVisitor; import li.strolch.model.xml.XmlModelSaxReader; import li.strolch.persistence.api.StrolchPersistenceException; import li.strolch.persistence.api.StrolchTransaction; @@ -523,7 +521,7 @@ public class Inspector { throw new StrolchException(MessageFormat.format("No Resource exists for {0}/{1}", type, id)); //$NON-NLS-1$ } - String asXml = new ResourceToXmlStringVisitor().visit(resource); + String asXml = resource.accept(new StrolchElementToXmlStringVisitor()); return Response.ok().type(MediaType.APPLICATION_XML).entity(asXml).build(); } @@ -563,7 +561,7 @@ public class Inspector { ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg); if (result.isOk()) { - String asXml = new ResourceToXmlStringVisitor().visit(resource); + String asXml = resource.accept(new StrolchElementToXmlStringVisitor()); return Response.ok().type(MediaType.APPLICATION_XML).entity(asXml).build(); } @@ -605,7 +603,7 @@ public class Inspector { throw new StrolchException(MessageFormat.format("No Order exists for {0}/{1}", type, id)); //$NON-NLS-1$ } - String asXml = new OrderToXmlStringVisitor().visit(order); + String asXml = order.accept(new StrolchElementToXmlStringVisitor()); return Response.ok().type(MediaType.APPLICATION_XML).entity(asXml).build(); } @@ -645,7 +643,7 @@ public class Inspector { ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg); if (result.isOk()) { - String asXml = new OrderToXmlStringVisitor().visit(order); + String asXml = order.accept(new StrolchElementToXmlStringVisitor()); return Response.ok().type(MediaType.APPLICATION_XML).entity(asXml).build(); } @@ -705,7 +703,7 @@ public class Inspector { throw new StrolchException(MessageFormat.format("No Activity exists for {0}/{1}", type, id)); //$NON-NLS-1$ } - String asXml = new ActivityToXmlStringVisitor().visit(activity); + String asXml = activity.accept(new StrolchElementToXmlStringVisitor()); return Response.ok().type(MediaType.APPLICATION_XML).entity(asXml).build(); } @@ -745,7 +743,7 @@ public class Inspector { ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg); if (result.isOk()) { - String asXml = new ActivityToXmlStringVisitor().visit(activity); + String asXml = activity.accept(new StrolchElementToXmlStringVisitor()); return Response.ok().type(MediaType.APPLICATION_XML).entity(asXml).build(); } diff --git a/li.strolch.service/src/main/java/li/strolch/command/XmlExportModelCommand.java b/li.strolch.service/src/main/java/li/strolch/command/XmlExportModelCommand.java index 62dd3385e..8e71ff3c8 100644 --- a/li.strolch.service/src/main/java/li/strolch/command/XmlExportModelCommand.java +++ b/li.strolch.service/src/main/java/li/strolch/command/XmlExportModelCommand.java @@ -27,11 +27,6 @@ import java.util.Set; import java.util.TreeSet; import java.util.concurrent.TimeUnit; -import javanet.staxutils.IndentingXMLStreamWriter; - -import javax.xml.stream.FactoryConfigurationError; -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import li.strolch.agent.api.ActivityMap; @@ -44,14 +39,9 @@ import li.strolch.model.Order; import li.strolch.model.Resource; import li.strolch.model.Tags; import li.strolch.model.activity.Activity; -import li.strolch.model.visitor.ActivityVisitor; -import li.strolch.model.visitor.OrderVisitor; -import li.strolch.model.visitor.ResourceVisitor; -import li.strolch.model.xml.ActivityToSaxWriterVisitor; -import li.strolch.model.xml.OrderToSaxWriterVisitor; -import li.strolch.model.xml.ResourceToSaxWriterVisitor; +import li.strolch.model.xml.StrolchElementToSaxWriterVisitor; +import li.strolch.model.xml.StrolchXmlHelper; import li.strolch.persistence.api.StrolchTransaction; -import li.strolch.runtime.StrolchConstants; import li.strolch.service.api.Command; import li.strolch.utils.dbc.DBC; @@ -167,7 +157,7 @@ public class XmlExportModelCommand extends Command { try (FileOutputStream out = new FileOutputStream(this.modelFile)) { createdFiles.add(this.modelFile); - XMLStreamWriter writer = openXmlStreamWriter(out); + XMLStreamWriter writer = StrolchXmlHelper.openXmlStreamWriter(out); if (this.doResources) { ResourceMap resourceMap = tx().getResourceMap(); @@ -191,7 +181,7 @@ public class XmlExportModelCommand extends Command { + typeXmlFileF.getAbsolutePath() + "..."); try (FileOutputStream typeOut = new FileOutputStream(typeXmlFileF)) { createdFiles.add(typeXmlFileF); - XMLStreamWriter typeWriter = openXmlStreamWriter(typeOut); + XMLStreamWriter typeWriter = StrolchXmlHelper.openXmlStreamWriter(typeOut); writeResourcesByType(typeWriter, resourceMap, type); typeWriter.writeEndDocument(); } @@ -220,7 +210,7 @@ public class XmlExportModelCommand extends Command { + typeXmlFileF.getAbsolutePath() + "..."); try (FileOutputStream typeOut = new FileOutputStream(typeXmlFileF)) { createdFiles.add(typeXmlFileF); - XMLStreamWriter typeWriter = openXmlStreamWriter(typeOut); + XMLStreamWriter typeWriter = StrolchXmlHelper.openXmlStreamWriter(typeOut); writeOrdersByType(typeWriter, orderMap, type); typeWriter.writeEndDocument(); } @@ -250,7 +240,7 @@ public class XmlExportModelCommand extends Command { + " Activities to path: " + typeXmlFileF.getAbsolutePath() + "..."); try (FileOutputStream typeOut = new FileOutputStream(typeXmlFileF)) { createdFiles.add(typeXmlFileF); - XMLStreamWriter typeWriter = openXmlStreamWriter(typeOut); + XMLStreamWriter typeWriter = StrolchXmlHelper.openXmlStreamWriter(typeOut); writeActivitiesByType(typeWriter, activityMap, type); typeWriter.writeEndDocument(); } @@ -284,11 +274,11 @@ public class XmlExportModelCommand extends Command { } private void writeOrdersByType(XMLStreamWriter writer, OrderMap orderMap, String type) { - OrderVisitor visitor = new OrderToSaxWriterVisitor(writer); + StrolchElementToSaxWriterVisitor visitor = new StrolchElementToSaxWriterVisitor(writer); Set keysByType = new TreeSet<>(orderMap.getKeysBy(tx(), type)); for (String id : keysByType) { Order order = orderMap.getBy(tx(), type, id); - visitor.visit(order); + order.accept(visitor); this.statistics.nrOfOrders++; logElementsWritten(); } @@ -302,39 +292,27 @@ public class XmlExportModelCommand extends Command { } private void writeResourcesByType(XMLStreamWriter writer, ResourceMap resourceMap, String type) { - ResourceVisitor visitor = new ResourceToSaxWriterVisitor(writer); + StrolchElementToSaxWriterVisitor visitor = new StrolchElementToSaxWriterVisitor(writer); Set keysByType = new TreeSet<>(resourceMap.getKeysBy(tx(), type)); for (String id : keysByType) { Resource resource = resourceMap.getBy(tx(), type, id); - visitor.visit(resource); + resource.accept(visitor); this.statistics.nrOfResources++; logElementsWritten(); } } private void writeActivitiesByType(XMLStreamWriter writer, ActivityMap activityMap, String type) { - ActivityVisitor visitor = new ActivityToSaxWriterVisitor(writer); + StrolchElementToSaxWriterVisitor visitor = new StrolchElementToSaxWriterVisitor(writer); Set keysByType = new TreeSet<>(activityMap.getKeysBy(tx(), type)); for (String id : keysByType) { Activity activity = activityMap.getBy(tx(), type, id); - visitor.visit(activity); + activity.accept(visitor); this.statistics.nrOfActivities++; logElementsWritten(); } } - private XMLStreamWriter openXmlStreamWriter(FileOutputStream out) - throws FactoryConfigurationError, XMLStreamException { - XMLOutputFactory factory = XMLOutputFactory.newInstance(); - XMLStreamWriter writer = factory.createXMLStreamWriter(out, StrolchConstants.DEFAULT_ENCODING); - writer = new IndentingXMLStreamWriter(writer); - - // start document - writer.writeStartDocument(StrolchConstants.DEFAULT_ENCODING, StrolchConstants.DEFAULT_XML_VERSION); - writer.writeStartElement(Tags.STROLCH_MODEL); - return writer; - } - /** * @param modelFile * the modelFile to set