001 /* 002 * Copyright (C) 2010 eXo Platform SAS. 003 * 004 * This is free software; you can redistribute it and/or modify it 005 * under the terms of the GNU Lesser General Public License as 006 * published by the Free Software Foundation; either version 2.1 of 007 * the License, or (at your option) any later version. 008 * 009 * This software is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * You should have received a copy of the GNU Lesser General Public 015 * License along with this software; if not, write to the Free 016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 018 */ 019 020 package org.crsh.shell.impl; 021 022 import org.crsh.command.ScriptException; 023 024 /** 025 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a> 026 * @version $Revision$ 027 */ 028 class Tokenizer { 029 030 /** . */ 031 private final CharSequence s; 032 033 /** . */ 034 private int index; 035 036 /** . */ 037 private Character c; 038 039 /** 040 * Create a new tokenizer. 041 * 042 * @param s the sequence to tokenize 043 * @throws NullPointerException if the sequence is null 044 */ 045 public Tokenizer(CharSequence s) throws NullPointerException { 046 if (s == null) { 047 throw new NullPointerException(); 048 } 049 this.s = s; 050 this.index = 0; 051 052 // Initialize state 053 // index points to next char to read 054 // c = s.charAt(index - 1); 055 this.c = index < s.length() ? s.charAt(index++) : null; 056 } 057 058 private void next() { 059 if (index < s.length()) { 060 c = s.charAt(index++); 061 } else { 062 c = null; 063 } 064 } 065 066 public Token nextToken() { 067 if (c == null) { 068 return Token.EOF; 069 } else { 070 switch (c) { 071 case '+': 072 next(); 073 return Token.PLUS; 074 case '|': 075 next(); 076 return Token.PIPE; 077 default: 078 return parseCommand(); 079 } 080 } 081 } 082 083 private Token parseCommand() throws ScriptException { 084 085 // 086 StringBuilder line = new StringBuilder(); 087 088 // 089 Character lastQuote = null; 090 while (c != null) { 091 if (lastQuote == null && (c == '+' || c == '|')) { 092 break; 093 } else { 094 line.append(c); 095 switch (c) { 096 case '"': 097 case '\'': 098 if (lastQuote == null) { 099 lastQuote = c; 100 } else if (lastQuote != c) { 101 } else { 102 lastQuote = null; 103 } 104 break; 105 default: 106 break; 107 } 108 } 109 110 // 111 next(); 112 } 113 114 // 115 return new Token.Command(line.toString()); 116 } 117 }