Example usage for java.text ParseException ParseException

List of usage examples for java.text ParseException ParseException

Introduction

In this page you can find the example usage for java.text ParseException ParseException.

Prototype

public ParseException(String s, int errorOffset) 

Source Link

Document

Constructs a ParseException with the specified detail message and offset.

Usage

From source file:org.kalypso.wspwin.core.CalculationBean.java

public static CalculationBean[] readBerFile(final File berFile) throws ParseException, IOException {
    // if a zustand has no calculations, no .ber file is present.
    if (!berFile.exists())
        return new CalculationBean[0];

    final List<CalculationBean> beans = new ArrayList<>(10);

    LineIterator lineIt = null;/*from   w  w  w  .  j a  v a2  s.c  om*/
    try {
        int count = 0;
        lineIt = FileUtils.lineIterator(berFile, "CP850"); //$NON-NLS-1$

        // ignore first line, we just read all lines
        lineIt.nextLine();
        count++;

        while (lineIt.hasNext()) {
            final String line = lineIt.nextLine();
            count++;

            if (line.length() < 60)
                throw new ParseException(Messages.getString("org.kalypso.wspwin.core.CalculationBean.0") + line, //$NON-NLS-1$
                        count);

            final String name = line.substring(0, 60).trim();

            final StringTokenizer tokenizer = new StringTokenizer(line.substring(60));

            if (tokenizer.countTokens() != 3)
                throw new ParseException(Messages.getString("org.kalypso.wspwin.core.CalculationBean.1") + line, //$NON-NLS-1$
                        count);

            final BigDecimal fromStation = new BigDecimal(tokenizer.nextToken());
            final BigDecimal toStation = new BigDecimal(tokenizer.nextToken());
            final String fileName = tokenizer.nextToken();

            beans.add(new CalculationBean(name, fileName, fromStation, toStation));
        }

        return beans.toArray(new CalculationBean[beans.size()]);
    } finally {
        LineIterator.closeQuietly(lineIt);
    }
}

From source file:com.cloud.utils.NumbersUtil.java

/**
 * Converts a string of the format 'yy-MM-dd'T'HH:mm:ss.SSS" into ms.
 *
 * @param str containing the interval.//from   w ww  . j a va2 s .c  om
 * @param defaultValue value to return if str doesn't parse.  If -1, throws VmopsRuntimeException
 * @return interval in ms
 */
public static long parseInterval(String str, long defaultValue) {
    try {
        if (str == null) {
            throw new ParseException("String is wrong", 0);
        }

        SimpleDateFormat sdf = null;
        if (str.contains("D")) {
            sdf = new SimpleDateFormat("dd'D'HH'h'mm'M'ss'S'SSS'ms'");
        } else if (str.contains("h")) {
            sdf = new SimpleDateFormat("HH'h'mm'M'ss'S'SSS'ms'");
        } else if (str.contains("M")) {
            sdf = new SimpleDateFormat("mm'M'ss'S'SSS'ms'");
        } else if (str.contains("S")) {
            sdf = new SimpleDateFormat("ss'S'SSS'ms'");
        } else if (str.contains("ms")) {
            sdf = new SimpleDateFormat("SSS'ms'");
        }
        if (sdf == null) {
            throw new ParseException("String is wrong", 0);
        }

        Date date = sdf.parse(str);
        return date.getTime();
    } catch (ParseException e) {
        if (defaultValue != -1) {
            return defaultValue;
        } else {
            throw new CloudRuntimeException("Unable to parse: " + str, e);
        }
    }
}

From source file:org.freedesktop.icons.IconTheme.java

public void initFromThemeProperties(INIFile iniFile, Properties themeProperties)
        throws IOException, ParseException {
    inherits = new ArrayList<String>();
    directories = new ArrayList<Directory>();

    if (themeProperties.containsKey(INHERITS)) {
        StringTokenizer t = new StringTokenizer(themeProperties.getProperty(INHERITS), ",");
        while (t.hasMoreTokens()) {
            inherits.add(t.nextToken());
        }/*from   w w  w .  j  a v  a 2  s. c om*/
    }

    if (!themeProperties.containsKey(DIRECTORIES)) {
        if (inherits.size() == 0) {
            throw new ParseException("Directories entry is required when no theme is inherited.", 0);
        }
    } else {
        StringTokenizer t = new StringTokenizer(themeProperties.getProperty(DIRECTORIES), ",");
        while (t.hasMoreTokens()) {
            String directoryName = t.nextToken();
            Properties directoryProperties = iniFile.get(directoryName);
            if (directoryProperties == null) {
                throw new ParseException(
                        "Entry '" + directoryName + "' in Directories does not have a corresponding section.",
                        0);
            }
            directories.add(new Directory(this, directoryName, directoryProperties));
        }
    }

    hidden = "true".equalsIgnoreCase(themeProperties.getProperty(HIDDEN));
}

From source file:Main.java

/**
 * <p>Parses a string representing a date by trying a variety of different parsers.</p>
 * //w  w  w  . j  a v a  2 s. co  m
 * <p>The parse will try each parse pattern in turn.
 * A parse is only deemed sucessful if it parses the whole of the input string.
 * If no parse patterns match, a ParseException is thrown.</p>
 * 
 * @param str  the date to parse, not null
 * @param parsePatterns  the date format patterns to use, see SimpleDateFormat, not null
 * @return the parsed date
 * @throws IllegalArgumentException if the date string or pattern array is null
 * @throws ParseException if none of the date patterns were suitable
 */
public static Date parseDate(String str, String[] parsePatterns) throws ParseException {
    if (str == null || parsePatterns == null) {
        throw new IllegalArgumentException("Date and Patterns must not be null");
    }

    SimpleDateFormat parser = null;
    ParsePosition pos = new ParsePosition(0);
    for (int i = 0; i < parsePatterns.length; i++) {
        if (i == 0) {
            parser = new SimpleDateFormat(parsePatterns[0]);
        } else {
            parser.applyPattern(parsePatterns[i]);
        }
        pos.setIndex(0);
        Date date = parser.parse(str, pos);
        if (date != null && pos.getIndex() == str.length()) {
            return date;
        }
    }
    throw new ParseException("Unable to parse the date: " + str, -1);
}

From source file:com.haulmont.chile.core.datatypes.impl.NumberDatatype.java

protected Number parse(String value, NumberFormat format) throws ParseException {
    ParsePosition pos = new ParsePosition(0);
    Number res = format.parse(value.trim(), pos);
    if (pos.getIndex() != value.length()) {
        throw new ParseException(String.format("Unparseable number: \"%s\"", value), pos.getErrorIndex());
    }/*from ww  w .  ja  va2s  .co m*/
    return res;
}

From source file:org.phoenicis.win32.registry.RegistryParser.java

public RegistryKey parseFile(File registryFile, String rootName) {
    try (BufferedReader bufferReader = new BufferedReader(new FileReader(registryFile))) {
        final RegistryKey root = new RegistryKey(rootName);
        RegistryKey lastNode = null;//ww  w  .  ja v  a2  s . c  o m
        int lineNumber = 1;
        for (String currentLine = bufferReader.readLine(); currentLine != null; currentLine = bufferReader
                .readLine()) {

            while (currentLine.trim().endsWith("\\")) {
                currentLine = StringUtils.substring(currentLine.trim(), 0, -1) + bufferReader.readLine().trim();
            }

            if (currentLine.startsWith(";") || currentLine.startsWith("#") || StringUtils.isBlank(currentLine)
                    || currentLine.startsWith("@")) {
                lineNumber++;
                continue;
            }

            if (currentLine.startsWith("[")) {
                lastNode = this.parseDirectoryLine(root, currentLine);
            } else if (lineNumber == 1) {
                lineNumber++;
                continue;
            } else if (lastNode == null) {
                throw new ParseException(String.format(PARSE_ERROR_MESSAGE, lineNumber), 0);
            } else {
                this.parseValueLine(lastNode, currentLine, lineNumber);
            }

            lineNumber++;
        }

        return root;
    } catch (IOException | ParseException e) {
        throw new IllegalArgumentException("Error while parsing the registry", e);
    }
}

From source file:org.osaf.cosmo.calendar.query.ParamFilter.java

/**
 * Construct a ParamFilter object from a DOM Element
 * @param element//from   w w  w. ja  va 2s . c  o m
 * @throws ParseException
 */
public ParamFilter(Element element) throws ParseException {
    // Get name which must be present
    name = DomUtil.getAttribute(element, ATTR_CALDAV_NAME, null);

    if (name == null) {
        throw new ParseException(
                "CALDAV:param-filter a property parameter name (e.g., \"PARTSTAT\") is required", -1);
    }

    // Can only have a single ext-match element
    ElementIterator i = DomUtil.getChildren(element);

    if (i.hasNext()) {

        Element child = i.nextElement();

        if (i.hasNext()) {
            throw new ParseException(
                    "CALDAV:param-filter only a single text-match or is-not-defined element is allowed", -1);
        }

        if (ELEMENT_CALDAV_TEXT_MATCH.equals(child.getLocalName())) {
            textMatchFilter = new TextMatchFilter(child);

        } else if (ELEMENT_CALDAV_IS_NOT_DEFINED.equals(child.getLocalName())) {

            isNotDefinedFilter = new IsNotDefinedFilter();
        } else
            throw new ParseException("CALDAV:param-filter an invalid element name found", -1);
    }
}

From source file:com.benfante.minimark.blo.ImporterBo.java

public List<Question> readQuestionSet(Reader r) throws ParseException, IOException {
    List<Question> result = new LinkedList<Question>();
    int lineCount = 0;
    String line = null;/*from   ww w  .  j  a  va 2 s  .c o m*/
    BufferedReader br = null;
    if (r instanceof BufferedReader) {
        br = (BufferedReader) r;
    } else {
        br = new BufferedReader(r);
    }
    endstream: while ((line = br.readLine()) != null) {
        lineCount++;
        // skip empty and comment lines
        if (StringUtils.isBlank(line) || line.charAt(0) == 'c') {
            continue;
        }
        char questionType = 0;
        String questionId = "";
        StringBuffer questionText = new StringBuffer();
        List<FixedAnswer> answers = new LinkedList<FixedAnswer>();
        String tmp;
        if (line.charAt(0) != 'd') {
            throw new ParseException("Start of question not found", lineCount);
        }
        StringTokenizer tk = new StringTokenizer(line, "|");
        tk.nextToken(); // skip the initial 'd'
        // question id
        tmp = tk.nextToken();
        if (tmp != null) {
            questionId = tmp;
        } else {
            throw new ParseException("Question id not found", lineCount);
        }
        // question type
        tmp = tk.nextToken();
        if ((tmp != null) && (tmp.length() > 0)) {
            questionType = Character.toUpperCase(tmp.charAt(0));
            if ((questionType != 'A') && (questionType != 'R') && (questionType != 'C')
                    && (questionType != 'T')) {
                throw new ParseException("Question type unknown", lineCount);
            }
        } else {
            throw new ParseException("Question type not found", lineCount);
        }
        // answer scrambling flag
        tmp = tk.nextToken();
        if ((tmp != null) && (tmp.length() > 0)) {
            //                char acf = tmp.charAt(0);
            //                switch (acf) {
            //                    case 'u':
            //                        answerScrambling = false;
            //                        break;
            //                    case 's':
            //                        answerScrambling = true;
            //                        break;
            //                    default:
            //                        throw new ParseException("Answer scrambling flag unknown", lineCount);
            //                }
        } else {
            throw new ParseException("Answer scrambling flag not found", lineCount);
        }
        // question text
        tmp = tk.nextToken();
        if ((tmp != null) && (tmp.length() > 0)) {
            questionText.append(tmp);
            if ((questionType == 'A') || (questionType == 'T')) { // multiline questions
                while ((line = br.readLine()) != null) {
                    lineCount++;
                    if (StringUtils.isBlank(line)) {
                        break;
                    }
                    questionText.append('\n').append(line);
                }
            }
        } else {
            throw new ParseException("Question text not found", lineCount);
        }
        // suggested answers
        if ((questionType == 'R') || (questionType == 'C')) {
            while ((line = br.readLine()) != null) {
                lineCount++;
                if (StringUtils.isBlank(line)) {
                    break;
                }
                tk = new StringTokenizer(line, "|");
                // correct char
                tmp = tk.nextToken();
                boolean correct = false;
                if ((tmp != null) && (tmp.length() > 0)) {
                    char correctChar = line.charAt(0);
                    if (correctChar == 'y') {
                        correct = true;
                    } else if (correctChar == 'n') {
                        correct = false;
                    } else {
                        throw new ParseException("Correct char unknown", lineCount);
                    }
                } else {
                    throw new ParseException("Correct char not found (" + tmp + ")", lineCount);
                }
                tmp = tk.nextToken();
                String answerText = null;
                if ((tmp != null) && (tmp.length() > 0)) {
                    answerText = tmp;
                } else {
                    throw new ParseException("Answer text not found", lineCount);
                }
                FixedAnswer fixedAnswer = new FixedAnswer();
                fixedAnswer.setContent(answerText);
                fixedAnswer.setContentFilter(TextFilterUtils.HTML_FILTER_CODE);
                fixedAnswer.setCorrect(correct);
                fixedAnswer.setWeight(BigDecimal.ONE);
                answers.add(fixedAnswer);
            }
        }
        Question newQuestion = makeQuestion(questionType, questionId, questionText.toString(), answers);
        result.add(newQuestion);
    }
    return result;
}

From source file:org.kalypso.wspwin.core.ProfileBean.java

public static ProfileBean[] readProfiles(final LineNumberReader reader, final int profilCount)
        throws IOException, ParseException {
    final List<ProfileBean> beans = new ArrayList<>(20);
    for (int i = 0; i < profilCount; i++) {
        if (!reader.ready())
            throw new ParseException(
                    Messages.getString("org.kalypso.wspwin.core.ProfileBean.0") + reader.getLineNumber(), //$NON-NLS-1$
                    reader.getLineNumber());

        final String line = reader.readLine();
        if (line == null || line.trim().length() == 0)
            throw new ParseException(
                    Messages.getString("org.kalypso.wspwin.core.ProfileBean.1") + reader.getLineNumber(), //$NON-NLS-1$
                    reader.getLineNumber());

        final StringTokenizer tokenizer = new StringTokenizer(line);
        if (tokenizer.countTokens() != 6)
            throw new ParseException(
                    Messages.getString("org.kalypso.wspwin.core.ProfileBean.2") + reader.getLineNumber(), //$NON-NLS-1$
                    reader.getLineNumber());

        try {// w  w  w  . ja  v a2 s.  c  o m
            final String waterName = tokenizer.nextToken();
            final BigDecimal station = new BigDecimal(tokenizer.nextToken());
            final String mfb = tokenizer.nextToken(); // Mehrfeldbrckenkennung
            final int vzk = parseVZK(tokenizer.nextToken()); // Verzweigungskennung
            final String zustandName = tokenizer.nextToken();
            final String fileName = tokenizer.nextToken();

            final ProfileBean bean = new ProfileBean(waterName, zustandName, station, fileName, mfb, vzk);
            beans.add(bean);
        } catch (final NumberFormatException e) {
            throw new ParseException(
                    Messages.getString("org.kalypso.wspwin.core.ProfileBean.6") + reader.getLineNumber(), //$NON-NLS-1$
                    reader.getLineNumber());
        }
    }

    return beans.toArray(new ProfileBean[beans.size()]);
}

From source file:org.home.petclinic2.formatter.PetTypeFormatter.java

@Override
public PetType parse(String text, Locale locale) throws ParseException {
    Collection<PetType> findPetTypes = clinicService.findPetTypes();
    for (PetType type : findPetTypes) {
        if (type.getName().equals(text)) {
            return type;
        }//from w w w . j a  v  a 2s.  c om
    }
    throw new ParseException("type not found: " + text, 0);
}