001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util;
028:
029: import java.util.*;
030: import java.sql.*;
031: import java.io.*;
032:
033: // MEIWriter program :
034: // Read MEI database file and create XML prototype files
035:
036: // Note that the file can be read from any JDBC driver for this schema,
037: // including any JDBC driver.
038: // In particular, intended for MS EXCEL/ACCESS files using ODBC/JDBC
039:
040: // Database Schema:
041: // create table MEI (
042: // Description String,
043: // NSN String,
044: // Alternate String,
045: // Length Double,
046: // Width Double,
047: // Height Double,
048: // FootprintArea Double,
049: // Volume Double,
050: // Mass Double,
051: // Moveable Boolean,
052: // CargoCategoryCode String
053: // );
054:
055: class MEIWriter {
056:
057: // Top level 'main' test program
058: // Usage : java MEIWriter <driver_name> <db_url> <db_user> <db_password>
059: // Default arguments refer to ODBC connection named 'MEI'
060: public static void main(String args[]) throws SQLException {
061: String driver_name = (args.length >= 1 ? args[0]
062: : "sun.jdbc.odbc.JdbcOdbcDriver");
063: String database_url = (args.length >= 2 ? args[1]
064: : "jdbc:odbc:MEI");
065: String database_user = (args.length >= 3 ? args[2] : "");
066: String database_password = (args.length >= 4 ? args[3] : "");
067: System.out.println("MEIWriter : " + driver_name + "/"
068: + database_url + "/" + database_user + "/"
069: + database_password);
070: new MEIWriter().parseMEIData(driver_name, database_url,
071: database_user, database_password);
072: }
073:
074: // Parse MEI data from given database
075: // Read info and write appropriate .ini and .dat files
076: // Given classname of JDBC driver, URL of database and user/password
077: protected void parseMEIData(String driver_classname,
078: String datasource_url, String username, String password) {
079:
080: // DriverManager.setLogWriter(new PrintWriter(System.out));
081:
082: System.out.println("Loading driver " + driver_classname);
083:
084: try {
085: Class driver = Class.forName(driver_classname);
086: } catch (ClassNotFoundException e) {
087: System.out.println("Could not load driver : "
088: + driver_classname);
089: e.printStackTrace();
090: }
091:
092: System.out.println("Connecting to the datasource : "
093: + datasource_url);
094: try {
095: Connection conn = DriverManager.getConnection(
096: datasource_url, username, password);
097:
098: Statement stmt = conn.createStatement();
099:
100: ResultSet rset = stmt
101: .executeQuery("select Description, NSN, Alternate, "
102: + "Length, Width, "
103: + "Height, FootprintArea, Volume, Mass, "
104: + "Moveable, CargoCategoryCode from MEI");
105:
106: while (rset.next()) {
107: desc = rset.getString(1);
108: nsn = rset.getString(2);
109: alternate = rset.getString(3);
110: length = rset.getDouble(4);
111: width = rset.getDouble(5);
112: height = rset.getDouble(6);
113: footprint_area = rset.getDouble(7);
114: volume = rset.getDouble(8);
115: mass = rset.getDouble(9);
116: moveable = rset.getBoolean(10);
117: cargo_category_code = rset.getString(11);
118:
119: String filename = generateFileName(nsn);
120: System.out.println("Generating XML prototype file : "
121: + filename + " for " + desc);
122:
123: PrintWriter pw = createPrintWriter(filename);
124:
125: writePrototype(pw);
126:
127: pw.close();
128: }
129:
130: conn.close();
131: } catch (IOException ie) {
132: System.out.println("IO Exception");
133: ie.printStackTrace();
134: } catch (SQLException sqle) {
135: System.out.println("SQL Exception");
136: sqle.printStackTrace();
137: }
138: }
139:
140: private void writePrototype(PrintWriter pw) {
141: print(pw, "<prototype name = " + '"' + nsn + '"' + ">", 0);
142: print(pw, "<object class = " + '"'
143: + "org.cougaar.planning.ldm.asset.Asset" + '"' + ">", 2);
144: writeTypeIdentificationPG(pw);
145: writePhysicalPG(pw);
146: if (moveable) {
147: writeMovabilityPG(pw);
148: }
149: print(pw, "</object>", 2);
150: print(pw, "</prototype>", 0);
151: }
152:
153: private void writeTypeIdentificationPG(PrintWriter pw) {
154: writeComplexFieldStart(pw, "TypeIdentificationPG",
155: "org.cougaar.planning.ldm.asset.NewTypeIdentificationPG");
156: writeSimpleField(pw, "TypeIdentification", "String", desc);
157: writeSimpleField(pw, "Nomenclature", "String", "NSN/"
158: + removeChar(nsn, '-'));
159: writeSimpleField(pw, "AlternateTypeIdentification", "String",
160: alternate);
161: writeComplexFieldEnd(pw);
162: }
163:
164: private void writePhysicalPG(PrintWriter pw) {
165: writeComplexFieldStart(pw, "PhysicalPG",
166: "org.cougaar.planning.ldm.asset.NewPhysicalPG");
167: writeMeasureField(pw, "Length",
168: "org.cougaar.planning.ldm.measure.Distance", "Inches",
169: length);
170: writeMeasureField(pw, "Width",
171: "org.cougaar.planning.ldm.measure.Distance", "Inches",
172: width);
173: writeMeasureField(pw, "Height",
174: "org.cougaar.planning.ldm.measure.Distance", "Inches",
175: height);
176: writeMeasureField(pw, "Mass",
177: "org.cougaar.planning.ldm.measure.Mass", "Pounds", mass);
178: writeMeasureField(pw, "FootprintArea",
179: "org.cougaar.planning.ldm.measure.Area", "SquareFeet",
180: footprint_area);
181: writeMeasureField(pw, "Volume",
182: "org.cougaar.planning.ldm.measure.Volume", "CubicFeet",
183: volume);
184: writeComplexFieldEnd(pw);
185: }
186:
187: private void writeMovabilityPG(PrintWriter pw) {
188: writeComplexFieldStart(pw, "MovabilityPG",
189: "org.cougaar.planning.ldm.asset.NewMovabilityPG");
190: writeSimpleField(pw, "Moveable", "boolean", (moveable ? "True"
191: : "False"));
192: writeSimpleField(pw, "CargoCategoryCode", "String",
193: cargo_category_code);
194: writeComplexFieldEnd(pw);
195:
196: }
197:
198: private void writeComplexFieldStart(PrintWriter pw,
199: String type_name, String class_name) {
200: print(pw, "<field name=" + '"' + type_name + '"' + " type = "
201: + '"' + "object" + '"' + ">", 4);
202: print(pw, "<value>", 6);
203: print(pw, "<object class = " + '"' + class_name + '"' + ">", 8);
204: }
205:
206: private void writeComplexFieldEnd(PrintWriter pw) {
207: print(pw, "</object>", 8);
208: print(pw, "</value>", 6);
209: print(pw, "</field>", 4);
210: }
211:
212: private void writeMeasureField(PrintWriter pw, String field_name,
213: String measure_class, String units, double value) {
214: print(pw, "<field name = " + '"' + field_name + '"' + " type="
215: + '"' + "object" + '"' + ">", 4);
216: print(pw, "<value>", 6);
217: print(pw, "<object class=" + '"' + measure_class + '"' + ">", 8);
218: print(pw, "<field name=" + '"' + units + '"' + " type=" + '"'
219: + "double" + '"' + ">", 10);
220: print(pw, "<value>" + value + "</value>", 12);
221: print(pw, "</field>", 10);
222: print(pw, "</object>", 8);
223: print(pw, "</value>", 6);
224: print(pw, "</field>", 4);
225: }
226:
227: private void writeSimpleField(PrintWriter pw, String field_name,
228: String field_type, String field_value) {
229: print(pw, "<field name=" + '"' + field_name + '"' + " type = "
230: + '"' + field_type + '"' + ">", 10);
231: print(pw, "<value>" + field_value + "</value>", 12);
232: print(pw, "</field>", 10);
233: }
234:
235: // Utility functions
236:
237: // Create a PrintWriter class for given filename
238: private PrintWriter createPrintWriter(String filename)
239: throws IOException {
240: return new PrintWriter(new OutputStreamWriter(
241: new FileOutputStream(filename)));
242: }
243:
244: // Print a string with given indentation
245: private void print(PrintWriter pw, String text, int indent) {
246: String indent_string = "";
247: for (int i = 0; i < indent; i++)
248: indent_string = indent_string + ' ';
249: pw.println(indent_string + text);
250: }
251:
252: private String removeChar(String original, char to_remove) {
253: String stripped = "";
254: for (int i = 0; i < original.length(); i++) {
255: char c = original.charAt(i);
256: if (c != to_remove)
257: stripped = stripped + c;
258: }
259: return stripped;
260: }
261:
262: // Turn an nsn into a filename by removing hyphens,
263: // adding 'NSN-' prefix, and '.xml suffix
264: private String generateFileName(String nsn)
265:
266: {
267: return "NSN-" + removeChar(nsn, '-') + ".xml";
268: }
269:
270: // private instance variables for storing information from table rows
271: String desc;
272: String nsn;
273: String alternate;
274: double length;
275: double width;
276: double height;
277: double footprint_area;
278: double volume;
279: double mass;
280: boolean moveable;
281: String cargo_category_code;
282:
283: }
|