Android Open Source - JayJayLab-Android-Demo G P X Parser






From Project

Back to project page JayJayLab-Android-Demo.

License

The source code is released under:

Apache License

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

Java Source Code

package com.jayjaylab.androiddemo.app.greyhound.util;
//  w ww.  j  a va  2  s  .  c  om
import android.location.Location;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import roboguice.util.Ln;

/**
 * Created by jongjoo on 1/10/15.
 */
public class GPXParser {
    private static final String PROVIDER = "jayjaylab";

    private static Location currentLocation;
    private static List<Location> list;

    /**
     * Parses the given gpx file in <var>absolutePath</var>
     * @param absolutePath an absolute path of gpx file
     * @return List a list storing Location objects
     */
    public static List<Location> parse(String absolutePath) {
        if(absolutePath == null || !new File(absolutePath).exists()) {
            return null;
        }

        if(list == null) {
            list = new ArrayList<Location>(40);
        } else {
            list.clear();
        }

        SAXParserFactory saxFactory = SAXParserFactory.newInstance();
        try {
            InputStream xmlInput = new FileInputStream(absolutePath);
            SAXParser saxParser = saxFactory.newSAXParser();
            saxParser.parse(xmlInput, handler);
        } catch(Exception e) {
            Ln.e(e);
        }

        return list;
    }

    private static final DefaultHandler handler = new DefaultHandler() {
        @Override
        public void startDocument() throws SAXException {
            Ln.d("startDocument()");
        }

        @Override
        public void endDocument() throws SAXException {
            Ln.d("endDocument()");
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            Ln.d("endElement() : qName : %s", qName);

            if(qName.equals("trkpt")) {
                list.add(currentLocation);
                currentLocation = null;
            }
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            Ln.d("startElement() : qName : %s", qName);
            if(qName.equals("trkpt")) {
                currentLocation = new Location(PROVIDER);
                currentLocation.setLatitude(Double.valueOf(attributes.getValue("lat")));
                currentLocation.setLongitude(Double.valueOf(attributes.getValue("lon")));
            }
        }

        @Override
        public void error(SAXParseException e) throws SAXException {
            Ln.d("error()");
        }

        @Override
        public void fatalError(SAXParseException e) throws SAXException {
            Ln.d("fatalError()");
        }

        @Override
        public void warning(SAXParseException e) throws SAXException {
            Ln.d("warning()");
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            Ln.d("characters()");
        }
    };
}




Java Source Code List

com.jayjaylab.androiddemo.ActivityIntroTest.java
com.jayjaylab.androiddemo.Application.java
com.jayjaylab.androiddemo.DaoMaster.java
com.jayjaylab.androiddemo.DaoSession.java
com.jayjaylab.androiddemo.PathDao.java
com.jayjaylab.androiddemo.Path.java
com.jayjaylab.androiddemo.app.greyhound.activity.ActivityMain.java
com.jayjaylab.androiddemo.app.greyhound.activity.ActivityMap.java
com.jayjaylab.androiddemo.app.greyhound.adapter.AdapterPathHistory.java
com.jayjaylab.androiddemo.app.greyhound.event.OnGPXParsingCompleteEvent.java
com.jayjaylab.androiddemo.app.greyhound.event.OnReceiveResultEvent.java
com.jayjaylab.androiddemo.app.greyhound.fragment.FragmentPathHistory.java
com.jayjaylab.androiddemo.app.greyhound.model.Path.java
com.jayjaylab.androiddemo.app.greyhound.service.ServiceRecordingPath.java
com.jayjaylab.androiddemo.app.greyhound.util.Constants.java
com.jayjaylab.androiddemo.app.greyhound.util.GPXParser.java
com.jayjaylab.androiddemo.app.greyhound.util.GPXWriter.java
com.jayjaylab.androiddemo.app.greyhound.util.MyResultReceiver.java
com.jayjaylab.androiddemo.app.greyhound.util.PreferenceHelper.java
com.jayjaylab.androiddemo.dialog.ProgressDialog.java
com.jayjaylab.androiddemo.event.OnClickEvent.java
com.jayjaylab.androiddemo.event.OnLongClickEvent.java
com.jayjaylab.androiddemo.event.ProgressBarEvent.java
com.jayjaylab.androiddemo.main.activity.ActivityBase.java
com.jayjaylab.androiddemo.main.activity.ActivityIntro.java
com.jayjaylab.androiddemo.main.activity.ActivityMain.java
com.jayjaylab.androiddemo.main.adapter.AdapterMain.java
com.jayjaylab.androiddemo.main.model.App.java
com.jayjaylab.androiddemo.util.AndroidHelper.java
com.jayjaylab.androiddemo.util.NIOHelper.java
com.jayjaylab.androiddemo.view.BubbleViewMaker.java
com.jayjaylab.androiddemo.view.CardView.java
com.jayjaylab.androiddemo.view.ImageViewThreadPool.java
com.jayjaylab.androiddemo.view.ViewThreadPool.java
de.greenrobot.daogenerator.gentest.ExampleDaoGenerator.java