1
2
3
4
5
6
7
8
9
10
11
12
13 package org.gscg.experiment;
14
15 import java.io.IOException;
16 import java.net.URL;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import javax.vecmath.Point3d;
21
22 import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
23 import nl.tudelft.simulation.logger.Logger;
24 import nl.tudelft.simulation.supplychain.product.Product;
25 import nl.tudelft.simulation.supplychain.roles.Role;
26
27 import org.gscg.common.gui.images.LatLonToSmallMap;
28 import org.gscg.game.Game;
29 import org.gscg.gameactors.GameSupplier;
30 import org.jdom.Element;
31 import org.jdom.Namespace;
32 import org.jdom.input.SAXBuilder;
33
34 /***
35 * The SupplierParser parses xml-based experiments into their java objects.
36 * <p>
37 *
38 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
39 * Delft, the Netherlands. All rights reserved.
40 *
41 * See for project information <a href="http://www.simulation.tudelft.nl/">
42 * www.simulation.tudelft.nl </a>.
43 *
44 * The source code and binary code of this software is proprietary information
45 * of Delft University of Technology.
46 *
47 * @author <a
48 * href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
49 * van Houten </a>
50 * @version $Revision: 1.1 $ $Date: 2005/08/09 15:43:40 $
51 * @since 1.0.0
52 */
53 public class SupplierParser
54 {
55 /*** builder the xerces parser with validation turned on */
56 private static SAXBuilder builder = new SAXBuilder(
57 "org.apache.xerces.parsers.SAXParser", false);
58 static
59 {
60
61 builder.setFeature("http://xml.org/sax/features/validation", true);
62 builder.setFeature("http://apache.org/xml/features/validation/schema",
63 true);
64 }
65
66 /*** for debugging */
67 private transient static boolean DEBUG = false;
68
69 /***
70 * constructs a new SupplierParser This is a Utility Class.
71 */
72 protected SupplierParser()
73 {
74
75 }
76
77 /***
78 * parses a product xml-file.
79 *
80 * @param schema the schema for validation of the xml-file
81 * @param input the inputstream
82 * @param simulator the simulator
83 * @return Suppliers the suppliers
84 * @throws IOException whenever parsing fails
85 */
86 public static Suppliers parseSuppliers(final String schema,
87 final URL input, final DEVSSimulatorInterface simulator)
88 throws IOException
89 {
90 builder
91 .setProperty(
92 "http://apache.org/xml/properties/schema/external-schemaLocation",
93 "http://www.simulation.tudelft.nl " + schema);
94 try
95 {
96 Element rootElement = builder.build(input).getRootElement();
97 Suppliers suppliers = new Suppliers();
98
99 List elements = rootElement.getChildren();
100
101 for (int i = 0; i < elements.size(); i++)
102 {
103 GameSupplier supplier = SupplierParser.parseSupplier(
104 (Element) elements.get(i), simulator);
105 if (!Suppliers.addSupplier(supplier))
106 {
107 Logger
108 .severe(
109 SupplierParser.class,
110 "parseSuppliers",
111 "SupplierParser was unable to add supplier with name: "
112 + supplier.getName()
113 + ". Probably there is already a supplier with the same name. Check the .xml description.");
114 }
115 }
116 if (SupplierParser.DEBUG)
117 {
118 System.out.println("SupplierParser: Number of Suppliers: "
119 + Suppliers.getSuppliersArray().length);
120 }
121 return suppliers;
122 } catch (Exception exception)
123 {
124 Logger.severe(SupplierParser.class, "parseSupplier", exception);
125 throw new IOException(exception.getMessage());
126 }
127 }
128
129
130
131 /***
132 * parses the supplier element
133 *
134 * @param element the xml-element
135 * @param simulator the simulator to use
136 * @return GameSupplier
137 * @throws Exception on failure
138 */
139 private static GameSupplier parseSupplier(final Element element,
140 final DEVSSimulatorInterface simulator) throws Exception
141 {
142 String name = null;
143 String nickName = null;
144 double lat = Double.NaN;
145 double lon = Double.NaN;
146 double initialBalance = Double.NaN;
147 Product[] products = null;
148 Double[] amounts = null;
149
150 Namespace space = element.getNamespace();
151 if (element.getChild("name", space) != null)
152 {
153 name = (element.getChildText("name", space));
154 }
155 if (SupplierParser.DEBUG)
156 {
157 System.out.println("SupplierParser: Parsing supplier: " + name);
158 }
159 if (element.getChild("nickname", space) != null)
160 {
161 nickName = (element.getChildText("nickname", space));
162 }
163 if (element.getChild("lat", space) != null)
164 {
165 lat = new Double(element.getChildText("lat", space)).doubleValue();
166 }
167 if (element.getChild("lon", space) != null)
168 {
169 lon = new Double(element.getChildText("lon", space)).doubleValue();
170 }
171 if (element.getChild("initial_balance", space) != null)
172 {
173 initialBalance = new Double(element.getChildText("initial_balance",
174 space)).doubleValue();
175 }
176 if (element.getChild("products", space) != null)
177 {
178 products = SupplierParser.parseProduct(element.getChild("products",
179 space));
180 amounts = SupplierParser.parseAmount(element.getChild("products",
181 space));
182 }
183
184
185 GameSupplier supplier = new GameSupplier(name, simulator, new Point3d(
186 lon, lat, 0.0), new Role[]{}, Game.getBank(), initialBalance,
187 products, amounts);
188 supplier.setSmallMapLocation(LatLonToSmallMap.getSmallMapLocation(lon,
189 lat, 238, 176));
190 supplier.setLocationDescription(nickName);
191
192 return supplier;
193 }
194
195 /***
196 * Parses an element representing a list of products
197 *
198 * @param element the element to parse
199 * @return returns an array with products
200 * @throws Exception thrown in case of an unknown name
201 */
202 private static Product[] parseProduct(final Element element)
203 throws Exception
204 {
205 List products = element.getChildren();
206 List result = new ArrayList();
207 for (int i = 0; i < products.size(); i++)
208 {
209 Element product = (Element) products.get(i);
210 Namespace space = element.getNamespace();
211 String name = null;
212 if (product.getChild("product", space) != null)
213 {
214 name = (product.getChildText("product", space));
215 }
216 result.add(Products.getProduct(name));
217 }
218 return (Product[]) result.toArray(new Product[result.size()]);
219 }
220
221 /***
222 * Parses an element representing a list of products
223 *
224 * @param element the element to parse
225 * @return returns an array with products
226 * @throws Exception thrown in case of an unknown name
227 */
228 private static Double[] parseAmount(final Element element) throws Exception
229 {
230 List products = element.getChildren();
231 List result = new ArrayList();
232 for (int i = 0; i < products.size(); i++)
233 {
234 Element product = (Element) products.get(i);
235 Namespace space = element.getNamespace();
236 double amount = Double.NaN;
237 if (product.getChild("initial_amount", space) != null)
238 {
239 amount = new Double(product.getChildText("initial_amount",
240 space)).doubleValue();
241 }
242 result.add(new Double(amount));
243 }
244 return (Double[]) result.toArray(new Double[result.size()]);
245 }
246 }