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.mashup.db.bootstrap;
042:
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Collections;
046: import java.util.HashMap;
047: import java.util.List;
048: import java.util.Map;
049:
050: import org.netbeans.modules.mashup.db.common.PropertyKeys;
051: import org.netbeans.modules.mashup.db.model.FlatfileDBTable;
052: import net.java.hulp.i18n.Logger;
053: import org.netbeans.modules.etl.logger.Localizer;
054: import org.netbeans.modules.etl.logger.LogUtil;
055:
056: /**
057: * Factory for creating instances of FlatfileBootstrapParser.
058: *
059: * @author Jonathan Giron
060: * @author Ahimanikya Satapathy
061: * @version $Revision$
062: * @see org.netbeans.modules.mashup.db.bootstrap.FlatfileBootstrapParser
063: */
064: public final class FlatfileBootstrapParserFactory {
065:
066: private static transient final Logger mLogger = LogUtil
067: .getLogger(FlatfileBootstrapParserFactory.class.getName());
068: private static transient final Localizer mLoc = Localizer.get();
069:
070: class NullBootstrapParser implements FlatfileBootstrapParser {
071:
072: public List buildFlatfileDBColumns(FlatfileDBTable table) {
073: return Collections.EMPTY_LIST;
074: }
075:
076: public void makeGuess(FlatfileDBTable file) {
077: }
078:
079: public boolean acceptable(FlatfileDBTable table) {
080: return false;
081: }
082: }
083:
084: /* Singleton instance of FlatfileBootstrapParserFactory */
085: private static FlatfileBootstrapParserFactory instance;
086: /* Log4J category string */
087: private static final String LOG_CATEGORY = FlatfileBootstrapParserFactory.class
088: .getName();
089: private static final String[][] PARSER_MAP_INFO = new String[][] {
090: { PropertyKeys.DELIMITED,
091: "org.netbeans.modules.mashup.db.bootstrap.DelimitedBootstrapParser" },
092: { PropertyKeys.FIXEDWIDTH,
093: "org.netbeans.modules.mashup.db.bootstrap.FixedWidthBootstrapParser" },
094: { PropertyKeys.XML,
095: "org.netbeans.modules.mashup.db.bootstrap.XMLBootstrapParser" },
096: { PropertyKeys.RSS,
097: "org.netbeans.modules.mashup.db.bootstrap.RSSBootstrapParser" },
098: { PropertyKeys.WEB,
099: "org.netbeans.modules.mashup.db.bootstrap.WebBootstrapParser" },
100: { PropertyKeys.WEBROWSET,
101: "org.netbeans.modules.mashup.db.bootstrap.WebrowsetBootstrapParser" },
102: { PropertyKeys.SPREADSHEET,
103: "org.netbeans.modules.mashup.db.bootstrap.SpreadsheetBootstrapParser" } };
104:
105: /**
106: * Gets an instance of FlatfileBootstrapParserFactory.
107: *
108: * @return FlatfileBootstrapParserFactory instance
109: */
110: public static FlatfileBootstrapParserFactory getInstance() {
111: if (instance == null) {
112: instance = new FlatfileBootstrapParserFactory();
113: }
114: return instance;
115: }
116:
117: public static Collection getParserTypes() {
118: List<String> types = new ArrayList<String>(7);
119: types.add(PropertyKeys.FIXEDWIDTH);
120: types.add(PropertyKeys.RSS);
121: types.add(PropertyKeys.WEBROWSET);
122: types.add(PropertyKeys.SPREADSHEET);
123: types.add(PropertyKeys.XML);
124: types.add(PropertyKeys.WEB);
125: types.add(PropertyKeys.DELIMITED);
126: return types;
127: }
128:
129: /* Map of parser type to FlatfileBootstrapParser instance */
130: private Map keynameToClassMap;
131:
132: /** Creates a private default instance of FlatfileBootstrapParserFactory. */
133: private FlatfileBootstrapParserFactory() {
134: keynameToClassMap = new HashMap();
135:
136: for (int i = 0; i < PARSER_MAP_INFO.length; i++) {
137: String key = PARSER_MAP_INFO[i][0];
138: String parserClass = PARSER_MAP_INFO[i][1];
139:
140: try {
141: keynameToClassMap.put(key, Class.forName(parserClass));
142: } catch (Exception ignore) {
143: mLogger
144: .errorNoloc(
145: mLoc
146: .t(
147: "PRSR051: Caught error while loading parser class names. {0}",
148: LOG_CATEGORY), ignore);
149: // Ignore: Log but continue
150: }
151: }
152: }
153:
154: /**
155: * Gets an instance of FlatfileBootstrapParser, if any, associated with the given type
156: * name.
157: *
158: * @param type type name of FlatfileBootstrapParser to be retrieved
159: * @return an instance of the associated FlatfileBootstrapParser, or null if no such
160: * instance is associated with type
161: */
162: public synchronized FlatfileBootstrapParser getBootstrapParser(
163: String type) {
164: Class parserClass = (Class) keynameToClassMap.get(type);
165: if (parserClass == null) {
166: return null;
167: }
168:
169: try {
170: return (FlatfileBootstrapParser) parserClass.newInstance();
171: } catch (Exception e) {
172: return new NullBootstrapParser();
173: }
174: }
175:
176: public String getParserType(FlatfileDBTable table) {
177: String result = PropertyKeys.DELIMITED;
178: FlatfileBootstrapParser bootstrapParser = null;
179: String[] parsers = (String[]) getParserTypes().toArray(
180: new String[0]);
181: for (String parser : parsers) {
182: bootstrapParser = getBootstrapParser(parser);
183: try {
184: if (bootstrapParser.acceptable(table)) {
185: result = parser;
186: break;
187: }
188: } catch (Exception e) {
189: // ignore
190: }
191: }
192: return result;
193: }
194: }
|