[New] Added .parse() to Resource, Order and Activity classes

This commit is contained in:
Robert von Burg 2022-07-26 15:16:06 +02:00
parent 21f6f8d07a
commit 07767c9c70
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
4 changed files with 106 additions and 15 deletions

View File

@ -24,6 +24,7 @@ import li.strolch.model.Locator.LocatorBuilder;
import li.strolch.model.policy.PolicyDef;
import li.strolch.model.policy.PolicyDefs;
import li.strolch.model.visitor.StrolchElementVisitor;
import li.strolch.model.xml.StrolchXmlHelper;
import li.strolch.utils.dbc.DBC;
import li.strolch.utils.iso8601.ISO8601;
@ -31,7 +32,7 @@ import li.strolch.utils.iso8601.ISO8601;
* The Order is an object used in the EDF to transfer data from one range to another. Orders are not to be thought of as
* Resources. Resources are supposed to be thought of as things i.e. a table, a machine and so forth, where a order is
* to be thought of as an object for doing something.
*
* <p>
* In this sense, orders do not need to be verified, so all verifier chracteristics are disabled and the
* getVerifier()-method will return the null reference
*
@ -318,7 +319,31 @@ public class Order extends AbstractStrolchRootElement implements StrolchRootElem
return getId().compareTo(o.getId());
}
/**
* Creates a {@link Locator} for orders of the given type and id
*
* @param type
* the type of order
* @param id
* the id of the order
*
* @return the locator
*/
public static Locator locatorFor(String type, String id) {
return Locator.valueOf(Tags.ORDER, type, id);
}
/**
* Parses the given XML and returns the order with the given ID
*
* @param xml
* the xml to parse
* @param id
* the id of the order to return from the parsed elements
*
* @return the order, or null if it does not exist
*/
public static Resource parse(String xml, String id) {
return StrolchXmlHelper.parseAndReturnResource(xml, id);
}
}

View File

@ -30,6 +30,7 @@ import li.strolch.model.policy.PolicyDefs;
import li.strolch.model.timedstate.StrolchTimedState;
import li.strolch.model.timevalue.IValue;
import li.strolch.model.visitor.StrolchElementVisitor;
import li.strolch.model.xml.StrolchXmlHelper;
import li.strolch.utils.dbc.DBC;
/**
@ -197,7 +198,8 @@ public class Resource extends AbstractStrolchRootElement implements StrolchRootE
if (this.timedStateMap == null || this.timedStateMap.isEmpty())
return Stream.empty();
return this.timedStateMap.values().stream()
return this.timedStateMap.values()
.stream()
.filter(s -> s.getInterpretation().equals(interpretation) && s.getUom().equals(uom));
}
@ -399,7 +401,31 @@ public class Resource extends AbstractStrolchRootElement implements StrolchRootE
return getId().compareTo(o.getId());
}
/**
* Creates a {@link Locator} for resources of the given type and id
*
* @param type
* the type of resource
* @param id
* the id of the resource
*
* @return the locator
*/
public static Locator locatorFor(String type, String id) {
return Locator.valueOf(Tags.RESOURCE, type, id);
}
/**
* Parses the given XML and returns the resource with the given ID
*
* @param xml
* the xml to parse
* @param id
* the id of the resource to return from the parsed elements
*
* @return the resource, or null if it does not exist
*/
public static Resource parse(String xml, String id) {
return StrolchXmlHelper.parseAndReturnResource(xml, id);
}
}

View File

@ -36,6 +36,7 @@ import li.strolch.model.parameter.Parameter;
import li.strolch.model.policy.PolicyDef;
import li.strolch.model.policy.PolicyDefs;
import li.strolch.model.visitor.StrolchElementVisitor;
import li.strolch.model.xml.StrolchXmlHelper;
import li.strolch.utils.dbc.DBC;
/**
@ -836,7 +837,31 @@ public class Activity extends AbstractStrolchRootElement
this.parent = activity;
}
/**
* Creates a {@link Locator} for activities of the given type and id
*
* @param type
* the type of activity
* @param id
* the id of the activity
*
* @return the locator
*/
public static Locator locatorFor(String type, String id) {
return Locator.valueOf(Tags.ACTIVITY, type, id);
}
/**
* Parses the given XML and returns the activity with the given ID
*
* @param xml
* the xml to parse
* @param id
* the id of the activity to return from the parsed elements
*
* @return the activity, or null if it does not exist
*/
public static Resource parse(String xml, String id) {
return StrolchXmlHelper.parseAndReturnResource(xml, id);
}
}

View File

@ -1,16 +1,16 @@
package li.strolch.model.xml;
import static java.nio.charset.StandardCharsets.UTF_8;
import static li.strolch.model.StrolchModelConstants.DEFAULT_ENCODING;
import static li.strolch.model.StrolchModelConstants.DEFAULT_XML_VERSION;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.io.*;
import java.nio.file.Files;
import java.util.Collection;
import java.util.List;
@ -32,22 +32,20 @@ public class StrolchXmlHelper {
private static final Logger logger = LoggerFactory.getLogger(StrolchXmlHelper.class);
public static Resource parseAndReturnResource(String xml, String id) {
return parse(xml).getResource(id);
}
public static Resource parseAndReturnResource(File file, String id) {
SimpleStrolchElementListener elementListener = new SimpleStrolchElementListener();
new XmlModelSaxFileReader(elementListener, file, false).parseFile();
return elementListener.getResource(id);
return parse(file).getResource(id);
}
public static Order parseAndReturnOrder(File file, String id) {
SimpleStrolchElementListener elementListener = new SimpleStrolchElementListener();
new XmlModelSaxFileReader(elementListener, file, false).parseFile();
return elementListener.getOrder(id);
return parse(file).getOrder(id);
}
public static Activity parseAndReturnActivity(File file, String id) {
SimpleStrolchElementListener elementListener = new SimpleStrolchElementListener();
new XmlModelSaxFileReader(elementListener, file, false).parseFile();
return elementListener.getActivity(id);
return parse(file).getActivity(id);
}
public static Map<String, StrolchRootElement> parseToMap(File file) {
@ -58,6 +56,23 @@ public class StrolchXmlHelper {
return parseStream(stream, encoding).stream().collect(Collectors.toMap(StrolchRootElement::getId, e -> e));
}
public static SimpleStrolchElementListener parse(File file) {
SimpleStrolchElementListener elementListener = new SimpleStrolchElementListener();
new XmlModelSaxFileReader(elementListener, file, false).parseFile();
return elementListener;
}
private static SimpleStrolchElementListener parse(String xml) {
try {
SimpleStrolchElementListener elementListener = new SimpleStrolchElementListener();
SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
sp.parse(new ByteArrayInputStream(xml.getBytes(UTF_8)), new XmlModelSaxReader(elementListener));
return elementListener;
} catch (Exception e) {
throw new IllegalStateException("Failed to parse XML", e);
}
}
public static List<StrolchRootElement> parseFile(File file) {
SimpleStrolchElementListener elementListener = new SimpleStrolchElementListener();
new XmlModelSaxFileReader(elementListener, file, false).parseFile();