com.nflabs.zeppelin.util.Util Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zeppelin-zengine Show documentation
Show all versions of zeppelin-zengine Show documentation
Zengine is java framework for data analysis on Hadoop. see http://nflabs.github.io/zeppelin/#/zengine
package com.nflabs.zeppelin.util;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Util {
public static String [] split(String str, char split){
return split(str, new String[]{String.valueOf(split)}, false);
}
public static String [] split(String str, String [] splitters, boolean includeSplitter){
String escapeSeq = "\"',;<%>";
char escapeChar = '\\';
String [] blockStart = new String[]{ "\"", "'", "<%", "N_<"};
String [] blockEnd = new String[]{ "\"", "'", "%>", "N_>"};
return split(str, escapeSeq, escapeChar, blockStart, blockEnd, splitters, includeSplitter);
}
public static String [] split(
String str,
String escapeSeq,
char escapeChar,
String [] blockStart,
String [] blockEnd,
String [] splitters,
boolean includeSplitter
){
List splits = new ArrayList();
String curString ="";
boolean escape = false; // true when escape char is found
int lastEscapeOffset = -1;
int blockStartPos = -1;
List blockStack = new LinkedList();
for(int i=0; i0){ // inside of block
curString += c;
// check multichar block
boolean multicharBlockDetected = false;
for(int b=0; b=0 && getBlockStr(blockStart[b]).compareTo(str.substring(blockStartPos, i))==0){
blockStack.remove(0);
blockStack.add(0, b);
multicharBlockDetected = true;
break;
}
}
if(multicharBlockDetected==true) continue;
// check if current block is nestable
if(isNestedBlock(blockStart[blockStack.get(0)])==true){
// try to find nested block start
if(curString.substring(lastEscapeOffset+1).endsWith(getBlockStr(blockStart[blockStack.get(0)]))==true){
blockStack.add(0, blockStack.get(0)); // block is started
blockStartPos = i;
continue;
}
}
// check if block is finishing
if(curString.substring(lastEscapeOffset+1).endsWith(getBlockStr(blockEnd[blockStack.get(0)]))){
// the block closer is one of the splitters (and not nested block)
if(isNestedBlock(blockEnd[blockStack.get(0)])==false){
for(String splitter : splitters){
if(splitter.compareTo(getBlockStr(blockEnd[blockStack.get(0)]))==0){
splits.add(curString);
if(includeSplitter==true){
splits.add(splitter);
}
curString = "";
lastEscapeOffset = -1;
break;
}
}
}
blockStartPos = -1;
blockStack.remove(0);
continue;
}
} else { // not in the block
boolean splitted = false;
for(String splitter : splitters){
// forward check for splitter
if(splitter.compareTo(str.substring(i, Math.min(i+splitter.length(), str.length())))==0){
splits.add(curString);
if(includeSplitter==true){
splits.add(splitter);
}
curString = "";
lastEscapeOffset = -1;
i+=splitter.length()-1;
splitted = true;
break;
}
}
if(splitted == true){
continue;
}
// add char to current string
curString += c;
// check if block is started
for(int b=0; b0)
splits.add(curString.trim());
return splits.toArray(new String[]{});
}
private static String getBlockStr(String blockDef){
if(blockDef.startsWith("N_")){
return blockDef.substring("N_".length());
} else {
return blockDef;
}
}
private static boolean isNestedBlock(String blockDef){
if(blockDef.startsWith("N_")){
return true;
} else {
return false;
}
}
}