/** * Tokenizes a string, regarding quotes. * * @param string the string to tokenize. * @return the tokens from the line. */ public static String[] tokenizeStringWithQuotes( String string ) { return tokenizeStringWithQuotes( string, true, false ); }
/** * Tokenizes a string, regarding quotes. Examples: * * o '"One two"' ==> [ "One two" ] * o 'One two' ==> [ "One", "two" ] * o 'One "two three" four' ==> [ "One", "two three", "four" ] * * @param string the string to tokenize. * @param trim whether or not to trim each token. * @param preserveEscapeCharacters whether or not to preserve escape characters '\', otherwise skip them. * @return the tokens from the line. */ public static String[] tokenizeStringWithQuotes( String string, boolean trim, boolean preserveEscapeCharacters ) { return tokenizeStringWithQuotes( string, trim, preserveEscapeCharacters, preserveEscapeCharacters ); }
public static String[] parseFileArgumentList( File file ) throws IOException { List<String> arguments = new ArrayList<>(); readTextFile( file, line -> arguments.addAll( asList( tokenizeStringWithQuotes( line, true, true, false ) ) ) ); return arguments.toArray( new String[arguments.size()] ); }
@Test void shouldPreserveBackslashes() { // given String untokenized = "First C:\\a\\b\\c"; // when String[] tokenized = TextUtil.tokenizeStringWithQuotes( untokenized, true, true ); // then assertArrayEquals( new String[] {"First", "C:\\a\\b\\c"}, tokenized ); }
@Test void preserveOnlyPathBackslashes() { // given String untokenized = "First C:\\a\\ r\\b\\c"; // when String[] tokenized = TextUtil.tokenizeStringWithQuotes( untokenized, true, true, false ); // then assertArrayEquals( new String[] {"First", "C:\\a r\\b\\c"}, tokenized ); } }
@Test void shouldTokenizeStringWithWithoutQuotes() { // given String untokenized = "First Second Third"; // when String[] tokenized = TextUtil.tokenizeStringWithQuotes( untokenized ); // then assertArrayEquals( new String[] {"First", "Second", "Third"}, tokenized ); }
@Test void shouldTokenStringWithWithQuotesAndEscapedSpaces() { // given String untokenized = "First \"Second one\" Third And\\ a\\ fourth"; // when String[] tokenized = TextUtil.tokenizeStringWithQuotes( untokenized ); // then assertArrayEquals( new String[] {"First", "Second one", "Third", "And a fourth"}, tokenized ); }
@Test void shouldTokenizeStringWithQuotes() { // given String untokenized = "First \"Second one\" Third \"And a fourth\""; // when String[] tokenized = TextUtil.tokenizeStringWithQuotes( untokenized ); // then assertArrayEquals( new String[] {"First", "Second one", "Third", "And a fourth"}, tokenized ); }
/** * Tokenizes a string, regarding quotes. * * @param string the string to tokenize. * @return the tokens from the line. */ public static String[] tokenizeStringWithQuotes( String string ) { return tokenizeStringWithQuotes( string, true, false ); }
/** * Tokenizes a string, regarding quotes. Examples: * * o '"One two"' ==> [ "One two" ] * o 'One two' ==> [ "One", "two" ] * o 'One "two three" four' ==> [ "One", "two three", "four" ] * * @param string the string to tokenize. * @param trim whether or not to trim each token. * @param preserveEscapeCharacters whether or not to preserve escape characters '\', otherwise skip them. * @return the tokens from the line. */ public static String[] tokenizeStringWithQuotes( String string, boolean trim, boolean preserveEscapeCharacters ) { return tokenizeStringWithQuotes( string, trim, preserveEscapeCharacters, preserveEscapeCharacters ); }
public static String[] parseFileArgumentList( File file ) throws IOException { List<String> arguments = new ArrayList<>(); readTextFile( file, line -> arguments.addAll( asList( tokenizeStringWithQuotes( line, true, true, false ) ) ) ); return arguments.toArray( new String[arguments.size()] ); }
private void parseParameters( String line ) throws ShellException { String rest = line.substring( appName.length() ).trim(); String[] parsed = tokenizeStringWithQuotes( rest, false, false ); for ( int i = 0; i < parsed.length; i++ ) { String string = parsed[i]; if ( app.takesOptions() && isMultiCharOption( string ) ) { String name = string.substring( 2 ); i = fetchArguments( parsed, i, name ); } else if ( app.takesOptions() && isSingleCharOption( string ) ) { String options = string.substring( 1 ); for ( int o = 0; o < options.length(); o++ ) { String name = String.valueOf( options.charAt( o ) ); i = fetchArguments( parsed, i, name ); } } else if ( string.length() > 0 ) { this.arguments.add( string ); } } }
private void runScripts( Object interpreter, Map<String, Object> properties, String line, String[] paths ) throws Exception { ArgReader reader = new ArgReader( tokenizeStringWithQuotes( line ) ); while ( reader.hasNext() ) { String arg = reader.next(); if ( arg.startsWith( "--" ) ) { String[] scriptArgs = getScriptArgs( reader ); String scriptName = arg.substring( 2 ); Map<String, Object> props = new HashMap<>( properties ); props.put( "args", scriptArgs ); this.runScript( interpreter, scriptName, props, paths ); } } }