View Javadoc
1   package org.opentrafficsim.road.network.factory;
2   
3   import java.io.OutputStream;
4   import java.io.Writer;
5   
6   import org.djutils.event.EventProducer;
7   import org.opentrafficsim.core.object.Detector;
8   import org.opentrafficsim.road.network.RoadNetwork;
9   
10  import com.thoughtworks.xstream.XStream;
11  
12  import nl.tudelft.simulation.naming.context.JvmContext;
13  
14  /**
15   * <p>
16   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18   * </p>
19   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
21   */
22  // TODO: SKL 2024.10.04 do we need this class? It is the only cause for the xstream dependency in ots-road
23  public final class RoadNetworkUtils
24  {
25  
26      /** Do not instantiate. */
27      private RoadNetworkUtils()
28      {
29          // do not instantiate.
30      }
31  
32      /**
33       * Make a copy of the network, without GTUs and listeners.
34       * @param network the network to copy
35       * @return a copy of the network
36       */
37      public static RoadNetwork copy(final RoadNetwork network)
38      {
39          XStream xstream = new XStream();
40          return (RoadNetwork) xstream.fromXML(toXml(network));
41      }
42  
43      /**
44       * Create an xml-version of the network.
45       * @param network the network to create an xml-version from
46       * @return an xml-string with the network
47       */
48      public static String toXml(final RoadNetwork network)
49      {
50          XStream xstream = new XStream();
51          xstream.omitField(RoadNetwork.class, "gtuMap"); // no GTUs
52          xstream.omitField(EventProducer.class, "listeners"); // no listeners
53          xstream.omitField(JvmContext.class, "atomicName"); // no JvmContext
54          xstream.omitField(JvmContext.class, "elements"); // no JvmContext
55          xstream.omitField(Detector.class, "simulator"); // no reference to a simulator
56          return xstream.toXML(network);
57      }
58  
59      /**
60       * Create an xml-version of the network.
61       * @param network the network to create an xml-version from
62       * @param out the stream to write the xml-string with the network to
63       */
64      public static void toXml(final RoadNetwork network, final OutputStream out)
65      {
66          XStream xstream = new XStream();
67          xstream.toXML(network, out);
68      }
69  
70      /**
71       * Create an xml-version of the network.
72       * @param network the network to create an xml-version from
73       * @param writer the writer to write the xml-string with the network to
74       */
75      public static void toXml(final RoadNetwork network, final Writer writer)
76      {
77          XStream xstream = new XStream();
78          xstream.toXML(network, writer);
79      }
80  }