1 /* 2 * db2sa: DB2 Syntax Assistant 3 * Copyright (C) Andres Gomez Casanova 4 * 5 * This file is part of db2sa. 6 * 7 * db2sa is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU Lesser General Public License as published by 9 * the Free Software Foundation; either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * db2sa is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public License 18 * along with this library; if not, see <http://www.gnu.org/licenses/>. 19 * 20 * Contact: 21 * a n g o c a at y a h o o dot c o m 22 * Cra. 45 No 61 - 31, Bogota, Colombia. 23 * 24 * Author: $LastChangedBy: angoca $: 25 * Date: $LastChangedDate: 2009-07-08 19:56:43 +0200 (Wed, 08 Jul 2009) $: 26 * Revision: $LastChangedRevision: 357 $: 27 * URL: $HeadURL: https://db2sa.svn.sourceforge.net/svnroot/db2sa/branches/db2sa_beta/source-code/src/main/java/name/angoca/db2sa/cli/jline/DB2Completor.java $: 28 */ 29 package name.angoca.db2sa.cli.jline; 30 31 import java.util.ArrayList; 32 import java.util.Collection; 33 import java.util.List; 34 35 import jline.Completor; 36 import name.angoca.db2sa.core.InterfaceCore; 37 import name.angoca.db2sa.core.ReturnOptions; 38 import name.angoca.db2sa.exceptions.AbstractDB2SAException; 39 40 import org.slf4j.Logger; 41 import org.slf4j.LoggerFactory; 42 43 /** 44 * This is the implementation of a JLine Completor. This class permits to search 45 * the options from db2sa and show them in Jline.<br/> 46 * <b>Control Version</b><br /> 47 * <ul> 48 * <li>0.0.1 Class creation.</li> 49 * <li>0.0.2 Logger messages.</li> 50 * <li>0.0.3 No override, no supress warnings.</li> 51 * <li>0.0.4 finals.</li> 52 * <li>1.0.0 Moved to version 1.</li> 53 * </ul> 54 * 55 * @author Andres Gomez Casanova <a 56 * href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a> 57 * @version 1.0.0 2009-07-19 58 */ 59 public class DB2Completor implements Completor { 60 /** 61 * Logger. 62 */ 63 private final static Logger LOGGER = LoggerFactory 64 .getLogger(DB2Completor.class); 65 66 /** 67 * Default constructor. 68 */ 69 public DB2Completor() { 70 // Nothing. 71 } 72 73 /* 74 * (non-Javadoc) 75 * 76 * @see jline.Completor#complete(java.lang.String, int, java.util.List) 77 */ 78 // The interface is generic, so there is a warning here. 79 @SuppressWarnings("unchecked") 80 public final int complete(final String/* ! */buffer, final int cursor, 81 final List/* <!>! */candidates) { 82 DB2Completor.LOGGER.debug("Completor: buffer '{}', cursor: {}", //$NON-NLS-1$ 83 buffer, Integer.valueOf(cursor)); 84 85 final String phrase = buffer.substring(0, cursor); 86 try { 87 final ReturnOptions options = InterfaceCore.analyzePhrase(phrase); 88 final String complete = options.getPhrase(); 89 if (complete.startsWith(buffer) && complete.length() > 0) { 90 DB2Completor.LOGGER.debug("Candidate: {}", //$NON-NLS-1$ 91 complete.substring(buffer.length())); 92 candidates.add(complete.substring(buffer.length())); 93 } 94 candidates.addAll(this.fromArrayToColletion(options.getPhrases())); 95 candidates.addAll(this.fromArrayToColletion(options.getOptions())); 96 } catch (AbstractDB2SAException e) { 97 DB2Completor.LOGGER.error("Exception: {} " + e.toString(), //$NON-NLS-1$ 98 e.getMessage()); 99 } 100 101 DB2Completor.LOGGER.debug("Completor: buffer '{}', cursor: {}", //$NON-NLS-1$ 102 buffer, Integer.valueOf(cursor)); 103 return cursor; 104 } 105 106 /** 107 * Converts a array into a collection. 108 * 109 * @param array 110 * array to convert. 111 * @return Collection. 112 */ 113 private Collection<String>/* <!>! */fromArrayToColletion( 114 final String[]/* [!]! */array) { 115 final List<String> collection = new ArrayList<String>(); 116 for (String string : array) { 117 collection.add(string); 118 } 119 return collection; 120 } 121 }