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.ManufacturerParser;
28 import org.gscg.experiment.Manufacturers;
29 import org.gscg.experiment.ProductParser;
30 import org.gscg.experiment.Products;
31 import org.jdom.Attribute;
32 import org.jdom.Element;
33 import org.jdom.input.SAXBuilder;
34 import org.jdom.output.Format;
35 import org.jdom.output.XMLOutputter;
36
37 /***
38 * <p>
39 *
40 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
41 * Delft, the Netherlands. All rights reserved.
42 *
43 * See for project information <a href="http://www.simulation.tudelft.nl/">
44 * www.simulation.tudelft.nl </a>.
45 *
46 * The source code and binary code of this software is proprietary information
47 * of Delft University of Technology.
48 *
49 * @author <a
50 * href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
51 * van Houten </a>
52 * @version $Revision: 1.3 $ $Date: 2005/08/09 15:43:42 $
53 * @since 1.0.0
54 */
55 public class DistributorXSDParser
56 {
57
58 /***
59 * constructs a new DistributorXSDParser
60 */
61 public DistributorXSDParser()
62 {
63 super();
64 this.parse();
65 }
66
67 /***
68 * starts parsing
69 */
70 private void parse()
71 {
72 try
73 {
74
75
76 Experiment experiment = TestExperiment.createExperiment();
77 DEVSSimulatorInterface simulator = new DEVSSimulator();
78 experiment.setSimulator(simulator);
79 experiment.setModel(new TestModel(experiment));
80
81 try
82 {
83 simulator.initialize(experiment.getTreatments()[0]
84 .getRunControl().getReplications()[0]);
85 } catch (Exception e)
86 {
87 e.printStackTrace();
88 }
89
90
91
92
93 if (Products.getProductsArray().length == 0)
94 {
95 try
96 {
97 String xsd = URLResource.getResource("/xsd/product.xsd")
98 .toExternalForm();
99 ProductParser.parseProducts(xsd, URLResource
100 .getResource("/xml/products.xml"));
101 } catch (Exception exception)
102 {
103 Logger.severe(this, "<init>", exception);
104 }
105 }
106
107
108
109
110 if (Manufacturers.getManufacturersArray().length == 0)
111 {
112 try
113 {
114 String xsd = URLResource.getResource("/manufacturer.xsd")
115 .toExternalForm();
116 ManufacturerParser
117 .parseManufacturers(xsd, URLResource
118 .getResource("/manufacturers.xml"), null,
119 simulator);
120 } catch (Exception exception)
121 {
122 Logger.severe(this, "<init>", exception);
123 }
124 }
125
126 String xsd = URLResource.getResource("/distributor.xsd")
127 .toExternalForm();
128
129 SAXBuilder builder = new SAXBuilder(
130 "org.apache.xerces.parsers.SAXParser", false);
131
132
133 builder.setFeature("http://xml.org/sax/features/validation", true);
134
135 Element rootElement = builder.build(xsd).getRootElement();
136 List list = rootElement.getChildren();
137 for (int i = 0; i < list.size(); i++)
138 {
139 Element child = (Element) list.get(i);
140
141 List attributes = child.getAttributes();
142 for (int attribute = 0; attribute < attributes.size(); attribute++)
143 {
144 Attribute element = (Attribute) attributes.get(attribute);
145 if (element.getValue().equalsIgnoreCase("productsEnum"))
146 {
147 for (int ii = 0; ii < child.getChildren().size(); ii++)
148 {
149 Element child2 = (Element) child.getChildren().get(
150 ii);
151 for (int iii = 0; iii < child2.getChildren().size(); iii++)
152 {
153 Element clone = (Element) ((Element) child2
154 .getChildren().get(iii)).clone();
155 child2.getChildren().clear();
156
157 for (int products = 0; products < Products
158 .getProductsArray().length; products++)
159 {
160 Element secondClone = (Element) clone
161 .clone();
162 Attribute attrib = secondClone
163 .getAttribute("value");
164 attrib.setValue((Products
165 .getProductsArray()[products])
166 .getName());
167 child2.addContent(secondClone);
168 }
169 break;
170 }
171 break;
172 }
173 }
174 if (element.getValue()
175 .equalsIgnoreCase("manufacturersEnum"))
176 {
177 for (int ii = 0; ii < child.getChildren().size(); ii++)
178 {
179 Element child2 = (Element) child.getChildren().get(
180 ii);
181 for (int iii = 0; iii < child2.getChildren().size(); iii++)
182 {
183 Element clone = (Element) ((Element) child2
184 .getChildren().get(iii)).clone();
185 child2.getChildren().clear();
186
187 for (int manufacturers = 0; manufacturers < Manufacturers
188 .getManufacturersArray().length; manufacturers++)
189 {
190 Element secondClone = (Element) clone
191 .clone();
192 Attribute attrib = secondClone
193 .getAttribute("value");
194 attrib
195 .setValue((Manufacturers
196 .getManufacturersArray()[manufacturers])
197 .getName());
198 child2.addContent(secondClone);
199 }
200 break;
201 }
202 break;
203 }
204 }
205 }
206 }
207
208 XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
209 outputter.setFormat(Format.getRawFormat());
210
211 FileOutputStream fos = new FileOutputStream("distributor.xsd");
212 outputter.output(rootElement, fos);
213 fos.close();
214 } catch (Exception exception)
215 {
216 exception.printStackTrace();
217 }
218 }
219
220 /***
221 * @param args command-line arguments
222 */
223 public static void main(final String[] args)
224 {
225 new DistributorXSDParser();
226 }
227 }