View Javadoc

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