View Javadoc

1   /*
2    * @(#) ManufacturerXSDParser.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.language.io.URLResource;
20  import nl.tudelft.simulation.logger.Logger;
21  
22  import org.gscg.experiment.ProductParser;
23  import org.gscg.experiment.Products;
24  import org.jdom.Attribute;
25  import org.jdom.Element;
26  import org.jdom.input.SAXBuilder;
27  import org.jdom.output.Format;
28  import org.jdom.output.XMLOutputter;
29  
30  /***
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/03 08:52:50 $
46   * @since 1.0.0
47   */
48  public class ManufacturerXSDParser
49  {
50  
51  	/***
52  	 * constructs a new ManufacturerXSDParser
53  	 */
54  	public ManufacturerXSDParser()
55  	{
56  		super();
57  		this.parse();
58  	}
59  
60  	/***
61  	 * starts parsing
62  	 */
63  	private void parse()
64  	{
65  		try
66  		{
67  
68  			//
69  			// create the products
70  			//
71  			if (Products.getProductsArray().length == 0)
72  			{
73  				try
74  				{
75  					String xsd = URLResource.getResource("/product.xsd")
76  							.toExternalForm();
77  					ProductParser.parseProducts(xsd, URLResource
78  							.getResource("/products.xml"));
79  				} catch (Exception exception)
80  				{
81  					Logger.severe(this, "<init>", exception);
82  				}
83  			}
84  
85  			String xsd = URLResource.getResource("/manufacturer.xsd")
86  					.toExternalForm();
87  
88  			SAXBuilder builder = new SAXBuilder(
89  					"org.apache.xerces.parsers.SAXParser", false);
90  
91  			// turns on Schema Validation with Xerces
92  			builder.setFeature("http://xml.org/sax/features/validation", true);
93  
94  			Element rootElement = builder.build(xsd).getRootElement();
95  			List list = rootElement.getChildren();
96  			for (int i = 0; i < list.size(); i++)
97  			{
98  				Element child = (Element) list.get(i);
99  
100 				List attributes = child.getAttributes();
101 				for (int attribute = 0; attribute < attributes.size(); attribute++)
102 				{
103 					Attribute element = (Attribute) attributes.get(attribute);
104 					if (element.getValue().equalsIgnoreCase("productsEnum"))
105 					{
106 						for (int ii = 0; ii < child.getChildren().size(); ii++)
107 						{
108 							Element child2 = (Element) child.getChildren().get(
109 									ii);
110 							for (int iii = 0; iii < child2.getChildren().size(); iii++)
111 							{
112 								Element clone = (Element) ((Element) child2
113 										.getChildren().get(iii)).clone();
114 								child2.getChildren().clear();
115 
116 								for (int products = 0; products < Products
117 										.getProductsArray().length; products++)
118 								{
119 									Element secondClone = (Element) clone
120 											.clone();
121 									Attribute attrib = secondClone
122 											.getAttribute("value");
123 									attrib.setValue((Products
124 											.getProductsArray()[products])
125 											.getName());
126 									child2.addContent(secondClone);
127 								}
128 								break;
129 							}
130 							break;
131 						}
132 					}
133 				}
134 			}
135 
136 			XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
137 			outputter.setFormat(Format.getRawFormat());
138 
139 			FileOutputStream fos = new FileOutputStream("manufacturer.xsd");
140 			outputter.output(rootElement, fos);
141 			fos.close();
142 		} catch (Exception exception)
143 		{
144 			exception.printStackTrace();
145 		}
146 	}
147 
148 	/***
149 	 * @param args command-line arguments
150 	 */
151 	public static void main(final String[] args)
152 	{
153 		new ManufacturerXSDParser();
154 	}
155 }