All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentcs.util.persistence.v002.V002PlantModelTO Maven / Gradle / Ivy

There is a newer version: 6.2.0
Show newest version
/**
 * Copyright (c) The openTCS Authors.
 *
 * This program is free software and subject to the MIT license. (For details,
 * see the licensing information (LICENSE.txt) you should have received with
 * this copy of the software.)
 */
package org.opentcs.util.persistence.v002;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import static java.util.Objects.requireNonNull;
import javax.annotation.Nonnull;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.opentcs.util.persistence.BasePlantModelTO;
import org.xml.sax.SAXException;

/**
 *
 * @author Martin Grzenia (Fraunhofer IML)
 */
@XmlRootElement(name = "model")
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(propOrder = {"version", "name", "points", "paths", "vehicles", "locationTypes",
                      "locations", "blocks", "staticRoutes", "groups", "visualLayouts",
                      "properties"})
public class V002PlantModelTO
    extends BasePlantModelTO {

  private String name = "";
  private List points = new ArrayList<>();
  private List paths = new ArrayList<>();
  private List vehicles = new ArrayList<>();
  private List locationTypes = new ArrayList<>();
  private List locations = new ArrayList<>();
  private List blocks = new ArrayList<>();
  private List staticRoutes = new ArrayList<>();
  private List groups = new ArrayList<>();
  private List visualLayouts = new ArrayList<>();
  private List properties = new ArrayList<>();

  @XmlAttribute(required = true)
  public String getName() {
    return name;
  }

  public V002PlantModelTO setName(@Nonnull String name) {
    requireNonNull(name, "name");
    this.name = name;
    return this;
  }

  @XmlElement(name = "point")
  public List getPoints() {
    return points;
  }

  public V002PlantModelTO setPoints(@Nonnull List points) {
    requireNonNull(points, "points");
    this.points = points;
    return this;
  }

  @XmlElement(name = "path")
  public List getPaths() {
    return paths;
  }

  public V002PlantModelTO setPaths(@Nonnull List paths) {
    requireNonNull(paths, "paths");
    this.paths = paths;
    return this;
  }

  @XmlElement(name = "vehicle")
  public List getVehicles() {
    return vehicles;
  }

  public V002PlantModelTO setVehicles(@Nonnull List vehicles) {
    requireNonNull(vehicles, "vehicles");
    this.vehicles = vehicles;
    return this;
  }

  @XmlElement(name = "locationType")
  public List getLocationTypes() {
    return locationTypes;
  }

  public V002PlantModelTO setLocationTypes(@Nonnull List locationTypes) {
    requireNonNull(locationTypes, "locationTypes");
    this.locationTypes = locationTypes;
    return this;
  }

  @XmlElement(name = "location")
  public List getLocations() {
    return locations;
  }

  public V002PlantModelTO setLocations(@Nonnull List locations) {
    requireNonNull(locations, "locations");
    this.locations = locations;
    return this;
  }

  @XmlElement(name = "block")
  public List getBlocks() {
    return blocks;
  }

  public V002PlantModelTO setBlocks(@Nonnull List blocks) {
    requireNonNull(blocks, "blocks");
    this.blocks = blocks;
    return this;
  }

  @XmlElement(name = "staticRoute")
  public List getStaticRoutes() {
    return staticRoutes;
  }

  public V002PlantModelTO setStaticRoutes(@Nonnull List staticRoutes) {
    requireNonNull(staticRoutes, "staticRoutes");
    this.staticRoutes = staticRoutes;
    return this;
  }

  @XmlElement(name = "group")
  public List getGroups() {
    return groups;
  }

  public V002PlantModelTO setGroups(@Nonnull List groups) {
    requireNonNull(groups, "groups");
    this.groups = groups;
    return this;
  }

  @XmlElement(name = "visualLayout")
  public List getVisualLayouts() {
    return visualLayouts;
  }

  public V002PlantModelTO setVisualLayouts(@Nonnull List visualLayouts) {
    requireNonNull(visualLayouts, "visualLayouts");
    this.visualLayouts = visualLayouts;
    return this;
  }

  @XmlElement(name = "property")
  public List getProperties() {
    return properties;
  }

  public V002PlantModelTO setProperties(@Nonnull List properties) {
    requireNonNull(properties, "properties");
    this.properties = properties;
    return this;
  }

  /**
   * Marshals this instance to its XML representation and writes it to the given writer.
   *
   * @param writer The writer to write this instance's XML representation to.
   * @throws IOException If there was a problem marshalling this instance.
   */
  public void toXml(@Nonnull Writer writer)
      throws IOException {
    requireNonNull(writer, "writer");

    try {
      createMarshaller().marshal(this, writer);
    }
    catch (JAXBException | SAXException exc) {
      throw new IOException("Exception marshalling data", exc);
    }
  }

  /**
   * Unmarshals an instance of this class from the given XML representation.
   *
   * @param reader Provides the XML representation to parse to an instance.
   * @return The instance unmarshalled from the given reader.
   * @throws IOException If there was a problem unmarshalling the given string.
   */
  public static V002PlantModelTO fromXml(@Nonnull Reader reader)
      throws IOException {
    requireNonNull(reader, "reader");

    try {
      return (V002PlantModelTO) createUnmarshaller().unmarshal(reader);
    }
    catch (JAXBException | SAXException exc) {
      throw new IOException("Exception unmarshalling data", exc);
    }
  }

  private static Marshaller createMarshaller()
      throws JAXBException, SAXException {
    Marshaller marshaller = createContext().createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setSchema(createSchema());
    return marshaller;
  }

  private static Unmarshaller createUnmarshaller()
      throws JAXBException, SAXException {
    Unmarshaller unmarshaller = createContext().createUnmarshaller();
    unmarshaller.setSchema(createSchema());
    return unmarshaller;
  }

  private static JAXBContext createContext()
      throws JAXBException {
    return JAXBContext.newInstance(V002PlantModelTO.class);
  }

  private static Schema createSchema()
      throws SAXException {
    URL schemaUrl = V002PlantModelTO.class.getResource("/org/opentcs/util/persistence/model-0.0.2.xsd");
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    return schemaFactory.newSchema(schemaUrl);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy