Example usage for java.lang Character toString

List of usage examples for java.lang Character toString

Introduction

In this page you can find the example usage for java.lang Character toString.

Prototype

public String toString() 

Source Link

Document

Returns a String object representing this Character 's value.

Usage

From source file:com.cgxlib.xq.rebind.JsniBundleGenerator.java

/**
 * Adapt a java-script block which could produce a syntax error when
 * embedding it in a JSNI block./*from   w  w  w. j  a v a2  s .com*/
 * <p/>
 * The objective is to replace any 'c' comment-ending occurrence to avoid closing
 * JSNI comment blocks prematurely.
 * <p/>
 * A Regexp based parser is not reliable, this approach is better and faster.
 */
// Note: this comment is intentionally using c++ style to allow writing the '*/' sequence.
//
// - Remove C comments: /* ... */
// - Remove C++ comments: // ...
// - Escape certain strings:  '...*/...' to '...*' + '/...'
// - Rewrite inline regex:  /...*/igm to new RegExp('...*' + 'igm');
private String parseJavascriptSource(String js) throws Exception {

    boolean isJS = true;
    boolean isSingQuot = false;
    boolean isDblQuot = false;
    boolean isSlash = false;
    boolean isCComment = false;
    boolean isCPPComment = false;
    boolean isRegex = false;
    boolean isOper = false;

    StringBuilder ret = new StringBuilder();
    String tmp = "";
    Character last = 0;
    Character prev = 0;

    for (int i = 0, l = js.length(); i < l; i++) {
        Character c = js.charAt(i);
        String out = c.toString();

        if (isJS) {
            isDblQuot = c == '"';
            isSingQuot = c == '\'';
            isSlash = c == '/';
            isJS = !isDblQuot && !isSingQuot && !isSlash;
            if (!isJS) {
                out = tmp = "";
                isCPPComment = isCComment = isRegex = false;
            }
        } else if (isSingQuot) {
            isJS = !(isSingQuot = last == '\\' || c != '\'');
            if (isJS)
                out = escapeQuotedString(tmp, c);
            else
                tmp += c;
        } else if (isDblQuot) {
            isJS = !(isDblQuot = last == '\\' || c != '"');
            if (isJS)
                out = escapeQuotedString(tmp, c);
            else
                tmp += c;
        } else if (isSlash) {
            if (!isCPPComment && !isCComment && !isRegex && !isOper) {
                isCPPComment = c == '/';
                isCComment = c == '*';
                isOper = !isCPPComment && !isCComment && !"=(&|?:;},".contains("" + prev);
                isRegex = !isCPPComment && !isCComment && !isOper;
            }
            if (isOper) {
                isJS = !(isSlash = isOper = false);
                out = "" + last + c;
            } else if (isCPPComment) {
                isJS = !(isSlash = isCPPComment = c != '\n');
                if (isJS)
                    out = "\n";
            } else if (isCComment) {
                isSlash = isCComment = !(isJS = (last == '*' && c == '/'));
                if (isJS)
                    out = "";
            } else if (isRegex) {
                isJS = !(isSlash = isRegex = (last == '\\' || c != '/'));
                if (isJS) {
                    String mod = "";
                    while (++i < l) {
                        c = js.charAt(i);
                        if ("igm".contains("" + c))
                            mod += c;
                        else
                            break;
                    }
                    out = escapeInlineRegex(tmp, mod) + c;
                } else {
                    tmp += c;
                }
            } else {
                isJS = true;
            }
        }

        if (isJS) {
            ret.append(out);
        }
        if (last != ' ') {
            prev = last;
        }
        last = prev == '\\' && c == '\\' ? 0 : c;
    }
    return ret.toString();
}

From source file:dk.statsbiblioteket.util.CachedCollator.java

/**
 * Fills the cache, based on mostCommon and {@link #subCollator}.
 *
 * @param mostCommon the most common characters in the application-specific
 *                   domain in prioritized order.
 *///w  w w .  ja  va  2  s . c  o  m
protected void buildCache(String mostCommon) {
    log.debug("Building cache for '" + mostCommon + "'");
    // Make sure the characters are unique
    Set<String> unique = new LinkedHashSet<String>(mostCommon.length());
    int highest = 0;
    for (Character c : mostCommon.toCharArray()) {
        unique.add(c.toString());
        highest = Math.max(highest, c);
    }
    // Sort the characters
    List<String> sorted = new ArrayList<String>(unique);
    Collections.sort(sorted, subCollator);
    if (log.isTraceEnabled()) {
        log.trace("mostCommon sorted: '" + Logs.expand(sorted, 5000) + "'");
    }

    // Split in low and high value characters.
    //        cachedPositions = new int[highest+1];
    int position = 1;
    char lastChar = 0;
    for (String cString : sorted) {
        if (cString.length() != 1) {
            log.warn("The expected character '" + cString + "' was of length " + cString.length()
                    + ". Skipping");
            continue;
        }
        char c = cString.charAt(0);
        if (lastChar == 0) {
            lastChar = c;
        }
        cachedPositions[c] = position;
        if (subCollator.compare(Character.toString(lastChar), Character.toString(c)) != 0) {
            position++;
        }
        lastChar = c;
    }
    log.debug("Finished building cache for " + position + " characters (" + (mostCommon.length() - position)
            + " duplicates removed, " + position + " unique positions) " + "of which the highest was "
            + highest);
}

From source file:com.netflix.lipstick.Main.java

static int run(String args[], PigProgressNotificationListener listener) {
    int rc = 1;/*from www.ja va  2s. c  o m*/
    boolean verbose = false;
    boolean gruntCalled = false;
    String logFileName = null;

    try {
        Configuration conf = new Configuration(false);
        GenericOptionsParser parser = new GenericOptionsParser(conf, args);
        conf = parser.getConfiguration();

        Properties properties = new Properties();
        PropertiesUtil.loadDefaultProperties(properties);
        properties.putAll(ConfigurationUtil.toProperties(conf));

        String[] pigArgs = parser.getRemainingArgs();

        boolean userSpecifiedLog = false;
        boolean checkScriptOnly = false;

        BufferedReader pin = null;
        boolean debug = false;
        boolean dryrun = false;
        boolean embedded = false;
        List<String> params = new ArrayList<String>();
        List<String> paramFiles = new ArrayList<String>();
        HashSet<String> optimizerRules = new HashSet<String>();

        CmdLineParser opts = new CmdLineParser(pigArgs);
        opts.registerOpt('4', "log4jconf", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('b', "brief", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('c', "check", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('d', "debug", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('e', "execute", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('f', "file", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('g', "embedded", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('h', "help", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('i', "version", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('l', "logfile", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('m', "param_file", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('p', "param", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('r', "dryrun", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('t', "optimizer_off", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('v', "verbose", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('w', "warning", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('x', "exectype", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('F', "stop_on_failure", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('M', "no_multiquery", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('P', "propertyFile", CmdLineParser.ValueExpected.REQUIRED);

        ExecMode mode = ExecMode.UNKNOWN;
        String file = null;
        String engine = null;
        ExecType execType = ExecType.MAPREDUCE;
        String execTypeString = properties.getProperty("exectype");
        if (execTypeString != null && execTypeString.length() > 0) {
            execType = ExecType.fromString(execTypeString);
        }

        // set up client side system properties in UDF context
        UDFContext.getUDFContext().setClientSystemProps(properties);

        char opt;
        while ((opt = opts.getNextOpt()) != CmdLineParser.EndOfOpts) {
            switch (opt) {
            case '4':
                String log4jconf = opts.getValStr();
                if (log4jconf != null) {
                    properties.setProperty(LOG4J_CONF, log4jconf);
                }
                break;

            case 'b':
                properties.setProperty(BRIEF, "true");
                break;

            case 'c':
                checkScriptOnly = true;
                break;

            case 'd':
                String logLevel = opts.getValStr();
                if (logLevel != null) {
                    properties.setProperty(DEBUG, logLevel);
                }
                debug = true;
                break;

            case 'e':
                mode = ExecMode.STRING;
                break;

            case 'f':
                mode = ExecMode.FILE;
                file = opts.getValStr();
                break;

            case 'g':
                embedded = true;
                engine = opts.getValStr();
                break;

            case 'F':
                properties.setProperty("stop.on.failure", "" + true);
                break;

            case 'h':
                String topic = opts.getValStr();
                if (topic != null)
                    if (topic.equalsIgnoreCase("properties"))
                        printProperties();
                    else {
                        System.out.println("Invalide help topic - " + topic);
                        usage();
                    }
                else
                    usage();
                return ReturnCode.SUCCESS;

            case 'i':
                System.out.println(getVersionString());
                return ReturnCode.SUCCESS;

            case 'l':
                // call to method that validates the path to the log file
                // and sets up the file to store the client side log file
                String logFileParameter = opts.getValStr();
                if (logFileParameter != null && logFileParameter.length() > 0) {
                    logFileName = validateLogFile(logFileParameter, null);
                } else {
                    logFileName = validateLogFile(logFileName, null);
                }
                userSpecifiedLog = true;
                properties.setProperty("pig.logfile", (logFileName == null ? "" : logFileName));
                break;

            case 'm':
                paramFiles.add(opts.getValStr());
                break;

            case 'M':
                // turns off multiquery optimization
                properties.setProperty("opt.multiquery", "" + false);
                break;

            case 'p':
                params.add(opts.getValStr());
                break;

            case 'r':
                // currently only used for parameter substitution
                // will be extended in the future
                dryrun = true;
                break;

            case 't':
                optimizerRules.add(opts.getValStr());
                break;

            case 'v':
                properties.setProperty(VERBOSE, "" + true);
                verbose = true;
                break;

            case 'w':
                properties.setProperty("aggregate.warning", "" + false);
                break;

            case 'x':
                try {
                    execType = ExecType.fromString(opts.getValStr());
                } catch (IOException e) {
                    throw new RuntimeException("ERROR: Unrecognized exectype.", e);
                }
                break;

            case 'P': {
                InputStream inputStream = null;
                try {
                    FileLocalizer.FetchFileRet localFileRet = FileLocalizer.fetchFile(properties,
                            opts.getValStr());
                    inputStream = new BufferedInputStream(new FileInputStream(localFileRet.file));
                    properties.load(inputStream);
                } catch (IOException e) {
                    throw new RuntimeException("Unable to parse properties file '" + opts.getValStr() + "'");
                } finally {
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                        }
                    }
                }
            }
                break;

            default: {
                Character cc = Character.valueOf(opt);
                throw new AssertionError("Unhandled option " + cc.toString());
            }
            }
        }
        // create the context with the parameter
        PigContext pigContext = new PigContext(execType, properties);

        // create the static script state object
        String commandLine = LoadFunc.join((AbstractList<String>) Arrays.asList(args), " ");
        ScriptState scriptState = ScriptState.start(commandLine, pigContext);

        pigContext.getProperties().setProperty("pig.cmd.args", commandLine);

        if (logFileName == null && !userSpecifiedLog) {
            logFileName = validateLogFile(properties.getProperty("pig.logfile"), null);
        }

        pigContext.getProperties().setProperty("pig.logfile", (logFileName == null ? "" : logFileName));

        // configure logging
        configureLog4J(properties, pigContext);
        log.info(getVersionString().replace("\n", ""));

        if (logFileName != null) {
            log.info("Logging error messages to: " + logFileName);
        }

        if (!Boolean.valueOf(properties.getProperty(PROP_FILT_SIMPL_OPT, "false"))) {
            // turn off if the user has not explicitly turned on this
            // optimization
            optimizerRules.add("FilterLogicExpressionSimplifier");
        }

        if (optimizerRules.size() > 0) {
            pigContext.getProperties().setProperty("pig.optimizer.rules",
                    ObjectSerializer.serialize(optimizerRules));
        }

        if (properties.get("udf.import.list") != null)
            PigContext.initializeImportList((String) properties.get("udf.import.list"));

        PigContext.setClassLoader(pigContext.createCl(null));
        if (listener != null) {
            scriptState.registerListener(listener);
        }
        if (properties.getProperty("pig.listeners") != null) {
            for (String clsName : properties.getProperty("pig.listeners").split(":")) {
                // TODO: Is there a better/cleaner way to create these
                // listeners?
                scriptState.registerListener((PigProgressNotificationListener) PigContext.getClassLoader()
                        .loadClass(clsName).newInstance());
            }
        } else {
            scriptState.registerListener(new com.netflix.lipstick.listeners.LipstickPPNL());
        }

        // construct the parameter substitution preprocessor
        LipstickGrunt grunt = null;
        BufferedReader in;
        String substFile = null;

        paramFiles = fetchRemoteParamFiles(paramFiles, properties);
        pigContext.setParams(params);
        pigContext.setParamFiles(paramFiles);

        switch (mode) {

        case FILE: {
            String remainders[] = opts.getRemainingArgs();
            pigContext.getProperties().setProperty(PigContext.PIG_CMD_ARGS_REMAINDERS,
                    ObjectSerializer.serialize(remainders));
            FileLocalizer.FetchFileRet localFileRet = FileLocalizer.fetchFile(properties, file);
            if (localFileRet.didFetch) {
                properties.setProperty("pig.jars.relative.to.dfs", "true");
            }

            scriptState.setFileName(file);

            if (embedded) {
                return runEmbeddedScript(pigContext, localFileRet.file.getPath(), engine);
            } else {
                SupportedScriptLang type = determineScriptType(localFileRet.file.getPath());
                if (type != null) {
                    return runEmbeddedScript(pigContext, localFileRet.file.getPath(),
                            type.name().toLowerCase());
                }
            }

            in = new BufferedReader(new FileReader(localFileRet.file));

            // run parameter substitution preprocessor first
            substFile = file + ".substituted";

            pin = runParamPreprocessor(pigContext, in, substFile, debug || dryrun || checkScriptOnly);
            if (dryrun) {
                if (dryrun(substFile, pigContext)) {
                    log.info("Dry run completed. Substituted pig script is at " + substFile
                            + ". Expanded pig script is at " + file + ".expanded");
                } else {
                    log.info("Dry run completed. Substituted pig script is at " + substFile);
                }
                return ReturnCode.SUCCESS;
            }

            logFileName = validateLogFile(logFileName, file);
            pigContext.getProperties().setProperty("pig.logfile", logFileName);

            // Set job name based on name of the script
            pigContext.getProperties().setProperty(PigContext.JOB_NAME, "PigLatin:" + new File(file).getName());

            if (!debug) {
                new File(substFile).deleteOnExit();
            }

            scriptState.setScript(new File(file));

            grunt = new LipstickGrunt(pin, pigContext);
            gruntCalled = true;

            if (checkScriptOnly) {
                grunt.checkScript(substFile);
                System.err.println(file + " syntax OK");
                rc = ReturnCode.SUCCESS;
            } else {
                int results[] = grunt.exec();
                rc = getReturnCodeForStats(results);
            }

            return rc;
        }

        case STRING: {
            if (checkScriptOnly) {
                System.err.println("ERROR:" + "-c (-check) option is only valid "
                        + "when executing pig with a pig script file)");
                return ReturnCode.ILLEGAL_ARGS;
            }
            // Gather up all the remaining arguments into a string and pass
            // them into
            // grunt.
            StringBuffer sb = new StringBuffer();
            String remainders[] = opts.getRemainingArgs();
            for (int i = 0; i < remainders.length; i++) {
                if (i != 0)
                    sb.append(' ');
                sb.append(remainders[i]);
            }

            scriptState.setScript(sb.toString());

            in = new BufferedReader(new StringReader(sb.toString()));

            grunt = new LipstickGrunt(in, pigContext);
            gruntCalled = true;
            int results[] = grunt.exec();
            return getReturnCodeForStats(results);
        }

        default:
            break;
        }

        // If we're here, we don't know yet what they want. They may have
        // just
        // given us a jar to execute, they might have given us a pig script
        // to
        // execute, or they might have given us a dash (or nothing) which
        // means to
        // run grunt interactive.
        String remainders[] = opts.getRemainingArgs();
        if (remainders == null) {
            if (checkScriptOnly) {
                System.err.println("ERROR:" + "-c (-check) option is only valid "
                        + "when executing pig with a pig script file)");
                return ReturnCode.ILLEGAL_ARGS;
            }
            // Interactive
            mode = ExecMode.SHELL;
            ConsoleReader reader = new ConsoleReader(System.in, new OutputStreamWriter(System.out));
            reader.setDefaultPrompt("grunt> ");
            final String HISTORYFILE = ".pig_history";
            String historyFile = System.getProperty("user.home") + File.separator + HISTORYFILE;
            reader.setHistory(new History(new File(historyFile)));
            ConsoleReaderInputStream inputStream = new ConsoleReaderInputStream(reader);
            grunt = new LipstickGrunt(new BufferedReader(new InputStreamReader(inputStream)), pigContext);
            grunt.setConsoleReader(reader);
            gruntCalled = true;
            grunt.run();
            return ReturnCode.SUCCESS;
        } else {
            pigContext.getProperties().setProperty(PigContext.PIG_CMD_ARGS_REMAINDERS,
                    ObjectSerializer.serialize(remainders));

            // They have a pig script they want us to run.
            mode = ExecMode.FILE;

            FileLocalizer.FetchFileRet localFileRet = FileLocalizer.fetchFile(properties, remainders[0]);
            if (localFileRet.didFetch) {
                properties.setProperty("pig.jars.relative.to.dfs", "true");
            }

            scriptState.setFileName(remainders[0]);

            if (embedded) {
                return runEmbeddedScript(pigContext, localFileRet.file.getPath(), engine);
            } else {
                SupportedScriptLang type = determineScriptType(localFileRet.file.getPath());
                if (type != null) {
                    return runEmbeddedScript(pigContext, localFileRet.file.getPath(),
                            type.name().toLowerCase());
                }
            }

            in = new BufferedReader(new FileReader(localFileRet.file));

            // run parameter substitution preprocessor first
            substFile = remainders[0] + ".substituted";
            pin = runParamPreprocessor(pigContext, in, substFile, debug || dryrun || checkScriptOnly);
            if (dryrun) {
                if (dryrun(substFile, pigContext)) {
                    log.info("Dry run completed. Substituted pig script is at " + substFile
                            + ". Expanded pig script is at " + remainders[0] + ".expanded");
                } else {
                    log.info("Dry run completed. Substituted pig script is at " + substFile);
                }
                return ReturnCode.SUCCESS;
            }

            logFileName = validateLogFile(logFileName, remainders[0]);
            pigContext.getProperties().setProperty("pig.logfile", logFileName);

            if (!debug) {
                new File(substFile).deleteOnExit();
            }

            // Set job name based on name of the script
            pigContext.getProperties().setProperty(PigContext.JOB_NAME,
                    "PigLatin:" + new File(remainders[0]).getName());

            scriptState.setScript(localFileRet.file);

            grunt = new LipstickGrunt(pin, pigContext);
            gruntCalled = true;

            if (checkScriptOnly) {
                grunt.checkScript(substFile);
                System.err.println(remainders[0] + " syntax OK");
                rc = ReturnCode.SUCCESS;
            } else {
                int results[] = grunt.exec();
                rc = getReturnCodeForStats(results);
            }
            return rc;
        }

        // Per Utkarsh and Chris invocation of jar file via pig depricated.
    } catch (ParseException e) {
        usage();
        rc = ReturnCode.PARSE_EXCEPTION;
        PigStatsUtil.setErrorMessage(e.getMessage());
    } catch (org.apache.pig.tools.parameters.ParseException e) {
        // usage();
        rc = ReturnCode.PARSE_EXCEPTION;
        PigStatsUtil.setErrorMessage(e.getMessage());
    } catch (IOException e) {
        if (e instanceof PigException) {
            PigException pe = (PigException) e;
            rc = (pe.retriable()) ? ReturnCode.RETRIABLE_EXCEPTION : ReturnCode.PIG_EXCEPTION;
            PigStatsUtil.setErrorMessage(pe.getMessage());
            PigStatsUtil.setErrorCode(pe.getErrorCode());
        } else {
            rc = ReturnCode.IO_EXCEPTION;
            PigStatsUtil.setErrorMessage(e.getMessage());
        }

        if (!gruntCalled) {
            LogUtils.writeLog(e, logFileName, log, verbose, "Error before Pig is launched");
        }
    } catch (Throwable e) {
        rc = ReturnCode.THROWABLE_EXCEPTION;
        PigStatsUtil.setErrorMessage(e.getMessage());
        if (!gruntCalled) {
            LogUtils.writeLog(e, logFileName, log, verbose, "Error before Pig is launched");
        }
    } finally {
        // clear temp files
        FileLocalizer.deleteTempFiles();
        // PerformanceTimerFactory.getPerfTimerFactory().dumpTimers();
    }

    return rc;
}

From source file:edu.jhu.pha.vospace.storage.SwiftKeystoneStorageManager.java

public static List<FilesObject> listObjectsStartingWith(String container, String startsWith, String path,
        int limit, String marker, String end_marker, Character delimiter) throws IOException, FilesException {

    HttpGet method = null;//from  w w w  .  jav a 2 s .c  o m

    LinkedList<NameValuePair> parameters = new LinkedList<NameValuePair>();
    parameters.add(new BasicNameValuePair("format", "json"));
    if (startsWith != null) {
        parameters.add(new BasicNameValuePair(FilesConstants.LIST_CONTAINER_NAME_QUERY, startsWith));
    }
    if (path != null) {
        parameters.add(new BasicNameValuePair("path", path));
    }
    if (limit > 0) {
        parameters.add(new BasicNameValuePair("limit", String.valueOf(limit)));
    }
    if (marker != null) {
        parameters.add(new BasicNameValuePair("marker", marker));
    }
    if (end_marker != null) {
        parameters.add(new BasicNameValuePair("marker", end_marker));
    }
    if (delimiter != null) {
        parameters.add(new BasicNameValuePair("delimiter", delimiter.toString()));
    }

    String uri = parameters.size() > 0 ? makeURI(getFileUrl + "/" + sanitizeForURI(container), parameters)
            : getFileUrl;
    method = new HttpGet(uri);
    method.getParams().setIntParameter("http.socket.timeout", connectionTimeOut);
    method.setHeader(FilesConstants.X_AUTH_TOKEN, strToken);
    FilesResponse response = new FilesResponse(client.execute(method));

    if (response.getStatusCode() == HttpStatus.SC_OK) {
        ArrayList<FilesObject> objectList = new ArrayList<FilesObject>();

        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createJsonParser(IOUtils.toString(response.getResponseBodyAsStream()));
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        TypeReference ref = new TypeReference<List<FilesObject>>() {
        };
        objectList = mapper.readValue(jp, ref);
        return objectList;

    } else if (response.getStatusCode() == HttpStatus.SC_NO_CONTENT) {
        logger.debug("Container " + container + " has no Objects");
        return new ArrayList<FilesObject>();
    } else if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
        throw new FilesNotFoundException("Container was not found", response.getResponseHeaders(),
                response.getStatusLine());
    } else {
        throw new FilesException("Unexpected Server Result", response.getResponseHeaders(),
                response.getStatusLine());
    }
}

From source file:com.fpil.android.remotesensor.MainDisplay.java

private void sendMessage(Character message) {
    String str = message.toString();

    sendMessage(str);
}

From source file:com.thinkbiganalytics.discovery.parsers.csv.CSVFileSchemaParser.java

private String stringForCharacter(String s) {
    if (StringUtils.isEmpty(s)) {
        return null;
    }/*from w  w  w  . j ava 2s  . c  om*/
    Character c = s.charAt(0);

    switch (c) {
    case ';':
        return "\\;";
    case '\t':
        return "\\t";
    case '\'':
        return "\\\'";
    case '\\':
        return "\\\\";
    default:
        return StringEscapeUtils.escapeJava(c.toString());
    }
}

From source file:eionet.gdem.utils.Utils.java

/**
 * Escape single character in text. If the character is already escaped, then avoid double escaping.
 *
 * @param pos/*from  w ww .  ja va 2 s .c o m*/
 *            Character position in text.
 * @param text
 *            Text to be escaped.
 * @return Return escaped character.
 */
public static String escapeXML(int pos, String text) {

    if (xmlEscapes == null) {
        setXmlEscapes();
    }
    Character c = new Character(text.charAt(pos));

    for (String esc : xmlEscapes.values()) {
        if (pos + esc.length() < text.length()) {
            String sub = text.substring(pos, pos + esc.length());
            if (sub.equals(esc)) {
                return c.toString();
            }
        }
    }

    if (pos + 1 < text.length() && text.charAt(pos + 1) == '#') {
        int semicolonPos = text.indexOf(';', pos + 1);
        if (semicolonPos != -1) {
            String sub = text.substring(pos + 2, semicolonPos);
            if (sub != null) {
                try {
                    // if the string between # and ; is a number then return
                    // true,
                    // because it is most probably an escape sequence
                    if (Integer.parseInt(sub) >= 0) {
                        return c.toString();
                    }
                } catch (NumberFormatException nfe) {
                }
            }
        }
    }

    String esc = xmlEscapes.get(c);
    if (esc != null) {
        return esc;
    } else if ((int) c > 10000) {
        return "&#" + (int) c + ";";
    } else {
        return c.toString();
    }
}

From source file:org.apache.pig.Main.java

static int run(String args[], PigProgressNotificationListener listener) {
    DateTime startTime = new DateTime();
    int rc = 1;/*from   ww w . j  a  v a 2s.c  o m*/
    boolean verbose = false;
    boolean gruntCalled = false;
    boolean deleteTempFiles = true;
    String logFileName = null;
    boolean printScriptRunTime = true;
    PigContext pigContext = null;

    try {
        Configuration conf = new Configuration(false);
        GenericOptionsParser parser = new GenericOptionsParser(conf, args);
        conf = parser.getConfiguration();

        Properties properties = new Properties();
        PropertiesUtil.loadDefaultProperties(properties);
        properties.putAll(ConfigurationUtil.toProperties(conf));

        if (listener == null) {
            listener = makeListener(properties);
        }
        String[] pigArgs = parser.getRemainingArgs();

        boolean userSpecifiedLog = false;
        boolean checkScriptOnly = false;

        BufferedReader pin = null;
        boolean debug = false;
        boolean dryrun = false;
        boolean embedded = false;
        List<String> params = new ArrayList<String>();
        List<String> paramFiles = new ArrayList<String>();
        HashSet<String> disabledOptimizerRules = new HashSet<String>();

        CmdLineParser opts = new CmdLineParser(pigArgs);
        opts.registerOpt('4', "log4jconf", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('b', "brief", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('c', "check", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('d', "debug", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('e', "execute", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('f', "file", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('g', "embedded", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('h', "help", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('i', "version", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('l', "logfile", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('m', "param_file", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('p', "param", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('r', "dryrun", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('t', "optimizer_off", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('v', "verbose", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('w', "warning", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('x', "exectype", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('F', "stop_on_failure", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('M', "no_multiquery", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('N', "no_fetch", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('P', "propertyFile", CmdLineParser.ValueExpected.REQUIRED);

        ExecMode mode = ExecMode.UNKNOWN;
        String file = null;
        String engine = null;

        // set up client side system properties in UDF context
        UDFContext.getUDFContext().setClientSystemProps(properties);

        char opt;
        while ((opt = opts.getNextOpt()) != CmdLineParser.EndOfOpts) {
            switch (opt) {
            case '4':
                String log4jconf = opts.getValStr();
                if (log4jconf != null) {
                    properties.setProperty(LOG4J_CONF, log4jconf);
                }
                break;

            case 'b':
                properties.setProperty(BRIEF, "true");
                break;

            case 'c':
                checkScriptOnly = true;
                break;

            case 'd':
                String logLevel = opts.getValStr();
                if (logLevel != null) {
                    properties.setProperty(DEBUG, logLevel);
                }
                debug = true;
                break;

            case 'e':
                mode = ExecMode.STRING;
                break;

            case 'f':
                mode = ExecMode.FILE;
                file = opts.getValStr();
                break;

            case 'g':
                embedded = true;
                engine = opts.getValStr();
                break;

            case 'F':
                properties.setProperty("stop.on.failure", "" + true);
                break;

            case 'h':
                String topic = opts.getValStr();
                if (topic != null)
                    if (topic.equalsIgnoreCase("properties"))
                        printProperties();
                    else {
                        System.out.println("Invalide help topic - " + topic);
                        usage();
                    }
                else
                    usage();
                return ReturnCode.SUCCESS;

            case 'i':
                printScriptRunTime = false;
                System.out.println(getVersionString());
                return ReturnCode.SUCCESS;

            case 'l':
                //call to method that validates the path to the log file
                //and sets up the file to store the client side log file
                String logFileParameter = opts.getValStr();
                if (logFileParameter != null && logFileParameter.length() > 0) {
                    logFileName = validateLogFile(logFileParameter, null);
                } else {
                    logFileName = validateLogFile(logFileName, null);
                }
                userSpecifiedLog = true;
                properties.setProperty("pig.logfile", (logFileName == null ? "" : logFileName));
                break;

            case 'm':
                paramFiles.add(opts.getValStr());
                break;

            case 'M':
                // turns off multiquery optimization
                properties.setProperty(PigConfiguration.PIG_OPT_MULTIQUERY, "" + false);
                break;

            case 'N':
                properties.setProperty(PigConfiguration.PIG_OPT_FETCH, "" + false);
                break;

            case 'p':
                params.add(opts.getValStr());
                break;

            case 'r':
                // currently only used for parameter substitution
                // will be extended in the future
                dryrun = true;
                break;

            case 't':
                disabledOptimizerRules.add(opts.getValStr());
                break;

            case 'v':
                properties.setProperty(VERBOSE, "" + true);
                verbose = true;
                break;

            case 'w':
                properties.setProperty("aggregate.warning", "" + false);
                break;

            case 'x':
                properties.setProperty("exectype", opts.getValStr());
                if (opts.getValStr().toLowerCase().contains("local")) {
                    UserGroupInformation.setConfiguration(new Configuration(false));
                }
                break;

            case 'P': {
                InputStream inputStream = null;
                try {
                    FileLocalizer.FetchFileRet localFileRet = FileLocalizer.fetchFile(properties,
                            opts.getValStr());
                    inputStream = new BufferedInputStream(new FileInputStream(localFileRet.file));
                    properties.load(inputStream);
                } catch (IOException e) {
                    throw new RuntimeException("Unable to parse properties file '" + opts.getValStr() + "'");
                } finally {
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                        }
                    }
                }
            }
                break;

            default: {
                Character cc = Character.valueOf(opt);
                throw new AssertionError("Unhandled option " + cc.toString());
            }
            }
        }

        // create the context with the parameter
        pigContext = new PigContext(properties);

        // create the static script state object
        ScriptState scriptState = pigContext.getExecutionEngine().instantiateScriptState();
        String commandLine = LoadFunc.join((AbstractList<String>) Arrays.asList(args), " ");
        scriptState.setCommandLine(commandLine);
        if (listener != null) {
            scriptState.registerListener(listener);
        }
        ScriptState.start(scriptState);

        pigContext.getProperties().setProperty("pig.cmd.args", commandLine);

        if (logFileName == null && !userSpecifiedLog) {
            logFileName = validateLogFile(properties.getProperty("pig.logfile"), null);
        }

        pigContext.getProperties().setProperty("pig.logfile", (logFileName == null ? "" : logFileName));

        // configure logging
        configureLog4J(properties, pigContext);

        log.info(getVersionString().replace("\n", ""));

        if (logFileName != null) {
            log.info("Logging error messages to: " + logFileName);
        }

        deleteTempFiles = Boolean
                .valueOf(properties.getProperty(PigConfiguration.PIG_DELETE_TEMP_FILE, "true"));

        pigContext.getProperties().setProperty(PigImplConstants.PIG_OPTIMIZER_RULES_KEY,
                ObjectSerializer.serialize(disabledOptimizerRules));

        PigContext.setClassLoader(pigContext.createCl(null));

        // construct the parameter substitution preprocessor
        Grunt grunt = null;
        BufferedReader in;
        String substFile = null;

        paramFiles = fetchRemoteParamFiles(paramFiles, properties);
        pigContext.setParams(params);
        pigContext.setParamFiles(paramFiles);

        switch (mode) {

        case FILE: {
            String remainders[] = opts.getRemainingArgs();
            if (remainders != null) {
                pigContext.getProperties().setProperty(PigContext.PIG_CMD_ARGS_REMAINDERS,
                        ObjectSerializer.serialize(remainders));
            }
            FileLocalizer.FetchFileRet localFileRet = FileLocalizer.fetchFile(properties, file);
            if (localFileRet.didFetch) {
                properties.setProperty("pig.jars.relative.to.dfs", "true");
            }

            scriptState.setFileName(file);

            if (embedded) {
                return runEmbeddedScript(pigContext, localFileRet.file.getPath(), engine);
            } else {
                SupportedScriptLang type = determineScriptType(localFileRet.file.getPath());
                if (type != null) {
                    return runEmbeddedScript(pigContext, localFileRet.file.getPath(),
                            type.name().toLowerCase());
                }
            }
            //Reader is created by first loading "pig.load.default.statements" or .pigbootup file if available
            in = new BufferedReader(new InputStreamReader(
                    Utils.getCompositeStream(new FileInputStream(localFileRet.file), properties)));

            // run parameter substitution preprocessor first
            substFile = file + ".substituted";

            pin = runParamPreprocessor(pigContext, in, substFile, debug || dryrun || checkScriptOnly);
            if (dryrun) {
                if (dryrun(substFile, pigContext)) {
                    log.info("Dry run completed. Substituted pig script is at " + substFile
                            + ". Expanded pig script is at " + file + ".expanded");
                } else {
                    log.info("Dry run completed. Substituted pig script is at " + substFile);
                }
                return ReturnCode.SUCCESS;
            }

            logFileName = validateLogFile(logFileName, localFileRet.file);
            pigContext.getProperties().setProperty("pig.logfile", (logFileName == null ? "" : logFileName));

            // Set job name based on name of the script
            pigContext.getProperties().setProperty(PigContext.JOB_NAME, "PigLatin:" + new File(file).getName());
            if (!debug) {
                new File(substFile).deleteOnExit();
            }

            scriptState.setScript(localFileRet.file);

            grunt = new Grunt(pin, pigContext);
            gruntCalled = true;

            if (checkScriptOnly) {
                grunt.checkScript(substFile);
                System.err.println(file + " syntax OK");
                rc = ReturnCode.SUCCESS;
            } else {
                int results[] = grunt.exec();
                rc = getReturnCodeForStats(results);
            }

            return rc;
        }

        case STRING: {
            if (checkScriptOnly) {
                System.err.println("ERROR:" + "-c (-check) option is only valid "
                        + "when executing pig with a pig script file)");
                return ReturnCode.ILLEGAL_ARGS;
            }
            // Gather up all the remaining arguments into a string and pass them into
            // grunt.
            StringBuffer sb = new StringBuffer();
            String remainders[] = opts.getRemainingArgs();
            for (int i = 0; i < remainders.length; i++) {
                if (i != 0)
                    sb.append(' ');
                sb.append(remainders[i]);
            }

            sb.append('\n');

            scriptState.setScript(sb.toString());

            in = new BufferedReader(new StringReader(sb.toString()));

            grunt = new Grunt(in, pigContext);
            gruntCalled = true;
            int results[] = grunt.exec();
            return getReturnCodeForStats(results);
        }

        default:
            break;
        }

        // If we're here, we don't know yet what they want.  They may have just
        // given us a jar to execute, they might have given us a pig script to
        // execute, or they might have given us a dash (or nothing) which means to
        // run grunt interactive.
        String remainders[] = opts.getRemainingArgs();
        if (remainders == null) {
            if (checkScriptOnly) {
                System.err.println("ERROR:" + "-c (-check) option is only valid "
                        + "when executing pig with a pig script file)");
                return ReturnCode.ILLEGAL_ARGS;
            }
            // Interactive
            mode = ExecMode.SHELL;
            //Reader is created by first loading "pig.load.default.statements" or .pigbootup file if available
            ConsoleReader reader = new ConsoleReader(Utils.getCompositeStream(System.in, properties),
                    new OutputStreamWriter(System.out));
            reader.setDefaultPrompt("grunt> ");
            final String HISTORYFILE = ".pig_history";
            String historyFile = System.getProperty("user.home") + File.separator + HISTORYFILE;
            reader.setHistory(new History(new File(historyFile)));
            ConsoleReaderInputStream inputStream = new ConsoleReaderInputStream(reader);
            grunt = new Grunt(new BufferedReader(new InputStreamReader(inputStream)), pigContext);
            grunt.setConsoleReader(reader);
            gruntCalled = true;
            grunt.run();
            return ReturnCode.SUCCESS;
        } else {
            pigContext.getProperties().setProperty(PigContext.PIG_CMD_ARGS_REMAINDERS,
                    ObjectSerializer.serialize(remainders));

            // They have a pig script they want us to run.
            mode = ExecMode.FILE;

            FileLocalizer.FetchFileRet localFileRet = FileLocalizer.fetchFile(properties, remainders[0]);
            if (localFileRet.didFetch) {
                properties.setProperty("pig.jars.relative.to.dfs", "true");
            }

            scriptState.setFileName(remainders[0]);

            if (embedded) {
                return runEmbeddedScript(pigContext, localFileRet.file.getPath(), engine);
            } else {
                SupportedScriptLang type = determineScriptType(localFileRet.file.getPath());
                if (type != null) {
                    return runEmbeddedScript(pigContext, localFileRet.file.getPath(),
                            type.name().toLowerCase());
                }
            }
            //Reader is created by first loading "pig.load.default.statements" or .pigbootup file if available
            InputStream seqInputStream = Utils.getCompositeStream(new FileInputStream(localFileRet.file),
                    properties);
            in = new BufferedReader(new InputStreamReader(seqInputStream));

            // run parameter substitution preprocessor first
            substFile = remainders[0] + ".substituted";
            pin = runParamPreprocessor(pigContext, in, substFile, debug || dryrun || checkScriptOnly);
            if (dryrun) {
                if (dryrun(substFile, pigContext)) {
                    log.info("Dry run completed. Substituted pig script is at " + substFile
                            + ". Expanded pig script is at " + remainders[0] + ".expanded");
                } else {
                    log.info("Dry run completed. Substituted pig script is at " + substFile);
                }
                return ReturnCode.SUCCESS;
            }

            logFileName = validateLogFile(logFileName, localFileRet.file);
            pigContext.getProperties().setProperty("pig.logfile", (logFileName == null ? "" : logFileName));

            if (!debug) {
                new File(substFile).deleteOnExit();
            }

            // Set job name based on name of the script
            pigContext.getProperties().setProperty(PigContext.JOB_NAME,
                    "PigLatin:" + new File(remainders[0]).getName());

            scriptState.setScript(localFileRet.file);

            grunt = new Grunt(pin, pigContext);
            gruntCalled = true;

            if (checkScriptOnly) {
                grunt.checkScript(substFile);
                System.err.println(remainders[0] + " syntax OK");
                rc = ReturnCode.SUCCESS;
            } else {
                int results[] = grunt.exec();
                rc = getReturnCodeForStats(results);
            }
            return rc;
        }

        // Per Utkarsh and Chris invocation of jar file via pig depricated.
    } catch (ParseException e) {
        usage();
        rc = ReturnCode.PARSE_EXCEPTION;
        PigStatsUtil.setErrorMessage(e.getMessage());
        PigStatsUtil.setErrorThrowable(e);
    } catch (org.apache.pig.tools.parameters.ParseException e) {
        // usage();
        rc = ReturnCode.PARSE_EXCEPTION;
        PigStatsUtil.setErrorMessage(e.getMessage());
        PigStatsUtil.setErrorThrowable(e);
    } catch (IOException e) {
        if (e instanceof PigException) {
            PigException pe = (PigException) e;
            rc = (pe.retriable()) ? ReturnCode.RETRIABLE_EXCEPTION : ReturnCode.PIG_EXCEPTION;
            PigStatsUtil.setErrorMessage(pe.getMessage());
            PigStatsUtil.setErrorCode(pe.getErrorCode());
        } else {
            rc = ReturnCode.IO_EXCEPTION;
            PigStatsUtil.setErrorMessage(e.getMessage());
        }
        PigStatsUtil.setErrorThrowable(e);

        if (!gruntCalled) {
            LogUtils.writeLog(e, logFileName, log, verbose, "Error before Pig is launched");
        }
    } catch (Throwable e) {
        rc = ReturnCode.THROWABLE_EXCEPTION;
        PigStatsUtil.setErrorMessage(e.getMessage());
        PigStatsUtil.setErrorThrowable(e);

        if (!gruntCalled) {
            LogUtils.writeLog(e, logFileName, log, verbose, "Error before Pig is launched");
        }
    } finally {
        if (printScriptRunTime) {
            printScriptRunTime(startTime);
        }
        if (deleteTempFiles) {
            // clear temp files
            FileLocalizer.deleteTempFiles();
        }
        if (pigContext != null) {
            pigContext.getExecutionEngine().destroy();
        }
        PerformanceTimerFactory.getPerfTimerFactory().dumpTimers();
    }

    return rc;
}

From source file:com.katamaditya.apps.weather4u.weathersync.Weather4USyncAdapter.java

private String getTitle() {
    String title = WeatherUtil.getPreferredLocation(getContext());
    if (title != null) {
        title = title.trim();/*from   w ww.  j a  v  a2  s  .  co m*/
        if (title.contains(",")) {
            String temp[] = title.split(",");
            Character c = temp[0].charAt(0);
            c = Character.toUpperCase(c);
            temp[0] = temp[0].replaceFirst(temp[0].substring(0, 1), c.toString());
            temp[1] = temp[1].toUpperCase();
            title = temp[0] + ", " + temp[1];
        }
        return getContext().getString(R.string.app_name) + " - " + title;
    }
    return getContext().getString(R.string.app_name);
}

From source file:com.eryansky.common.orm.hibernate.HibernateDao.java

/**
 * ??Criterion,./*  w ww.ja va  2 s .co m*/
 */
protected Criterion buildCriterion(final String propertyName, final Object propertyValue,
        final MatchType matchType) {
    Assert.hasText(propertyName, "propertyName?");
    Criterion criterion = null;
    String value;
    Character ESCAPE = '!';
    // ?MatchTypecriterion
    switch (matchType) {
    case EQ:
        criterion = Restrictions.eq(propertyName, propertyValue);
        break;
    case NE:
        criterion = Restrictions.ne(propertyName, propertyValue);
        break;
    case LIKE:
        // 
        value = (String) propertyValue;
        if ((ESCAPE.toString()).equals(value)) {
            criterion = Restrictions.like(propertyName, (String) propertyValue, MatchMode.ANYWHERE);
        } else {
            criterion = new LikeExpression(propertyName, (String) propertyValue, MatchMode.ANYWHERE, ESCAPE,
                    true);
        }
        break;
    case SLIKE:
        // 
        value = (String) propertyValue;
        if ((ESCAPE.toString()).equals(value)) {
            criterion = Restrictions.like(propertyName, (String) propertyValue, MatchMode.START);
        } else {
            criterion = new LikeExpression(propertyName, (String) propertyValue, MatchMode.START, ESCAPE, true);
        }
        break;
    case ELIKE:
        // 
        value = (String) propertyValue;
        if ((ESCAPE.toString()).equals(value)) {
            criterion = Restrictions.like(propertyName, (String) propertyValue, MatchMode.END);
        } else {
            criterion = new LikeExpression(propertyName, (String) propertyValue, MatchMode.END, ESCAPE, true);
        }
        break;
    case LE:
        criterion = Restrictions.le(propertyName, propertyValue);
        break;
    case LT:
        criterion = Restrictions.lt(propertyName, propertyValue);
        break;
    case GE:
        criterion = Restrictions.ge(propertyName, propertyValue);
        break;
    case GT:
        criterion = Restrictions.gt(propertyName, propertyValue);
        break;
    case ISNULL:
        criterion = Restrictions.isNull(propertyName);
    }
    return criterion;
}