001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.codegen;
042:
043: import java.io.BufferedReader;
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.io.InputStreamReader;
047: import java.io.Reader;
048: import java.util.HashMap;
049: import java.util.Map;
050: import com.sun.sql.framework.utils.Attribute;
051: import com.sun.sql.framework.utils.AttributeFactory;
052: import com.sun.sql.framework.utils.AttributeParser;
053:
054: /**
055: * @author Ritesh Adval
056: * @author Ahimanikya Satapathy
057: * @version $Revision$
058: */
059: public abstract class AbstractDB implements DB {
060:
061: protected Statements statements;
062: protected AbstractGeneratorFactory generatorFactory;
063: protected TypeGenerator typeGenerator;
064: protected Map templateMaps = new HashMap();
065:
066: public AbstractDB() {
067: this .generatorFactory = this .createGeneratorFactory();
068: this .typeGenerator = this .createTypeGenerator();
069: this .statements = this .createStatements();
070: this .loadTemplates();
071: }
072:
073: public abstract int getCastingRule(int sourceType, int targetType);
074:
075: public abstract String getDefaultDateFormat();
076:
077: public abstract boolean isAnsiJoinSyntaxSupported();
078:
079: public abstract int getMaxTableNameLength();
080:
081: public abstract String getEscapedName(String name);
082:
083: public abstract String getUnescapedName(String name);
084:
085: public abstract String getEscapedCatalogName(String name);
086:
087: public abstract String getEscapedSchemaName(String name);
088:
089: public abstract Statements createStatements();
090:
091: public abstract AbstractGeneratorFactory createGeneratorFactory();
092:
093: public abstract TypeGenerator createTypeGenerator();
094:
095: /**
096: * get the operator factory for this data base
097: *
098: * @return operator factory
099: */
100: public abstract SQLOperatorFactory getOperatorFactory();
101:
102: public Statements getStatements() {
103: return this .statements;
104: }
105:
106: public AbstractGeneratorFactory getGeneratorFactory() {
107: return this .generatorFactory;
108: }
109:
110: public TypeGenerator getTypeGenerator() {
111: return this .typeGenerator;
112: }
113:
114: protected abstract Map loadTemplates();
115:
116: protected Map loadTemplates(String filename) {
117: InputStream in = null;
118: AttributeFactory fac = new AttributeFactory();
119:
120: try {
121: in = this .getClass().getResourceAsStream(filename);
122: AttributeParser aParser = new AttributeParser(in, fac);
123: aParser.parse();
124: } finally {
125: if (in != null) {
126: try {
127: in.close();
128: } catch (IOException ignore) {
129: // ignore
130: }
131: }
132: }
133:
134: return fac.getAttributeMap();
135: }
136:
137: public String getTemplateFileName(String templateKey) {
138: Attribute attr = (Attribute) templateMaps.get(templateKey);
139:
140: if (attr != null) {
141: String val = (String) attr.getAttributeValue();
142: return val;
143: }
144:
145: return null;
146: }
147:
148: public Reader getTemplateContent(String templateKey) {
149: String templateFileName = getTemplateFileName(templateKey);
150: if (templateFileName == null) {
151: return null;
152: }
153:
154: InputStream inStream = this .getClass().getResourceAsStream(
155: templateFileName);
156: BufferedReader br = new BufferedReader(new InputStreamReader(
157: inStream));
158:
159: return br;
160: }
161: }
|