001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.etl.codegen;
021:
022: import java.util.HashMap;
023: import java.util.Map;
024: import org.w3c.dom.Element;
025: import org.w3c.dom.Node;
026: import org.w3c.dom.NodeList;
027: import org.netbeans.modules.sql.framework.common.jdbc.SQLDBConnectionDefinition;
028: import org.netbeans.modules.sql.framework.common.utils.XmlUtil;
029: import com.sun.sql.framework.exception.BaseException;
030: import com.sun.sql.framework.jdbc.DBConnectionParameters;
031: import org.netbeans.modules.sql.framework.model.SQLModelObjectFactory;
032: import com.sun.sql.framework.utils.StringUtil;
033:
034: /**
035: * DBConnectionDefinitionTemplate basicaly reads the template and stores for codegen
036: *
037: * @author Sudhi Seshachala
038: */
039: // FIXME: DO We still need this class? -- Ahi
040: public class DBConnectionDefinitionTemplate {
041:
042: public static final String KEY_DATABASE_NAME = "DatabaseName";
043: public static final String KEY_HOST_NAME = "HostName";
044: public static final String KEY_HOST_PORT = "HostPort";
045: public static final String KEY_LOCATION_NAME = "LocationName";
046: public static final String KEY_METADATA_DIR = "MetadataDir";
047: public static final String KEY_PARAM_LIST = "ParamList";
048: private static final String TEMPLATE = "org/netbeans/modules/etl/codegen/CodegenConnectionDefinitionTemplate.xml";
049: private Map<String, SQLDBConnectionDefinition> connectionDefinitions = new HashMap<String, SQLDBConnectionDefinition>();
050:
051: /**
052: * Default Constructor
053: *
054: * @throws BaseException, if fails to read Template.
055: */
056: public DBConnectionDefinitionTemplate() throws BaseException {
057: Element connectionTemplateRoot = XmlUtil.loadXMLFile(TEMPLATE);
058: parseXML(connectionTemplateRoot);
059: }
060:
061: /**
062: * Returns a DB Connection Definition template for a given db type
063: *
064: * @param dbType database type
065: * @return a DB Connection Definition template for a given db type
066: */
067: public SQLDBConnectionDefinition getDBConnectionDefinition(
068: String dbType) {
069: if (!StringUtil.isNullString(dbType)) {
070: SQLDBConnectionDefinition orig = connectionDefinitions
071: .get(dbType.toLowerCase());
072: if (orig != null) {
073: orig = (SQLDBConnectionDefinition) orig.cloneObject();
074: }
075: return orig;
076: }
077: return null;
078: }
079:
080: private void parseXML(Element connectionTemplateRoot)
081: throws BaseException {
082: if (connectionTemplateRoot == null) {
083: throw new BaseException("Invalid connection template:"
084: + TEMPLATE);
085: }
086:
087: NodeList children = connectionTemplateRoot.getChildNodes();
088: for (int i = 0; i < children.getLength(); i++) {
089: Node child = children.item(i);
090:
091: if (child.getNodeType() == Node.ELEMENT_NODE
092: && child
093: .getNodeName()
094: .equals(
095: DBConnectionParameters.CONNECTION_DEFINITION_TAG)) {
096: SQLDBConnectionDefinition conDefnTemplate = SQLModelObjectFactory
097: .getInstance().createDBConnectionDefinition(
098: (Element) child);
099: connectionDefinitions.put(conDefnTemplate.getName()
100: .toLowerCase(), conDefnTemplate);
101: }
102: }
103: }
104: }
|