Example usage for java.util.regex Matcher groupCount

List of usage examples for java.util.regex Matcher groupCount

Introduction

In this page you can find the example usage for java.util.regex Matcher groupCount.

Prototype

public int groupCount() 

Source Link

Document

Returns the number of capturing groups in this matcher's pattern.

Usage

From source file:carnero.cgeo.cgBase.java

public cgTrackable parseTrackable(String page) {
    if (page == null || page.length() == 0) {
        Log.e(cgSettings.tag, "cgeoBase.parseTrackable: No page given");
        return null;
    }/*from w  w  w. ja v a2  s  .c  om*/

    final Pattern patternTrackableId = Pattern.compile(
            "<a id=\"ctl00_ContentBody_LogLink\" title=\"[^\"]*\" href=\".*log\\.aspx\\?wid=([a-z0-9\\-]+)\"[^>]*>[^<]*</a>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternGeocode = Pattern.compile(
            "<span id=\"ctl00_ContentBody_BugDetails_BugTBNum\" String=\"[^\"]*\">Use[^<]*<strong>(TB[0-9a-z]+)[^<]*</strong> to reference this item.[^<]*</span>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternName = Pattern.compile(
            "<h2>([^<]*<img[^>]*>)?[^<]*<span id=\"ctl00_ContentBody_lbHeading\">([^<]+)</span>[^<]*</h2>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternOwner = Pattern.compile(
            "<dt>[^\\w]*Owner:[^<]*</dt>[^<]*<dd>[^<]*<a id=\"ctl00_ContentBody_BugDetails_BugOwner\" title=\"[^\"]*\" href=\"[^\"]*/profile/\\?guid=([a-z0-9\\-]+)\">([^<]+)<\\/a>[^<]*</dd>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternReleased = Pattern.compile(
            "<dt>[^\\w]*Released:[^<]*</dt>[^<]*<dd>[^<]*<span id=\"ctl00_ContentBody_BugDetails_BugReleaseDate\">([^<]+)<\\/span>[^<]*</dd>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternOrigin = Pattern.compile(
            "<dt>[^\\w]*Origin:[^<]*</dt>[^<]*<dd>[^<]*<span id=\"ctl00_ContentBody_BugDetails_BugOrigin\">([^<]+)<\\/span>[^<]*</dd>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternSpottedCache = Pattern.compile(
            "<dt>[^\\w]*Recently Spotted:[^<]*</dt>[^<]*<dd>[^<]*<a id=\"ctl00_ContentBody_BugDetails_BugLocation\" title=\"[^\"]*\" href=\"[^\"]*/seek/cache_details.aspx\\?guid=([a-z0-9\\-]+)\">In ([^<]+)</a>[^<]*</dd>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternSpottedUser = Pattern.compile(
            "<dt>[^\\w]*Recently Spotted:[^<]*</dt>[^<]*<dd>[^<]*<a id=\"ctl00_ContentBody_BugDetails_BugLocation\" href=\"[^\"]*/profile/\\?guid=([a-z0-9\\-]+)\">In the hands of ([^<]+).</a>[^<]*</dd>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternSpottedUnknown = Pattern.compile(
            "<dt>[^\\w]*Recently Spotted:[^<]*</dt>[^<]*<dd>[^<]*<a id=\"ctl00_ContentBody_BugDetails_BugLocation\">Unknown Location[^<]*</a>[^<]*</dd>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternSpottedOwner = Pattern.compile(
            "<dt>[^\\w]*Recently Spotted:[^<]*</dt>[^<]*<dd>[^<]*<a id=\"ctl00_ContentBody_BugDetails_BugLocation\">In the hands of the owner[^<]*</a>[^<]*</dd>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternGoal = Pattern.compile(
            "<h3>[^\\w]*Current GOAL[^<]*</h3>[^<]*<p[^>]*>(.*)</p>[^<]*<h3>[^\\w]*About This Item[^<]*</h3>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternDetailsImage = Pattern.compile(
            "<h3>[^\\w]*About This Item[^<]*</h3>([^<]*<p>([^<]*<img id=\"ctl00_ContentBody_BugDetails_BugImage\" class=\"[^\"]+\" src=\"([^\"]+)\"[^>]*>)?[^<]*</p>)?[^<]*<p[^>]*>(.*)</p>[^<]*<div id=\"ctl00_ContentBody_BugDetails_uxAbuseReport\">",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternLogs = Pattern.compile(
            "<table class=\"TrackableItemLogTable Table\">(.*)<\\/table>[^<]*<ul", Pattern.CASE_INSENSITIVE);
    final Pattern patternIcon = Pattern.compile(
            "<img id=\"ctl00_ContentBody_BugTypeImage\" class=\"TravelBugHeaderIcon\" src=\"([^\"]+)\"[^>]*>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternType = Pattern.compile(
            "<img id=\"ctl00_ContentBody_BugTypeImage\" class=\"TravelBugHeaderIcon\" src=\"[^\"]+\" alt=\"([^\"]+)\"[^>]*>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternDistance = Pattern.compile(
            "<h4[^>]*[^\\w]*Tracking History \\(([0-9\\.,]+(km|mi))[^\\)]*\\)", Pattern.CASE_INSENSITIVE);

    final cgTrackable trackable = new cgTrackable();

    // trackable geocode
    try {
        final Matcher matcherGeocode = patternGeocode.matcher(page);
        while (matcherGeocode.find()) {
            if (matcherGeocode.groupCount() > 0) {
                trackable.geocode = matcherGeocode.group(1).toUpperCase();
            }
        }
    } catch (Exception e) {
        // failed to parse trackable geocode
        Log.w(cgSettings.tag, "cgeoBase.parseTrackable: Failed to parse trackable geocode");
    }

    // trackable id
    try {
        final Matcher matcherTrackableId = patternTrackableId.matcher(page);
        while (matcherTrackableId.find()) {
            if (matcherTrackableId.groupCount() > 0) {
                trackable.guid = matcherTrackableId.group(1);
            }
        }
    } catch (Exception e) {
        // failed to parse trackable id
        Log.w(cgSettings.tag, "cgeoBase.parseTrackable: Failed to parse trackable id");
    }

    // trackable icon
    try {
        final Matcher matcherTrackableIcon = patternIcon.matcher(page);
        while (matcherTrackableIcon.find()) {
            if (matcherTrackableIcon.groupCount() > 0) {
                trackable.iconUrl = matcherTrackableIcon.group(1);
            }
        }
    } catch (Exception e) {
        // failed to parse trackable icon
        Log.w(cgSettings.tag, "cgeoBase.parseTrackable: Failed to parse trackable icon");
    }

    // trackable name
    try {
        final Matcher matcherName = patternName.matcher(page);
        while (matcherName.find()) {
            if (matcherName.groupCount() > 1) {
                trackable.name = matcherName.group(2);
            }
        }
    } catch (Exception e) {
        // failed to parse trackable name
        Log.w(cgSettings.tag, "cgeoBase.parseTrackable: Failed to parse trackable name");
    }

    // trackable type
    if (trackable.name != null && trackable.name.length() > 0) {
        try {
            final Matcher matcherType = patternType.matcher(page);
            while (matcherType.find()) {
                if (matcherType.groupCount() > 0) {
                    trackable.type = matcherType.group(1);
                }
            }
        } catch (Exception e) {
            // failed to parse trackable type
            Log.w(cgSettings.tag, "cgeoBase.parseTrackable: Failed to parse trackable type");
        }
    }

    // trackable owner name
    try {
        final Matcher matcherOwner = patternOwner.matcher(page);
        while (matcherOwner.find()) {
            if (matcherOwner.groupCount() > 0) {
                trackable.ownerGuid = matcherOwner.group(1);
                trackable.owner = matcherOwner.group(2);
            }
        }
    } catch (Exception e) {
        // failed to parse trackable owner name
        Log.w(cgSettings.tag, "cgeoBase.parseTrackable: Failed to parse trackable owner name");
    }

    // trackable origin
    try {
        final Matcher matcherOrigin = patternOrigin.matcher(page);
        while (matcherOrigin.find()) {
            if (matcherOrigin.groupCount() > 0) {
                trackable.origin = matcherOrigin.group(1);
            }
        }
    } catch (Exception e) {
        // failed to parse trackable origin
        Log.w(cgSettings.tag, "cgeoBase.parseTrackable: Failed to parse trackable origin");
    }

    // trackable spotted
    try {
        final Matcher matcherSpottedCache = patternSpottedCache.matcher(page);
        while (matcherSpottedCache.find()) {
            if (matcherSpottedCache.groupCount() > 0) {
                trackable.spottedGuid = matcherSpottedCache.group(1);
                trackable.spottedName = matcherSpottedCache.group(2);
                trackable.spottedType = cgTrackable.SPOTTED_CACHE;
            }
        }

        final Matcher matcherSpottedUser = patternSpottedUser.matcher(page);
        while (matcherSpottedUser.find()) {
            if (matcherSpottedUser.groupCount() > 0) {
                trackable.spottedGuid = matcherSpottedUser.group(1);
                trackable.spottedName = matcherSpottedUser.group(2);
                trackable.spottedType = cgTrackable.SPOTTED_USER;
            }
        }

        final Matcher matcherSpottedUnknown = patternSpottedUnknown.matcher(page);
        if (matcherSpottedUnknown.find()) {
            trackable.spottedType = cgTrackable.SPOTTED_UNKNOWN;
        }

        final Matcher matcherSpottedOwner = patternSpottedOwner.matcher(page);
        if (matcherSpottedOwner.find()) {
            trackable.spottedType = cgTrackable.SPOTTED_OWNER;
        }
    } catch (Exception e) {
        // failed to parse trackable last known place
        Log.w(cgSettings.tag, "cgeoBase.parseTrackable: Failed to parse trackable last known place");
    }

    // released
    try {
        final Matcher matcherReleased = patternReleased.matcher(page);
        while (matcherReleased.find()) {
            if (matcherReleased.groupCount() > 0 && matcherReleased.group(1) != null) {
                try {
                    if (trackable.released == null) {
                        trackable.released = dateTbIn1.parse(matcherReleased.group(1));
                    }
                } catch (Exception e) {
                    //
                }

                try {
                    if (trackable.released == null) {
                        trackable.released = dateTbIn2.parse(matcherReleased.group(1));
                    }
                } catch (Exception e) {
                    //
                }
            }
        }
    } catch (Exception e) {
        // failed to parse trackable released date
        Log.w(cgSettings.tag, "cgeoBase.parseTrackable: Failed to parse trackable released date");
    }

    // trackable distance
    try {
        final Matcher matcherDistance = patternDistance.matcher(page);
        while (matcherDistance.find()) {
            if (matcherDistance.groupCount() > 0) {
                trackable.distance = parseDistance(matcherDistance.group(1));
            }
        }
    } catch (Exception e) {
        // failed to parse trackable distance
        Log.w(cgSettings.tag, "cgeoBase.parseTrackable: Failed to parse trackable distance");
    }

    // trackable goal
    try {
        final Matcher matcherGoal = patternGoal.matcher(page);
        while (matcherGoal.find()) {
            if (matcherGoal.groupCount() > 0) {
                trackable.goal = matcherGoal.group(1);
            }
        }
    } catch (Exception e) {
        // failed to parse trackable goal
        Log.w(cgSettings.tag, "cgeoBase.parseTrackable: Failed to parse trackable goal");
    }

    // trackable details & image
    try {
        final Matcher matcherDetailsImage = patternDetailsImage.matcher(page);
        while (matcherDetailsImage.find()) {
            if (matcherDetailsImage.groupCount() > 0) {
                final String image = matcherDetailsImage.group(3);
                final String details = matcherDetailsImage.group(4);

                if (image != null) {
                    trackable.image = image;
                }
                if (details != null) {
                    trackable.details = details;
                }
            }
        }
    } catch (Exception e) {
        // failed to parse trackable details & image
        Log.w(cgSettings.tag, "cgeoBase.parseTrackable: Failed to parse trackable details & image");
    }

    // trackable logs
    try {
        final Matcher matcherLogs = patternLogs.matcher(page);
        while (matcherLogs.find()) {
            if (matcherLogs.groupCount() > 0) {
                final Pattern patternLog = Pattern.compile("[^>]*>"
                        + "[^<]*<td[^<]*<img src=[\"|'].*\\/icons\\/([^\\.]+)\\.[a-z]{2,5}[\"|'][^>]*>&nbsp;(\\d+).(\\d+).(\\d+)[^<]*</td>"
                        + "[^<]*<td>[^<]*<a href=[^>]+>([^<]+)<.a>([^<]*|[^<]*<a href=[\"|'].*guid=([^\"]*)\">([^<]*)</a>[^<]*)</td>"
                        + "[^<]*<td>[^<]*</td>" + "[^<]*<td[^<]*<a href=[^>]+>[^<]+</a>[^<]*</td>[^<]*</tr>"
                        + "[^<]*<tr[^>]*>[^<]*<td[^>]*>(.*?)</td>[^<]*</tr>.*" + "");
                // 1 filename == type
                // 2 month
                // 3 date
                // 4 year
                // 5 user
                // 6 action dependent
                // 7 cache guid
                // 8 cache name
                // 9 text
                final String[] logs = matcherLogs.group(1).split("<tr class=\"Data BorderTop");
                final int logsCnt = logs.length;

                for (int k = 1; k < logsCnt; k++) {
                    final Matcher matcherLog = patternLog.matcher(logs[k]);
                    if (matcherLog.find()) {
                        final cgLog logDone = new cgLog();

                        String logTmp = matcherLog.group(9);
                        logTmp = Pattern.compile("<p>").matcher(logTmp).replaceAll("\n");
                        logTmp = Pattern.compile("<br[^>]*>").matcher(logTmp).replaceAll("\n");
                        logTmp = Pattern.compile("<\\/p>").matcher(logTmp).replaceAll("");
                        logTmp = Pattern.compile("\r+").matcher(logTmp).replaceAll("\n");

                        int day = -1;
                        try {
                            day = Integer.parseInt(matcherLog.group(3));
                        } catch (Exception e) {
                            Log.w(cgSettings.tag, "Failed to parse logs date (day): " + e.toString());
                        }

                        int month = -1;
                        try {
                            month = Integer.parseInt(matcherLog.group(2));
                            month -= 1;
                        } catch (Exception e) {
                            Log.w(cgSettings.tag, "Failed to parse logs date (month): " + e.toString());
                        }

                        int year = -1;
                        try {
                            year = Integer.parseInt(matcherLog.group(4));
                        } catch (Exception e) {
                            Log.w(cgSettings.tag, "Failed to parse logs date (year): " + e.toString());
                        }

                        long logDate;
                        if (year > 0 && month >= 0 && day > 0) {
                            Calendar date = Calendar.getInstance();
                            date.set(year, month, day, 12, 0, 0);
                            logDate = date.getTimeInMillis();
                            logDate = (long) (Math.ceil(logDate / 1000)) * 1000;
                        } else {
                            logDate = 0;
                        }

                        if (logTypes.containsKey(matcherLog.group(1).toLowerCase()) == true) {
                            logDone.type = logTypes.get(matcherLog.group(1).toLowerCase());
                        } else {
                            logDone.type = logTypes.get("icon_note");
                        }

                        logDone.author = Html.fromHtml(matcherLog.group(5)).toString();
                        logDone.date = logDate;
                        logDone.log = logTmp;
                        if (matcherLog.group(7) != null && matcherLog.group(8) != null) {
                            logDone.cacheGuid = matcherLog.group(7);
                            logDone.cacheName = matcherLog.group(8);
                        }

                        trackable.logs.add(logDone);
                    }
                }
            }
        }
    } catch (Exception e) {
        // failed to parse logs
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache logs");
    }

    app.saveTrackable(trackable);

    return trackable;
}

From source file:carnero.cgeo.cgBase.java

public HashMap<String, Object> parseCoordinate(String coord, String latlon) {
    final HashMap<String, Object> coords = new HashMap<String, Object>();

    final Pattern patternA = Pattern.compile("^([NSWE])[^\\d]*(\\d+)? +(\\d+)([\\.|,](\\d+))?$",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternB = Pattern.compile("^([NSWE])[^\\d]*(\\d+)([\\.|,](\\d+))?$",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternC = Pattern.compile("^(-?\\d+)([\\.|,](\\d+))?$", Pattern.CASE_INSENSITIVE);
    final Pattern patternD = Pattern.compile("^([NSWE])[^\\d]*(\\d+)?$", Pattern.CASE_INSENSITIVE);
    final Pattern patternE = Pattern.compile("^(-?\\d+)?$", Pattern.CASE_INSENSITIVE);
    final Pattern patternF = Pattern.compile("^([NSWE])[^\\d]*(\\d+)$", Pattern.CASE_INSENSITIVE);
    final Pattern pattern0 = Pattern.compile("^(-?\\d+)([\\.|,](\\d+))?$", Pattern.CASE_INSENSITIVE);

    coord = coord.trim().toUpperCase();/*  w ww .  ja  va  2  s . com*/

    final Matcher matcherA = patternA.matcher(coord);
    final Matcher matcherB = patternB.matcher(coord);
    final Matcher matcherC = patternC.matcher(coord);
    final Matcher matcherD = patternD.matcher(coord);
    final Matcher matcherE = patternE.matcher(coord);
    final Matcher matcherF = patternF.matcher(coord);
    final Matcher matcher0 = pattern0.matcher(coord);

    int latlonNegative;
    if (matcherA.find() == true && matcherA.groupCount() > 0) {
        if (matcherA.group(1).equalsIgnoreCase("N") || matcherA.group(1).equalsIgnoreCase("E")) {
            latlonNegative = 1;
        } else {
            latlonNegative = -1;
        }

        if (matcherA.groupCount() < 5 || matcherA.group(5) == null) {
            coords.put("coordinate", new Double(latlonNegative
                    * (new Double(matcherA.group(2)) + new Double(matcherA.group(3) + ".0") / 60)));
            coords.put("string",
                    matcherA.group(1) + " " + matcherA.group(2) + " " + matcherA.group(3) + ".000");
        } else {
            coords.put("coordinate", new Double(latlonNegative * (new Double(matcherA.group(2))
                    + new Double(matcherA.group(3) + "." + matcherA.group(5)) / 60)));
            coords.put("string", matcherA.group(1) + " " + matcherA.group(2) + " " + matcherA.group(3) + "."
                    + matcherA.group(5));
        }

        return coords;
    } else if (matcherB.find() == true && matcherB.groupCount() > 0) {
        if (matcherB.group(1).equalsIgnoreCase("N") || matcherB.group(1).equalsIgnoreCase("E")) {
            latlonNegative = 1;
        } else {
            latlonNegative = -1;
        }

        if (matcherB.groupCount() < 4 || matcherB.group(4) == null) {
            coords.put("coordinate", new Double(latlonNegative * (new Double(matcherB.group(2) + ".0"))));
        } else {
            coords.put("coordinate",
                    new Double(latlonNegative * (new Double(matcherB.group(2) + "." + matcherB.group(4)))));
        }
    } else if (matcherC.find() == true && matcherC.groupCount() > 0) {
        if (matcherC.groupCount() < 3 || matcherC.group(3) == null) {
            coords.put("coordinate", new Double(new Float(matcherC.group(1) + ".0")));
        } else {
            coords.put("coordinate", new Double(new Float(matcherC.group(1) + "." + matcherC.group(3))));
        }
    } else if (matcherD.find() == true && matcherD.groupCount() > 0) {
        if (matcherD.group(1).equalsIgnoreCase("N") || matcherD.group(1).equalsIgnoreCase("E")) {
            latlonNegative = 1;
        } else {
            latlonNegative = -1;
        }

        coords.put("coordinate", new Double(latlonNegative * (new Double(matcherB.group(2)))));
    } else if (matcherE.find() == true && matcherE.groupCount() > 0) {
        coords.put("coordinate", new Double(matcherE.group(1)));
    } else if (matcherF.find() == true && matcherF.groupCount() > 0) {
        if (matcherF.group(1).equalsIgnoreCase("N") || matcherF.group(1).equalsIgnoreCase("E")) {
            latlonNegative = 1;
        } else {
            latlonNegative = -1;
        }

        coords.put("coordinate", new Double(latlonNegative * (new Double(matcherB.group(2)))));
    } else {
        return null;
    }

    if (matcher0.find() == true && matcher0.groupCount() > 0) {
        String tmpDir = null;
        Float tmpCoord;
        if (matcher0.groupCount() < 3 || matcher0.group(3) == null) {
            tmpCoord = new Float("0.0");
        } else {
            tmpCoord = new Float("0." + matcher0.group(3));
        }

        if (latlon.equalsIgnoreCase("lat")) {
            if (matcher0.group(1).equals("+")) {
                tmpDir = "N";
            }
            if (matcher0.group(1).equals("-")) {
                tmpDir = "S";
            }
        } else if (latlon.equalsIgnoreCase("lon")) {
            if (matcher0.group(1).equals("+")) {
                tmpDir = "E";
            }
            if (matcher0.group(1).equals("-")) {
                tmpDir = "W";
            }
        }

        coords.put("string",
                tmpDir + " " + matcher0.group(1) + " " + (Math.round(tmpCoord / (1 / 60) * 1000) * 1000));

        return coords;
    } else {
        return new HashMap<String, Object>();
    }
}

From source file:carnero.cgeo.original.libs.Base.java

public CacheWrap parseCache(String page, int reason) {
    if (page == null || page.length() == 0) {
        Log.e(Settings.tag, "cgeoBase.parseCache: No page given");
        return null;
    }//from   ww w  . j  a v a2  s .  c  om

    final Pattern patternGeocode = Pattern.compile(
            "<meta name=\"og:url\" content=\"[^\"]+/(GC[0-9A-Z]+)\"[^>]*>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternCacheId = Pattern.compile("/seek/log\\.aspx\\?ID=(\\d+)",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternCacheGuid = Pattern.compile(
            "<link rel=\"alternate\" href=\"[^\"]*/datastore/rss_galleryimages\\.ashx\\?guid=([0-9a-z\\-]+)\"[^>]*>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternType = Pattern.compile(
            "<img src=\"[^\"]*/WptTypes/\\d+\\.gif\" alt=\"([^\"]+)\" (title=\"[^\"]*\" )?width=\"32\" height=\"32\"[^>]*>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

    final Pattern patternName = Pattern.compile(
            "<h2[^>]*>[^<]*<span id=\"ctl00_ContentBody_CacheName\">([^<]+)<\\/span>[^<]*<\\/h2>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternSize = Pattern.compile(
            "<div class=\"CacheSize[^\"]*\">[^<]*<p[^>]*>[^S]*Size[^:]*:[^<]*<span[^>]*>[^<]*<img src=\"[^\"]*/icons/container/[a-z_]+\\.gif\" alt=\"Size: ([^\"]+)\"[^>]*>[^<]*<small>[^<]*</small>[^<]*</span>[^<]*</p>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternDifficulty = Pattern.compile(
            "<span id=\"ctl00_ContentBody_uxLegendScale\"[^>]*>[^<]*<img src=\"[^\"]*/images/stars/stars([0-9_]+)\\.gif\" alt=\"[^\"]+\"[^>]*>[^<]*</span>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternTerrain = Pattern.compile(
            "<span id=\"ctl00_ContentBody_Localize6\"[^>]*>[^<]*<img src=\"[^\"]*/images/stars/stars([0-9_]+)\\.gif\" alt=\"[^\"]+\"[^>]*>[^<]*</span>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternOwner = Pattern.compile(
            "<span class=\"minorCacheDetails\">[^\\w]*An?([^\\w]*Event)?[^\\w]*cache[^\\w]*by[^<]*<a href=\"[^\"]+\">([^<]+)</a>[^<]*</span>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternOwnerReal = Pattern.compile(
            "<a id=\"ctl00_ContentBody_uxFindLinksHiddenByThisUser\" href=\"[^\"]*/seek/nearest\\.aspx\\?u=*([^\"]+)\">[^<]+</a>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternHidden = Pattern.compile(
            "<span[^>]*>[^\\w]*Hidden[^:]*:[^\\d]*((\\d+)\\/(\\d+)\\/(\\d+))[^<]*</span>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternHiddenEvent = Pattern.compile(
            "<span[^>]*>[^\\w]*Event[^\\w]*Date[^:]*:[^\\w]*[a-zA-Z]+,[^\\d]*((\\d+)[^\\w]*(\\w+)[^\\d]*(\\d+))[^<]*</span>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternFavourite = Pattern.compile(
            "<a id=\"uxFavContainerLink\"[^>]*>[^<]*<div[^<]*<span class=\"favorite-value\">[^\\d]*([0-9]+)[^\\d^<]*</span>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

    final Pattern patternFound = Pattern.compile(
            "<p>[^<]*<a id=\"ctl00_ContentBody_hlFoundItLog\"[^<]*<img src=\".*/images/stockholm/16x16/check\\.gif\"[^>]*>[^<]*</a>[^<]*</p>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternLatLon = Pattern.compile("<span id=\"uxLatLon\"[^>]*>([^<]*)<\\/span>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternLocation = Pattern.compile("<span id=\"ctl00_ContentBody_Location\"[^>]*>In ([^<]*)",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternHint = Pattern.compile(
            "<p>([^<]*<strong>)?[^\\w]*Additional Hints([^<]*<\\/strong>)?[^\\(]*\\(<a[^>]+>Encrypt</a>\\)[^<]*<\\/p>[^<]*<div id=\"div_hint\"[^>]*>(.*)</div>[^<]*<div id=[\\'|\"]dk[\\'|\"]",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternDescShort = Pattern.compile(
            "<div class=\"UserSuppliedContent\">[^<]*<span id=\"ctl00_ContentBody_ShortDescription\"[^>]*>((?:(?!</span>[^\\w^<]*</div>).)*)</span>[^\\w^<]*</div>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternDesc = Pattern.compile(
            "<div class=\"UserSuppliedContent\">[^<]*<span id=\"ctl00_ContentBody_LongDescription\"[^>]*>((?:(?!</span>[^\\w^<]*</div>).)*)</span>[^<]*</div>[^<]*<p>[^<]*</p>[^<]*<p>[^<]*<strong>[^\\w]*Additional Hints</strong>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternCountLogs = Pattern.compile(
            "<span id=\"ctl00_ContentBody_lblFindCounts\"><p>(.*)<\\/p><\\/span>", Pattern.CASE_INSENSITIVE);
    final Pattern patternCountLog = Pattern.compile(
            " src=\"\\/images\\/icons\\/([^\\.]*).gif\" alt=\"[^\"]*\" title=\"[^\"]*\" />([0-9]*)[^0-9]+",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    // final Pattern patternLogs = Pattern.compile("<table class=\"LogsTable[^\"]*\"[^>]*>((?:(?!</table>).)*)</table>[^<]*<p", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternLogs = Pattern.compile("<table class=\"LogsTable[^\"]*\"[^>]*>(.*)</table>[^<]*<p",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    /*
     * <tr>
     * <td class="Nothing">
     * <div class="FloatLeft LogDisplayLeft">
     * <p class="logOwnerProfileName">
     * <strong><a href="/profile/?guid=96c94662-f2c9-450a-99ef-4c034dcde72b" id="184034162">CERV.cz</a></strong>
     * </p>
     * <p class="logOwnerBadge"><img src='/images/icons/prem_user.gif' title='Premium Member' /> Premium Member</p>
     * <p class="logOwnerAvatar"><a href="/profile/?guid=96c94662-f2c9-450a-99ef-4c034dcde72b"><img src="/images/default_avatar.jpg" height='48' width='48' /></a></p>
     * <p class="logOwnerStats">
     * <img src="/images/icons/icon_smile.png" title="Caches Found" /> 567</div>
     * <div class="FloatLeft LogDisplayRight">
     * <div class="HalfLeft LogType">
     * <strong><img src="http://www.geocaching.com/images/icons/icon_smile.gif" alt="Found it" title="Found it" />&nbsp;Found it</strong>
     * </div>
     * <div class="HalfRight AlignRight">
     * <span class="minorDetails LogDate">09/03/2011</span>
     * </div>
     * <div class="Clear LogContent">
     * <p class="LogText">13:29 diky za kes!</p>
     * <div class="AlignRight">
     * <small><a href="log.aspx?LUID=8da01276-7881-4ec9-8d23-8938d7f2984e" title="View Log">View Log</a></small>
     * </div></div></div>
     * </td>
     * </tr>
     */
    final Pattern patternLogUser = Pattern.compile(
            "<p class=\"logOwnerProfileName\">[^<]*<strong>[^<]*<a[^>]*>([^<]+)</a>[^<]*</strong>[^<]*</p>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternLogFounds = Pattern.compile(
            "<p class=\"logOwnerStats\"><img[^>]*>[^\\d]*(\\d+)[^<]*</div>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternLogIcon = Pattern.compile(
            "<strong>[^<]*<img src=\"[^\"]*/images/icons/([^\"]+)\\.gif\"[^>]*>[^<]+</strong>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternLogDate = Pattern.compile(
            "<span class=\"minorDetails LogDate\">([0-9]+/[0-9]+/[0-9]+)[^<]*</span>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternLogText = Pattern.compile("<p class=\"LogText\">((?:(?!</p>).)*)</p>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

    final Pattern patternAttributes = Pattern.compile(
            "<h3 class=\"WidgetHeader\">[^<]*<img[^>]+>[^\\w]*Attributes[^<]*</h3>[^<]*<div class=\"WidgetBody\">(([^<]*<img src=\"[^\"]+\" alt=\"[^\"]+\"[^>]*>)+)[^<]*<p",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternAttributesInside = Pattern.compile("[^<]*<img src=\"([^\"]+)\" alt=\"([^\"]+)\"[^>]*>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternSpoilers = Pattern.compile(
            "<span id=\"ctl00_ContentBody_Images\">((<a href=\"[^\"]+\"[^>]*>[^<]*<img[^>]+>[^<]*<span>[^>]+</span>[^<]*</a>[^<]*<br[^>]*>([^<]*(<br[^>]*>)+)?)+)[^<]*</span>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternSpoilersInside = Pattern.compile(
            "[^<]*<a href=\"([^\"]+)\"[^>]*>[^<]*<img[^>]+>[^<]*<span>([^>]+)</span>[^<]*</a>[^<]*<br[^>]*>(([^<]*)(<br[^<]*>)+)?",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternInventory = Pattern.compile(
            "<span id=\"ctl00_ContentBody_uxTravelBugList_uxInventoryLabel\">[^\\w]*Inventory[^<]*</span>[^<]*</h3>[^<]*<div class=\"WidgetBody\">([^<]*<ul>(([^<]*<li>[^<]*<a href=\"[^\"]+\"[^>]*>[^<]*<img src=\"[^\"]+\"[^>]*>[^<]*<span>[^<]+<\\/span>[^<]*<\\/a>[^<]*<\\/li>)+)[^<]*<\\/ul>)?",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternInventoryInside = Pattern.compile(
            "[^<]*<li>[^<]*<a href=\"[a-z0-9\\-\\_\\.\\?\\/\\:\\@]*\\/track\\/details\\.aspx\\?guid=([0-9a-z\\-]+)[^\"]*\"[^>]*>[^<]*<img src=\"[^\"]+\"[^>]*>[^<]*<span>([^<]+)<\\/span>[^<]*<\\/a>[^<]*<\\/li>",
            Pattern.CASE_INSENSITIVE);

    final CacheWrap caches = new CacheWrap();
    final Cache cache = new Cache();

    if (page.indexOf("Cache is Unpublished") > -1) {
        caches.error = "cache was unpublished";
        return caches;
    }

    if (page.indexOf("Sorry, the owner of this listing has made it viewable to Premium Members only.") != -1) {
        caches.error = "requested cache is for premium members only";
        return caches;
    }

    if (page.indexOf("has chosen to make this cache listing visible to Premium Members only.") != -1) {
        caches.error = "requested cache is for premium members only";
        return caches;
    }

    if (page.indexOf("<li>This cache is temporarily unavailable.") != -1) {
        cache.disabled = true;
    } else {
        cache.disabled = false;
    }

    if (page.indexOf("<li>This cache has been archived,") != -1) {
        cache.archived = true;
    } else {
        cache.archived = false;
    }

    if (page.indexOf("<p class=\"Warning\">This is a Premium Member Only cache.</p>") != -1) {
        cache.members = true;
    } else {
        cache.members = false;
    }

    cache.reason = reason;

    // cache geocode
    try {
        final Matcher matcherGeocode = patternGeocode.matcher(page);
        while (matcherGeocode.find()) {
            if (matcherGeocode.groupCount() > 0) {
                cache.geocode = (String) matcherGeocode.group(1);
            }
        }
    } catch (Exception e) {
        // failed to parse cache geocode
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache geocode");
    }

    // cache id
    try {
        final Matcher matcherCacheId = patternCacheId.matcher(page);
        while (matcherCacheId.find()) {
            if (matcherCacheId.groupCount() > 0) {
                cache.cacheid = (String) matcherCacheId.group(1);
            }
        }
    } catch (Exception e) {
        // failed to parse cache id
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache id");
    }

    // cache guid
    try {
        final Matcher matcherCacheGuid = patternCacheGuid.matcher(page);
        while (matcherCacheGuid.find()) {
            if (matcherCacheGuid.groupCount() > 0) {
                cache.guid = (String) matcherCacheGuid.group(1);
            }
        }
    } catch (Exception e) {
        // failed to parse cache guid
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache guid");
    }

    // name
    try {
        final Matcher matcherName = patternName.matcher(page);
        while (matcherName.find()) {
            if (matcherName.groupCount() > 0) {
                cache.name = Html.fromHtml(matcherName.group(1)).toString();
            }
        }
    } catch (Exception e) {
        // failed to parse cache name
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache name");
    }

    // owner real name
    try {
        final Matcher matcherOwnerReal = patternOwnerReal.matcher(page);
        while (matcherOwnerReal.find()) {
            if (matcherOwnerReal.groupCount() > 0) {
                cache.ownerReal = URLDecoder.decode(matcherOwnerReal.group(1));
            }
        }
    } catch (Exception e) {
        // failed to parse owner real name
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache owner real name");
    }

    final String username = settings.getUsername();
    if (cache.ownerReal != null && username != null && cache.ownerReal.equalsIgnoreCase(username)) {
        cache.own = true;
    }

    int pos = -1;
    String tableInside = page;

    pos = tableInside.indexOf("id=\"cacheDetails\"");
    if (pos == -1) {
        Log.e(Settings.tag, "cgeoBase.parseCache: ID \"cacheDetails\" not found on page");
        return null;
    }

    tableInside = tableInside.substring(pos);

    pos = tableInside.indexOf("<div class=\"CacheInformationTable\"");
    if (pos == -1) {
        Log.e(Settings.tag, "cgeoBase.parseCache: ID \"CacheInformationTable\" not found on page");
        return null;
    }

    tableInside = tableInside.substring(0, pos);

    if (tableInside != null && tableInside.length() > 0) {
        // cache terrain
        try {
            final Matcher matcherTerrain = patternTerrain.matcher(tableInside);
            while (matcherTerrain.find()) {
                if (matcherTerrain.groupCount() > 0) {
                    cache.terrain = new Float(
                            Pattern.compile("_").matcher(matcherTerrain.group(1)).replaceAll("."));
                }
            }
        } catch (Exception e) {
            // failed to parse terrain
            Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache terrain");
        }

        // cache difficulty
        try {
            final Matcher matcherDifficulty = patternDifficulty.matcher(tableInside);
            while (matcherDifficulty.find()) {
                if (matcherDifficulty.groupCount() > 0) {
                    cache.difficulty = new Float(
                            Pattern.compile("_").matcher(matcherDifficulty.group(1)).replaceAll("."));
                }
            }
        } catch (Exception e) {
            // failed to parse difficulty
            Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache difficulty");
        }

        // owner
        try {
            final Matcher matcherOwner = patternOwner.matcher(tableInside);
            while (matcherOwner.find()) {
                if (matcherOwner.groupCount() > 0) {
                    cache.owner = Html.fromHtml(matcherOwner.group(2)).toString();
                }
            }
        } catch (Exception e) {
            // failed to parse owner
            Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache owner");
        }

        // hidden
        try {
            final Matcher matcherHidden = patternHidden.matcher(tableInside);
            while (matcherHidden.find()) {
                if (matcherHidden.groupCount() > 0) {
                    cache.hidden = dateIn.parse(matcherHidden.group(1));
                }
            }
        } catch (Exception e) {
            // failed to parse cache hidden date
            Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache hidden date");
        }

        if (cache.hidden == null) {
            // event date
            try {
                final Matcher matcherHiddenEvent = patternHiddenEvent.matcher(tableInside);
                while (matcherHiddenEvent.find()) {
                    if (matcherHiddenEvent.groupCount() > 0) {
                        cache.hidden = dateEvIn.parse(matcherHiddenEvent.group(1));
                    }
                }
            } catch (Exception e) {
                // failed to parse cache event date
                Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache event date");
            }
        }

        // favourite
        try {
            final Matcher matcherFavourite = patternFavourite.matcher(tableInside);
            while (matcherFavourite.find()) {
                if (matcherFavourite.groupCount() > 0) {
                    cache.favouriteCnt = Integer.parseInt(matcherFavourite.group(1));
                }
            }
        } catch (Exception e) {
            // failed to parse favourite count
            Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse favourite count");
        }

        // cache size
        try {
            final Matcher matcherSize = patternSize.matcher(tableInside);
            while (matcherSize.find()) {
                if (matcherSize.groupCount() > 0) {
                    cache.size = matcherSize.group(1).toLowerCase();
                }
            }
        } catch (Exception e) {
            // failed to parse size
            Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache size");
        }
    }

    // cache found
    try {
        final Matcher matcherFound = patternFound.matcher(page);
        while (matcherFound.find()) {
            if (matcherFound.group() != null && matcherFound.group().length() > 0) {
                cache.found = true;
            }
        }
    } catch (Exception e) {
        // failed to parse found
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse found");
    }

    // cache type
    try {
        final Matcher matcherType = patternType.matcher(page);
        while (matcherType.find()) {
            if (matcherType.groupCount() > 0) {
                cache.type = cacheTypes.get(matcherType.group(1).toLowerCase());
            }
        }
    } catch (Exception e) {
        // failed to parse type
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache type");
    }

    // latitude and logitude
    try {
        final Matcher matcherLatLon = patternLatLon.matcher(page);
        while (matcherLatLon.find()) {
            if (matcherLatLon.groupCount() > 0) {
                cache.latlon = matcherLatLon.group(1);

                HashMap<String, Object> tmp = this.parseLatlon(cache.latlon);
                if (tmp.size() > 0) {
                    cache.latitude = (Double) tmp.get("latitude");
                    cache.longitude = (Double) tmp.get("longitude");
                    cache.latitudeString = (String) tmp.get("latitudeString");
                    cache.longitudeString = (String) tmp.get("longitudeString");
                    cache.reliableLatLon = true;
                }
                tmp = null;
            }
        }
    } catch (Exception e) {
        // failed to parse latitude and/or longitude
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache coordinates");
    }

    // cache location
    try {
        final Matcher matcherLocation = patternLocation.matcher(page);
        while (matcherLocation.find()) {
            if (matcherLocation.groupCount() > 0) {
                cache.location = matcherLocation.group(1);
            }
        }
    } catch (Exception e) {
        // failed to parse location
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache location");
    }

    // cache hint
    try {
        final Matcher matcherHint = patternHint.matcher(page);
        while (matcherHint.find()) {
            if (matcherHint.groupCount() > 2 && matcherHint.group(3) != null) {
                // replace linebreak and paragraph tags
                String hint = Pattern.compile("<(br|p)[^>]*>").matcher(matcherHint.group(3)).replaceAll("\n");
                if (hint != null) {
                    cache.hint = hint.replaceAll(Pattern.quote("</p>"), "").trim();
                }
            }
        }
    } catch (Exception e) {
        // failed to parse hint
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache hint");
    }

    // cache short description
    try {
        final Matcher matcherDescShort = patternDescShort.matcher(page);
        while (matcherDescShort.find()) {
            if (matcherDescShort.groupCount() > 0) {
                cache.shortdesc = matcherDescShort.group(1).trim();
            }
        }
    } catch (Exception e) {
        // failed to parse short description
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache short description");
    }

    // cache description
    try {
        final Matcher matcherDesc = patternDesc.matcher(page);
        while (matcherDesc.find()) {
            if (matcherDesc.groupCount() > 0) {
                cache.description = matcherDesc.group(1);
            }
        }
    } catch (Exception e) {
        // failed to parse short description
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache description");
    }

    // cache attributes
    try {
        final Matcher matcherAttributes = patternAttributes.matcher(page);
        while (matcherAttributes.find()) {
            if (matcherAttributes.groupCount() > 0) {
                final String attributesPre = matcherAttributes.group(1);
                final Matcher matcherAttributesInside = patternAttributesInside.matcher(attributesPre);

                while (matcherAttributesInside.find()) {
                    if (matcherAttributesInside.groupCount() > 1
                            && matcherAttributesInside.group(2).equalsIgnoreCase("blank") != true) {
                        if (cache.attributes == null) {
                            cache.attributes = new ArrayList<String>();
                        }
                        // by default, use the tooltip of the attribute
                        String attribute = matcherAttributesInside.group(2).toLowerCase();

                        // if the image name can be recognized, use the image name as attribute
                        String imageName = matcherAttributesInside.group(1).trim();
                        if (imageName.length() > 0) {
                            int start = imageName.lastIndexOf('/');
                            int end = imageName.lastIndexOf('.');
                            if (start >= 0 && end >= 0) {
                                attribute = imageName.substring(start + 1, end).replace('-', '_');
                            }
                        }
                        cache.attributes.add(attribute);
                    }
                }
            }
        }
    } catch (Exception e) {
        // failed to parse cache attributes
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache attributes");
    }

    // cache spoilers
    try {
        final Matcher matcherSpoilers = patternSpoilers.matcher(page);
        while (matcherSpoilers.find()) {
            if (matcherSpoilers.groupCount() > 0) {
                final String spoilersPre = matcherSpoilers.group(1);
                final Matcher matcherSpoilersInside = patternSpoilersInside.matcher(spoilersPre);

                while (matcherSpoilersInside.find()) {
                    if (matcherSpoilersInside.groupCount() > 0) {
                        final Spoiler spoiler = new Spoiler();
                        spoiler.url = matcherSpoilersInside.group(1);

                        if (matcherSpoilersInside.group(2) != null) {
                            spoiler.title = matcherSpoilersInside.group(2);
                        }
                        if (matcherSpoilersInside.group(4) != null) {
                            spoiler.description = matcherSpoilersInside.group(4);
                        }

                        if (cache.spoilers == null) {
                            cache.spoilers = new ArrayList<Spoiler>();
                        }
                        cache.spoilers.add(spoiler);
                    }
                }
            }
        }
    } catch (Exception e) {
        // failed to parse cache spoilers
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache spoilers");
    }

    // cache inventory
    try {
        cache.inventoryItems = 0;

        final Matcher matcherInventory = patternInventory.matcher(page);
        while (matcherInventory.find()) {
            if (cache.inventory == null) {
                cache.inventory = new ArrayList<Trackable>();
            }

            if (matcherInventory.groupCount() > 1) {
                final String inventoryPre = matcherInventory.group(2);

                if (inventoryPre != null && inventoryPre.length() > 0) {
                    final Matcher matcherInventoryInside = patternInventoryInside.matcher(inventoryPre);

                    while (matcherInventoryInside.find()) {
                        if (matcherInventoryInside.groupCount() > 0) {
                            final Trackable inventoryItem = new Trackable();
                            inventoryItem.guid = matcherInventoryInside.group(1);
                            inventoryItem.name = matcherInventoryInside.group(2);

                            cache.inventory.add(inventoryItem);
                            cache.inventoryItems++;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        // failed to parse cache inventory
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache inventory (2)");
    }

    // cache logs counts
    try {
        final Matcher matcherLogCounts = patternCountLogs.matcher(page);
        while (matcherLogCounts.find()) {
            if (matcherLogCounts.groupCount() > 0) {
                final String[] logs = matcherLogCounts.group(1).split("<img");
                final int logsCnt = logs.length;

                for (int k = 1; k < logsCnt; k++) {
                    Integer type = null;
                    Integer count = null;
                    final Matcher matcherLog = patternCountLog.matcher(logs[k]);

                    if (matcherLog.find()) {
                        String typeStr = matcherLog.group(1);
                        String countStr = matcherLog.group(2);
                        if (typeStr != null && typeStr.length() > 0) {
                            if (logTypes.containsKey(typeStr.toLowerCase()) == true) {
                                type = logTypes.get(typeStr.toLowerCase());
                            }
                        }
                        if (countStr != null && countStr.length() > 0) {
                            count = Integer.parseInt(countStr);
                        }
                        if (type != null && count != null) {
                            cache.logCounts.put(type, count);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        // failed to parse logs
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache log count");
    }

    // cache logs
    try {
        final Matcher matcherLogs = patternLogs.matcher(page);
        while (matcherLogs.find()) {
            Log.d(">>>>", "cnt: " + matcherLogs.groupCount());

            if (matcherLogs.groupCount() > 0) {
                final String[] logs = matcherLogs.group(1).split("</tr><tr>");
                final int logsCnt = logs.length;

                for (int k = 0; k < logsCnt; k++) {
                    final CacheLog logDone = new CacheLog();
                    Matcher matcher;

                    matcher = patternLogUser.matcher(logs[k]);
                    if (matcher.find() && matcher.groupCount() > 0) {
                        logDone.author = matcher.group(1).trim();
                        logDone.author = Html.fromHtml(logDone.author).toString();
                    }

                    matcher = patternLogFounds.matcher(logs[k]);
                    if (matcher.find() && matcher.groupCount() > 0) {
                        try {
                            logDone.found = Integer.parseInt(matcher.group(1).trim());
                        } catch (Exception e) {
                            // NaN
                        }
                    }

                    matcher = patternLogIcon.matcher(logs[k]);
                    if (matcher.find() && matcher.groupCount() > 0) {
                        if (logTypes.containsKey(matcher.group(1).toLowerCase()) == true) {
                            logDone.type = logTypes.get(matcher.group(1).toLowerCase());
                        } else {
                            logDone.type = logTypes.get("icon_note");
                        }
                    }

                    matcher = patternLogDate.matcher(logs[k]);
                    if (matcher.find() && matcher.groupCount() > 0) {
                        Date logDate = dateLogIn.parse(matcher.group(1));
                        if (logDate != null) {
                            logDone.date = logDate.getTime();
                        }
                    }

                    matcher = patternLogText.matcher(logs[k]);
                    if (matcher.find() && matcher.groupCount() > 0) {
                        logDone.log = matcher.group(1).trim();
                    }

                    if (cache.logs == null) {
                        cache.logs = new ArrayList<CacheLog>();
                    }
                    cache.logs.add(logDone);
                }
            }
        }
    } catch (Exception e) {
        // failed to parse logs
        Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse cache logs");
    }

    int wpBegin = 0;
    int wpEnd = 0;

    wpBegin = page.indexOf("<table class=\"Table\" id=\"ctl00_ContentBody_Waypoints\">");
    if (wpBegin != -1) { // parse waypoints
        final Pattern patternWpType = Pattern.compile("\\/wpttypes\\/sm\\/(.+)\\.jpg",
                Pattern.CASE_INSENSITIVE);
        final Pattern patternWpPrefixOrLookupOrLatlon = Pattern
                .compile(">([^<]*<[^>]+>)?([^<]+)(<[^>]+>[^<]*)?<\\/td>", Pattern.CASE_INSENSITIVE);
        final Pattern patternWpName = Pattern.compile(">[^<]*<a[^>]+>([^<]*)<\\/a>", Pattern.CASE_INSENSITIVE);
        final Pattern patternWpNote = Pattern.compile("colspan=\"6\">(.*)<\\/td>", Pattern.CASE_INSENSITIVE);

        String wpList = page.substring(wpBegin);

        wpEnd = wpList.indexOf("</p>");
        if (wpEnd > -1 && wpEnd <= wpList.length()) {
            wpList = wpList.substring(0, wpEnd);
        }

        if (wpList.indexOf("No additional waypoints to display.") == -1) {
            wpEnd = wpList.indexOf("</table>");
            wpList = wpList.substring(0, wpEnd);

            wpBegin = wpList.indexOf("<tbody>");
            wpEnd = wpList.indexOf("</tbody>");
            if (wpBegin >= 0 && wpEnd >= 0 && wpEnd <= wpList.length()) {
                wpList = wpList.substring(wpBegin + 7, wpEnd);
            }

            final String[] wpItems = wpList.split("<tr");

            String[] wp;
            for (int j = 1; j < wpItems.length; j++) {
                final Waypoint waypoint = new Waypoint();

                wp = wpItems[j].split("<td");

                // waypoint type
                try {
                    final Matcher matcherWpType = patternWpType.matcher(wp[3]);
                    while (matcherWpType.find()) {
                        if (matcherWpType.groupCount() > 0) {
                            waypoint.type = matcherWpType.group(1);
                            if (waypoint.type != null) {
                                waypoint.type = waypoint.type.trim();
                            }
                        }
                    }
                } catch (Exception e) {
                    // failed to parse type
                    Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse waypoint type");
                }

                // waypoint prefix
                try {
                    final Matcher matcherWpPrefix = patternWpPrefixOrLookupOrLatlon.matcher(wp[4]);
                    while (matcherWpPrefix.find()) {
                        if (matcherWpPrefix.groupCount() > 1) {
                            waypoint.prefix = matcherWpPrefix.group(2);
                            if (waypoint.prefix != null) {
                                waypoint.prefix = waypoint.prefix.trim();
                            }
                        }
                    }
                } catch (Exception e) {
                    // failed to parse prefix
                    Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse waypoint prefix");
                }

                // waypoint lookup
                try {
                    final Matcher matcherWpLookup = patternWpPrefixOrLookupOrLatlon.matcher(wp[5]);
                    while (matcherWpLookup.find()) {
                        if (matcherWpLookup.groupCount() > 1) {
                            waypoint.lookup = matcherWpLookup.group(2);
                            if (waypoint.lookup != null) {
                                waypoint.lookup = waypoint.lookup.trim();
                            }
                        }
                    }
                } catch (Exception e) {
                    // failed to parse lookup
                    Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse waypoint lookup");
                }

                // waypoint name
                try {
                    final Matcher matcherWpName = patternWpName.matcher(wp[6]);
                    while (matcherWpName.find()) {
                        if (matcherWpName.groupCount() > 0) {
                            waypoint.name = matcherWpName.group(1);
                            if (waypoint.name != null) {
                                waypoint.name = waypoint.name.trim();
                            }
                        }
                    }
                } catch (Exception e) {
                    // failed to parse name
                    Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse waypoint name");
                }

                // waypoint latitude and logitude
                try {
                    final Matcher matcherWpLatLon = patternWpPrefixOrLookupOrLatlon.matcher(wp[7]);
                    while (matcherWpLatLon.find()) {
                        if (matcherWpLatLon.groupCount() > 1) {
                            waypoint.latlon = Html.fromHtml(matcherWpLatLon.group(2)).toString();

                            final HashMap<String, Object> tmp = this.parseLatlon(waypoint.latlon);
                            if (tmp.size() > 0) {
                                waypoint.latitude = (Double) tmp.get("latitude");
                                waypoint.longitude = (Double) tmp.get("longitude");
                                waypoint.latitudeString = (String) tmp.get("latitudeString");
                                waypoint.longitudeString = (String) tmp.get("longitudeString");
                            }
                        }
                    }
                } catch (Exception e) {
                    // failed to parse latitude and/or longitude
                    Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse waypoint coordinates");
                }

                j++;
                if (wpItems.length > j) {
                    wp = wpItems[j].split("<td");
                }

                // waypoint note
                try {
                    final Matcher matcherWpNote = patternWpNote.matcher(wp[3]);
                    while (matcherWpNote.find()) {
                        if (matcherWpNote.groupCount() > 0) {
                            waypoint.note = matcherWpNote.group(1);
                            if (waypoint.note != null) {
                                waypoint.note = waypoint.note.trim();
                            }
                        }
                    }
                } catch (Exception e) {
                    // failed to parse note
                    Log.w(Settings.tag, "cgeoBase.parseCache: Failed to parse waypoint note");
                }

                if (cache.waypoints == null)
                    cache.waypoints = new ArrayList<Waypoint>();
                cache.waypoints.add(waypoint);
            }
        }
    }

    if (cache.latitude != null && cache.longitude != null) {
        cache.elevation = getElevation(cache.latitude, cache.longitude);
    }

    cache.updated = System.currentTimeMillis();
    cache.detailedUpdate = System.currentTimeMillis();
    cache.detailed = true;
    caches.cacheList.add(cache);

    return caches;
}

From source file:carnero.cgeo.cgBase.java

public cgCacheWrap parseSearch(cgSearchThread thread, String url, String page, boolean showCaptcha) {
    if (page == null || page.length() == 0) {
        Log.e(cgSettings.tag, "cgeoBase.parseSearch: No page given");
        return null;
    }/* w  w w  .j a  v a 2s. co m*/

    final cgCacheWrap caches = new cgCacheWrap();
    final ArrayList<String> cids = new ArrayList<String>();
    final ArrayList<String> guids = new ArrayList<String>();
    String recaptchaChallenge = null;
    String recaptchaText = null;

    caches.url = url;

    final Pattern patternCacheType = Pattern.compile(
            "<td class=\"Merge\">[^<]*<a href=\"[^\"]*/seek/cache_details\\.aspx\\?guid=[^\"]+\"[^>]+>[^<]*<img src=\"[^\"]*/images/wpttypes/[^\\.]+\\.gif\" alt=\"([^\"]+)\" title=\"[^\"]+\"[^>]*>[^<]*</a>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternGuidAndDisabled = Pattern.compile(
            "<img src=\"[^\"]*/images/wpttypes/[^>]*>[^<]*</a></td><td class=\"Merge\">[^<]*<a href=\"[^\"]*/seek/cache_details\\.aspx\\?guid=([a-z0-9\\-]+)\" class=\"lnk([^\"]*)\">([^<]*<span>)?([^<]*)(</span>[^<]*)?</a>[^<]+<br />([^<]*)<span[^>]+>([^<]*)</span>([^<]*<img[^>]+>)?[^<]*<br />[^<]*</td>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternTbs = Pattern.compile(
            "<a id=\"ctl00_ContentBody_dlResults_ctl[0-9]+_uxTravelBugList\" class=\"tblist\" data-tbcount=\"([0-9]+)\" data-id=\"[^\"]*\"[^>]*>(.*)</a>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternTbsInside = Pattern.compile(
            "(<img src=\"[^\"]+\" alt=\"([^\"]+)\" title=\"[^\"]*\" />[^<]*)",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternDirection = Pattern.compile(
            "<img id=\"ctl00_ContentBody_dlResults_ctl[0-9]+_uxDistanceAndHeading\" title=\"[^\"]*\" src=\"[^\"]*/seek/CacheDir\\.ashx\\?k=([^\"]+)\"[^>]*>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternCode = Pattern.compile("\\|[^\\w]*(GC[a-z0-9]+)[^\\|]*\\|",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternId = Pattern.compile("name=\"CID\"[^v]*value=\"([0-9]+)\"", Pattern.CASE_INSENSITIVE);
    final Pattern patternFavourite = Pattern.compile(
            "<span id=\"ctl00_ContentBody_dlResults_ctl[0-9]+_uxFavoritesValue\" title=\"[^\"]*\" class=\"favorite-rank\">([0-9]+)</span>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternTotalCnt = Pattern.compile(
            "<td class=\"PageBuilderWidget\"><span>Total Records[^<]*<b>(\\d+)<\\/b>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternRecaptcha = Pattern.compile(
            "<script[^>]*src=\"[^\"]*/recaptcha/api/challenge\\?k=([^\"]+)\"[^>]*>", Pattern.CASE_INSENSITIVE);
    final Pattern patternRecaptchaChallenge = Pattern.compile("challenge : '([^']+)'",
            Pattern.CASE_INSENSITIVE);

    caches.viewstate = findViewstate(page, 0);
    caches.viewstate1 = findViewstate(page, 1);

    // recaptcha
    if (showCaptcha == true) {
        try {
            String recaptchaJsParam = null;
            final Matcher matcherRecaptcha = patternRecaptcha.matcher(page);
            while (matcherRecaptcha.find()) {
                if (matcherRecaptcha.groupCount() > 0) {
                    recaptchaJsParam = matcherRecaptcha.group(1);
                }
            }

            if (recaptchaJsParam != null) {
                final String recaptchaJs = request(false, "www.google.com", "/recaptcha/api/challenge", "GET",
                        "k=" + urlencode_rfc3986(recaptchaJsParam.trim()), 0, true).getData();

                if (recaptchaJs != null && recaptchaJs.length() > 0) {
                    final Matcher matcherRecaptchaChallenge = patternRecaptchaChallenge.matcher(recaptchaJs);
                    while (matcherRecaptchaChallenge.find()) {
                        if (matcherRecaptchaChallenge.groupCount() > 0) {
                            recaptchaChallenge = matcherRecaptchaChallenge.group(1).trim();
                        }
                    }
                }
            }
        } catch (Exception e) {
            // failed to parse recaptcha challenge
            Log.w(cgSettings.tag, "cgeoBase.parseSearch: Failed to parse recaptcha challenge");
        }

        if (thread != null && recaptchaChallenge != null && recaptchaChallenge.length() > 0) {
            thread.setChallenge(recaptchaChallenge);
            thread.notifyNeed();
        }
    }

    int startPos = -1;
    int endPos = -1;

    startPos = page.indexOf("<div id=\"ctl00_ContentBody_ResultsPanel\"");
    if (startPos == -1) {
        Log.e(cgSettings.tag, "cgeoBase.parseSearch: ID \"ctl00_ContentBody_dlResults\" not found on page");
        return null;
    }

    page = page.substring(startPos); // cut on <table

    startPos = page.indexOf(">");
    endPos = page.indexOf("ctl00_ContentBody_UnitTxt");
    if (startPos == -1 || endPos == -1) {
        Log.e(cgSettings.tag, "cgeoBase.parseSearch: ID \"ctl00_ContentBody_UnitTxt\" not found on page");
        return null;
    }

    page = page.substring(startPos + 1, endPos - startPos + 1); // cut between <table> and </table>

    final String[] rows = page.split("<tr class=");
    final int rows_count = rows.length;

    for (int z = 1; z < rows_count; z++) {
        cgCache cache = new cgCache();
        String row = rows[z];

        // check for cache type presence
        if (row.indexOf("images/wpttypes") == -1) {
            continue;
        }

        try {
            final Matcher matcherGuidAndDisabled = patternGuidAndDisabled.matcher(row);

            while (matcherGuidAndDisabled.find()) {
                if (matcherGuidAndDisabled.groupCount() > 0) {
                    guids.add(matcherGuidAndDisabled.group(1));

                    cache.guid = matcherGuidAndDisabled.group(1);
                    if (matcherGuidAndDisabled.group(4) != null) {
                        cache.name = Html.fromHtml(matcherGuidAndDisabled.group(4).trim()).toString();
                    }
                    if (matcherGuidAndDisabled.group(6) != null) {
                        cache.location = Html.fromHtml(matcherGuidAndDisabled.group(6).trim()).toString();
                    }

                    final String attr = matcherGuidAndDisabled.group(2);
                    if (attr != null) {
                        if (attr.contains("Strike") == true) {
                            cache.disabled = true;
                        } else {
                            cache.disabled = false;
                        }

                        if (attr.contains("OldWarning") == true) {
                            cache.archived = true;
                        } else {
                            cache.archived = false;
                        }
                    }
                }
            }
        } catch (Exception e) {
            // failed to parse GUID and/or Disabled
            Log.w(cgSettings.tag, "cgeoBase.parseSearch: Failed to parse GUID and/or Disabled data");
        }

        if (settings.excludeDisabled == 1 && (cache.disabled == true || cache.archived == true)) {
            // skip disabled and archived caches
            cache = null;
            continue;
        }

        String inventoryPre = null;

        // GC* code
        try {
            final Matcher matcherCode = patternCode.matcher(row);
            while (matcherCode.find()) {
                if (matcherCode.groupCount() > 0) {
                    cache.geocode = matcherCode.group(1).toUpperCase();
                }
            }
        } catch (Exception e) {
            // failed to parse code
            Log.w(cgSettings.tag, "cgeoBase.parseSearch: Failed to parse cache code");
        }

        // cache type
        try {
            final Matcher matcherCacheType = patternCacheType.matcher(row);
            while (matcherCacheType.find()) {
                if (matcherCacheType.groupCount() > 0) {
                    cache.type = cacheTypes.get(matcherCacheType.group(1).toLowerCase());
                }
            }
        } catch (Exception e) {
            // failed to parse type
            Log.w(cgSettings.tag, "cgeoBase.parseSearch: Failed to parse cache type");
        }

        // cache direction - image
        try {
            final Matcher matcherDirection = patternDirection.matcher(row);
            while (matcherDirection.find()) {
                if (matcherDirection.groupCount() > 0) {
                    cache.directionImg = matcherDirection.group(1);
                }
            }
        } catch (Exception e) {
            // failed to parse direction image
            Log.w(cgSettings.tag, "cgeoBase.parseSearch: Failed to parse cache direction image");
        }

        // cache inventory
        try {
            final Matcher matcherTbs = patternTbs.matcher(row);
            while (matcherTbs.find()) {
                if (matcherTbs.groupCount() > 0) {
                    cache.inventoryItems = Integer.parseInt(matcherTbs.group(1));
                    inventoryPre = matcherTbs.group(2);
                }
            }
        } catch (Exception e) {
            // failed to parse inventory
            Log.w(cgSettings.tag, "cgeoBase.parseSearch: Failed to parse cache inventory (1)");
        }

        if (inventoryPre != null && inventoryPre.trim().length() > 0) {
            try {
                final Matcher matcherTbsInside = patternTbsInside.matcher(inventoryPre);
                while (matcherTbsInside.find()) {
                    if (matcherTbsInside.groupCount() == 2 && matcherTbsInside.group(2) != null) {
                        final String inventoryItem = matcherTbsInside.group(2).toLowerCase();
                        if (inventoryItem.equals("premium member only cache")) {
                            continue;
                        } else {
                            if (cache.inventoryItems <= 0) {
                                cache.inventoryItems = 1;
                            }
                        }
                    }
                }
            } catch (Exception e) {
                // failed to parse cache inventory info
                Log.w(cgSettings.tag, "cgeoBase.parseSearch: Failed to parse cache inventory info");
            }
        }

        // premium cache
        if (row.indexOf("/images/small_profile.gif") != -1) {
            cache.members = true;
        } else {
            cache.members = false;
        }

        // found it
        if (row.indexOf("/images/icons/icon_smile.gif") != -1) {
            cache.found = true;
        } else {
            cache.found = false;
        }

        // own it
        if (row.indexOf("/images/silk/star.png") != -1) {
            cache.own = true;
        } else {
            cache.own = false;
        }

        // id
        try {
            final Matcher matcherId = patternId.matcher(row);
            while (matcherId.find()) {
                if (matcherId.groupCount() > 0) {
                    cache.cacheid = matcherId.group(1);
                    cids.add(cache.cacheid);
                }
            }
        } catch (Exception e) {
            // failed to parse cache id
            Log.w(cgSettings.tag, "cgeoBase.parseSearch: Failed to parse cache id");
        }

        // favourite count
        try {
            final Matcher matcherFavourite = patternFavourite.matcher(row);
            while (matcherFavourite.find()) {
                if (matcherFavourite.groupCount() > 0) {
                    cache.favouriteCnt = Integer.parseInt(matcherFavourite.group(1));
                }
            }
        } catch (Exception e) {
            // failed to parse favourite count
            Log.w(cgSettings.tag, "cgeoBase.parseSearch: Failed to parse favourite count");
        }

        if (cache.nameSp == null) {
            cache.nameSp = (new Spannable.Factory()).newSpannable(cache.name);
            if (cache.disabled == true || cache.archived == true) { // strike
                cache.nameSp.setSpan(new StrikethroughSpan(), 0, cache.nameSp.toString().length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }

        caches.cacheList.add(cache);
    }

    // total caches found
    try {
        final Matcher matcherTotalCnt = patternTotalCnt.matcher(page);
        while (matcherTotalCnt.find()) {
            if (matcherTotalCnt.groupCount() > 0) {
                if (matcherTotalCnt.group(1) != null) {
                    caches.totalCnt = new Integer(matcherTotalCnt.group(1));
                }
            }
        }
    } catch (Exception e) {
        // failed to parse cache count
        Log.w(cgSettings.tag, "cgeoBase.parseSearch: Failed to parse cache count");
    }

    if (thread != null && recaptchaChallenge != null) {
        if (thread.getText() == null) {
            thread.waitForUser();
        }

        recaptchaText = thread.getText();
    }

    if (cids.size() > 0 && (recaptchaChallenge == null
            || (recaptchaChallenge != null && recaptchaText != null && recaptchaText.length() > 0))) {
        Log.i(cgSettings.tag, "Trying to get .loc for " + cids.size() + " caches");

        try {
            // get coordinates for parsed caches
            final String host = "www.geocaching.com";
            final String path = "/seek/nearest.aspx";
            final StringBuilder params = new StringBuilder();
            params.append("__EVENTTARGET=");
            params.append("&");
            params.append("__EVENTARGUMENT=");
            params.append("&");
            params.append("__VIEWSTATE=");
            params.append(urlencode_rfc3986(caches.viewstate));
            if (caches.viewstate1 != null) {
                params.append("&");
                params.append("__VIEWSTATE1=");
                params.append(urlencode_rfc3986(caches.viewstate1));
                params.append("&");
                params.append("__VIEWSTATEFIELDCOUNT=2");
            }

            for (String cid : cids) {
                params.append("&");
                params.append("CID=");
                params.append(urlencode_rfc3986(cid));
            }

            if (recaptchaChallenge != null && recaptchaText != null && recaptchaText.length() > 0) {
                params.append("&");
                params.append("recaptcha_challenge_field=");
                params.append(urlencode_rfc3986(recaptchaChallenge));
                params.append("&");
                params.append("recaptcha_response_field=");
                params.append(urlencode_rfc3986(recaptchaText));
            }
            params.append("&");
            params.append("ctl00%24ContentBody%24uxDownloadLoc=Download+Waypoints");

            final String coordinates = request(false, host, path, "POST", params.toString(), 0, true).getData();

            if (coordinates != null && coordinates.length() > 0) {
                if (coordinates.indexOf(
                        "You have not agreed to the license agreement. The license agreement is required before you can start downloading GPX or LOC files from Geocaching.com") > -1) {
                    Log.i(cgSettings.tag,
                            "User has not agreed to the license agreement. Can\'t download .loc file.");

                    caches.error = errorRetrieve.get(-7);

                    return caches;
                }
            }

            if (coordinates != null && coordinates.length() > 0) {
                final HashMap<String, cgCoord> cidCoords = new HashMap<String, cgCoord>();
                final Pattern patternCidCode = Pattern.compile("name id=\"([^\"]+)\"");
                final Pattern patternCidLat = Pattern.compile("lat=\"([^\"]+)\"");
                final Pattern patternCidLon = Pattern.compile("lon=\"([^\"]+)\"");
                // premium only >>
                final Pattern patternCidDif = Pattern.compile("<difficulty>([^<]+)</difficulty>");
                final Pattern patternCidTer = Pattern.compile("<terrain>([^<]+)</terrain>");
                final Pattern patternCidCon = Pattern.compile("<container>([^<]+)</container>");
                // >> premium only

                final String[] points = coordinates.split("<waypoint>");

                // parse coordinates
                for (String point : points) {
                    final cgCoord pointCoord = new cgCoord();
                    final Matcher matcherCidCode = patternCidCode.matcher(point);
                    final Matcher matcherLatCode = patternCidLat.matcher(point);
                    final Matcher matcherLonCode = patternCidLon.matcher(point);
                    final Matcher matcherDifCode = patternCidDif.matcher(point);
                    final Matcher matcherTerCode = patternCidTer.matcher(point);
                    final Matcher matcherConCode = patternCidCon.matcher(point);
                    HashMap<String, Object> tmp = null;

                    if (matcherCidCode.find() == true) {
                        pointCoord.name = matcherCidCode.group(1).trim().toUpperCase();
                    }
                    if (matcherLatCode.find() == true) {
                        tmp = parseCoordinate(matcherLatCode.group(1), "lat");
                        pointCoord.latitude = (Double) tmp.get("coordinate");
                    }
                    if (matcherLonCode.find() == true) {
                        tmp = parseCoordinate(matcherLonCode.group(1), "lon");
                        pointCoord.longitude = (Double) tmp.get("coordinate");
                    }
                    if (matcherDifCode.find() == true) {
                        pointCoord.difficulty = new Float(matcherDifCode.group(1));
                    }
                    if (matcherTerCode.find() == true) {
                        pointCoord.terrain = new Float(matcherTerCode.group(1));
                    }
                    if (matcherConCode.find() == true) {
                        final int size = Integer.parseInt(matcherConCode.group(1));

                        if (size == 1) {
                            pointCoord.size = "not chosen";
                        } else if (size == 2) {
                            pointCoord.size = "micro";
                        } else if (size == 3) {
                            pointCoord.size = "regular";
                        } else if (size == 4) {
                            pointCoord.size = "large";
                        } else if (size == 5) {
                            pointCoord.size = "virtual";
                        } else if (size == 6) {
                            pointCoord.size = "other";
                        } else if (size == 8) {
                            pointCoord.size = "small";
                        } else {
                            pointCoord.size = "unknown";
                        }
                    }

                    cidCoords.put(pointCoord.name, pointCoord);
                }

                Log.i(cgSettings.tag, "Coordinates found in .loc file: " + cidCoords.size());

                // save found cache coordinates
                for (cgCache oneCache : caches.cacheList) {
                    if (cidCoords.containsKey(oneCache.geocode) == true) {
                        cgCoord thisCoords = cidCoords.get(oneCache.geocode);

                        oneCache.latitude = thisCoords.latitude;
                        oneCache.longitude = thisCoords.longitude;
                        oneCache.difficulty = thisCoords.difficulty;
                        oneCache.terrain = thisCoords.terrain;
                        oneCache.size = thisCoords.size;
                    }
                }
            }
        } catch (Exception e) {
            Log.e(cgSettings.tag, "cgBase.parseSearch.CIDs: " + e.toString());
        }
    }

    // get direction images
    cgDirectionImg dirImgDownloader = new cgDirectionImg(settings);
    for (cgCache oneCache : caches.cacheList) {
        if (oneCache.latitude == null && oneCache.longitude == null && oneCache.direction == null
                && oneCache.directionImg != null) {
            dirImgDownloader.getDrawable(oneCache.geocode, oneCache.directionImg);
        }
    }
    dirImgDownloader = null;

    // get ratings
    if (guids.size() > 0) {
        Log.i(cgSettings.tag, "Trying to get ratings for " + cids.size() + " caches");

        try {
            final HashMap<String, cgRating> ratings = getRating(guids, null);

            if (ratings != null) {
                // save found cache coordinates
                for (cgCache oneCache : caches.cacheList) {
                    if (ratings.containsKey(oneCache.guid) == true) {
                        cgRating thisRating = ratings.get(oneCache.guid);

                        oneCache.rating = thisRating.rating;
                        oneCache.votes = thisRating.votes;
                        oneCache.myVote = thisRating.myVote;
                    }
                }
            }
        } catch (Exception e) {
            Log.e(cgSettings.tag, "cgBase.parseSearch.GCvote: " + e.toString());
        }
    }

    return caches;
}

From source file:com.stratelia.webactiv.beans.admin.Admin.java

/**
 * Gets all the profile instances defined for the specified resource in the specified component
 * instance.//from w  w  w  .j  a  v  a2  s  . co m
 *
 * @param resourceId the unique idenifier of a resource managed in a component instance.
 * @param instanceId the unique identifier of the component instance.
 * @return a list of profile instances.
 */
private List<ProfileInst> getProfileInstsFor(String resourceId, String instanceId) throws AdminException {
    Pattern objectIdPattern = Pattern.compile("([a-zA-Z]+)(\\d+)");
    Matcher matcher = objectIdPattern.matcher(resourceId);
    if (matcher.matches() && matcher.groupCount() == 2) {
        String type = matcher.group(1);
        String id = matcher.group(2);
        return getProfilesByObject(id, type, instanceId);
    }
    throw new AdminPersistenceException("Admin.getProfileInstFor", SilverTrace.TRACE_LEVEL_ERROR,
            "Bad resource identifier: " + resourceId);
}

From source file:carnero.cgeo.cgBase.java

public cgCacheWrap parseCache(String page, int reason) {
    if (page == null || page.length() == 0) {
        Log.e(cgSettings.tag, "cgeoBase.parseCache: No page given");
        return null;
    }/*www  . ja  v  a2 s .co  m*/

    final Pattern patternGeocode = Pattern.compile(
            "<meta name=\"og:url\" content=\"[^\"]+/(GC[0-9A-Z]+)\"[^>]*>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternCacheId = Pattern.compile("/seek/log\\.aspx\\?ID=(\\d+)",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternCacheGuid = Pattern.compile(
            "<link rel=\"alternate\" href=\"[^\"]*/datastore/rss_galleryimages\\.ashx\\?guid=([0-9a-z\\-]+)\"[^>]*>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternType = Pattern.compile(
            "<img src=\"[^\"]*/WptTypes/\\d+\\.gif\" alt=\"([^\"]+)\" (title=\"[^\"]*\" )?width=\"32\" height=\"32\"[^>]*>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

    final Pattern patternName = Pattern.compile(
            "<h2[^>]*>[^<]*<span id=\"ctl00_ContentBody_CacheName\">([^<]+)<\\/span>[^<]*<\\/h2>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternSize = Pattern.compile(
            "<div class=\"CacheSize[^\"]*\">[^<]*<p[^>]*>[^S]*Size[^:]*:[^<]*<span[^>]*>[^<]*<img src=\"[^\"]*/icons/container/[a-z_]+\\.gif\" alt=\"Size: ([^\"]+)\"[^>]*>[^<]*<small>[^<]*</small>[^<]*</span>[^<]*</p>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternDifficulty = Pattern.compile(
            "<span id=\"ctl00_ContentBody_uxLegendScale\"[^>]*>[^<]*<img src=\"[^\"]*/images/stars/stars([0-9_]+)\\.gif\" alt=\"[^\"]+\"[^>]*>[^<]*</span>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternTerrain = Pattern.compile(
            "<span id=\"ctl00_ContentBody_Localize6\"[^>]*>[^<]*<img src=\"[^\"]*/images/stars/stars([0-9_]+)\\.gif\" alt=\"[^\"]+\"[^>]*>[^<]*</span>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternOwner = Pattern.compile(
            "<span class=\"minorCacheDetails\">[^\\w]*An?([^\\w]*Event)?[^\\w]*cache[^\\w]*by[^<]*<a href=\"[^\"]+\">([^<]+)</a>[^<]*</span>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternOwnerReal = Pattern.compile(
            "<a id=\"ctl00_ContentBody_uxFindLinksHiddenByThisUser\" href=\"[^\"]*/seek/nearest\\.aspx\\?u=*([^\"]+)\">[^<]+</a>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternHidden = Pattern.compile(
            "<span[^>]*>[^\\w]*Hidden[^:]*:[^\\d]*((\\d+)\\/(\\d+)\\/(\\d+))[^<]*</span>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternHiddenEvent = Pattern.compile(
            "<span[^>]*>[^\\w]*Event[^\\w]*Date[^:]*:[^\\w]*[a-zA-Z]+,[^\\d]*((\\d+)[^\\w]*(\\w+)[^\\d]*(\\d+))[^<]*</span>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternFavourite = Pattern.compile(
            "<a id=\"uxFavContainerLink\"[^>]*>[^<]*<div[^<]*<span class=\"favorite-value\">[^\\d]*([0-9]+)[^\\d^<]*</span>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

    final Pattern patternFound = Pattern.compile(
            "<p>[^<]*<a id=\"ctl00_ContentBody_hlFoundItLog\"[^<]*<img src=\".*/images/stockholm/16x16/check\\.gif\"[^>]*>[^<]*</a>[^<]*</p>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternLatLon = Pattern.compile(
            "<span id=\"ctl00_ContentBody_LatLon\"[^>]*>(<b>)?([^<]*)(<\\/b>)?<\\/span>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternLocation = Pattern.compile("<span id=\"ctl00_ContentBody_Location\"[^>]*>In ([^<]*)",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternHint = Pattern.compile(
            "<p>([^<]*<strong>)?[^\\w]*Additional Hints([^<]*<\\/strong>)?[^\\(]*\\(<a[^>]+>Encrypt</a>\\)[^<]*<\\/p>[^<]*<div id=\"div_hint\"[^>]*>(.*)</div>[^<]*<div id=[\\'|\"]dk[\\'|\"]",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternDescShort = Pattern.compile(
            "<div class=\"UserSuppliedContent\">[^<]*<span id=\"ctl00_ContentBody_ShortDescription\"[^>]*>((?:(?!</span>[^\\w^<]*</div>).)*)</span>[^\\w^<]*</div>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternDesc = Pattern.compile(
            "<div class=\"UserSuppliedContent\">[^<]*<span id=\"ctl00_ContentBody_LongDescription\"[^>]*>((?:(?!</span>[^\\w^<]*</div>).)*)</span>[^<]*</div>[^<]*<p>[^<]*</p>[^<]*<p>[^<]*<strong>[^\\w]*Additional Hints</strong>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternCountLogs = Pattern.compile(
            "<span id=\"ctl00_ContentBody_lblFindCounts\"><p>(.*)<\\/p><\\/span>", Pattern.CASE_INSENSITIVE);
    final Pattern patternCountLog = Pattern.compile(
            " src=\"\\/images\\/icons\\/([^\\.]*).gif\" alt=\"[^\"]*\" title=\"[^\"]*\" />([0-9]*)[^0-9]+",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternLogs = Pattern.compile(
            "<table class=\"LogsTable[^\"]*\"[^>]*>[^<]*<tr>(.*)</tr>[^<]*</table>[^<]*<p",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternLog = Pattern.compile(
            "<td[^>]*>[^<]*<strong>[^<]*<img src=\"[^\"]*/images/icons/([^\\.]+)\\.[a-z]{2,5}\"[^>]*>&nbsp;([a-zA-Z]+) (\\d+)(, (\\d+))? by <a href=[^>]+>([^<]+)</a>[<^]*</strong>([^\\(]*\\((\\d+) found\\))?(<br[^>]*>)+((?:(?!<small>).)*)(<br[^>]*>)+<small>",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    final Pattern patternAttributes = Pattern.compile(
            "<h3 class=\"WidgetHeader\">[^<]*<img[^>]+>[^\\w]*Attributes[^<]*</h3>[^<]*<div class=\"WidgetBody\">(([^<]*<img src=\"[^\"]+\" alt=\"[^\"]+\"[^>]*>)+)[^<]*<p",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternAttributesInside = Pattern.compile("[^<]*<img src=\"([^\"]+)\" alt=\"([^\"]+)\"[^>]*>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternSpoilers = Pattern.compile(
            "<span id=\"ctl00_ContentBody_Images\">((<a href=\"[^\"]+\"[^>]*>[^<]*<img[^>]+>[^<]*<span>[^>]+</span>[^<]*</a>[^<]*<br[^>]*>([^<]*(<br[^>]*>)+)?)+)[^<]*</span>",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternSpoilersInside = Pattern.compile(
            "[^<]*<a href=\"([^\"]+)\"[^>]*>[^<]*<img[^>]+>[^<]*<span>([^>]+)</span>[^<]*</a>[^<]*<br[^>]*>(([^<]*)(<br[^<]*>)+)?",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternInventory = Pattern.compile(
            "<span id=\"ctl00_ContentBody_uxTravelBugList_uxInventoryLabel\">[^\\w]*Inventory[^<]*</span>[^<]*</h3>[^<]*<div class=\"WidgetBody\">([^<]*<ul>(([^<]*<li>[^<]*<a href=\"[^\"]+\"[^>]*>[^<]*<img src=\"[^\"]+\"[^>]*>[^<]*<span>[^<]+<\\/span>[^<]*<\\/a>[^<]*<\\/li>)+)[^<]*<\\/ul>)?",
            Pattern.CASE_INSENSITIVE);
    final Pattern patternInventoryInside = Pattern.compile(
            "[^<]*<li>[^<]*<a href=\"[a-z0-9\\-\\_\\.\\?\\/\\:\\@]*\\/track\\/details\\.aspx\\?guid=([0-9a-z\\-]+)[^\"]*\"[^>]*>[^<]*<img src=\"[^\"]+\"[^>]*>[^<]*<span>([^<]+)<\\/span>[^<]*<\\/a>[^<]*<\\/li>",
            Pattern.CASE_INSENSITIVE);

    final cgCacheWrap caches = new cgCacheWrap();
    final cgCache cache = new cgCache();

    if (page.indexOf("Cache is Unpublished") > -1) {
        caches.error = "cache was unpublished";
        return caches;
    }

    if (page.indexOf("Sorry, the owner of this listing has made it viewable to Premium Members only.") != -1) {
        caches.error = "requested cache is for premium members only";
        return caches;
    }

    if (page.indexOf("has chosen to make this cache listing visible to Premium Members only.") != -1) {
        caches.error = "requested cache is for premium members only";
        return caches;
    }

    if (page.indexOf("<li>This cache is temporarily unavailable.") != -1) {
        cache.disabled = true;
    } else {
        cache.disabled = false;
    }

    if (page.indexOf("<li>This cache has been archived,") != -1) {
        cache.archived = true;
    } else {
        cache.archived = false;
    }

    if (page.indexOf("<p class=\"Warning\">This is a Premium Member Only cache.</p>") != -1) {
        cache.members = true;
    } else {
        cache.members = false;
    }

    cache.reason = reason;

    // cache geocode
    try {
        final Matcher matcherGeocode = patternGeocode.matcher(page);
        while (matcherGeocode.find()) {
            if (matcherGeocode.groupCount() > 0) {
                cache.geocode = (String) matcherGeocode.group(1);
            }
        }
    } catch (Exception e) {
        // failed to parse cache geocode
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache geocode");
    }

    // cache id
    try {
        final Matcher matcherCacheId = patternCacheId.matcher(page);
        while (matcherCacheId.find()) {
            if (matcherCacheId.groupCount() > 0) {
                cache.cacheid = (String) matcherCacheId.group(1);
            }
        }
    } catch (Exception e) {
        // failed to parse cache id
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache id");
    }

    // cache guid
    try {
        final Matcher matcherCacheGuid = patternCacheGuid.matcher(page);
        while (matcherCacheGuid.find()) {
            if (matcherCacheGuid.groupCount() > 0) {
                cache.guid = (String) matcherCacheGuid.group(1);
            }
        }
    } catch (Exception e) {
        // failed to parse cache guid
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache guid");
    }

    // name
    try {
        final Matcher matcherName = patternName.matcher(page);
        while (matcherName.find()) {
            if (matcherName.groupCount() > 0) {
                cache.name = Html.fromHtml(matcherName.group(1)).toString();
            }
        }
    } catch (Exception e) {
        // failed to parse cache name
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache name");
    }

    // owner real name
    try {
        final Matcher matcherOwnerReal = patternOwnerReal.matcher(page);
        while (matcherOwnerReal.find()) {
            if (matcherOwnerReal.groupCount() > 0) {
                cache.ownerReal = URLDecoder.decode(matcherOwnerReal.group(1));
            }
        }
    } catch (Exception e) {
        // failed to parse owner real name
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache owner real name");
    }

    final String username = settings.getUsername();
    if (cache.ownerReal != null && username != null && cache.ownerReal.equalsIgnoreCase(username)) {
        cache.own = true;
    }

    int pos = -1;
    String tableInside = page;

    pos = tableInside.indexOf("id=\"cacheDetails\"");
    if (pos == -1) {
        Log.e(cgSettings.tag, "cgeoBase.parseCache: ID \"cacheDetails\" not found on page");
        return null;
    }

    tableInside = tableInside.substring(pos);

    pos = tableInside.indexOf("<div class=\"CacheInformationTable\"");
    if (pos == -1) {
        Log.e(cgSettings.tag, "cgeoBase.parseCache: ID \"CacheInformationTable\" not found on page");
        return null;
    }

    tableInside = tableInside.substring(0, pos);

    if (tableInside != null && tableInside.length() > 0) {
        // cache terrain
        try {
            final Matcher matcherTerrain = patternTerrain.matcher(tableInside);
            while (matcherTerrain.find()) {
                if (matcherTerrain.groupCount() > 0) {
                    cache.terrain = new Float(
                            Pattern.compile("_").matcher(matcherTerrain.group(1)).replaceAll("."));
                }
            }
        } catch (Exception e) {
            // failed to parse terrain
            Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache terrain");
        }

        // cache difficulty
        try {
            final Matcher matcherDifficulty = patternDifficulty.matcher(tableInside);
            while (matcherDifficulty.find()) {
                if (matcherDifficulty.groupCount() > 0) {
                    cache.difficulty = new Float(
                            Pattern.compile("_").matcher(matcherDifficulty.group(1)).replaceAll("."));
                }
            }
        } catch (Exception e) {
            // failed to parse difficulty
            Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache difficulty");
        }

        // owner
        try {
            final Matcher matcherOwner = patternOwner.matcher(tableInside);
            while (matcherOwner.find()) {
                if (matcherOwner.groupCount() > 0) {
                    cache.owner = Html.fromHtml(matcherOwner.group(2)).toString();
                }
            }
        } catch (Exception e) {
            // failed to parse owner
            Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache owner");
        }

        // hidden
        try {
            final Matcher matcherHidden = patternHidden.matcher(tableInside);
            while (matcherHidden.find()) {
                if (matcherHidden.groupCount() > 0) {
                    cache.hidden = dateIn.parse(matcherHidden.group(1));
                }
            }
        } catch (Exception e) {
            // failed to parse cache hidden date
            Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache hidden date");
        }

        if (cache.hidden == null) {
            // event date
            try {
                final Matcher matcherHiddenEvent = patternHiddenEvent.matcher(tableInside);
                while (matcherHiddenEvent.find()) {
                    if (matcherHiddenEvent.groupCount() > 0) {
                        cache.hidden = dateEvIn.parse(matcherHiddenEvent.group(1));
                    }
                }
            } catch (Exception e) {
                // failed to parse cache event date
                Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache event date");
            }
        }

        // favourite
        try {
            final Matcher matcherFavourite = patternFavourite.matcher(tableInside);
            while (matcherFavourite.find()) {
                if (matcherFavourite.groupCount() > 0) {
                    cache.favouriteCnt = Integer.parseInt(matcherFavourite.group(1));
                }
            }
        } catch (Exception e) {
            // failed to parse favourite count
            Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse favourite count");
        }

        // cache size
        try {
            final Matcher matcherSize = patternSize.matcher(tableInside);
            while (matcherSize.find()) {
                if (matcherSize.groupCount() > 0) {
                    cache.size = matcherSize.group(1).toLowerCase();
                }
            }
        } catch (Exception e) {
            // failed to parse size
            Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache size");
        }
    }

    // cache found
    try {
        final Matcher matcherFound = patternFound.matcher(page);
        while (matcherFound.find()) {
            if (matcherFound.group() != null && matcherFound.group().length() > 0) {
                cache.found = true;
            }
        }
    } catch (Exception e) {
        // failed to parse found
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse found");
    }

    // cache type
    try {
        final Matcher matcherType = patternType.matcher(page);
        while (matcherType.find()) {
            if (matcherType.groupCount() > 0) {
                cache.type = cacheTypes.get(matcherType.group(1).toLowerCase());
            }
        }
    } catch (Exception e) {
        // failed to parse type
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache type");
    }

    // latitude and logitude
    try {
        final Matcher matcherLatLon = patternLatLon.matcher(page);
        while (matcherLatLon.find()) {
            if (matcherLatLon.groupCount() > 0) {
                cache.latlon = matcherLatLon.group(2); // first is <b>

                HashMap<String, Object> tmp = this.parseLatlon(cache.latlon);
                if (tmp.size() > 0) {
                    cache.latitude = (Double) tmp.get("latitude");
                    cache.longitude = (Double) tmp.get("longitude");
                    cache.latitudeString = (String) tmp.get("latitudeString");
                    cache.longitudeString = (String) tmp.get("longitudeString");
                    cache.reliableLatLon = true;
                }
                tmp = null;
            }
        }
    } catch (Exception e) {
        // failed to parse latitude and/or longitude
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache coordinates");
    }

    // cache location
    try {
        final Matcher matcherLocation = patternLocation.matcher(page);
        while (matcherLocation.find()) {
            if (matcherLocation.groupCount() > 0) {
                cache.location = matcherLocation.group(1);
            }
        }
    } catch (Exception e) {
        // failed to parse location
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache location");
    }

    // cache hint
    try {
        final Matcher matcherHint = patternHint.matcher(page);
        while (matcherHint.find()) {
            if (matcherHint.groupCount() > 2 && matcherHint.group(3) != null) {
                // replace linebreak and paragraph tags
                String hint = Pattern.compile("<(br|p)[^>]*>").matcher(matcherHint.group(3)).replaceAll("\n");
                if (hint != null) {
                    cache.hint = hint.replaceAll(Pattern.quote("</p>"), "").trim();
                }
            }
        }
    } catch (Exception e) {
        // failed to parse hint
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache hint");
    }

    /*
    // short info debug
    Log.d(cgSettings.tag, "gc-code: " + cache.geocode);
    Log.d(cgSettings.tag, "id: " + cache.cacheid);
    Log.d(cgSettings.tag, "guid: " + cache.guid);
    Log.d(cgSettings.tag, "name: " + cache.name);
    Log.d(cgSettings.tag, "terrain: " + cache.terrain);
    Log.d(cgSettings.tag, "difficulty: " + cache.difficulty);
    Log.d(cgSettings.tag, "owner: " + cache.owner);
    Log.d(cgSettings.tag, "owner (real): " + cache.ownerReal);
    Log.d(cgSettings.tag, "hidden: " + dateOutShort.format(cache.hidden));
    Log.d(cgSettings.tag, "favorite: " + cache.favouriteCnt);
    Log.d(cgSettings.tag, "size: " + cache.size);
    if (cache.found) {
       Log.d(cgSettings.tag, "found!");
    } else {
       Log.d(cgSettings.tag, "not found");
    }
    Log.d(cgSettings.tag, "type: " + cache.type);
    Log.d(cgSettings.tag, "latitude: " + String.format("%.6f", cache.latitude));
    Log.d(cgSettings.tag, "longitude: " + String.format("%.6f", cache.longitude));
    Log.d(cgSettings.tag, "location: " + cache.location);
    Log.d(cgSettings.tag, "hint: " + cache.hint);
    */

    // cache short description
    try {
        final Matcher matcherDescShort = patternDescShort.matcher(page);
        while (matcherDescShort.find()) {
            if (matcherDescShort.groupCount() > 0) {
                cache.shortdesc = matcherDescShort.group(1).trim();
            }
        }
    } catch (Exception e) {
        // failed to parse short description
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache short description");
    }

    // cache description
    try {
        final Matcher matcherDesc = patternDesc.matcher(page);
        while (matcherDesc.find()) {
            if (matcherDesc.groupCount() > 0) {
                cache.description = matcherDesc.group(1);
            }
        }
    } catch (Exception e) {
        // failed to parse short description
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache description");
    }

    // cache attributes
    try {
        final Matcher matcherAttributes = patternAttributes.matcher(page);
        while (matcherAttributes.find()) {
            if (matcherAttributes.groupCount() > 0) {
                final String attributesPre = matcherAttributes.group(1);
                final Matcher matcherAttributesInside = patternAttributesInside.matcher(attributesPre);

                while (matcherAttributesInside.find()) {
                    if (matcherAttributesInside.groupCount() > 1
                            && matcherAttributesInside.group(2).equalsIgnoreCase("blank") != true) {
                        if (cache.attributes == null) {
                            cache.attributes = new ArrayList<String>();
                        }
                        // by default, use the tooltip of the attribute
                        String attribute = matcherAttributesInside.group(2).toLowerCase();

                        // if the image name can be recognized, use the image name as attribute
                        String imageName = matcherAttributesInside.group(1).trim();
                        if (imageName.length() > 0) {
                            int start = imageName.lastIndexOf('/');
                            int end = imageName.lastIndexOf('.');
                            if (start >= 0 && end >= 0) {
                                attribute = imageName.substring(start + 1, end).replace('-', '_');
                            }
                        }
                        cache.attributes.add(attribute);
                    }
                }
            }
        }
    } catch (Exception e) {
        // failed to parse cache attributes
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache attributes");
    }

    // cache spoilers
    try {
        final Matcher matcherSpoilers = patternSpoilers.matcher(page);
        while (matcherSpoilers.find()) {
            if (matcherSpoilers.groupCount() > 0) {
                final String spoilersPre = matcherSpoilers.group(1);
                final Matcher matcherSpoilersInside = patternSpoilersInside.matcher(spoilersPre);

                while (matcherSpoilersInside.find()) {
                    if (matcherSpoilersInside.groupCount() > 0) {
                        final cgSpoiler spoiler = new cgSpoiler();
                        spoiler.url = matcherSpoilersInside.group(1);

                        if (matcherSpoilersInside.group(2) != null) {
                            spoiler.title = matcherSpoilersInside.group(2);
                        }
                        if (matcherSpoilersInside.group(4) != null) {
                            spoiler.description = matcherSpoilersInside.group(4);
                        }

                        if (cache.spoilers == null) {
                            cache.spoilers = new ArrayList<cgSpoiler>();
                        }
                        cache.spoilers.add(spoiler);
                    }
                }
            }
        }
    } catch (Exception e) {
        // failed to parse cache spoilers
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache spoilers");
    }

    // cache inventory
    try {
        cache.inventoryItems = 0;

        final Matcher matcherInventory = patternInventory.matcher(page);
        while (matcherInventory.find()) {
            if (cache.inventory == null) {
                cache.inventory = new ArrayList<cgTrackable>();
            }

            if (matcherInventory.groupCount() > 1) {
                final String inventoryPre = matcherInventory.group(2);

                if (inventoryPre != null && inventoryPre.length() > 0) {
                    final Matcher matcherInventoryInside = patternInventoryInside.matcher(inventoryPre);

                    while (matcherInventoryInside.find()) {
                        if (matcherInventoryInside.groupCount() > 0) {
                            final cgTrackable inventoryItem = new cgTrackable();
                            inventoryItem.guid = matcherInventoryInside.group(1);
                            inventoryItem.name = matcherInventoryInside.group(2);

                            cache.inventory.add(inventoryItem);
                            cache.inventoryItems++;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        // failed to parse cache inventory
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache inventory (2)");
    }

    // cache logs counts
    try {
        final Matcher matcherLogCounts = patternCountLogs.matcher(page);
        while (matcherLogCounts.find()) {
            if (matcherLogCounts.groupCount() > 0) {
                final String[] logs = matcherLogCounts.group(1).split("<img");
                final int logsCnt = logs.length;

                for (int k = 1; k < logsCnt; k++) {
                    Integer type = null;
                    Integer count = null;
                    final Matcher matcherLog = patternCountLog.matcher(logs[k]);

                    if (matcherLog.find()) {
                        String typeStr = matcherLog.group(1);
                        String countStr = matcherLog.group(2);
                        if (typeStr != null && typeStr.length() > 0) {
                            if (logTypes.containsKey(typeStr.toLowerCase()) == true) {
                                type = logTypes.get(typeStr.toLowerCase());
                            }
                        }
                        if (countStr != null && countStr.length() > 0) {
                            count = Integer.parseInt(countStr);
                        }
                        if (type != null && count != null) {
                            cache.logCounts.put(type, count);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        // failed to parse logs
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache log count");
    }

    // cache logs
    try {
        final Matcher matcherLogs = patternLogs.matcher(page);
        while (matcherLogs.find()) {
            if (matcherLogs.groupCount() > 0) {
                final String[] logs = matcherLogs.group(1).split("</tr><tr>");
                final int logsCnt = logs.length;

                for (int k = 0; k < logsCnt; k++) {
                    final Matcher matcherLog = patternLog.matcher(logs[k]);

                    if (matcherLog.find()) {
                        final cgLog logDone = new cgLog();

                        String logTmp = matcherLog.group(10);

                        int day = -1;
                        try {
                            day = Integer.parseInt(matcherLog.group(3));
                        } catch (Exception e) {
                            Log.w(cgSettings.tag, "Failed to parse logs date (day): " + e.toString());
                        }

                        int month = -1;
                        // January  | February  | March  | April  | May | June | July | August  | September | October  | November  | December
                        if (matcherLog.group(2).equalsIgnoreCase("January")) {
                            month = 0;
                        } else if (matcherLog.group(2).equalsIgnoreCase("February")) {
                            month = 1;
                        } else if (matcherLog.group(2).equalsIgnoreCase("March")) {
                            month = 2;
                        } else if (matcherLog.group(2).equalsIgnoreCase("April")) {
                            month = 3;
                        } else if (matcherLog.group(2).equalsIgnoreCase("May")) {
                            month = 4;
                        } else if (matcherLog.group(2).equalsIgnoreCase("June")) {
                            month = 5;
                        } else if (matcherLog.group(2).equalsIgnoreCase("July")) {
                            month = 6;
                        } else if (matcherLog.group(2).equalsIgnoreCase("August")) {
                            month = 7;
                        } else if (matcherLog.group(2).equalsIgnoreCase("September")) {
                            month = 8;
                        } else if (matcherLog.group(2).equalsIgnoreCase("October")) {
                            month = 9;
                        } else if (matcherLog.group(2).equalsIgnoreCase("November")) {
                            month = 10;
                        } else if (matcherLog.group(2).equalsIgnoreCase("December")) {
                            month = 11;
                        } else {
                            Log.w(cgSettings.tag, "Failed to parse logs date (month).");
                        }

                        int year = -1;
                        final String yearPre = matcherLog.group(5);

                        if (yearPre == null) {
                            Calendar date = Calendar.getInstance();
                            year = date.get(Calendar.YEAR);
                        } else {
                            try {
                                year = Integer.parseInt(matcherLog.group(5));
                            } catch (Exception e) {
                                Log.w(cgSettings.tag, "Failed to parse logs date (year): " + e.toString());
                            }
                        }

                        long logDate;
                        if (year > 0 && month >= 0 && day > 0) {
                            Calendar date = Calendar.getInstance();
                            date.set(year, month, day, 12, 0, 0);
                            logDate = date.getTimeInMillis();
                            logDate = (long) (Math.ceil(logDate / 1000)) * 1000;
                        } else {
                            logDate = 0;
                        }

                        if (logTypes.containsKey(matcherLog.group(1).toLowerCase()) == true) {
                            logDone.type = logTypes.get(matcherLog.group(1).toLowerCase());
                        } else {
                            logDone.type = logTypes.get("icon_note");
                        }

                        logDone.author = Html.fromHtml(matcherLog.group(6)).toString();
                        logDone.date = logDate;
                        if (matcherLog.group(8) != null) {
                            logDone.found = new Integer(matcherLog.group(8));
                        }
                        logDone.log = logTmp;

                        if (cache.logs == null) {
                            cache.logs = new ArrayList<cgLog>();
                        }
                        cache.logs.add(logDone);
                    }
                }
            }
        }
    } catch (Exception e) {
        // failed to parse logs
        Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse cache logs");
    }

    int wpBegin = 0;
    int wpEnd = 0;

    wpBegin = page.indexOf("<table class=\"Table\" id=\"ctl00_ContentBody_Waypoints\">");
    if (wpBegin != -1) { // parse waypoints
        final Pattern patternWpType = Pattern.compile("\\/wpttypes\\/sm\\/(.+)\\.jpg",
                Pattern.CASE_INSENSITIVE);
        final Pattern patternWpPrefixOrLookupOrLatlon = Pattern
                .compile(">([^<]*<[^>]+>)?([^<]+)(<[^>]+>[^<]*)?<\\/td>", Pattern.CASE_INSENSITIVE);
        final Pattern patternWpName = Pattern.compile(">[^<]*<a[^>]+>([^<]*)<\\/a>", Pattern.CASE_INSENSITIVE);
        final Pattern patternWpNote = Pattern.compile("colspan=\"6\">(.*)<\\/td>", Pattern.CASE_INSENSITIVE);

        String wpList = page.substring(wpBegin);

        wpEnd = wpList.indexOf("</p>");
        if (wpEnd > -1 && wpEnd <= wpList.length()) {
            wpList = wpList.substring(0, wpEnd);
        }

        if (wpList.indexOf("No additional waypoints to display.") == -1) {
            wpEnd = wpList.indexOf("</table>");
            wpList = wpList.substring(0, wpEnd);

            wpBegin = wpList.indexOf("<tbody>");
            wpEnd = wpList.indexOf("</tbody>");
            if (wpBegin >= 0 && wpEnd >= 0 && wpEnd <= wpList.length()) {
                wpList = wpList.substring(wpBegin + 7, wpEnd);
            }

            final String[] wpItems = wpList.split("<tr");

            String[] wp;
            for (int j = 1; j < wpItems.length; j++) {
                final cgWaypoint waypoint = new cgWaypoint();

                wp = wpItems[j].split("<td");

                // waypoint type
                try {
                    final Matcher matcherWpType = patternWpType.matcher(wp[3]);
                    while (matcherWpType.find()) {
                        if (matcherWpType.groupCount() > 0) {
                            waypoint.type = matcherWpType.group(1);
                            if (waypoint.type != null) {
                                waypoint.type = waypoint.type.trim();
                            }
                        }
                    }
                } catch (Exception e) {
                    // failed to parse type
                    Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse waypoint type");
                }

                // waypoint prefix
                try {
                    final Matcher matcherWpPrefix = patternWpPrefixOrLookupOrLatlon.matcher(wp[4]);
                    while (matcherWpPrefix.find()) {
                        if (matcherWpPrefix.groupCount() > 1) {
                            waypoint.prefix = matcherWpPrefix.group(2);
                            if (waypoint.prefix != null) {
                                waypoint.prefix = waypoint.prefix.trim();
                            }
                        }
                    }
                } catch (Exception e) {
                    // failed to parse prefix
                    Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse waypoint prefix");
                }

                // waypoint lookup
                try {
                    final Matcher matcherWpLookup = patternWpPrefixOrLookupOrLatlon.matcher(wp[5]);
                    while (matcherWpLookup.find()) {
                        if (matcherWpLookup.groupCount() > 1) {
                            waypoint.lookup = matcherWpLookup.group(2);
                            if (waypoint.lookup != null) {
                                waypoint.lookup = waypoint.lookup.trim();
                            }
                        }
                    }
                } catch (Exception e) {
                    // failed to parse lookup
                    Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse waypoint lookup");
                }

                // waypoint name
                try {
                    final Matcher matcherWpName = patternWpName.matcher(wp[6]);
                    while (matcherWpName.find()) {
                        if (matcherWpName.groupCount() > 0) {
                            waypoint.name = matcherWpName.group(1);
                            if (waypoint.name != null) {
                                waypoint.name = waypoint.name.trim();
                            }
                        }
                    }
                } catch (Exception e) {
                    // failed to parse name
                    Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse waypoint name");
                }

                // waypoint latitude and logitude
                try {
                    final Matcher matcherWpLatLon = patternWpPrefixOrLookupOrLatlon.matcher(wp[7]);
                    while (matcherWpLatLon.find()) {
                        if (matcherWpLatLon.groupCount() > 1) {
                            waypoint.latlon = Html.fromHtml(matcherWpLatLon.group(2)).toString();

                            final HashMap<String, Object> tmp = this.parseLatlon(waypoint.latlon);
                            if (tmp.size() > 0) {
                                waypoint.latitude = (Double) tmp.get("latitude");
                                waypoint.longitude = (Double) tmp.get("longitude");
                                waypoint.latitudeString = (String) tmp.get("latitudeString");
                                waypoint.longitudeString = (String) tmp.get("longitudeString");
                            }
                        }
                    }
                } catch (Exception e) {
                    // failed to parse latitude and/or longitude
                    Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse waypoint coordinates");
                }

                j++;
                if (wpItems.length > j) {
                    wp = wpItems[j].split("<td");
                }

                // waypoint note
                try {
                    final Matcher matcherWpNote = patternWpNote.matcher(wp[3]);
                    while (matcherWpNote.find()) {
                        if (matcherWpNote.groupCount() > 0) {
                            waypoint.note = matcherWpNote.group(1);
                            if (waypoint.note != null) {
                                waypoint.note = waypoint.note.trim();
                            }
                        }
                    }
                } catch (Exception e) {
                    // failed to parse note
                    Log.w(cgSettings.tag, "cgeoBase.parseCache: Failed to parse waypoint note");
                }

                if (cache.waypoints == null)
                    cache.waypoints = new ArrayList<cgWaypoint>();
                cache.waypoints.add(waypoint);
            }
        }
    }

    if (cache.latitude != null && cache.longitude != null) {
        cache.elevation = getElevation(cache.latitude, cache.longitude);
    }

    final cgRating rating = getRating(cache.guid, cache.geocode);
    if (rating != null) {
        cache.rating = rating.rating;
        cache.votes = rating.votes;
        cache.myVote = rating.myVote;
    }

    cache.updated = System.currentTimeMillis();
    cache.detailedUpdate = System.currentTimeMillis();
    cache.detailed = true;
    caches.cacheList.add(cache);

    return caches;
}

From source file:com.processing.core.PApplet.java

/**
 * Match a string with a regular expression, and returns the match as an
 * array. The first index is the matching expression, and array elements
 * [1] and higher represent each of the groups (sequences found in parens).
 *
 * This uses multiline matching (Pattern.MULTILINE) and dotall mode
 * (Pattern.DOTALL) by default, so that ^ and $ match the beginning and
 * end of any lines found in the source, and the . operator will also
 * pick up newline characters./*from   w  w w  .ja  v a2  s .com*/
 */
static public String[] match(String what, String regexp) {
    Pattern p = matchPattern(regexp);
    Matcher m = p.matcher(what);
    if (m.find()) {
        int count = m.groupCount() + 1;
        String[] groups = new String[count];
        for (int i = 0; i < count; i++) {
            groups[i] = m.group(i);
        }
        return groups;
    }
    return null;
}

From source file:com.processing.core.PApplet.java

/**
 * Identical to match(), except that it returns an array of all matches in
 * the specified String, rather than just the first.
 *//*from  www.  j  a va  2s. c o m*/
static public String[][] matchAll(String what, String regexp) {
    Pattern p = matchPattern(regexp);
    Matcher m = p.matcher(what);
    ArrayList<String[]> results = new ArrayList<String[]>();
    int count = m.groupCount() + 1;
    while (m.find()) {
        String[] groups = new String[count];
        for (int i = 0; i < count; i++) {
            groups[i] = m.group(i);
        }
        results.add(groups);
    }
    if (results.isEmpty()) {
        return null;
    }
    String[][] matches = new String[results.size()][count];
    for (int i = 0; i < matches.length; i++) {
        matches[i] = (String[]) results.get(i);
    }
    return matches;
}