Java SQL Type parse(String txt, int level)

Here you can find the source of parse(String txt, int level)

Description

parse

License

Apache License

Declaration

public static List parse(String txt, int level) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List parse(String txt) throws SQLException {
        return parse(txt, 1);
    }/*from ww  w .  j  a  va  2  s  .c om*/

    public static List parse(String txt, int level) {
        ArrayList list = new ArrayList();
        String quotesChars = getQuotesChars(level);
        String outerQuotes = getQuotesChars(level - 1);
        if (!txt.startsWith(outerQuotes + "(") && !txt.endsWith(outerQuotes + ")")) {
            throw new RuntimeException("Not a valid construct for a Row");
        } else {
            txt = txt.substring(outerQuotes.length() + 1);
            txt = txt.substring(0, txt.length() - outerQuotes.length() - 1);
            boolean inQuotes = false;
            String currentToken = "";

            for (int i = 0; i < txt.length(); ++i) {
                if (txt.startsWith(quotesChars, i)) {
                    inQuotes = !inQuotes;
                    currentToken = currentToken + quotesChars;
                    i += quotesChars.length() - 1;
                } else if (txt.charAt(i) == 44) {
                    if (inQuotes) {
                        currentToken = currentToken + ",";
                    } else {
                        if (currentToken.length() > 0 && currentToken.startsWith(quotesChars + "(")
                                && currentToken.endsWith(")" + quotesChars)) {
                            list.add(parse(currentToken, level + 1));
                        } else {
                            list.add(adjustLevel(currentToken, Types.ARRAY));
                        }

                        currentToken = "";
                    }
                } else {
                    currentToken = currentToken + txt.charAt(i);
                }
            }

            if (currentToken.length() > 0 && currentToken.startsWith(quotesChars + "(")
                    && currentToken.endsWith(")" + quotesChars)) {
                list.add(parse(currentToken, level + 1));
            } else {
                list.add(adjustLevel(currentToken, Types.ARRAY));
            }

            return list;
        }
    }

    private static String getQuotesChars(int level) {
        if (level <= 0) {
            return "";
        } else {
            String src = "";
            int iterations = (int) Math.pow(2.0D, (double) (level - 1));

            for (int i = 1; i <= iterations; ++i) {
                src = src + "\"";
            }

            return src;
        }
    }

    private static String adjustLevel(String attribute, int type) {
        return adjustLevel(attribute, type, "\"", false);
    }

    private static String adjustLevel(String attribute, int type, String quote, boolean normalize) {
        StringBuffer buffer = new StringBuffer();
        char[] chars = attribute.toCharArray();
        ArrayList lastType = new ArrayList();
        lastType.add(new Integer(type));
        ArrayList lastQuote = new ArrayList();
        lastQuote.add(quote);
        int level = 0;
        int calculatedLevel = 0;
        int firstCalculatedLevel = -2;

        for (int i = 0; i < chars.length; ++i) {
            StringBuffer tempBuffer = null;
            boolean quoteFound = false;

            while (chars[i] == 92 || chars[i] == 34) {
                if (tempBuffer == null) {
                    tempBuffer = new StringBuffer();
                }

                tempBuffer.append(chars[i]);
                ++i;
                if (firstCalculatedLevel >= 0
                        && (double) tempBuffer.length() == Math.pow(2.0D, (double) (level + firstCalculatedLevel))
                        || i == chars.length) {
                    break;
                }
            }

            String replacedQuote;
            if (tempBuffer != null) {
                --i;
                int length = tempBuffer.length();
                calculatedLevel = log(length, 2);
                if (firstCalculatedLevel == -2) {
                    firstCalculatedLevel = calculatedLevel;
                    if (level == 0) {
                        firstCalculatedLevel = calculatedLevel + 1;
                    }
                }

                if (!normalize) {
                    if (calculatedLevel - firstCalculatedLevel >= 0) {
                        replacedQuote = (String) lastQuote.get(calculatedLevel - firstCalculatedLevel);
                    } else {
                        replacedQuote = "";
                    }

                    while (calculatedLevel - firstCalculatedLevel < level - 1) {
                        lastQuote.remove(level);
                        lastType.remove(level);
                        --level;
                    }
                } else {
                    if (level == 0) {
                        replacedQuote = "";
                    } else {
                        replacedQuote = "\"";
                    }

                    while (calculatedLevel - firstCalculatedLevel < level - 1) {
                        --level;
                    }
                }

                buffer.append(replacedQuote);
                quoteFound = true;
            }

            if (!quoteFound) {
                if (level <= calculatedLevel + 1) {
                    if (123 == chars[i]) {
                        if (!normalize) {
                            lastType.add(Types.ARRAY);
                            replacedQuote = getNextQuotes((String) lastQuote.get(level), Types.ARRAY);
                            lastQuote.add(replacedQuote);
                        }

                        ++level;
                    } else if (40 == chars[i]) {
                        if (!normalize) {
                            lastType.add(Types.STRUCT);
                            replacedQuote = getNextQuotes((String) lastQuote.get(level), Types.STRUCT);
                            lastQuote.add(replacedQuote);
                        }

                        ++level;
                    }
                }

                buffer.append(chars[i]);
            }
        }

        return buffer.toString();
    }

    private static int log(int x, int base) {
        return (int) (Math.log((double) x) / Math.log((double) base));
    }

    private static String getNextQuotes(String lastQuotes, int lastType) {
        StringBuffer quotes = new StringBuffer();
        if (lastQuotes.length() == 0) {
            return "\"";
        } else {
            if (lastType == Types.ARRAY) {
                int length = lastQuotes.length();

                for (int i = 0; i < length; ++i) {
                    quotes.append('\\');
                }

                quotes.append(lastQuotes);
            } else {
                quotes.append(lastQuotes).append(lastQuotes);
            }

            return quotes.toString();
        }
    }
}

Related

  1. marshalCurlType(String javaType, boolean isAllowNull, boolean generateGenerics)
  2. matchMethods(String name, Method[] methods, Class[] spt, int mrs, boolean exact)
  3. nativeSQL(String sql, boolean noBackslashEscapes)
  4. newArrayOfType(int typeCode, int size)
  5. newInstance(Class clazz, Object... params)
  6. prettyPrint(Object obj)
  7. readAscii(Clob clob, String defaultValue)
  8. readMapListBySQL(String fileDir, String sql, List headers, List headerTypes)
  9. readObject(DataInput in, int sqlType)