Code example for RootElement

Methods: getContentHandler

0
 
        final CacheHolder cacheHolder = new CacheHolder();
        final CacheLog logHolder = new CacheLog();
        final CacheDescription descHolder = new CacheDescription();
 
        final RootElement root = new RootElement("oc11xml");
        final Element cacheNode = root.getChild("cache");
 
        final boolean ignoreFilters = ignoreFiltersIn;
 
        // cache 
        cacheNode.setStartElementListener(new StartElementListener() {
 
            @Override 
            public void start(Attributes attributes) {
                resetCache(cacheHolder);
            } 
 
        }); 
 
        cacheNode.setEndElementListener(new EndElementListener() {
 
            @Override 
            public void end() { 
                Geocache cache = cacheHolder.cache;
                Geopoint coords = new Geopoint(cacheHolder.latitude, cacheHolder.longitude);
                cache.setCoords(coords);
                if (caches.size() < CACHE_PARSE_LIMIT && isValid(cache) && (ignoreFilters || !isExcluded(cache))) {
                    cache.setDetailedUpdatedNow();
                    caches.put(cache.getCacheId(), cache);
                } 
            } 
 
            private boolean isExcluded(Geocache cache) {
                if (cache.isArchived()) {
                    return true; 
                } 
                if (cache.isDisabled() && Settings.isExcludeDisabledCaches()) {
                    return true; 
                } 
                if ((cache.isFound() || cache.isOwner()) && Settings.isExcludeMyCaches()) {
                    return true; 
                } 
                return !Settings.getCacheType().contains(cache);
            } 
 
            private boolean isValid(Geocache cache) {
                return StringUtils.isNotBlank(cache.getGeocode()) && !cache.getCoords().equals(Geopoint.ZERO);
            } 
        }); 
 
        // cache.id 
        cacheNode.getChild("id").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                cacheHolder.cache.setCacheId(body);
            } 
        }); 
 
        // cache.longitude 
        cacheNode.getChild("longitude").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                String longitude = body.trim();
                if (StringUtils.isNotBlank(longitude)) {
                    cacheHolder.longitude = longitude;
                } 
            } 
        }); 
 
        // cache.latitude 
        cacheNode.getChild("latitude").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                String latitude = body.trim();
                if (StringUtils.isNotBlank(latitude)) {
                    cacheHolder.latitude = latitude;
                } 
            } 
        }); 
 
        // cache.name 
        cacheNode.getChild("name").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                final String content = body.trim();
                cacheHolder.cache.setName(content);
            } 
        }); 
 
        // cache.waypoints[oc] 
        cacheNode.getChild("waypoints").setStartElementListener(new StartElementListener() {
 
            @Override 
            public void start(Attributes attrs) {
                if (attrs.getIndex("oc") > -1) {
                    cacheHolder.cache.setGeocode(attrs.getValue("oc"));
                } 
                if (attrs.getIndex("gccom") > -1) {
                    String gccode = attrs.getValue("gccom");
                    if (!StringUtils.isBlank(gccode)) {
                        cacheHolder.cache.setDescription(res.getString(R.string.cache_listed_on, GCConnector.getInstance().getName()) + ": <a href=\"http://coord.info/" + gccode + "\">" + gccode + "</a><br /><br />");
                    } 
                } 
            } 
        }); 
 
        // cache.type[id] 
        cacheNode.getChild("type").setStartElementListener(new StartElementListener() {
 
            @Override 
            public void start(Attributes attrs) {
                if (attrs.getIndex("id") > -1) {
                    final String typeId = attrs.getValue("id");
                    cacheHolder.cache.setType(getCacheType(typeId));
                } 
            } 
        }); 
 
        // cache.status[id] 
        cacheNode.getChild("status").setStartElementListener(new StartElementListener() {
 
            @Override 
            public void start(Attributes attrs) {
                if (attrs.getIndex("id") > -1) {
                    try { 
                        final int statusId = Integer.parseInt(attrs.getValue("id"));
                        setCacheStatus(statusId, cacheHolder.cache);
                    } catch (NumberFormatException e) {
                        Log.w(String.format("Failed to parse status of cache '%s'.", cacheHolder.cache.getGeocode()));
                    } 
                } 
            } 
        }); 
 
        // cache.size[id] 
        cacheNode.getChild("size").setStartElementListener(new StartElementListener() {
 
            @Override 
            public void start(Attributes attrs) {
                if (attrs.getIndex("id") > -1) {
                    final String typeId = attrs.getValue("id");
                    cacheHolder.cache.setSize(getCacheSize(typeId));
                } 
            } 
        }); 
 
        // cache.difficulty 
        cacheNode.getChild("difficulty").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                final String content = body.trim();
                try { 
                    cacheHolder.cache.setDifficulty(Float.valueOf(content));
                } catch (NumberFormatException e) {
                    Log.e("OC11XMLParser: unknown difficulty " + content, e);
                } 
            } 
        }); 
 
        // cache.terrain 
        cacheNode.getChild("terrain").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                final String content = body.trim();
                try { 
                    cacheHolder.cache.setTerrain(Float.valueOf(content));
                } catch (NumberFormatException e) {
                    Log.e("OC11XMLParser: unknown terrain " + content, e);
                } 
            } 
        }); 
 
        // cache.datehidden 
        cacheNode.getChild("datehidden").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                final String content = body.trim();
                cacheHolder.cache.setHidden(parseFullDate(content));
            } 
        }); 
 
        // cache.userid 
        final Element useridNode = cacheNode.getChild("userid");
 
        useridNode.setStartElementListener(new StartElementListener() {
 
            @Override 
            public void start(Attributes attributes) {
                if (attributes.getIndex("id") > -1) {
                    cacheHolder.cache.setOwnerUserId(attributes.getValue("id"));
                } 
            } 
        }); 
 
        useridNode.setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                cacheHolder.cache.setOwnerDisplayName(body);
            } 
        }); 
 
        // cache.attributes.attribute 
        final Element attributeNode = cacheNode.getChild("attributes").getChild("attribute");
 
        attributeNode.setStartElementListener(new StartElementListener() {
 
            @Override 
            public void start(Attributes attributes) {
                if (attributes.getIndex("id") > -1) {
                    try { 
                        attributeId = Integer.parseInt(attributes.getValue("id"));
                    } catch (NumberFormatException e) {
                        Log.w(String.format("Failed to parse attribute id of cache '%s'.", cacheHolder.cache.getGeocode()));
                    } 
                } 
            } 
        }); 
 
        attributeNode.setEndTextElementListener(new EndTextElementListener() {
            @Override 
            public void end(String body) {
                CacheAttribute attribute = CacheAttribute.getByOcId(attributeId);
                if (attribute != null) {
                    // semantic of attributes on opencaching is always "yes" 
                    cacheHolder.cache.getAttributes().add(attribute.getAttributeName(true));
                } 
                else { 
                    if (StringUtils.isNotBlank(body)) {
                        cacheHolder.cache.getAttributes().add(body.trim());
                    } 
                } 
            } 
        }); 
 
        // cachedesc 
        final Element cacheDesc = root.getChild("cachedesc");
 
        cacheDesc.setStartElementListener(new StartElementListener() {
 
            @Override 
            public void start(Attributes attributes) {
                resetDesc(descHolder);
            } 
        }); 
 
        cacheDesc.setEndElementListener(new EndElementListener() {
 
            @Override 
            public void end() { 
                final Geocache cache = caches.get(descHolder.cacheId);
                if (cache != null) {
                    cache.setShortDescription(descHolder.shortDesc);
                    cache.setDescription(cache.getDescription() + descHolder.desc);
                    cache.setHint(descHolder.hint);
                } 
            } 
        }); 
 
        // cachedesc.cacheid 
        cacheDesc.getChild("cacheid").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                descHolder.cacheId = body;
            } 
        }); 
 
        // cachedesc.desc 
        cacheDesc.getChild("shortdesc").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                final String content = body.trim();
                descHolder.shortDesc = linkify(stripMarkup(content));
            } 
        }); 
 
        // cachedesc.desc 
        cacheDesc.getChild("desc").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                final String content = body.trim();
                descHolder.desc = linkify(stripMarkup(content));
            } 
        }); 
 
        // cachedesc.hint 
        cacheDesc.getChild("hint").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                descHolder.hint = body.trim();
            } 
        }); 
 
        // cachelog 
        final Element cacheLog = root.getChild("cachelog");
 
        cacheLog.setStartElementListener(new StartElementListener() {
 
            @Override 
            public void start(Attributes attrs) {
                resetLog(logHolder);
            } 
        }); 
 
        cacheLog.setEndElementListener(new EndElementListener() {
 
            @Override 
            public void end() { 
                final Geocache cache = caches.get(logHolder.cacheId);
                if (cache != null && logHolder.logEntry.type != LogType.UNKNOWN) {
                    logs.put(logHolder.id, logHolder.logEntry);
                    cache.getLogs().add(0, logHolder.logEntry);
                    if ((logHolder.logEntry.type == LogType.FOUND_IT || logHolder.logEntry.type == LogType.ATTENDED)
                            && StringUtils.equalsIgnoreCase(logHolder.logEntry.author, Settings.getOCConnectorUserName())) {
                        cache.setFound(true);
                        cache.setVisitedDate(logHolder.logEntry.date);
                    } 
                } 
            } 
        }); 
 
        // cachelog.id 
        cacheLog.getChild("id").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                logHolder.id = StringUtils.trim(body);
            } 
        }); 
 
        // cachelog.cacheid 
        cacheLog.getChild("cacheid").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                logHolder.cacheId = body;
            } 
        }); 
 
        // cachelog.date 
        cacheLog.getChild("date").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                try { 
                    logHolder.logEntry.date = parseDayDate(body).getTime();
                } catch (NullPointerException e) {
                    Log.w("Failed to parse log date", e);
                } 
            } 
        }); 
 
        // cachelog.logtype 
        cacheLog.getChild("logtype").setStartElementListener(new StartElementListener() {
 
            @Override 
            public void start(Attributes attrs) {
                if (attrs.getIndex("id") > -1) {
                    final String id = attrs.getValue("id");
                    try { 
                        final int typeId = Integer.parseInt(id);
                        logHolder.logEntry.type = getLogType(typeId);
                    } catch (NumberFormatException e) {
                        Log.e("OC11XMLParser, unknown logtype " + id, e);
                    } 
                } 
            } 
        }); 
 
        // cachelog.userid 
        cacheLog.getChild("userid").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String finderName) {
                logHolder.logEntry.author = finderName;
            } 
        }); 
 
        // cachelog.text 
        cacheLog.getChild("text").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String logText) {
                logHolder.logEntry.log = stripMarkup(logText);
            } 
        }); 
 
        // pictures 
        final Element picture = root.getChild("picture");
 
        picture.setStartElementListener(new StartElementListener() {
 
            @Override 
            public void start(Attributes attrs) {
                imageHolder = new ImageHolder();
            } 
        }); 
 
        picture.setEndElementListener(new EndElementListener() {
 
            @Override 
            public void end() { 
                if (imageHolder.isSpoiler) {
                    final Geocache cache = caches.get(imageHolder.objectId);
                    if (cache != null) {
                        Image spoiler = new Image(imageHolder.url, imageHolder.title);
                        cache.addSpoiler(spoiler);
                    } 
                } 
                else { 
                    final LogEntry log = logs.get(imageHolder.objectId);
                    if (log != null) {
                        log.addLogImage(new Image(imageHolder.url, imageHolder.title));
                    } 
                } 
            } 
        }); 
 
        // picture.object 
        picture.getChild("object").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                imageHolder.objectId = StringUtils.trim(body);
            } 
        }); 
 
        // picture.title 
        picture.getChild("title").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                imageHolder.title = StringUtils.trim(body);
            } 
        }); 
 
        // picture.url 
        picture.getChild("url").setEndTextElementListener(new EndTextElementListener() {
 
            @Override 
            public void end(String body) {
                imageHolder.url = StringUtils.trim(body);
            } 
        }); 
 
        // picture.attributes 
        picture.getChild("attributes").setStartElementListener(new StartElementListener() {
 
            @Override 
            public void start(Attributes attributes) {
                if (attributes.getIndex("spoiler") > -1) {
                    String spoiler = attributes.getValue("spoiler");
                    imageHolder.isSpoiler = ("1".equals(spoiler));
                } 
            } 
        }); 
 
        try { 
            Xml.parse(stream, Xml.Encoding.UTF_8, root.getContentHandler());
            return caches.values();
        } catch (SAXException e) {
            Log.e("Cannot parse .gpx file as oc11xml: could not parse XML", e);
            return null; 
        }