Java String Sanitize sanitizeLine(String line)

Here you can find the source of sanitizeLine(String line)

Description

Sanitize a single line of the "meta data".

License

Open Source License

Parameter

Parameter Description
line one single line of the metadata

Return

the sanitized string

Declaration

private static String sanitizeLine(String line) 

Method Source Code

//package com.java2s;
/**/*from   ww w.ja  v a2 s.  c om*/
 *    Hiker Application. For educational purposes only.
 * 
 *  Copyright (C) 2016  Julia Katheder
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String CSV_SEPARATOR = ";";

    /**
     * <p>
     * Sanitize a single line of the "meta data".
     * </p>
     * <p>
     * For Id (1 Element in line): This method replaces any String which can't
     * be casted to an {@link Integer} to {@link Code -1}.
     * </p>
     * <p>
     * For Name (2 Element in line): No check implemented.
     * </p>
     * <p>
     * For Weight (3 Element in line): This method replaces any String which
     * can't be casted to an {@link Integer} to {@link Code -1}.
     * </p>
     * 
     * @param line
     *            one single line of the metadata
     * @return the sanitized string
     */
    private static String sanitizeLine(String line) {
        final StringBuilder result = new StringBuilder();
        final String[] lineRecord = line.split(CSV_SEPARATOR);

        try {
            Integer.valueOf(lineRecord[0]);
            result.append(lineRecord[0]);
        } catch (NumberFormatException e) {
            result.append(-1);
        }
        result.append(CSV_SEPARATOR);
        result.append(lineRecord[1]);
        result.append(CSV_SEPARATOR);
        try {
            Integer.valueOf(lineRecord[2]);
            result.append(lineRecord[2]);
        } catch (NumberFormatException e) {
            result.append(-1);
        }
        return result.toString();
    }
}

Related

  1. sanitizeIdentifierName(String input)
  2. sanitizeIDs(String str)
  3. sanitizeInput(String input)
  4. sanitizeInput(String string)
  5. sanitizeJavascript(String originalString)
  6. sanitizeLineSpaces(String line)
  7. sanitizeLoggerNamePart(String name)
  8. sanitizeMethodName(String methodName)
  9. sanitizeMimeType(final String mimeType)