1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.gscg.experiment;
15
16 import java.io.IOException;
17 import java.net.URL;
18 import java.util.List;
19
20 import nl.tudelft.simulation.logger.Logger;
21 import nl.tudelft.simulation.supplychain.product.BillOfMaterials;
22 import nl.tudelft.simulation.supplychain.product.Product;
23 import nl.tudelft.simulation.supplychain.product.Unit;
24
25 import org.jdom.Element;
26 import org.jdom.Namespace;
27 import org.jdom.input.SAXBuilder;
28
29 /***
30 * The ProductParser parses xml-based experiments into their java objects.
31 * <p>
32 *
33 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
34 * Delft, the Netherlands. All rights reserved.
35 *
36 * See for project information <a href="http://www.simulation.tudelft.nl/">
37 * www.simulation.tudelft.nl </a>.
38 *
39 * The source code and binary code of this software is proprietary information
40 * of Delft University of Technology.
41 *
42 * @author <a
43 * href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
44 * van Houten </a>
45 * @version $Revision: 1.2 $ $Date: 2005/08/09 15:43:40 $
46 * @since 1.0.0
47 */
48 public class ProductParser
49 {
50
51 /*** builder the xerces parser with validation turned on */
52 private static SAXBuilder builder = new SAXBuilder(
53 "org.apache.xerces.parsers.SAXParser", false);
54 static
55 {
56
57 builder.setFeature("http://xml.org/sax/features/validation", true);
58 builder.setFeature("http://apache.org/xml/features/validation/schema",
59 true);
60 }
61
62 /*** for debugging */
63 private static final boolean DEBUG = true;
64
65 /***
66 * constructs a new ProductParser This is a Utility Class.
67 */
68 protected ProductParser()
69 {
70
71 }
72
73 /***
74 * parses a product xml-file.
75 *
76 * @param schema the schema for validation of the xml-file
77 * @param input the inputstream
78 * @return Scenario the experiment
79 * @throws IOException whenever parsing fails
80 */
81 public static Products parseProducts(final String schema, final URL input)
82 throws IOException
83 {
84 builder
85 .setProperty(
86 "http://apache.org/xml/properties/schema/external-schemaLocation",
87 "http://www.simulation.tudelft.nl " + schema);
88 try
89 {
90 Element rootElement = builder.build(input).getRootElement();
91 Products products = new Products();
92
93 List elements = rootElement.getChildren();
94
95 for (int i = 0; i < elements.size(); i++)
96 {
97 Product product = ProductParser.parseProduct((Element) elements
98 .get(i));
99 if (!Products.addProduct(product))
100 {
101 Logger
102 .severe(
103 ProductParser.class,
104 "parseProducts",
105 "ProductParser was unable to add product with name: "
106 + product.getName()
107 + ". Probably there is already a product with the same name. Check the .xml description.");
108 }
109 }
110 return products;
111 } catch (Exception exception)
112 {
113 Logger.warning(ProductParser.class, "parseExperiment", exception);
114 throw new IOException(exception.getMessage());
115 }
116 }
117
118
119
120 /***
121 * parses the product
122 *
123 * @param element the xml-element
124 * @return Product
125 * @throws Exception on failure
126 */
127 private static Product parseProduct(final Element element) throws Exception
128 {
129 String name = null;
130 Unit unit = null;
131 double baseprice = Double.NaN;
132 double averageUnitWeight = Double.NaN;
133 double depreciation = Double.NaN;
134
135 if (element.getName().equalsIgnoreCase("productwithoutbom"))
136 {
137 Namespace space = element.getNamespace();
138 if (element.getChild("name", space) != null)
139 {
140 name = (element.getChildText("name", space));
141 }
142 if (element.getChild("unit", space) != null)
143 {
144 unit = ProductParser.parseUnit(element.getChildText("unit"));
145 }
146 if (element.getChild("baseprice", space) != null)
147 {
148 baseprice = new Double(element.getChildText("baseprice"))
149 .doubleValue();
150 }
151 if (element.getChild("averageunitweight", space) != null)
152 {
153 averageUnitWeight = new Double(element
154 .getChildText("averageunitweight")).doubleValue();
155 }
156 if (element.getChild("depreciation", space) != null)
157 {
158 depreciation = new Double(element.getChildText("depreciation"))
159 .doubleValue();
160 }
161 return new Product(name, unit, baseprice, averageUnitWeight,
162 depreciation);
163 }
164 if (element.getName().equalsIgnoreCase("productwithbom"))
165 {
166 Namespace space = element.getNamespace();
167 if (element.getChild("name", space) != null)
168 {
169 name = (element.getChildText("name", space));
170 }
171 if (element.getChild("unit", space) != null)
172 {
173 unit = ProductParser.parseUnit(element.getChildText("unit"));
174 }
175 if (element.getChild("baseprice", space) != null)
176 {
177 baseprice = new Double(element.getChildText("baseprice"))
178 .doubleValue();
179 }
180 if (element.getChild("averageunitweight", space) != null)
181 {
182 averageUnitWeight = new Double(element
183 .getChildText("averageunitweight")).doubleValue();
184 }
185 if (element.getChild("depreciation", space) != null)
186 {
187 depreciation = new Double(element.getChildText("depreciation"))
188 .doubleValue();
189 }
190
191 Product productWithBom = new Product(name, unit, baseprice,
192 averageUnitWeight, depreciation);
193 Element bomElement = element.getChild("bom");
194 List productsForBom = bomElement.getChildren("product");
195 BillOfMaterials bom = new BillOfMaterials(productWithBom);
196
197 for (int ii = 0; ii < productsForBom.size(); ii++)
198 {
199 Element bomProductElement = (Element) productsForBom.get(ii);
200 String bomProductName = null;
201 Product bomProduct = null;
202 double bomProductAmount = Double.NaN;
203 bomProductName = bomProductElement.getChildText("product",
204 space);
205 bomProductAmount = new Double(bomProductElement.getChildText(
206 "amount", space)).doubleValue();
207 bomProduct = Products.getProduct(bomProductName);
208 if (ProductParser.DEBUG)
209 {
210 System.out
211 .println("DEBUG -- ProductParser: bomProduct found: "
212 + bomProduct);
213 }
214
215 bom.add(bomProduct, bomProductAmount);
216 }
217
218 productWithBom.setBillOfMaterials(bom);
219 if (ProductParser.DEBUG)
220 {
221 System.out.println("DEBUG -- ProductParser: bom created: "
222 + bom);
223 }
224 return productWithBom;
225 }
226 return null;
227 }
228
229 /***
230 * Parses a string representing a unit into a unit
231 *
232 * @param name the name of the unit
233 * @return returns a unit based on the name
234 * @throws Exception thrown in case of an unknown name
235 */
236 public static Unit parseUnit(final String name) throws Exception
237 {
238 if (name.equals("BOX"))
239 {
240 return Unit.BOX;
241 }
242 if (name.equals("CONTAINER 20FT"))
243 {
244 return Unit.CONTAINER20FT;
245 }
246 if (name.equals("CONTAINER 40FT"))
247 {
248 return Unit.CONTAINER40FT;
249 }
250 if (name.equals("KG"))
251 {
252 return Unit.KG;
253 }
254 if (name.equals("M3"))
255 {
256 return Unit.M3;
257 }
258 if (name.equals("PALLET"))
259 {
260 return Unit.PALLET;
261 }
262 if (name.equals("PIECE"))
263 {
264 return Unit.PIECE;
265 }
266 throw new Exception("ProductParser: parseUnit.. unknown argument: "
267 + name);
268 }
269 }