1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.gscg.experiment.xsd;
15
16 import java.io.FileOutputStream;
17 import java.util.List;
18
19 import nl.tudelft.simulation.dsol.experiment.Experiment;
20 import nl.tudelft.simulation.dsol.simulators.DEVSSimulator;
21 import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
22 import nl.tudelft.simulation.language.io.URLResource;
23 import nl.tudelft.simulation.logger.Logger;
24
25 import org.gscg.TestExperiment;
26 import org.gscg.TestModel;
27 import org.gscg.experiment.DistributorParser;
28 import org.gscg.experiment.Distributors;
29 import org.gscg.experiment.ManufacturerParser;
30 import org.gscg.experiment.Manufacturers;
31 import org.gscg.experiment.MarketParser;
32 import org.gscg.experiment.Markets;
33 import org.gscg.experiment.ProductParser;
34 import org.gscg.experiment.Products;
35 import org.jdom.Attribute;
36 import org.jdom.Element;
37 import org.jdom.input.SAXBuilder;
38 import org.jdom.output.Format;
39 import org.jdom.output.XMLOutputter;
40
41 /***
42 * The ScriptXSDParser is used to parse a xml-schema file, and update it with
43 * the latest information on which it depends such as the number of
44 * manufacturers in a supply chain game.
45 * <p>
46 *
47 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
48 * Delft, the Netherlands. All rights reserved.
49 *
50 * See for project information <a href="http://www.simulation.tudelft.nl/">
51 * www.simulation.tudelft.nl </a>.
52 *
53 * The source code and binary code of this software is proprietary information
54 * of Delft University of Technology.
55 *
56 * @author <a
57 * href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
58 * van Houten </a>
59 * @version $Revision: 1.3 $ $Date: 2005/08/09 15:43:42 $
60 * @since 1.0.0
61 */
62 public class ScriptXSDParser
63 {
64 /***
65 * constructs a new ScriptXSDParser
66 */
67 public ScriptXSDParser()
68 {
69 super();
70 this.parse();
71 }
72
73 /***
74 * starts parsing
75 */
76 private void parse()
77 {
78 try
79 {
80
81
82 Experiment experiment = TestExperiment.createExperiment();
83 DEVSSimulatorInterface simulator = new DEVSSimulator();
84 experiment.setSimulator(simulator);
85 experiment.setModel(new TestModel(experiment));
86
87 try
88 {
89 simulator.initialize(experiment.getTreatments()[0]
90 .getRunControl().getReplications()[0]);
91 } catch (Exception e)
92 {
93 e.printStackTrace();
94 }
95
96
97
98
99 if (Products.getProductsArray().length == 0)
100 {
101 try
102 {
103 String xsd = URLResource.getResource("/product.xsd")
104 .toExternalForm();
105 ProductParser.parseProducts(xsd, URLResource
106 .getResource("/products.xml"));
107 } catch (Exception exception)
108 {
109 Logger.severe(this, "<init>", exception);
110 }
111 }
112
113
114
115
116 if (Manufacturers.getManufacturersArray().length == 0)
117 {
118 try
119 {
120 String xsd = URLResource.getResource("/manufacturer.xsd")
121 .toExternalForm();
122 ManufacturerParser
123 .parseManufacturers(xsd, URLResource
124 .getResource("/manufacturers.xml"), null,
125 simulator);
126 } catch (Exception exception)
127 {
128 Logger.severe(this, "<init>", exception);
129 }
130 }
131
132
133
134
135 if (Distributors.getDistributorsArray().length == 0)
136 {
137 try
138 {
139 String xsd = URLResource.getResource("/distributor.xsd")
140 .toExternalForm();
141 DistributorParser.parseDistributors(xsd, URLResource
142 .getResource("/distributors.xml"), null, simulator);
143 } catch (Exception exception)
144 {
145 Logger.severe(this, "<init>", exception);
146 }
147 }
148
149
150
151
152 if (Markets.getMarketsArray().length == 0)
153 {
154 try
155 {
156 String xsd = URLResource.getResource("/market.xsd")
157 .toExternalForm();
158 MarketParser.parseMarkets(xsd, URLResource
159 .getResource("/markets.xml"), simulator);
160 } catch (Exception exception)
161 {
162 Logger.severe(this, "<init>", exception);
163 }
164 }
165
166 String xsd = URLResource.getResource("/scenario.xsd")
167 .toExternalForm();
168
169 SAXBuilder builder = new SAXBuilder(
170 "org.apache.xerces.parsers.SAXParser", false);
171
172
173 builder.setFeature("http://xml.org/sax/features/validation", true);
174
175 Element rootElement = builder.build(xsd).getRootElement();
176 List list = rootElement.getChildren();
177 for (int i = 0; i < list.size(); i++)
178 {
179 Element child = (Element) list.get(i);
180
181 List attributes = child.getAttributes();
182 for (int attribute = 0; attribute < attributes.size(); attribute++)
183 {
184 Attribute element = (Attribute) attributes.get(attribute);
185 if (element.getValue().equalsIgnoreCase("productsEnum"))
186 {
187 for (int ii = 0; ii < child.getChildren().size(); ii++)
188 {
189 Element child2 = (Element) child.getChildren().get(
190 ii);
191 for (int iii = 0; iii < child2.getChildren().size(); iii++)
192 {
193 Element clone = (Element) ((Element) child2
194 .getChildren().get(iii)).clone();
195 child2.getChildren().clear();
196
197 for (int products = 0; products < Products
198 .getProductsArray().length; products++)
199 {
200 Element secondClone = (Element) clone
201 .clone();
202 Attribute attrib = secondClone
203 .getAttribute("value");
204 attrib.setValue((Products
205 .getProductsArray()[products])
206 .getName());
207 child2.addContent(secondClone);
208 }
209 break;
210 }
211 break;
212 }
213 }
214 if (element.getValue()
215 .equalsIgnoreCase("manufacturersEnum"))
216 {
217 for (int ii = 0; ii < child.getChildren().size(); ii++)
218 {
219 Element child2 = (Element) child.getChildren().get(
220 ii);
221 for (int iii = 0; iii < child2.getChildren().size(); iii++)
222 {
223 Element clone = (Element) ((Element) child2
224 .getChildren().get(iii)).clone();
225 child2.getChildren().clear();
226
227 for (int manufacturers = 0; manufacturers < Manufacturers
228 .getManufacturersArray().length; manufacturers++)
229 {
230 Element secondClone = (Element) clone
231 .clone();
232 Attribute attrib = secondClone
233 .getAttribute("value");
234 attrib
235 .setValue((Manufacturers
236 .getManufacturersArray()[manufacturers])
237 .getName());
238 child2.addContent(secondClone);
239 }
240 break;
241 }
242 break;
243 }
244 }
245 if (element.getValue().equalsIgnoreCase("distributorsEnum"))
246 {
247 for (int ii = 0; ii < child.getChildren().size(); ii++)
248 {
249 Element child2 = (Element) child.getChildren().get(
250 ii);
251 for (int iii = 0; iii < child2.getChildren().size(); iii++)
252 {
253 Element clone = (Element) ((Element) child2
254 .getChildren().get(iii)).clone();
255 child2.getChildren().clear();
256
257 for (int distributors = 0; distributors < Distributors
258 .getDistributorsArray().length; distributors++)
259 {
260 Element secondClone = (Element) clone
261 .clone();
262 Attribute attrib = secondClone
263 .getAttribute("value");
264 attrib
265 .setValue((Distributors
266 .getDistributorsArray()[distributors])
267 .getName());
268 child2.addContent(secondClone);
269 }
270 break;
271 }
272 break;
273 }
274 }
275 if (element.getValue().equalsIgnoreCase("marketsEnum"))
276 {
277 for (int ii = 0; ii < child.getChildren().size(); ii++)
278 {
279 Element child2 = (Element) child.getChildren().get(
280 ii);
281 for (int iii = 0; iii < child2.getChildren().size(); iii++)
282 {
283 Element clone = (Element) ((Element) child2
284 .getChildren().get(iii)).clone();
285 child2.getChildren().clear();
286
287 for (int markets = 0; markets < Markets
288 .getMarketsArray().length; markets++)
289 {
290 Element secondClone = (Element) clone
291 .clone();
292 Attribute attrib = secondClone
293 .getAttribute("value");
294 attrib
295 .setValue((Markets
296 .getMarketsArray()[markets])
297 .getName());
298 child2.addContent(secondClone);
299 }
300 break;
301 }
302 break;
303 }
304 }
305 }
306 }
307
308 XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
309 outputter.setFormat(Format.getRawFormat());
310
311 FileOutputStream fos = new FileOutputStream("scenario.xsd");
312 outputter.output(rootElement, fos);
313 fos.close();
314 } catch (Exception exception)
315 {
316 exception.printStackTrace();
317 }
318 }
319
320 /***
321 * @param args command-line arguments
322 */
323 public static void main(final String[] args)
324 {
325 new ScriptXSDParser();
326 }
327 }