Java Scanner Usage getConstantPoolsFromText(String text)

Here you can find the source of getConstantPoolsFromText(String text)

Description

get Constant Pools From Text

License

Open Source License

Declaration

public static List<List<String>> getConstantPoolsFromText(String text) 

Method Source Code

//package com.java2s;
/*/*from w w  w  .  j a  v  a 2  s  .com*/
 *  Copyright (C) 2010-2015 JPEXS, All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 */

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class Main {
    public static List<List<String>> getConstantPoolsFromText(String text) {
        Scanner scanner = new Scanner(text);
        scanner.nextLine(); // ignore first line
        List<List<String>> result = new ArrayList<>();
        List<String> cPool = new ArrayList<>();
        result.add(cPool);
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.startsWith("---")) {
                cPool = new ArrayList<>();
                result.add(cPool);
            }

            String[] parts = line.split("\\|", 2);
            if (parts.length >= 2) {
                cPool.add(unescapeJavaString(parts[1]));
            }
        }

        return result;
    }

    /**
     * Unescapes a string that contains standard Java escape sequences.
     * <ul>
     * <li><strong>&#92;b &#92;f &#92;n &#92;r &#92;t &#92;" &#92;'</strong> :
     * BS, FF, NL, CR, TAB, double and single quote.</li>
     * <li><strong>&#92;X &#92;XX &#92;XXX</strong> : Octal character
     * specification (0 - 377, 0x00 - 0xFF).</li>
     * <li><strong>&#92;uXXXX</strong> : Hexadecimal based Unicode
     * character.</li>
     * </ul>
     *
     * @param st A string optionally containing standard java escape sequences.
     * @return The translated string.
     */
    public static String unescapeJavaString(String st) {

        StringBuilder sb = new StringBuilder(st.length());

        for (int i = 0; i < st.length(); i++) {
            char ch = st.charAt(i);
            if (ch == '\\') {
                char nextChar = (i == st.length() - 1) ? '\\' : st
                        .charAt(i + 1);
                // Octal escape?
                if (nextChar >= '0' && nextChar <= '7') {
                    String code = "" + nextChar;
                    i++;
                    if ((i < st.length() - 1) && st.charAt(i + 1) >= '0'
                            && st.charAt(i + 1) <= '7') {
                        code += st.charAt(i + 1);
                        i++;
                        if ((i < st.length() - 1)
                                && st.charAt(i + 1) >= '0'
                                && st.charAt(i + 1) <= '7') {
                            code += st.charAt(i + 1);
                            i++;
                        }
                    }
                    sb.append((char) Integer.parseInt(code, 8));
                    continue;
                }

                switch (nextChar) {
                case '\\':
                    ch = '\\';
                    break;
                case 'b':
                    ch = '\b';
                    break;
                case 'f':
                    ch = '\f';
                    break;
                case 'n':
                    ch = '\n';
                    break;
                case 'r':
                    ch = '\r';
                    break;
                case 't':
                    ch = '\t';
                    break;
                case '\"':
                    ch = '\"';
                    break;
                case '\'':
                    ch = '\'';
                    break;
                // Hex Unicode: u????
                case 'u':
                    if (i >= st.length() - 5) {
                        ch = 'u';
                        break;
                    }
                    int code = Integer.parseInt(
                            "" + st.charAt(i + 2) + st.charAt(i + 3)
                                    + st.charAt(i + 4) + st.charAt(i + 5),
                            16);
                    sb.append(Character.toChars(code));
                    i += 5;
                    continue;
                }

                i++;
            }

            sb.append(ch);
        }

        return sb.toString();
    }
}

Related

  1. enterToContinue()
  2. equalsToIgnoreEndline(String expected, String actual)
  3. fromStringToQuery(String value)
  4. getBoolean()
  5. getChar()
  6. getDirectoryString()
  7. getEndLine(String cqlString)
  8. getExtensions(Scanner scanner)
  9. getFirstInt(String s)