[Major] Removed many visitors and implemented proper visitor pattern...

This commit is contained in:
Robert von Burg 2017-03-02 16:52:50 +01:00
parent 6cc22c21a8
commit e0149cc4b0
27 changed files with 282 additions and 695 deletions

View File

@ -1,52 +0,0 @@
/*
* 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.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 <eitch@eitchnet.ch>
*/
public class ActivityToDomVisitor extends StrolchElementToDomVisitor implements ActivityVisitor<Document> {
@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);
}
}

View File

@ -1,48 +0,0 @@
/*
* 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.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 <eitch@eitchnet.ch>
*/
public class ActivityToSaxVisitor extends StrolchElementToSaxVisitor implements ActivityVisitor<Void> {
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;
}
}

View File

@ -1,49 +0,0 @@
/*
* 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.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 <eitch@eitchnet.ch>
*/
public class ActivityToSaxWriterVisitor extends StrolchElementToSaxWriterVisitor implements ActivityVisitor<Void> {
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;
}
}

View File

@ -1,41 +0,0 @@
/*
* 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.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 <eitch@eitchnet.ch>
*/
public class OrderToDomVisitor extends StrolchElementToDomVisitor implements OrderVisitor<Document> {
@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;
}
}

View File

@ -1,49 +0,0 @@
/*
* 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.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 <eitch@eitchnet.ch>
*/
public class OrderToSaxVisitor extends StrolchElementToSaxVisitor implements OrderVisitor<Void> {
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;
}
}

View File

@ -1,49 +0,0 @@
/*
* 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.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 <eitch@eitchnet.ch>
*/
public class OrderToSaxWriterVisitor extends StrolchElementToSaxWriterVisitor implements OrderVisitor<Void> {
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;
}
}

View File

@ -1,55 +0,0 @@
/*
* 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.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 <eitch@eitchnet.ch>
*/
public class OrderToXmlStringVisitor implements OrderVisitor<String> {
@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);
}
}
}

View File

@ -1,41 +0,0 @@
/*
* 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.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 <eitch@eitchnet.ch>
*/
public class ResourceToDomVisitor extends StrolchElementToDomVisitor implements ResourceVisitor<Document> {
@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;
}
}

View File

@ -1,48 +0,0 @@
/*
* 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.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 <eitch@eitchnet.ch>
*/
public class ResourceToSaxVisitor extends StrolchElementToSaxVisitor implements ResourceVisitor<Void> {
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;
}
}

View File

@ -1,49 +0,0 @@
/*
* 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.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 <eitch@eitchnet.ch>
*/
public class ResourceToSaxWriterVisitor extends StrolchElementToSaxWriterVisitor implements ResourceVisitor<Void> {
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;
}
}

View File

@ -1,55 +0,0 @@
/*
* 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.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 <eitch@eitchnet.ch>
*/
public class ResourceToXmlStringVisitor implements ResourceVisitor<String> {
@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);
}
}
}

View File

@ -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 <eitch@eitchnet.ch>
*/
public class StrolchElementToDomVisitor {
public class StrolchElementToDomVisitor implements StrolchRootElementVisitor<Document> {
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);

View File

@ -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 <eitch@eitchnet.ch>
*/
public abstract class StrolchElementToSaxVisitor {
public class StrolchElementToSaxVisitor implements StrolchRootElementVisitor<Void> {
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));

View File

@ -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 <eitch@eitchnet.ch>
*/
public abstract class StrolchElementToSaxWriterVisitor {
public class StrolchElementToSaxWriterVisitor implements StrolchRootElementVisitor<Void> {
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);

View File

@ -1,18 +1,3 @@
/*
* 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.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 <eitch@eitchnet.ch>
*/
public class ActivityToXmlStringVisitor implements ActivityVisitor<String> {
public class StrolchElementToXmlStringVisitor implements StrolchRootElementVisitor<String> {
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<String> {
// 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);
}
}

View File

@ -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<StrolchRootElement> 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;
}
}

View File

@ -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);
}
}

View File

@ -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 <eitch@eitchnet.ch>
@ -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);

View File

@ -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());

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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<Activity> 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;
}

View File

@ -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<Order> 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;
}

View File

@ -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<Resource> 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;
}

View File

@ -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();
}

View File

@ -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<String> 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<String> 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<String> 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