Android Open Source - mobile-connector-sdk-android Engage Expiration Parser






From Project

Back to project page mobile-connector-sdk-android.

License

The source code is released under:

Apache License

If you think the Android project mobile-connector-sdk-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.silverpop.engage.util;
//w  w w  . j a  v a2 s  . c  o m
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by jeremydyer on 6/2/14.
 */
public class EngageExpirationParser {

    private Date fromDate;
    private Date expiresAtDate;

    private int dayValue;
    private int hourValue;
    private int minuteValue;
    private int secondValue;

    private static final String VALID_FOR_REGEX_PATTERN = "(\\d+\\s*[d|h|m|s|D|H|M|S])";
    private static final String EXPIRATION_DATE_REGEX_PATTERN = "(\\d{4}/\\d{2}/\\d{2}\\s{1}\\d{2}:\\d{2}:\\d{2})";
    private static final String VALUE_REGEX_PATTERN = "(\\d+)";
    private static final String ENGAGE_DATE_PATTERN = "yyyy/MM/dd HH:mm:ss";

    private static Pattern validForPattern;
    private static Pattern expiresAtPattern;
    private static Pattern valuePattern;
    private static SimpleDateFormat engageDateFormat;

    static {
        validForPattern = Pattern.compile(VALID_FOR_REGEX_PATTERN);
        expiresAtPattern = Pattern.compile(EXPIRATION_DATE_REGEX_PATTERN);
        valuePattern = Pattern.compile(VALUE_REGEX_PATTERN);
        engageDateFormat = new SimpleDateFormat(ENGAGE_DATE_PATTERN);
        engageDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

    public EngageExpirationParser(String expirationString, Date startFromDate) {

        fromDate = startFromDate;
        dayValue = -1;
        hourValue = -1;
        minuteValue = -1;
        secondValue = -1;
        expiresAtDate = null;

        Matcher match = validForPattern.matcher(expirationString);

        boolean foundMatch = false;
        while (match.find()) {
            foundMatch = true;
            match.reset();
            break;
        }

        if (!foundMatch) {
            match = expiresAtPattern.matcher(expirationString);
        }

        while (match.find()) {
            String result = match.group();

            if (result.contains("d")) {
                dayValue = valueFromRegexResult(result);
            } else if (result.contains("h")) {
                hourValue = valueFromRegexResult(result);
            } else if (result.contains("m")) {
                minuteValue = valueFromRegexResult(result);
            } else if (result.contains("s")) {
                secondValue = valueFromRegexResult(result);
            } else if (result.contains("/")) {
                try {
                    expiresAtDate = engageDateFormat.parse(result);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("EngageExpirationParser Regex result '" + result + "' does not match any pattern");
            }
        }

        //If the expiration date was not already set by the parameter than it is set here.
        if (expiresAtDate == null) {
            //Creates the actual expiration date.
            Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            if (fromDate != null) {
                cal.setTime(fromDate);
            }

            if (dayValue > -1) cal.set(Calendar.DAY_OF_MONTH, (cal.get(Calendar.DAY_OF_MONTH) + dayValue));
            if (hourValue > -1) cal.set(Calendar.HOUR_OF_DAY, (cal.get(Calendar.HOUR_OF_DAY) + hourValue));
            if (minuteValue > -1) cal.set(Calendar.MINUTE, (cal.get(Calendar.MINUTE) + minuteValue));
            if (secondValue > -1) cal.set(Calendar.SECOND, (cal.get(Calendar.SECOND) + secondValue));
            expiresAtDate = cal.getTime();
        }
    }


    /**
     * Gets the value for the match.
     *
     * @param result
     *      Integer match value.
     *
     * @return
     *      Value for the date element.
     */
    private Integer valueFromRegexResult(String result) {
        Matcher match = valuePattern.matcher(result);
        match.find();
        return new Integer(match.group());
    }


    public long expirationTimeStamp() {
        return expiresAtDate.getTime();
    }

    public Date expirationDate() {
        return expiresAtDate;
    }

    public long secondsParsedFromExpiration() {
        long seconds = 0;
        if (dayValue > -1) seconds = dayValue * 86400;
        if (hourValue > -1) seconds = seconds + hourValue * 3600;
        if (minuteValue > -1) seconds = seconds + minuteValue * 60;
        if (secondValue > -1) seconds = seconds + secondValue;
        return seconds;
    }

    public long millisecondsParsedFromExpiration() {
        return (secondsParsedFromExpiration() * 1000);
    }
}




Java Source Code List

com.silverpop.engage.EngageApplication.java
com.silverpop.engage.UBFManager.java
com.silverpop.engage.XMLAPIManager.java
com.silverpop.engage.augmentation.UBFAugmentationServiceImpl.java
com.silverpop.engage.augmentation.UBFAugmentationService.java
com.silverpop.engage.augmentation.plugin.UBFAddressAugmentationPlugin.java
com.silverpop.engage.augmentation.plugin.UBFAugmentationPlugin.java
com.silverpop.engage.augmentation.plugin.UBFCoordinatesAugmentationPlugin.java
com.silverpop.engage.augmentation.plugin.UBFLocationNameAugmentationPlugin.java
com.silverpop.engage.config.EngageConfigManagerTests.java
com.silverpop.engage.config.EngageConfigManager.java
com.silverpop.engage.config.EngageConfigTest.java
com.silverpop.engage.config.EngageConfig.java
com.silverpop.engage.deeplinking.EngageDeepLinkManager.java
com.silverpop.engage.demo.engagetest.Application.java
com.silverpop.engage.demo.engagetest.EngageNotificationReceiver.java
com.silverpop.engage.demo.engagetest.MainActivity.java
com.silverpop.engage.demo.engagetest.PushReceiver.java
com.silverpop.engage.demo.engagetest.fragment.EngageConfigFragment.java
com.silverpop.engage.demo.engagetest.fragment.UBFAPIFragment.java
com.silverpop.engage.demo.engagetest.fragment.XMLAPIFragment.java
com.silverpop.engage.domain.EngageEvent.java
com.silverpop.engage.domain.JSONable.java
com.silverpop.engage.domain.UBFTests.java
com.silverpop.engage.domain.UBF.java
com.silverpop.engage.domain.XMLAPIEnum.java
com.silverpop.engage.domain.XMLAPITest.java
com.silverpop.engage.domain.XMLAPI.java
com.silverpop.engage.exception.XMLResponseParseException.java
com.silverpop.engage.location.manager.EngageLocationManager.java
com.silverpop.engage.location.manager.plugin.EngageLocationManagerDefault.java
com.silverpop.engage.location.receiver.EngageLocationReceiver.java
com.silverpop.engage.location.receiver.plugin.EngageLocationReceiverBase.java
com.silverpop.engage.location.receiver.plugin.EngageLocationReceiverGeocode.java
com.silverpop.engage.location.receiver.plugin.EngageLocationReceiverHardcodeTest.java
com.silverpop.engage.network.Credential.java
com.silverpop.engage.network.EngageClient.java
com.silverpop.engage.network.RequestCacheWrapper.java
com.silverpop.engage.network.UBFClient.java
com.silverpop.engage.network.XMLAPIClient.java
com.silverpop.engage.response.EngageResponseXMLTests.java
com.silverpop.engage.response.EngageResponseXML.java
com.silverpop.engage.response.XMLAPIResponseNode.java
com.silverpop.engage.store.EngageLocalEventStoreTest.java
com.silverpop.engage.store.EngageLocalEventStore.java
com.silverpop.engage.store.EngageSQLiteHelper.java
com.silverpop.engage.util.EngageExpirationParserTests.java
com.silverpop.engage.util.EngageExpirationParser.java
com.silverpop.engage.util.TimedAsyncTask.java