All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.java.de.k3b.geo.io.gpx.GeoXmlOrTextParser Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2015-2016 by k3b.
 *
 * This file is part of k3b-geoHelper library.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.k3b.geo.io.gpx;

import org.xml.sax.InputSource;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import de.k3b.geo.api.GeoPointDto;
import de.k3b.geo.api.IGeoPointInfo;
import de.k3b.geo.io.GeoFileRepository;
import de.k3b.geo.io.GeoUri;

/**
 * Parser to extract {@link IGeoPointInfo} items from {@link String}.
 *
 * Created by k3b on 20.04.2015.
 */
public class GeoXmlOrTextParser {

    /** Get {@link IGeoPointInfo} items from cr/lf delimited Text lines or from xml fragments. */
    public List get(String textLinesOrXml) {
        return get(new GeoPointDto(), textLinesOrXml);
    }

    /** Get {@link IGeoPointInfo} items from cr/lf delimited Text lines or from xml fragments. */
    public List get(GeoPointDto geoPointDtoFactoryItem, String textLinesOrXml) {
        if (textLinesOrXml == null) return null;

        if (textLinesOrXml.startsWith(GeoFileRepository.COMMENT) || textLinesOrXml.startsWith(GeoUri.GEO_SCHEME)) {
            // lines of comments "#.." or geo-uris seperated by cr/lf
            GeoFileRepository parser = new GeoFileRepository(null, geoPointDtoFactoryItem) {
                @Override
                protected boolean isValid(IGeoPointInfo geo) {
                    return true;
                }
            };
            StringReader rd = new StringReader(textLinesOrXml);
            ArrayList result = new ArrayList();

            try {
                parser.load(result, rd);
                rd.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        } else {
            if (!textLinesOrXml.startsWith("" + textLinesOrXml + "";
            }
            GpxReader parser = new GpxReader(geoPointDtoFactoryItem);
            StringReader rd = new StringReader(textLinesOrXml);
            List result = null;

            try {
                result = parser.getTracks(new InputSource(rd));
            } catch (IOException e) {
                e.printStackTrace();
            }
            rd.close();
            return result;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy