001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.tools.dbloader;
007:
008: import java.io.File;
009:
010: import org.w3c.dom.Document;
011: import org.w3c.dom.Element;
012: import org.w3c.dom.Node;
013: import org.w3c.dom.NodeList;
014: import org.w3c.dom.Text;
015:
016: /**
017: * Utility class centralizing various DOM related functions used during
018: * loading of the database.
019: *
020: * @author Ken Weiner, kweiner@unicon.net
021: * @author Mark Boyd {@link <a href="mailto:mark.boyd@engineer.com">mark.boyd@engineer.com</a>}
022: * @version $Revision: 36690 $
023: */
024: class DomUtils {
025: static void replaceDataTypes(Configuration config,
026: Document tablesDoc) {
027: Element tables = tablesDoc.getDocumentElement();
028: NodeList types = tables.getElementsByTagName("type");
029:
030: for (int i = 0; i < types.getLength(); i++) {
031: Node type = (Node) types.item(i);
032: NodeList typeChildren = type.getChildNodes();
033:
034: for (int j = 0; j < typeChildren.getLength(); j++) {
035: Node text = (Node) typeChildren.item(j);
036: String genericType = text.getNodeValue();
037:
038: // Replace generic type with mapped local type
039: text.setNodeValue(DbUtils.getLocalDataTypeName(config,
040: genericType));
041: }
042: }
043: }
044:
045: private static String getNodeValue(Node node) {
046: String nodeVal = null;
047:
048: for (Node ch = node.getFirstChild(); ch != null; ch = ch
049: .getNextSibling()) {
050: if (ch instanceof Text)
051: nodeVal = ch.getNodeValue();
052: }
053:
054: return nodeVal;
055: }
056:
057: private static Element getTableWithName(Document tablesDoc,
058: String tableName) {
059: Element tableElement = null;
060: NodeList tables = tablesDoc.getElementsByTagName("table");
061:
062: for (int i = 0; i < tables.getLength(); i++) {
063: Node table = (Node) tables.item(i);
064:
065: for (Node tableChild = table.getFirstChild(); tableChild != null; tableChild = tableChild
066: .getNextSibling()) {
067: if (tableChild instanceof Element
068: && tableChild.getNodeName() != null
069: && tableChild.getNodeName().equals("name")) {
070: if (tableName.equals(getNodeValue(tableChild))) {
071: tableElement = (Element) table;
072: break;
073: }
074: }
075: }
076: }
077:
078: return tableElement;
079: }
080:
081: static int getJavaSqlDataTypeOfColumn(Configuration config,
082: String tableName, String columnName) {
083: Document tablesDocGeneric = config.getGenericTablesDoc();
084: int dataType = 0;
085: String hashKey = tableName + File.separator + columnName;
086:
087: // Try to use cached version first
088: if (config.getTableColumnTypes().get(hashKey) != null) {
089: dataType = ((Integer) config.getTableColumnTypes().get(
090: hashKey)).intValue();
091: } else {
092: // Find the right table element
093: Element table = getTableWithName(tablesDocGeneric,
094: tableName);
095:
096: // Find the columns element within
097: Element columns = getFirstChildWithName(table, "columns");
098:
099: // Search for the first column who's name is columnName
100: for (Node ch = columns.getFirstChild(); ch != null; ch = ch
101: .getNextSibling()) {
102: if (ch instanceof Element
103: && ch.getNodeName().equals("column")) {
104: Element name = getFirstChildWithName((Element) ch,
105: "name");
106: if (getNodeValue(name).equals(columnName)) {
107: // Get the corresponding type and return it's type code
108: Element value = getFirstChildWithName(
109: (Element) ch, "type");
110: dataType = DbUtils
111: .getJavaSqlType(getNodeValue(value));
112: }
113: }
114: }
115:
116: // Store value in hashtable for next call to this method.
117: // This prevents repeating xml parsing which takes a very long time
118: config.getTableColumnTypes().put(hashKey,
119: new Integer(dataType));
120: }
121:
122: return dataType;
123: }
124:
125: private static Element getFirstChildWithName(Element parent,
126: String name) {
127: Element child = null;
128: for (Node ch = parent.getFirstChild(); ch != null; ch = ch
129: .getNextSibling()) {
130: if (ch instanceof Element && ch.getNodeName().equals(name)) {
131: child = (Element) ch;
132: break;
133: }
134: }
135:
136: return child;
137: }
138: }
|