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.kie.workbench.common.stunner.bpmn.backend.forms.conditions.parser.ParsingUtils.java

public static String parseJavaName(final String token, final int startIndex, final char[] stopCharacters)
        throws ParseException {
    if (startIndex < 0 || startIndex >= token.length()) {
        throw new IndexOutOfBoundsException("startIndex: " + startIndex + " exceeds token bounds: " + token);
    }/*  w w w.j  a v a  2 s  . c  o m*/
    final StringBuilder javaName = new StringBuilder();
    char currentChar;
    int currentIndex = startIndex;
    while (currentIndex < token.length()) {
        currentChar = token.charAt(currentIndex);
        if (ArrayUtils.contains(stopCharacters, currentChar)) {
            break;
        } else {
            javaName.append(currentChar);
        }
        currentIndex++;
    }

    if (javaName.length() == 0) {
        throw new ParseException("Expected java name was not found at position: " + startIndex, startIndex);
    } else if (!SourceVersion.isName(javaName)) {
        throw new ParseException("Invalid java name was found at position: " + startIndex, startIndex);
    }
    return javaName.toString();
}

From source file:Main.java

private static Date getDate(String date) throws ParseException {
    if (date == null) {
        return null;
    }/*from w w w.  j  a  va  2  s .  c o m*/

    int yyyy = Integer.parseInt(date.substring(0, 4));
    int mm = Integer.parseInt(date.substring(4, 6));
    int dd = Integer.parseInt(date.substring(6, 8));
    int hh = Integer.parseInt(date.substring(8, 10));
    int mi = Integer.parseInt(date.substring(10, 12));
    int ss = Integer.parseInt(date.substring(12, 14));

    if (yyyy <= 1900 || yyyy >= 2999) {
        throw new ParseException("Invalid year.", yyyy);
    }
    if (mm < 1 || mm > 12) {
        throw new ParseException("Invalid month.", mm);
    }
    if (dd < 1 || dd > 31) {
        throw new ParseException("Invalid Day.", dd);
    }
    if (hh < 0 || hh > 23) {
        throw new ParseException("Invalid hour.", hh);
    }
    if (mi < 0 || mi > 59) {
        throw new ParseException("Invalid minute.", mi);
    }
    if (ss < 0 || ss > 59) {
        throw new ParseException("Invalid second.", ss);
    }

    return new SimpleDateFormat(DEFAULT_DATE_PATTERN).parse(date);
}

From source file:com.whizzosoftware.hobson.rest.v1.util.URLVariableParser.java

/**
 * Parse a String value into a URLInfo object.
 *
 * @param value the String value to parse
 *
 * @return a URIInfo object/* w w  w.ja  v  a 2s.  co  m*/
 * @throws ParseException on failure
 * @throws URISyntaxException on failure
 */
public static URIInfo parse(String value) throws ParseException, URISyntaxException {
    String ts = value.trim();

    // assume that value starting with '{' is a JSON object
    if (ts.charAt(0) == '{') {
        JSONObject json = new JSONObject(new JSONTokener(value));

        // "url" is a required pair
        if (!json.has(PROP_URL)) {
            throw new ParseException("'url' is a required key in JSON object", 0);
        }

        URIInfo info = new URIInfo(json.getString(PROP_URL));

        // add headers if any are defined
        if (json.has(PROP_HEADERS)) {
            JSONObject headers = json.getJSONObject(PROP_HEADERS);
            for (Object o : headers.keySet()) {
                String key = o.toString();
                info.addHeader(key, headers.getString(key));
            }
        }

        // add auth information if any is defined
        if (json.has(PROP_AUTH)) {
            JSONObject auth = json.getJSONObject(PROP_AUTH);
            if (!auth.has(PROP_USERNAME) || !auth.has(PROP_PASSWORD) || !auth.has(PROP_TYPE)) {
                throw new ParseException("'username', 'password' and 'type' are required in 'auth' JSON object",
                        0);
            }
            info.setAuthInfo(new URLAuthInfo(auth.getString(PROP_USERNAME), auth.getString(PROP_PASSWORD),
                    auth.getString(PROP_TYPE)));
        }

        return info;

        // otherwise, simply treat the value as a full URL
    } else {
        return new URIInfo(ts);
    }
}

From source file:eu.crisis_economics.configuration.StringDefinitionExpression.java

static StringDefinitionExpression tryCreate(String expression, FromFileConfigurationContext context)
        throws ParseException {
    List<String> expressions = StringDefinitionExpression.isExpressionOfType(expression, context);
    if (expression == null)
        throw new ParseException(expression, 0);
    return new StringDefinitionExpression(expressions.get(0), context);
}

From source file:eu.crisis_economics.configuration.NameDeclarationExpression.java

static NameDeclarationExpression tryCreate(String expression, FromFileConfigurationContext context)
        throws ParseException {
    List<String> expressions = NameDeclarationExpression.isExpressionOfType(expression, context);
    if (expression == null)
        throw new ParseException(expression, 0);
    return new NameDeclarationExpression(expressions.get(0), context);
}

From source file:gov.nih.nci.cabig.caaers.domain.DateValue.java

/**
 * String to date value.//from www. jav  a2 s  . co m
 *
 * @param date the date
 * @return the date value
 * @throws ParseException the parse exception
 */
public static DateValue stringToDateValue(String date) throws ParseException {
    if (StringUtils.isBlank(date)) {
        return null;
    }
    String[] dateParts = date.split("/");
    int size = dateParts.length;
    if (size != 3)
        throw new ParseException("Unknown format, expected format is 'mm/dd/yyyy'", 0);
    DateValue dateValue = new DateValue();
    try {
        dateValue.setMonth(Integer.parseInt(dateParts[0]));
        dateValue.setDay(Integer.parseInt(dateParts[1]));
        dateValue.setYear(Integer.parseInt(dateParts[2]));
    } catch (NumberFormatException e) {
        throw new ParseException(
                "Unknown format, unable to parse the date values, expected format is 'mm/dd/yyyy'", 0);
    }
    return dateValue;
}

From source file:io.fluo.commoncrawl.data.util.ArchiveUtil.java

public static Page buildPage(ArchiveRecord archiveRecord) throws IOException, ParseException {
    if (archiveRecord.getHeader().getMimetype().equalsIgnoreCase("application/json")) {
        byte[] rawData = IOUtils.toByteArray(archiveRecord, archiveRecord.available());
        if (rawData.length == 0) {
            return Page.EMPTY;
        }//from   w ww  .j  a  v a2  s  .c  o  m
        String jsonString = new String(rawData);
        if (jsonString.isEmpty()) {
            return Page.EMPTY;
        }
        JSONObject json;
        try {
            json = new JSONObject(new String(rawData));
        } catch (JSONException e) {
            throw new ParseException(e.getMessage(), 0);
        }
        String pageUrl = archiveRecord.getHeader().getUrl();
        if (!LinkUtil.isValid(pageUrl)) {
            return Page.EMPTY;
        }
        String pageDomain = LinkUtil.getTopPrivate(pageUrl);
        Page page = new Page(archiveRecord.getHeader().getUrl());
        page.setCrawlDate(archiveRecord.getHeader().getDate());
        try {
            JSONObject responseMeta = json.getJSONObject("Envelope").getJSONObject("Payload-Metadata")
                    .getJSONObject("HTTP-Response-Metadata");

            if (archiveRecord.getHeader().getMimetype().equals("application/json")) {
                try {
                    JSONArray links = responseMeta.getJSONObject("HTML-Metadata").getJSONArray("Links");
                    for (int i = 0; i < links.length(); i++) {
                        JSONObject link = links.getJSONObject(i);
                        if (link.has("path") && link.get("path").equals("A@/href") && link.has("url")) {
                            String anchorText = "";
                            if (link.has("text")) {
                                anchorText = link.getString("text");
                            } else if (link.has("title")) {
                                anchorText = link.getString("title");
                            }
                            String linkUrl = link.getString("url");
                            if (LinkUtil.isValid(linkUrl)) {
                                String linkDomain = LinkUtil.getTopPrivate(linkUrl);
                                if (!pageDomain.equalsIgnoreCase(linkDomain)) {
                                    page.addOutboundLink(linkUrl, anchorText);
                                }
                            }
                        }
                    }
                } catch (JSONException e) {
                    log.debug("Exception trying retrieve links", e);
                }
            }
            try {
                page.setTitle(
                        responseMeta.getJSONObject("HTML-Metadata").getJSONObject("Head").getString("Title"));
            } catch (JSONException e) {
                log.debug("Failed to retrieve title", e);
            }
            try {
                page.setServer(responseMeta.getJSONObject("Headers").getString("Server"));
            } catch (JSONException e) {
                log.debug("Failed to retrieve server", e);
            }
        } catch (JSONException e) {
            log.debug("Exception trying retrieve responseMeta", e);
        }
        return page;
    }
    return Page.EMPTY;
}

From source file:Main.java

/**
 * For some reason, can't find this utility method in the java framework.
 * //from  www.  java2 s. c o m
 * @param sDateTime
 *            an xsd:dateTime string
 * @return an equivalent java.util.Date
 * @throws ParseException
 */
public static Date parseXsdDateTime(String sDateTime) throws ParseException {
    final DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

    int iDotPosition = NORMAL_IDOT_POSITION;
    if (sDateTime.charAt(0) == '-') {
        iDotPosition = IDOT_POSITION_IFNEG;
    }
    Date result;
    if (sDateTime.length() <= iDotPosition) {
        return format.parse(sDateTime + "Z");
    }

    String millis = null;
    char c = sDateTime.charAt(iDotPosition);
    if (c == '.') {
        // if datetime has milliseconds, separate them
        int eoms = iDotPosition + 1;
        while (Character.isDigit(sDateTime.charAt(eoms))) {
            eoms += 1;
        }
        millis = sDateTime.substring(iDotPosition, eoms);
        sDateTime = sDateTime.substring(0, iDotPosition) + sDateTime.substring(eoms);
        c = sDateTime.charAt(iDotPosition);
    }
    if (c == '+' || c == '-') {
        format.setTimeZone(TimeZone.getTimeZone("GMT" + sDateTime.substring(iDotPosition)));
        sDateTime = sDateTime.substring(0, iDotPosition) + "Z";
    } else if (c != 'Z') {
        throw new ParseException("Illegal timezone specification.", iDotPosition);
    }

    result = format.parse(sDateTime);
    if (millis != null) {
        result.setTime(result.getTime() + Math.round(Float.parseFloat(millis) * ONE_SEC_IN_MILLISECS));
    }

    result = offsetDateFromGMT(result);
    return result;
}

From source file:webindex.data.util.ArchiveUtil.java

public static Page buildPage(ArchiveRecord archiveRecord) throws IOException, ParseException {
    if (archiveRecord.getHeader().getMimetype().equalsIgnoreCase("application/json")) {
        byte[] rawData = IOUtils.toByteArray(archiveRecord, archiveRecord.available());
        if (rawData.length == 0) {
            return Page.EMPTY;
        }/*from  w w  w  .j  a  v a 2  s .  c o  m*/
        String jsonString = new String(rawData);
        if (jsonString.isEmpty()) {
            return Page.EMPTY;
        }
        JSONObject json;
        try {
            json = new JSONObject(new String(rawData));
        } catch (JSONException e) {
            throw new ParseException(e.getMessage(), 0);
        }
        String rawPageUrl = archiveRecord.getHeader().getUrl();
        URL pageUrl;
        try {
            pageUrl = URL.from(rawPageUrl);
        } catch (IllegalArgumentException e) {
            return Page.EMPTY;
        } catch (Exception e) {
            log.error("Unexpected exception while parsing raw page URL: " + rawPageUrl, e);
            return Page.EMPTY;
        }
        Page page = new Page(pageUrl.toUri());
        page.setCrawlDate(archiveRecord.getHeader().getDate());
        try {
            JSONObject responseMeta = json.getJSONObject("Envelope").getJSONObject("Payload-Metadata")
                    .getJSONObject("HTTP-Response-Metadata");

            if (archiveRecord.getHeader().getMimetype().equals("application/json")) {
                try {
                    JSONArray links = responseMeta.getJSONObject("HTML-Metadata").getJSONArray("Links");
                    for (int i = 0; i < links.length(); i++) {
                        JSONObject link = links.getJSONObject(i);
                        if (link.has("path") && link.get("path").equals("A@/href") && link.has("url")) {
                            String anchorText = "";
                            if (link.has("text")) {
                                anchorText = link.getString("text");
                            } else if (link.has("title")) {
                                anchorText = link.getString("title");
                            }
                            String rawLinkUrl = link.getString("url");
                            URL linkUrl;
                            try {
                                linkUrl = URL.from(rawLinkUrl);
                                if (!page.getDomain().equals(linkUrl.getDomain())) {
                                    page.addOutbound(Link.of(linkUrl, anchorText));
                                }
                            } catch (IllegalArgumentException e) {
                                log.debug("Failed to parse link: " + rawLinkUrl);
                            } catch (Exception e) {
                                log.error("Unexpected exception while parsing link URL: " + rawLinkUrl, e);
                            }
                        }
                    }
                } catch (JSONException e) {
                    log.debug("Exception trying retrieve links", e);
                }
            }
            try {
                page.setTitle(
                        responseMeta.getJSONObject("HTML-Metadata").getJSONObject("Head").getString("Title"));
            } catch (JSONException e) {
                log.debug("Failed to retrieve title", e);
            }
            try {
                page.setServer(responseMeta.getJSONObject("Headers").getString("Server"));
            } catch (JSONException e) {
                log.debug("Failed to retrieve server", e);
            }
        } catch (JSONException e) {
            log.debug("Exception trying retrieve responseMeta", e);
        }
        return page;
    }
    return Page.EMPTY;
}

From source file:io.fluo.webindex.data.util.ArchiveUtil.java

public static Page buildPage(ArchiveRecord archiveRecord) throws IOException, ParseException {
    if (archiveRecord.getHeader().getMimetype().equalsIgnoreCase("application/json")) {
        byte[] rawData = IOUtils.toByteArray(archiveRecord, archiveRecord.available());
        if (rawData.length == 0) {
            return Page.EMPTY;
        }/*from ww  w .j ava 2s .c  o m*/
        String jsonString = new String(rawData);
        if (jsonString.isEmpty()) {
            return Page.EMPTY;
        }
        JSONObject json;
        try {
            json = new JSONObject(new String(rawData));
        } catch (JSONException e) {
            throw new ParseException(e.getMessage(), 0);
        }
        String rawPageUrl = archiveRecord.getHeader().getUrl();
        URL pageUrl;
        try {
            pageUrl = DataUrl.from(rawPageUrl);
        } catch (IllegalArgumentException e) {
            return Page.EMPTY;
        } catch (Exception e) {
            log.error("Unexpected exception while parsing raw page URL: " + rawPageUrl, e);
            return Page.EMPTY;
        }
        Page page = new Page(pageUrl.toPageID());
        page.setCrawlDate(archiveRecord.getHeader().getDate());
        try {
            JSONObject responseMeta = json.getJSONObject("Envelope").getJSONObject("Payload-Metadata")
                    .getJSONObject("HTTP-Response-Metadata");

            if (archiveRecord.getHeader().getMimetype().equals("application/json")) {
                try {
                    JSONArray links = responseMeta.getJSONObject("HTML-Metadata").getJSONArray("Links");
                    for (int i = 0; i < links.length(); i++) {
                        JSONObject link = links.getJSONObject(i);
                        if (link.has("path") && link.get("path").equals("A@/href") && link.has("url")) {
                            String anchorText = "";
                            if (link.has("text")) {
                                anchorText = link.getString("text");
                            } else if (link.has("title")) {
                                anchorText = link.getString("title");
                            }
                            String rawLinkUrl = link.getString("url");
                            URL linkUrl;
                            try {
                                linkUrl = DataUrl.from(rawLinkUrl);
                                if (!page.getDomain().equals(linkUrl.getDomain())) {
                                    page.addOutbound(Link.of(linkUrl, anchorText));
                                }
                            } catch (IllegalArgumentException e) {
                                log.debug("Failed to parse link: " + rawLinkUrl);
                            } catch (Exception e) {
                                log.error("Unexpected exception while parsing link URL: " + rawLinkUrl, e);
                            }
                        }
                    }
                } catch (JSONException e) {
                    log.debug("Exception trying retrieve links", e);
                }
            }
            try {
                page.setTitle(
                        responseMeta.getJSONObject("HTML-Metadata").getJSONObject("Head").getString("Title"));
            } catch (JSONException e) {
                log.debug("Failed to retrieve title", e);
            }
            try {
                page.setServer(responseMeta.getJSONObject("Headers").getString("Server"));
            } catch (JSONException e) {
                log.debug("Failed to retrieve server", e);
            }
        } catch (JSONException e) {
            log.debug("Exception trying retrieve responseMeta", e);
        }
        return page;
    }
    return Page.EMPTY;
}