// ============================================================================ // // Copyright (C) 2006-2015 Talend Inc. - www.talend.com // // This source code is available under agreement available at // %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt // // You should have received a copy of the agreement // along with this program; if not, write to Talend SA // 9 rue Pages 92150 Suresnes, France // // ============================================================================ package routines.system; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CoderResult; import java.nio.charset.CodingErrorAction; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringUtils { public static final String[] EMPTY_STRING_ARRAY = new String[0]; public static final String EMPTY = ""; public static String newStringFromSplit(CharsetDecoder decoder, CharsetDecoder utf8Decoder, String encoding, byte[] fieldBytes, int length) { ByteBuffer fieldBuf = ByteBuffer.wrap(fieldBytes, 0, length); CharBuffer fieldCharBuf = CharBuffer.allocate(length); utf8Decoder.reset(); CoderResult res = utf8Decoder.decode(fieldBuf, fieldCharBuf, true); if (res.isError() && decoder != null) { decoder.reset(); res = decoder.decode(fieldBuf, fieldCharBuf, true); if (!res.isError()) { decoder.flush(fieldCharBuf); return new String(fieldCharBuf.array()); } } else { utf8Decoder.flush(fieldCharBuf); return new String(fieldCharBuf.array()); } return ""; } public static String[] splitNotRegexWithEncoding(byte[] bline, String encoding, String separatorChars) throws UnsupportedEncodingException { if (bline == null) { return null; } ByteBuffer line = ByteBuffer.wrap(bline); byte[] sep = null; CharsetDecoder decoder = null; if (encoding != null) { sep = separatorChars.getBytes(encoding); decoder = Charset.forName(encoding).newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPORT); decoder.onUnmappableCharacter(CodingErrorAction.REPORT); } else { sep = separatorChars.getBytes(); } if (sep.length == 0) { String[] result = new String[1]; result[0] = new String(bline, encoding); return result; } CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder(); //$NON-NLS-1$ utf8Decoder.onMalformedInput(CodingErrorAction.REPORT); utf8Decoder.onUnmappableCharacter(CodingErrorAction.REPORT); ArrayList substrings = new ArrayList(); int lineLength = line.limit(); int sepCursor = 0; int fieldCursor = 0; byte[] fieldBytes = new byte[lineLength]; while (line.position() < line.limit()) { if (sepCursor < sep.length) { byte currentByte = line.get(); if (currentByte == sep[sepCursor]) { sepCursor++; } else { sepCursor = 0; fieldBytes[fieldCursor++] = currentByte; } } else { // we found a new field if (fieldCursor > 0) { substrings.add(newStringFromSplit(decoder, utf8Decoder, encoding, fieldBytes, fieldCursor)); fieldCursor = 0; } else { // empty field substrings.add(""); //$NON-NLS-1$ } sepCursor = 0; } } if (fieldCursor > 0) { substrings.add(newStringFromSplit(decoder, utf8Decoder, encoding, fieldBytes, fieldCursor)); } if (sepCursor == sep.length) { substrings.add(""); //$NON-NLS-1$ } int resultSize = substrings.size(); if (resultSize == 0) { // no delimiter found so we have only one column String[] result = new String[1]; result[0] = new String(bline, encoding); return result; } String[] result = new String[resultSize]; substrings.toArray(result); return result; } /** * replace the method : String.split(String regex) * * @param str * @param separatorChars * @return */ public static String[] splitNotRegex(String str, String separatorChars) { if (str == null) { return null; } int len = str.length(); if (len == 0) { return EMPTY_STRING_ARRAY; } int separatorLength = separatorChars.length(); ArrayList substrings = new ArrayList(); int beg = 0; int end = 0; while (end < len) { end = str.indexOf(separatorChars, beg); if (end > -1) { if (end > beg) { substrings.add(str.substring(beg, end)); beg = end + separatorLength; } else { substrings.add(EMPTY); beg = end + separatorLength; } } else { substrings.add(str.substring(beg)); end = len; } } int resultSize = substrings.size(); while (resultSize > 0 && substrings.get(resultSize - 1).equals("")) { resultSize--; } String[] result = new String[resultSize]; return substrings.subList(0, resultSize).toArray(result); } /** * split SQL columns like that : * from : * id, name, CONCAT(name,UPPER(address)), CONCAT(age,name) * to * [id] [name] [CONCAT(name,UPPER(address))] [CONCAT(age,name)] */ public static String[] splitSQLColumns(String sql) { List result = new ArrayList(); int blockCount = 0; int start = 0; for(int i=0;i