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.apache.falcon.util.DateUtil.java

/**
 * Parses a datetime in ISO8601 format in the  process timezone.
 *
 * @param s string with the datetime to parse.
 * @return the corresponding {@link java.util.Date} instance for the parsed date.
 * @throws java.text.ParseException thrown if the given string was
 * not an ISO8601 value for the  process timezone.
 *//*from   w  w  w  .  j  a va2s  .c o m*/
public static Date parseDateFalconTZ(String s) throws ParseException {
    s = s.trim();
    ParsePosition pos = new ParsePosition(0);
    Date d = getISO8601DateFormat(activeTimeZone, activeTimeMask).parse(s, pos);
    if (d == null) {
        throw new ParseException("Could not parse [" + s + "] using [" + activeTimeMask + "] mask",
                pos.getErrorIndex());
    }
    if (s.length() > pos.getIndex()) {
        throw new ParseException("Correct datetime string is followed by invalid characters: " + s,
                pos.getIndex());
    }
    return d;
}

From source file:de.codesourcery.utils.xml.XmlHelper.java

public static Element getElement(Element parent, String tagName, boolean isRequired) throws ParseException {

    NodeList list = parent.getElementsByTagName(tagName);

    if (list.getLength() == 0) {
        if (isRequired) {
            log.error("getElement(): XML is lacking required tag <" + tagName + ">");
            throw new ParseException("XML is lacking required tag <" + tagName + ">", -1);
        }/*from   ww  w .  j a  v  a 2  s. com*/
        return null;
    }

    if (list.getLength() > 1) {
        log.error("getElement(): XML is contains multiple <" + tagName + "> tags, expected only one");
        throw new ParseException("XML is contains multiple <" + tagName + "> tags, expected only one", -1);
    }

    Node n = list.item(0);
    if (!(n instanceof Element)) {
        log.error("getElement(): Internal error, expected Element but got Node");
        throw new ParseException("Internal error, expected Element but got Node", -1);
    }
    return (Element) n;
}

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

/** Reads the first line of a profproj.txt or .str file. Returns 2 ints: profile count + second count. */
public static int[] readStrHeader(final LineNumberReader reader) throws IOException, ParseException {
    final String firstLine = reader.readLine();
    if (firstLine == null || firstLine.length() == 0)
        throw new ParseException(Messages.getString("org.kalypso.wspwin.core.WspCfg.8"), //$NON-NLS-1$
                reader.getLineNumber());

    // ignore the values, we read the count from the linecount
    // just parse the type
    final StringTokenizer firstLineTokenizer = new StringTokenizer(firstLine);
    if (firstLineTokenizer.countTokens() < 2)
        throw new ParseException(Messages.getString("org.kalypso.wspwin.core.WspCfg.9"), //$NON-NLS-1$
                reader.getLineNumber());

    final int[] counts = new int[2];
    counts[0] = Integer.parseInt(firstLineTokenizer.nextToken());
    counts[1] = Integer.parseInt(firstLineTokenizer.nextToken());

    // if it is a .str file, we ignore the following name and waterName

    return counts;
}

From source file:edu.cornell.kfs.fp.businessobject.USBankRecordFieldUtils.java

/**
 * Extracts a KualiDecimal from a substring less any ending whitespace for a given beginning and ending position.
 * //from   w  ww.  j av  a2 s  .c  om
 * @param line Superstring
 * @param begin Beginning index
 * @param end Ending index
 * @param lineCount The current line number
 * @return The KualiDecimal parsed from the trimmed substring
 * @throws ParseException When unable to parse the KualiDecimal
 */
public static KualiDecimal extractDecimal(String line, int begin, int end, int lineCount)
        throws ParseException {
    KualiDecimal theDecimal;
    try {
        String sanitized = line.substring(begin, end);
        sanitized = StringUtils.remove(sanitized, '-');
        sanitized = StringUtils.remove(sanitized, '+');
        theDecimal = new KualiDecimal(sanitized);
    } catch (StringIndexOutOfBoundsException | IllegalArgumentException e) {
        // May encounter a StringIndexOutOfBoundsException if the string bounds do not match or 
        // an IllegalArgumentException if the Decimal does not parse correctly
        throw new ParseException(
                "Unable to parse " + line.substring(begin, end) + " into a decimal value on line " + lineCount,
                lineCount);
    }
    return theDecimal;
}

From source file:com.teamsun.framework.util.DateUtil.java

/**
 * This method converts a String to a date using the datePattern
 * /*from w  ww.  ja  v  a  2 s.c om*/
 * @param strDate the date to convert (in format yyyy-MM-dd)
 * @return a date object
 * 
 * @throws ParseException
 */
public static Date convertStringToDate(String strDate) throws ParseException {
    Date aDate = null;

    try {
        if (log.isDebugEnabled()) {
            log.debug("converting date with pattern: " + datePattern);
        }

        aDate = convertStringToDate(datePattern, strDate);
    } catch (ParseException pe) {
        log.error("Could not convert '" + strDate + "' to a date, throwing exception");
        pe.printStackTrace();
        throw new ParseException(pe.getMessage(), pe.getErrorOffset());

    }

    return aDate;
}

From source file:com.couchbase.client.protocol.views.HttpOperationImpl.java

protected OperationStatus parseViewForStatus(String json, int errorcode) throws ParseException {
    if (json != null) {
        try {//w  w  w  .  j  ava  2  s .  com
            JSONObject base = new JSONObject(json);
            if (base.has("error")) {
                String error = "Error Code: " + errorcode + " Error: " + base.getString("error");
                if (base.has("reason")) {
                    error += " Reason: " + base.getString("reason");
                }
                return new OperationStatus(false, error);
            } else {
                return new OperationStatus(true, "Error Code: " + errorcode);
            }
        } catch (JSONException e) {
            throw new ParseException("Cannot read json: " + json, 0);
        }
    }
    return new OperationStatus(false, "Error Code: " + errorcode + "No entity");
}

From source file:org.hypertable.hadoop.mapred.TextTableInputFormat.java

public void parseOptions(JobConf job) throws ParseException {
    String str = job.get(OPTIONS);
    if (str != null) {
        String[] strs = str.split("\\s");
        for (int i = 0; i < strs.length; i++) {
            strs[i] = strs[i].toUpperCase();
            if (strs[i].equals("MAX_VERSIONS") || strs[i].equals("REVS")) {
                i++;/*ww  w  . j a  va  2  s.c om*/
                if (i == strs.length)
                    throw new ParseException("Bad OPTIONS spec", i);
                int value = Integer.parseInt(strs[i]);
                m_base_spec.setVersions(value);
            } else if (strs[i].equals("OFFSET")) {
                i++;
                if (i == strs.length)
                    throw new ParseException("Bad OPTIONS spec", i);
                int value = Integer.parseInt(strs[i]);
                m_base_spec.setRow_offset(value);
            } else if (strs[i].equals("CELL_OFFSET")) {
                i++;
                if (i == strs.length)
                    throw new ParseException("Bad OPTIONS spec", i);
                int value = Integer.parseInt(strs[i]);
                m_base_spec.setCell_offset(value);
            } else if (strs[i].equals("LIMIT")) {
                i++;
                if (i == strs.length)
                    throw new ParseException("Bad OPTIONS spec", i);
                int value = Integer.parseInt(strs[i]);
                m_base_spec.setRow_limit(value);
            } else if (strs[i].equals("CELL_LIMIT")) {
                i++;
                if (i == strs.length)
                    throw new ParseException("Bad OPTIONS spec", i);
                int value = Integer.parseInt(strs[i]);
                m_base_spec.setCell_limit(value);
            } else if (strs[i].equals("CELL_LIMIT_PER_FAMILY")) {
                i++;
                if (i == strs.length)
                    throw new ParseException("Bad OPTIONS spec", i);
                int value = Integer.parseInt(strs[i]);
                m_base_spec.setCell_limit_per_family(value);
            } else if (strs[i].equals("KEYS_ONLY")) {
                m_base_spec.setKeys_only(true);
            } else if (strs[i].equals("RETURN_DELETES")) {
                m_base_spec.setReturn_deletes(true);
            } else
                throw new ParseException("Bad OPTIONS spec: " + strs[i], i);
        }
    }
}

From source file:com.microsoft.tfs.core.clients.versioncontrol.internal.fileattributes.FileAttributesEntry.java

/**
 * Parses the given serialized entry (a string like
 * "Class.java:x|b=false|i=34") into a file name and set of attributes.
 * <p>/*w w w .  ja  va2  s  . co  m*/
 * Parsing is liberal. Empty attributes are ignored and not added to the
 * returned entry object's attribute set. If an equals sign is found in the
 * attribute, the attribute is parsed as a StringPairAttribute, otherwise it
 * is parsed as a BooleanFileAttribute.
 * <p>
 * Leading and trailing whitespace is stripped from boolean attributes. An
 * attribute like " x " is parsed equal to attribute "x" or " x".
 * <p>
 * Leading and trailing whitespace is stripped from the keys and values of
 * string pair attributes. " name=value ", "name=value", " name = value",
 * and "name = value" are all equal.
 * <p>
 * Multiple attributes of some types may appear in the returned entry. For
 * instance, if the boolean attribute "b" is read twice, it exists only once
 * in the set. But if both string pair file attributes "zap=abc" and
 * "zap=xyz" are parsed, they will both exist in the returned set.
 *
 * @param serializedEntry
 *        the serialized entry string (like "Name.java:x|b=false|i=34").
 * @returns the parsed file attributes entry.
 * @throws ParseException
 *         if the serialized entry is empty or another error occurred
 *         parsing it.
 * @throws PatternSyntaxException
 *         if the filename expression could not be parsed.
 */
public static FileAttributesEntry parse(final String serializedEntry)
        throws ParseException, PatternSyntaxException {
    Check.notNull(serializedEntry, "serializedEntry"); //$NON-NLS-1$

    if (serializedEntry.length() == 0) {
        throw new ParseException(Messages.getString("FileAttributesEntry.FileAttributesEntryIsEmpty"), 0); //$NON-NLS-1$
    }

    /*
     * Find the first separator.
     */
    final int sepIndex = serializedEntry.indexOf(FILE_AND_ATTRIBUTES_SEPARATOR);
    if (sepIndex == -1) {
        throw new ParseException(MessageFormat.format(
                Messages.getString("FileAttributesEntry.FileAndAttributesSeparatorNotFoundFormat"), //$NON-NLS-1$
                FILE_AND_ATTRIBUTES_SEPARATOR), 0);
    }

    final String filenameExpression = serializedEntry.substring(0, sepIndex);

    final String rest = serializedEntry.substring(sepIndex + 1);

    /*
     * Split on all separators.
     */
    final String[] attributeStrings = rest.split("\\" + ATTRIBUTE_SEPARATOR); //$NON-NLS-1$

    final List<FileAttribute> attributes = new ArrayList<FileAttribute>();
    for (int i = 0; i < attributeStrings.length; i++) {
        final String attributeString = attributeStrings[i];

        // Ignore empty attributes.
        if (attributeString.length() == 0) {
            continue;
        }

        FileAttribute a = null;

        if (StringPairFileAttribute.looksLikeStringPairFileAttribute(attributeString)) {
            a = StringPairFileAttribute.parse(attributeString);
        } else {
            a = BooleanFileAttribute.parse(attributeString);
        }

        if (a != null) {
            attributes.add(a);
        } else {
            log.warn(MessageFormat.format("Ignoring malformed file attribute ''{0}''", attributeString)); //$NON-NLS-1$
        }
    }

    return new FileAttributesEntry(filenameExpression,
            attributes.toArray(new FileAttribute[attributes.size()]));
}

From source file:org.bedework.util.vcard.BuildState.java

public void setPropertyValue(final String val) throws ParseException, URISyntaxException {
    final String value = Strings.unescape(val);

    try {//from  w  ww. j a v  a2s  .  c o  m
        if (params == null) {
            params = new ArrayList<>();
        }

        if (propertyFactory instanceof ExtendedFactory) {
            final ExtendedFactory xfactory = (ExtendedFactory) propertyFactory;

            if (group == null) {
                property = xfactory.createProperty(propertyName, params, value);
            } else {
                property = xfactory.createProperty(group, propertyName, params, value);
            }

            return;
        }

        if (group == null) {
            property = propertyFactory.createProperty(params, value);
        } else {
            property = propertyFactory.createProperty(group, params, value);
        }
    } catch (final DecoderException de) {
        throw new ParseException(de.getMessage(), 0);
    }
}

From source file:org.mongeez.reader.FormattedJavascriptChangeSetReader.java

private void addScriptToChangeSet(ChangeSet changeSet, StringBuilder scriptBody) throws ParseException {
    if (changeSet != null) {
        String body = scriptBody.toString();
        if (body.trim().isEmpty()) {
            throw new ParseException("No JavaScript found for changeset " + toString(changeSet), -1);
        }/*w  w w.  j ava 2 s  .c  o  m*/
        Script script = new Script();
        script.setBody(body);
        changeSet.add(script);
    }
}