From: Sasa M. <sa...@us...> - 2004-07-15 14:24:11
|
Update of /cvsroot/jrobin/src/org/jrobin/cmd In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11635/org/jrobin/cmd Modified Files: RrdCmdScanner.java RrdCommander.java RrdCreateCmd.java RrdToolCmd.java RrdUpdateCommand.java Log Message: Major changes and simplifications... Index: RrdCmdScanner.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/cmd/RrdCmdScanner.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** RrdCmdScanner.java 13 Jul 2004 20:57:44 -0000 1.2 --- RrdCmdScanner.java 15 Jul 2004 14:23:53 -0000 1.3 *************** *** 29,179 **** import java.util.LinkedList; ! import java.util.regex.Pattern; ! import java.util.regex.Matcher; class RrdCmdScanner { ! //private static final Pattern PATTERN = Pattern.compile("([^\"\\s]*\"[^\"]*\")|([^\"\\s]+)"); ! ! private String cmdType; ! private String command; ! ! private LinkedList options = new LinkedList(); ! private LinkedList words = new LinkedList(); ! RrdCmdScanner(String[] cmdWords) { ! for(int i = 0; i < cmdWords.length; i++) { ! words.add(cmdWords[i]); ! if(words.size() == 1) { ! cmdType = cmdWords[i]; } } } ! RrdCmdScanner( String command ) ! { ! // Set the command type only ! String cmd = command.trim(); ! int typePos = command.indexOf( ' ' ); ! cmdType = cmd.substring( 0, typePos ); ! ! this.command = cmd.substring( typePos ).trim(); ! ! //parseWords(command); ! } ! ! protected void parse( String[] keywords ) ! { ! StringBuffer sbuf = new StringBuffer( "( -)"); ! ! for ( int i = 0; i < keywords.length; i++ ) ! sbuf.append( "|( " + keywords[i] + ":)" ); ! ! Pattern pattern = Pattern.compile( sbuf.toString() ); ! ! parseWords( command, pattern ); } ! private void parseWords( String command, Pattern pattern ) ! { ! int start = 0, stop = 0; ! ! Matcher m = pattern.matcher(command); ! ! while ( m.find() ) ! { ! if ( start == 0 ) ! start = m.start(); ! else ! { ! stop = m.start(); ! ! // Put this 'word' away ! storeWord( command.substring( start, stop ).trim() ); ! ! // This is a new start ! start = stop; ! stop = 0; ! } } ! ! // Ok, see if we have to put the last word away ! if ( start > 0 && stop == 0 ) ! storeWord( command.substring( start ).trim() ); } ! private void storeWord( String word ) ! { ! if ( word.charAt(0) == '-' ) // This is an option ! options.add( word ); ! else // This is a general 'word' ! { ! // TODO: Remove \ characters or other 'in between' characters that are used in scripting ! // TODO: Remove leading single and double quotes in text, make sure all special characters are detected and treated okay ! // TODO: Best way to try this probably to put the code from: http://www.jrobin.org/phpBB2/viewtopic.php?t=39 in a file ! // TODO: read that file in, and then see if the resulting string parses correctly ! words.add( word ); } } ! /* ! private void parseWords2(String command) { ! // Make this a bit more complex, read until ' -', or keyword ! Matcher m = PATTERN.matcher(command); ! while(m.find()) { ! String word = m.group(); ! word = word.replaceAll("\"", ""); ! // System.out.println("Adding: [" + word + "]"); ! words.add(word); ! if(words.size() == 1) { ! cmdType = word; ! } } } - */ ! String getCmdType() { ! return cmdType; } ! String getOptionValue( String shortFormWord, String longFormWord ) throws RrdException ! { ! String shortForm = "-" + shortFormWord; ! String longForm = "--" + longFormWord; ! ! for ( int i = 0; i < options.size(); i++ ) ! { ! String value = null; ! String option = (String) options.get( i ); ! ! if ( shortForm != null && option.startsWith( shortForm ) ) ! value = option.substring( shortForm.length() ).trim(); ! else if ( longForm != null && option.startsWith( longForm ) ) ! { ! // Next character might be = ! value = option.substring( longForm.length() ).trim(); ! if ( value.length() > 1 && value.charAt(0) == '=' ) ! value = value.substring( 1 ); ! } ! ! if ( value != null ) ! { ! options.remove( i ); ! ! return value; } } ! // Option not found ! return null; } ! /* ! String getOptionValue(String shortForm, String longForm) throws RrdException { for(int i = 0; i < words.size(); i++) { String word = (String) words.get(i); ! if((shortForm != null && word.equals("-" + shortForm)) || ! (longForm != null && word.equals("--" + longForm))) { ! // match found ! if(i < words.size() - 1) { ! // value available String value = (String) words.get(i + 1); words.remove(i + 1); --- 29,139 ---- import java.util.LinkedList; ! import java.io.BufferedReader; ! import java.io.InputStreamReader; ! import java.io.IOException; class RrdCmdScanner { ! private LinkedList words = new LinkedList(); ! private StringBuffer buff; ! RrdCmdScanner(String command) throws RrdException { ! String cmd = command.trim(); ! // parse words ! char activeQuote = 0; ! for(int i = 0; i < cmd.length(); i++) { ! char c = cmd.charAt(i); ! if((c == '"' || c == '\'') && activeQuote == 0) { ! // opening double or single quote ! initWord(); ! activeQuote = c; ! continue; ! } ! if(c == activeQuote) { ! // closing quote ! activeQuote = 0; ! continue; ! } ! if(c == ' ' && activeQuote == 0) { ! // separator encountered ! finishWord(); ! continue; } + if(c == '\\' && activeQuote == '"' && i + 1 < cmd.length()) { + // check for \" and \\ inside double quotes + char c2 = cmd.charAt(i + 1); + if(c2 == '\\' || c2 == '"') { + appendWord(c2); + i++; + continue; + } + } + // ordinary character + appendWord(c); } + if(activeQuote != 0) { + throw new RrdException("End of command reached but " + activeQuote + " expected"); + } + finishWord(); } ! String getCmdType() { ! if(words.size() > 0) { ! return (String) words.get(0); ! } ! else { ! return null; ! } } ! private void appendWord(char c) { ! if(buff == null) { ! buff = new StringBuffer(""); } ! buff.append(c); } ! private void finishWord() { ! if(buff != null) { ! words.add(buff.toString()); ! buff = null; } } ! private void initWord() { ! if(buff == null) { ! buff = new StringBuffer(""); } } ! void dump() { ! for(int i = 0; i < words.size(); i++) { ! System.out.println(words.get(i)); ! } } ! String getOptionValue(String shortForm, String longForm, String defaultValue) ! throws RrdException { ! String value = getOptionValue("-" + shortForm); ! if(value == null) { ! value = getOptionValue("--" + longForm); ! if(value == null) { ! value = defaultValue; } } + return value; + } ! String getOptionValue(String shortForm, String longForm) ! throws RrdException { ! return getOptionValue(shortForm, longForm, null); } ! ! private String getOptionValue(String fullForm) throws RrdException { for(int i = 0; i < words.size(); i++) { String word = (String) words.get(i); ! if(word.equals(fullForm)) { ! // full match ! // the value is in the next word ! if(i + 1 < words.size()) { String value = (String) words.get(i + 1); words.remove(i + 1); *************** *** 182,209 **** } else { ! throw new RrdException("Option found but value is not available"); } } } return null; } - */ ! String getOptionValue(String shortForm, String longForm, String defaultValue) throws RrdException { ! String value = getOptionValue(shortForm, longForm); ! return value != null? value: defaultValue; ! } ! ! boolean getBooleanOption(String shortForm, String longForm) throws RrdException { ! return (getOptionValue( shortForm, longForm ) != null); ! } ! ! /* ! boolean getBooleanOption2(String shortForm, String longForm) throws RrdException { for(int i = 0; i < words.size(); i++) { String word = (String) words.get(i); ! if((shortForm != null && word.equals("-" + shortForm)) || ! (longForm != null && word.equals("--" + longForm))) { ! // match found words.remove(i); return true; --- 142,165 ---- } else { ! throw new RrdException("Value for option " + fullForm + " expected but not found"); } } + if(word.startsWith(fullForm)) { + int pos = fullForm.length(); + if(word.charAt(pos) == '=') { + // skip '=' if present + pos++; + } + words.remove(i); + return word.substring(pos); + } } return null; } ! boolean getBooleanOption(String shortForm, String longForm) { for(int i = 0; i < words.size(); i++) { String word = (String) words.get(i); ! if(word.equals("-" + shortForm) || word.equals("--" + longForm)) { words.remove(i); return true; *************** *** 212,219 **** return false; } - */ String[] getRemainingWords() { return (String[]) words.toArray(new String[0]); } } --- 168,190 ---- return false; } String[] getRemainingWords() { return (String[]) words.toArray(new String[0]); } + + public static void main(String[] args) { + BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); + while (true) { + try { + System.out.print("$ "); + String s = r.readLine(); + RrdCmdScanner sc = new RrdCmdScanner(s); + System.out.println("Value for option x is: [" + sc.getOptionValue("x", "xx") + "]"); + } catch (IOException e) { + System.err.println(e); + } catch (RrdException e) { + System.err.println(e); + } + } + } } Index: RrdToolCmd.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/cmd/RrdToolCmd.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** RrdToolCmd.java 13 Jul 2004 20:57:47 -0000 1.2 --- RrdToolCmd.java 15 Jul 2004 14:23:53 -0000 1.3 *************** *** 35,40 **** abstract class RrdToolCmd { ! // Holds the list of keywords ! static String[] keywords = new String[0]; static boolean rrdDbPoolUsed = true; --- 35,62 ---- abstract class RrdToolCmd { ! private RrdCmdScanner cmdScanner; ! ! abstract String getCmdType(); ! abstract Object execute() throws RrdException, IOException; ! ! public void setCommand(String command) throws RrdException { ! cmdScanner = new RrdCmdScanner(command); ! } ! ! String getOptionValue(String shortForm, String longForm) throws RrdException { ! return cmdScanner.getOptionValue(shortForm, longForm); ! } ! ! String getOptionValue(String shortForm, String longForm, String defaultValue) throws RrdException { ! return cmdScanner.getOptionValue(shortForm, longForm, defaultValue); ! } ! ! boolean getBooleanOption(String shortForm, String longForm) { ! return cmdScanner.getBooleanOption(shortForm, longForm); ! } ! ! String[] getRemainingWords() { ! return cmdScanner.getRemainingWords(); ! } static boolean rrdDbPoolUsed = true; *************** *** 57,83 **** } - - RrdCmdScanner cmdScanner; - - RrdToolCmd(RrdCmdScanner cmdScanner) { - this.cmdScanner = cmdScanner; - } - - abstract String getCmdType(); - - Object go() throws IOException, RrdException - { - if(!getCmdType().equals(cmdScanner.getCmdType())) { - return null; - } - - // Parse the command based on the keywords - cmdScanner.parse( keywords ); - - return execute(); - } - - abstract Object execute() throws RrdException, IOException; - static long parseLong(String value) throws RrdException { try { --- 79,82 ---- Index: RrdCreateCmd.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/cmd/RrdCreateCmd.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RrdCreateCmd.java 12 Jul 2004 13:35:17 -0000 1.1 --- RrdCreateCmd.java 15 Jul 2004 14:23:53 -0000 1.2 *************** *** 38,45 **** private RrdDef rrdDef; - public RrdCreateCmd(RrdCmdScanner cmdScanner) { - super(cmdScanner); - } - String getCmdType() { return "create"; --- 38,41 ---- *************** *** 47,56 **** Object execute() throws RrdException, IOException { ! String startStr = cmdScanner.getOptionValue("b", "start", DEFAULT_START); TimeSpec spec = new TimeParser(startStr).parse(); long start = spec.getTimestamp(); ! String stepStr = cmdScanner.getOptionValue("s", "step", DEFAULT_STEP); long step = parseLong(stepStr); ! String[] words = cmdScanner.getRemainingWords(); if(words.length < 2) { throw new RrdException("RRD file path not specified"); --- 43,52 ---- Object execute() throws RrdException, IOException { ! String startStr = getOptionValue("b", "start", DEFAULT_START); TimeSpec spec = new TimeParser(startStr).parse(); long start = spec.getTimestamp(); ! String stepStr = getOptionValue("s", "step", DEFAULT_STEP); long step = parseLong(stepStr); ! String[] words = getRemainingWords(); if(words.length < 2) { throw new RrdException("RRD file path not specified"); *************** *** 66,70 **** } else { ! throw new RrdException("Invalid word in the rrdcreate syntax: " + words[i]); } } --- 62,66 ---- } else { ! throw new RrdException("Invalid rrdcreate syntax: " + words[i]); } } Index: RrdUpdateCommand.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/cmd/RrdUpdateCommand.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RrdUpdateCommand.java 12 Jul 2004 13:35:17 -0000 1.1 --- RrdUpdateCommand.java 15 Jul 2004 14:23:53 -0000 1.2 *************** *** 32,39 **** private String[] dsNames; - RrdUpdateCommand(RrdCmdScanner cmdScanner) { - super(cmdScanner); - } - String getCmdType() { return "update"; --- 32,35 ---- *************** *** 41,49 **** Object execute() throws RrdException, IOException { ! String template = cmdScanner.getOptionValue("t", "template"); if (template != null) { dsNames = template.split(":"); } ! String[] words = cmdScanner.getRemainingWords(); if (words.length < 3) { throw new RrdException("Insufficent number of parameters for rrdupdate"); --- 37,45 ---- Object execute() throws RrdException, IOException { ! String template = getOptionValue("t", "template"); if (template != null) { dsNames = template.split(":"); } ! String[] words = getRemainingWords(); if (words.length < 3) { throw new RrdException("Insufficent number of parameters for rrdupdate"); Index: RrdCommander.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/cmd/RrdCommander.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** RrdCommander.java 13 Jul 2004 20:57:47 -0000 1.4 --- RrdCommander.java 15 Jul 2004 14:23:53 -0000 1.5 *************** *** 36,39 **** --- 36,45 ---- */ public class RrdCommander { + // just add all new commands here + private static final RrdToolCmd[] rrdCommands = { + new RrdCreateCmd(), + new RrdUpdateCommand() + }; + /** * Checks if the output from any RRDTool command will be visible on the standard output device *************** *** 105,143 **** public static synchronized Object execute(String command) throws IOException, RrdException { ! RrdCmdScanner cmdScanner = new RrdCmdScanner(command); ! RrdToolCmd[] commanders = { ! new RrdCreateCmd(cmdScanner), ! new RrdLastCmd(cmdScanner), ! new RrdUpdateCommand(cmdScanner), ! new RrdDumpCmd(cmdScanner), ! new RrdFetchCmd(cmdScanner), ! new RrdRestoreCmd(cmdScanner) ! }; ! for(int i = 0; i < commanders.length; i++) { ! Object result = commanders[i].go(); ! if(result != null) { ! return result; } } throw new RrdException("Unknown RRDTool command: " + command); } - /*public static synchronized Object execute(String command) throws IOException, RrdException { - RrdCmdScanner cmdScanner = new RrdCmdScanner(command); - RrdToolCmd[] commanders = { - new RrdCreateCmd(cmdScanner), - new RrdLastCmd(cmdScanner), - new RrdUpdateCommand(cmdScanner), - new RrdDumpCmd(cmdScanner), - new RrdFetchCmd(cmdScanner), - new RrdXportCmd(cmdScanner) - }; - for(int i = 0; i < commanders.length; i++) { - Object result = commanders[i].go(); - if(result != null) { - return result; - } - } - throw new RrdException("Unknown RRDTool command: " + command); - }*/ public static void main(String[] args) { --- 111,123 ---- public static synchronized Object execute(String command) throws IOException, RrdException { ! String cmd = command.trim(); ! for(int i = 0; i < rrdCommands.length; i++) { ! if(cmd.startsWith(rrdCommands[i].getCmdType())) { ! rrdCommands[i].setCommand(cmd); ! return rrdCommands[i].execute(); } } throw new RrdException("Unknown RRDTool command: " + command); } public static void main(String[] args) { *************** *** 147,150 **** --- 127,131 ---- System.out.println("Use any word starting with a dot '.' to bail out"); System.out.println("================================"); + RrdToolCmd.setRrdDbPoolUsed(false); BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); while (true) { *************** *** 157,163 **** execute(s); } catch (IOException e) { ! System.err.println(e); } catch (RrdException e) { ! System.err.println(e); } } --- 138,144 ---- execute(s); } catch (IOException e) { ! e.printStackTrace(); } catch (RrdException e) { ! e.printStackTrace(); } } |