001: package com.salmonllc.xml;
002:
003: /////////////////////////
004: //$Archive: /SOFIA/SourceCode/com/salmonllc/xml/XMLStoreParser.java $
005: //$Author: Len $
006: //$Revision: 28 $
007: //$Modtime: 6/17/03 2:14p $
008: /////////////////////////
009: /**
010: * The XML Store paser is used to parse any XML file specified as datastore.dtd file.
011: */
012: import java.io.*;
013:
014: import org.w3c.dom.*;
015:
016: import com.salmonllc.sql.DataStoreEvaluator;
017: import com.salmonllc.util.Util;
018:
019: public class XMLStoreParser {
020:
021: /** Default parser name. */
022: private static final String DEFAULT_PARSER_NAME = "com.salmonllc.xml.DOMParser";
023:
024: private static ResultSetMetaData _rsMetaData = new ResultSetMetaData();
025: private static XMLDataStore _dataStore = new XMLDataStore();
026: private static String URIpath = "";
027:
028: /** Default Encoding */
029: private static String PRINTWRITER_ENCODING = "UTF8";
030:
031: /*private static String MIME2JAVA_ENCODINGS[] =
032: {
033: "Default",
034: "UTF-8",
035: "US-ASCII",
036: "ISO-8859-1",
037: "ISO-8859-2",
038: "ISO-8859-3",
039: "ISO-8859-4",
040: "ISO-8859-5",
041: "ISO-8859-6",
042: "ISO-8859-7",
043: "ISO-8859-8",
044: "ISO-8859-9",
045: "ISO-2022-JP",
046: "SHIFT_JIS",
047: "EUC-JP",
048: "GB2312",
049: "BIG5",
050: "EUC-KR",
051: "ISO-2022-KR",
052: "KOI8-R",
053: "EBCDIC-CP-US",
054: "EBCDIC-CP-CA",
055: "EBCDIC-CP-NL",
056: "EBCDIC-CP-DK",
057: "EBCDIC-CP-NO",
058: "EBCDIC-CP-FI",
059: "EBCDIC-CP-SE",
060: "EBCDIC-CP-IT",
061: "EBCDIC-CP-ES",
062: "EBCDIC-CP-GB",
063: "EBCDIC-CP-FR",
064: "EBCDIC-CP-AR1",
065: "EBCDIC-CP-HE",
066: "EBCDIC-CP-CH",
067: "EBCDIC-CP-ROECE",
068: "EBCDIC-CP-YU",
069: "EBCDIC-CP-IS",
070: "EBCDIC-CP-AR2",
071: "UTF-16" };*/
072:
073: /** Print writer. */
074: protected PrintWriter out;
075:
076: /** Canonical output. */
077: protected boolean canonical;
078:
079: public XMLStoreParser(String encoding, boolean canonical)
080: throws UnsupportedEncodingException {
081: out = new PrintWriter(new OutputStreamWriter(System.out,
082: encoding));
083: this .canonical = canonical;
084: _rsMetaData = new ResultSetMetaData();
085: } // <init>(String,boolean)
086:
087: //
088: // Constructors
089: //
090:
091: /** Default constructor. */
092: public XMLStoreParser(boolean canonical)
093: throws UnsupportedEncodingException {
094: this (getWriterEncoding(), canonical);
095: }
096:
097: /**
098: * This method returns the XML datastore created by reading the XML file
099: * Creation date: (8/7/01 5:17:42 PM)
100: * @return com.salmonllc.xml.XMLDataStore
101: */
102: public static XMLDataStore getDataStore() {
103: return _dataStore;
104: }
105:
106: /**
107: * Gets the ResuletSetMetaData for the XML file uploaded.
108: * Creation date: (7/18/01 4:29:55 PM)
109: * @return com.afg.applications.investment.xml.ResultSetMetaData
110: */
111: public ResultSetMetaData getMetaData() {
112: return _rsMetaData;
113: }
114:
115: /**
116: * Creation date: (7/20/01 2:03:28 PM)
117: * @return java.lang.String
118: * @param node org.w3c.dom.Node
119: * @param columnName java.lang.String
120: */
121: private boolean getNodeBooleanValue(Node node, String columnName) {
122:
123: Node temp = node.getAttributes().getNamedItem(columnName);
124: if (temp != null)
125: return temp.getNodeValue().equals("true");
126:
127: return false;
128: }
129:
130: /**
131: * Creation date: (7/20/01 2:03:28 PM)
132: * @return java.lang.String
133: * @param node org.w3c.dom.Node
134: * @param columnName java.lang.String
135: */
136: private String getNodeValue(Node node, String columnName) {
137:
138: Node temp = node.getAttributes().getNamedItem(columnName);
139: if (temp != null)
140: return temp.getNodeValue();
141:
142: return null;
143: }
144:
145: // getWriterEncoding
146: private static String getWriterEncoding() {
147: return (PRINTWRITER_ENCODING);
148: }
149:
150: /** Normalizes the given string. */
151: protected String normalize(String s) {
152: StringBuffer str = new StringBuffer();
153:
154: int len = (s != null) ? s.length() : 0;
155: for (int i = 0; i < len; i++) {
156: char ch = s.charAt(i);
157: switch (ch) {
158: case '<': {
159: str.append("<");
160: break;
161: }
162: case '>': {
163: str.append(">");
164: break;
165: }
166: case '&': {
167: str.append("&");
168: break;
169: }
170: case '"': {
171: str.append(""");
172: break;
173: }
174: case '\r':
175: case '\n': {
176: if (canonical) {
177: str.append("&#");
178: str.append(Integer.toString(ch));
179: str.append(';');
180: break;
181: }
182: // else, default append char
183: }
184: default: {
185: str.append(ch);
186: }
187: }
188: }
189:
190: return (str.toString());
191:
192: } // normalize(String):String
193:
194: /**
195: * This method is used to create the XMLDatastore object out of the XML file. XML file name
196: * is passed as uri. Method getDataStore or getMetaData is used after this method is called.
197: *
198: */
199: public static void parseFile(String uri) throws Exception {
200: parseFile(DEFAULT_PARSER_NAME, uri, false);
201:
202: } // print(String,String,boolean)
203:
204: /** Prints the resulting document tree. */
205: private static void parseFile(String parserWrapperName, String uri,
206: boolean canonical) throws Exception {
207: int nUriLastSlashIndex = uri.lastIndexOf("\\");
208: boolean anypath = true;
209: if (nUriLastSlashIndex == -1) {
210: anypath = false;
211: nUriLastSlashIndex = uri.lastIndexOf("/");
212: if (nUriLastSlashIndex != -1) {
213: anypath = true;
214: }
215: }
216:
217: if (anypath)
218: URIpath = uri.substring(0, nUriLastSlashIndex);
219:
220: DOMParserWrapper parser = (DOMParserWrapper) Class.forName(
221: parserWrapperName).newInstance();
222: Document document = parser.parse(uri);
223: XMLStoreParser writer = new XMLStoreParser(canonical);
224: writer.parseNode(document);
225: System.out.println("XML Parsing done" + _dataStore);
226: } // print(String,String,boolean)
227:
228: /** Prints the specified node, recursively. */
229: private void parseNode(Node node) {
230:
231: // is there anything to do?
232: if (node == null) {
233: return;
234: }
235:
236: int type = node.getNodeType();
237: switch (type) {
238: case Node.DOCUMENT_NODE:
239: parseNode(((Document) node).getDocumentElement());
240: break;
241: // print element with attributes
242: case Node.ELEMENT_NODE:
243:
244: if (node.getNodeName()
245: .equalsIgnoreCase("ResultSetMetaData")) {
246: NodeList children = node.getChildNodes();
247: if (children != null) {
248: if (_dataStore == null)
249: _dataStore = new XMLDataStore();
250:
251: _rsMetaData.setMetaData(new java.util.Vector());
252: _dataStore.setRsm(_rsMetaData);
253:
254: int len = children.getLength();
255: for (int i = 0; i < len; i++) {
256: Node nodeChild = children.item(i);
257: if (nodeChild.getNodeName().equalsIgnoreCase(
258: "ColumnMetaData")) {
259: ColumnMetaData columnMeta = new ColumnMetaData();
260: columnMeta.setComponent(getNodeValue(
261: nodeChild, "component"));
262: columnMeta.setComponentType(getNodeValue(
263: nodeChild, "componenttype"));
264:
265: columnMeta.setInternalName(getNodeValue(
266: nodeChild, "internalname"));
267:
268: columnMeta.setColumnName(getNodeValue(
269: nodeChild, "columnname"));
270: columnMeta.setColumnType(getNodeValue(
271: nodeChild, "columntype"));
272:
273: columnMeta.setNullable(getNodeBooleanValue(
274: nodeChild, "nullable"));
275: columnMeta
276: .setPrimaryKey(getNodeBooleanValue(
277: nodeChild, "primarykey"));
278: columnMeta
279: .setAutoIncrement(getNodeBooleanValue(
280: nodeChild, "autoincrement"));
281: columnMeta.setNotBound(getNodeBooleanValue(
282: nodeChild, "isnotbound"));
283: columnMeta.setOnClick(getNodeValue(
284: nodeChild, "onclick"));
285:
286: columnMeta.setTable(getNodeValue(nodeChild,
287: "tablename"));
288: columnMeta.setSchema(getNodeValue(
289: nodeChild, "schemaname"));
290:
291: //columnMeta.setMode(getNodeValue(nodeChild, "mode"));
292: columnMeta.setMaxLength(getNodeValue(
293: nodeChild, "maxlength"));
294: columnMeta.setSize(getNodeValue(nodeChild,
295: "size"));
296: columnMeta
297: .setSearchDisplay(getNodeBooleanValue(
298: nodeChild, "searchdisplay"));
299: columnMeta
300: .setListDisplay(getNodeBooleanValue(
301: nodeChild, "listdisplay"));
302: columnMeta
303: .setDetailDisplay(getNodeBooleanValue(
304: nodeChild, "detaildisplay"));
305:
306: columnMeta.setFormat(getNodeValue(
307: nodeChild, "format"));
308: columnMeta.setHref(getNodeValue(nodeChild,
309: "href"));
310:
311: columnMeta.setCaption(getNodeValue(
312: nodeChild, "caption"));
313: columnMeta
314: .setPrecedence(getNodeBooleanValue(
315: nodeChild, "precedence"));
316: columnMeta
317: .setLeadingWildCard(getNodeBooleanValue(
318: nodeChild,
319: "leadingwildcard"));
320:
321: columnMeta
322: .setExactMatch(getNodeBooleanValue(
323: nodeChild, "exactmatch"));
324: columnMeta
325: .setCaseSensitive(getNodeBooleanValue(
326: nodeChild, "casesensitive"));
327: columnMeta.setLocked(getNodeBooleanValue(
328: nodeChild, "locked"));
329: columnMeta
330: .setMandatory(getNodeBooleanValue(
331: nodeChild, "mandatory"));
332: columnMeta
333: .setAdvanceSearch(getNodeBooleanValue(
334: nodeChild, "advancesearch"));
335:
336: columnMeta.setBucket(getNodeBooleanValue(
337: nodeChild, "isbucket"));
338: columnMeta
339: .setUpdateable(getNodeBooleanValue(
340: nodeChild, "updateable"));
341: columnMeta.setSameRow(getNodeBooleanValue(
342: nodeChild, "samerow"));
343: columnMeta.setReadOnly(getNodeBooleanValue(
344: nodeChild, "readonly"));
345: columnMeta
346: .setOrderByColumn(getNodeBooleanValue(
347: nodeChild, "orderbycolumn"));
348: columnMeta.setDefaultValue(getNodeValue(
349: nodeChild, "defaultvalue"));
350: columnMeta.setImageFile(getNodeValue(
351: nodeChild, "imageFile"));
352: columnMeta.setJoinTo(getNodeValue(
353: nodeChild, "jointo"));
354:
355: // Get The Values and Parms if Any
356: Options values = null;
357: Options parms = null;
358: NodeList childValues = nodeChild
359: .getChildNodes();
360: int lenValues = childValues.getLength();
361: for (int k = 0; k < lenValues; k++) {
362: Node nodeValues = childValues.item(k);
363: if (nodeValues.getNodeName()
364: .equalsIgnoreCase("Values")) {
365: values = new Options();
366: NodeList childValue = nodeValues
367: .getChildNodes();
368: int lenValue = childValue
369: .getLength();
370: values.setType(getNodeValue(
371: nodeValues, "type"));
372: values.setComponent(getNodeValue(
373: nodeValues, "component"));
374: values.setTable(getNodeValue(
375: nodeValues, "table"));
376: values.setInitColumn(getNodeValue(
377: nodeValues, "initcolumn"));
378: values.setDescColumn(getNodeValue(
379: nodeValues, "desccolumn"));
380: values
381: .setMandatory(getNodeBooleanValue(
382: nodeValues,
383: "mandatory"));
384: String xmlFileName = getNodeValue(
385: nodeValues, "xmlfilename");
386: if (values.getType()
387: .equalsIgnoreCase("Static")) {
388: if (xmlFileName != null
389: && !xmlFileName
390: .equals("")) {
391: xmlFileName = URIpath
392: + "\\"
393: + xmlFileName;
394: try {
395: XMLOptionsParser opParser = new XMLOptionsParser(
396: false);
397: XMLOptionsParser
398: .parseFile(
399: xmlFileName,
400: values);
401: values = opParser
402: .getOptions();
403: } catch (java.io.UnsupportedEncodingException e) {
404: // values become null
405: }
406: } else {
407: for (int j = 0; j < lenValue; j++) {
408: Node nodeValue = childValue
409: .item(j);
410: if (nodeValue
411: .getNodeName()
412: .equalsIgnoreCase(
413: "Option")) {
414: values
415: .put(
416: getNodeValue(
417: nodeValue,
418: "key"),
419: getNodeValue(
420: nodeValue,
421: "data"));
422: }
423: }
424: }
425: }
426:
427: } // end if (nodeValues.getNodeName().equalsIgnoreCase("Values"))
428: else if (nodeValues.getNodeName()
429: .equalsIgnoreCase("parms")) {
430:
431: parms = new Options();
432: NodeList childValue = nodeValues
433: .getChildNodes();
434: int lenValue = childValue
435: .getLength();
436: parms.setComponent(getNodeValue(
437: nodeValues, "component"));
438: String xmlFileName = getNodeValue(
439: nodeValues, "xmlfilename");
440:
441: if (Util.isFilled(xmlFileName)) {
442: xmlFileName = URIpath + "\\"
443: + xmlFileName;
444: try {
445: XMLOptionsParser opParser = new XMLOptionsParser(
446: false);
447: XMLOptionsParser.parseFile(
448: xmlFileName, parms);
449: parms = opParser
450: .getOptions();
451: } catch (java.io.UnsupportedEncodingException e) {
452: // parms become null
453: }
454: } else {
455: for (int j = 0; j < lenValue; j++) {
456: Node nodeValue = childValue
457: .item(j);
458: if (nodeValue.getNodeName()
459: .equalsIgnoreCase(
460: "Option")) {
461:
462: String dataVal = getNodeValue(
463: nodeValue,
464: "data");
465: Object typedData = new Object();
466:
467: if (Util
468: .isFilled(dataVal)) {
469:
470: if (DataStoreEvaluator
471: .isNumber(dataVal)) {
472: typedData = new Integer(
473: dataVal);
474: } else if (DataStoreEvaluator
475: .isBoolean(dataVal)) {
476: typedData = new Boolean(
477: dataVal);
478: } else {
479: typedData = new String(
480: dataVal);
481: }
482:
483: }
484: parms.put(getNodeValue(
485: nodeValue,
486: "key"),
487: typedData);
488: }
489: }
490: }
491:
492: } // end else if (nodeValues.getNodeName().equalsIgnoreCase("parms"))
493: } // end for (int k = 0; k < lenValues; k++)
494: columnMeta.setValues(values);
495: columnMeta.setParms(parms);
496: _rsMetaData.getMetaData().add(columnMeta);
497: }
498: }
499: }
500: } // ResultSetData
501: else if (node.getNodeName().equalsIgnoreCase(
502: "ResultSetData")) {
503: ResultSetData rsData = new ResultSetData();
504: if (_dataStore == null)
505: _dataStore = new XMLDataStore();
506: _dataStore.setRsData(rsData); // Get All the Rows
507: NodeList children = node.getChildNodes();
508: if (children != null) {
509: int len = children.getLength();
510: for (int i = 0; i < len; i++) {
511: Node nodeChild = children.item(i);
512: if (nodeChild.getNodeName().equalsIgnoreCase(
513: "Row")) {
514: Row row = new Row();
515: rsData.getRows().add(row); // Get All Columns
516: NodeList childValues = nodeChild
517: .getChildNodes();
518: int lenValues = childValues.getLength();
519: for (int k = 0; k < lenValues; k++) {
520: Node nodeColumn = childValues.item(k);
521: if (nodeColumn.getNodeName()
522: .equalsIgnoreCase("Column")) {
523: Column column = new Column();
524: column.setColumnName(getNodeValue(
525: nodeColumn, "name"));
526: column.setTableName(getNodeValue(
527: nodeColumn, "table"));
528: column.setValue(getNodeValue(
529: nodeColumn, "value")); // put the column in row
530: row.put(column.getTableName() + "."
531: + column.getColumnName(),
532: column);
533: }
534:
535: }
536:
537: }
538: }
539: }
540: } // recursive call
541: NodeList children = node.getChildNodes();
542: if (children != null) {
543: int len = children.getLength();
544: for (int i = 0; i < len; i++) {
545: parseNode(children.item(i));
546: }
547: }
548:
549: break;
550: }
551: } // parseNode(Node)
552:
553: /** Returns a sorted list of attributes. */
554: protected Attr[] sortAttributes(NamedNodeMap attrs) {
555:
556: int len = (attrs != null) ? attrs.getLength() : 0;
557: Attr array[] = new Attr[len];
558: for (int i = 0; i < len; i++) {
559: array[i] = (Attr) attrs.item(i);
560: }
561: for (int i = 0; i < len - 1; i++) {
562: String name = array[i].getNodeName();
563: int index = i;
564: for (int j = i + 1; j < len; j++) {
565: String curName = array[j].getNodeName();
566: if (curName.compareTo(name) < 0) {
567: name = curName;
568: index = j;
569: }
570: }
571: if (index != i) {
572: Attr temp = array[i];
573: array[i] = array[index];
574: array[index] = temp;
575: }
576: }
577:
578: return (array);
579:
580: } // sortAttributes(NamedNodeMap):Attr[]
581: }
|