Skip to content

Commit

Permalink
Ability to provide import arguments as -f argument
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint committed Dec 1, 2017
1 parent 91be5a8 commit 339d1d6
Show file tree
Hide file tree
Showing 12 changed files with 297 additions and 130 deletions.
Expand Up @@ -17,7 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.shell;
package org.neo4j.helpers;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -44,7 +44,7 @@ public static String templateString( String templateString,
{
// Sort data strings on length.
Map<Integer, List<String>> lengthMap =
new HashMap<Integer, List<String>>();
new HashMap<>();
int longest = 0;
for ( String key : data.keySet() )
{
Expand All @@ -62,7 +62,7 @@ public static String templateString( String templateString,
}
else
{
innerList = new ArrayList<String>();
innerList = new ArrayList<>();
lengthMap.put( innerKey, innerList );
}
innerList.add( key );
Expand Down Expand Up @@ -116,17 +116,17 @@ public static String lastWordOrQuoteOf( String text, boolean preserveQuotation )
return lastWord;
}

public static String[] splitAndKeepEscapedSpaces( String string, boolean preserveEscapes )
private static String[] splitAndKeepEscapedSpaces( String string, boolean preserveEscapes )
{
Collection<String> result = new ArrayList<String>();
Collection<String> result = new ArrayList<>();
StringBuilder current = new StringBuilder();
for ( int i = 0; i < string.length(); i++ )
{
char ch = string.charAt( i );
if ( ch == ' ' )
{
boolean isGluedSpace = i > 0 && string.charAt( i - 1 ) == '\\';
if ( !isGluedSpace )
boolean isEscapedSpace = i > 0 && string.charAt( i - 1 ) == '\\';
if ( !isEscapedSpace )
{
result.add( current.toString() );
current = new StringBuilder();
Expand All @@ -146,29 +146,6 @@ public static String[] splitAndKeepEscapedSpaces( String string, boolean preserv
return result.toArray( new String[result.size()] );
}

public static String multiplyString( String string, int times )
{
StringBuilder result = new StringBuilder();
for ( int i = 0; i < times; i++ )
{
result.append( string );
}
return result.toString();
}

public static String removeSpaces( String command )
{
while ( command.length() > 0 && command.charAt( 0 ) == ' ' )
{
command = command.substring( 1 );
}
while ( command.length() > 0 && command.charAt( command.length() - 1 ) == ' ' )
{
command = command.substring( 0, command.length() - 1 );
}
return command;
}

/**
* Tokenizes a string, regarding quotes.
*
Expand All @@ -177,7 +154,7 @@ public static String removeSpaces( String command )
*/
public static String[] tokenizeStringWithQuotes( String string )
{
return tokenizeStringWithQuotes( string, true );
return tokenizeStringWithQuotes( string, true, false );
}

/**
Expand All @@ -189,15 +166,16 @@ public static String[] tokenizeStringWithQuotes( String string )
*
* @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 )
public static String[] tokenizeStringWithQuotes( String string, boolean trim, boolean preserveEscapeCharacters )
{
if ( trim )
{
string = string.trim();
}
ArrayList<String> result = new ArrayList<String>();
ArrayList<String> result = new ArrayList<>();
string = string.trim();
boolean inside = string.startsWith( "\"" );
StringTokenizer quoteTokenizer = new StringTokenizer( string, "\"" );
Expand All @@ -219,22 +197,10 @@ else if ( inside )
}
else
{
Collections.addAll( result, TextUtil.splitAndKeepEscapedSpaces( token, false ) );
Collections.addAll( result, TextUtil.splitAndKeepEscapedSpaces( token, preserveEscapeCharacters ) );
}
inside = !inside;
}
return result.toArray( new String[result.size()] );
}

public static String stripFromQuotes( String string )
{
if ( string != null )
{
if ( string.startsWith( "\"" ) && string.endsWith( "\"" ) )
{
return string.substring( 1, string.length() - 1 );
}
}
return string;
}
}
100 changes: 100 additions & 0 deletions community/common/src/test/java/org/neo4j/helpers/TextUtilTest.java
@@ -0,0 +1,100 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.helpers;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

public class TextUtilTest
{
@Test
public void shouldReplaceVariablesWithValuesInTemplateString() throws Exception
{
// given
String template = "This is a $FIRST that $SECOND $THIRD!";
Map<String,String> values = new HashMap<>();
values.put( "FIRST", "String" );
values.put( "SECOND", "should" );
values.put( "THIRD", "act as a template!" );

// when
String string = TextUtil.templateString( template, values );

// then
assertEquals( "This is a String that should act as a template!!", string );
}

@Test
public void shouldTokenizeStringWithWithoutQuotes() throws Exception
{
// given
String untokenized = "First Second Third";

// when
String[] tokenized = TextUtil.tokenizeStringWithQuotes( untokenized );

// then
assertArrayEquals( new String[] {"First", "Second", "Third"}, tokenized );
}

@Test
public void shouldTokenizeStringWithQuotes() throws Exception
{
// 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
public void shouldTokenStringWithWithQuotesAndEscapedSpaces() throws Exception
{
// 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
public void shouldPreserveBackslashes() throws Exception
{
// 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 );
}
}

0 comments on commit 339d1d6

Please sign in to comment.