/* Copyright (c) 2013 OpenPlans. All rights reserved.
 * This code is licensed under the BSD New License, available at the root
 * application directory.
 */
package org.geogit.osm.internal;

import java.util.Collection;
import java.util.Iterator;

import javax.annotation.Nullable;

import org.geotools.data.DataUtilities;
import org.geotools.referencing.CRS;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.openstreetmap.osmosis.core.domain.v0_6.Tag;

import com.google.common.base.Throwables;
import com.google.common.collect.Lists;

public class OSMUtils {

    public static final String OSM_FETCH_BRANCH = "OSM_FETCH";

    public static final String DEFAULT_API_ENDPOINT = "http://overpass-api.de/api/interpreter";

    public static final String FR_API_ENDPOINT = "http://api.openstreetmap.fr/oapi/interpreter/";

    public static final String RU_API_ENDPOINT = "http://overpass.osm.rambler.ru/";

    public static final String NODE_TYPE_NAME = "node";

    public static final String WAY_TYPE_NAME = "way";

    public static final String NAMESPACE = "www.openstreetmap.org";

    private static SimpleFeatureType NodeType;

    public synchronized static SimpleFeatureType nodeType() {
        if (NodeType == null) {
            String typeSpec = "visible:Boolean,version:Integer,timestamp:java.lang.Long,tags:String,"
                    + "changeset:java.lang.Long,user:String,location:Point:srid=4326";
            try {
                SimpleFeatureType type = DataUtilities.createType(NAMESPACE,
                        OSMUtils.NODE_TYPE_NAME, typeSpec);
                boolean longitudeFirst = true;
                CoordinateReferenceSystem forceLonLat = CRS.decode("EPSG:4326", longitudeFirst);
                NodeType = DataUtilities.createSubType(type, null, forceLonLat);
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
        }
        return NodeType;
    }

    private static SimpleFeatureType WayType;

    public synchronized static SimpleFeatureType wayType() {
        if (WayType == null) {
            String typeSpec = "visible:Boolean,version:Integer,timestamp:java.lang.Long,tags:String,"
                    + "changeset:java.lang.Long,user:String,nodes:String,way:LineString:srid=4326";
            try {
                SimpleFeatureType type = DataUtilities.createType(NAMESPACE,
                        OSMUtils.WAY_TYPE_NAME, typeSpec);
                boolean longitudeFirst = true;
                CoordinateReferenceSystem forceLonLat = CRS.decode("EPSG:4326", longitudeFirst);
                WayType = DataUtilities.createSubType(type, null, forceLonLat);
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
        }
        return WayType;
    }

    /**
     * @param collection
     * @return
     */
    @Nullable
    public static String buildTagsString(Collection<Tag> collection) {
        if (collection.isEmpty()) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (Iterator<Tag> it = collection.iterator(); it.hasNext();) {
            Tag e = it.next();
            String key = e.getKey();
            if (key == null || key.isEmpty()) {
                continue;
            }
            String value = e.getValue();
            sb.append(key).append(':').append(value);
            if (it.hasNext()) {
                sb.append('|');
            }
        }
        return sb.toString();
    }

    public static Collection<Tag> buildTagsCollectionFromString(String tagsString) {
        Collection<Tag> tags = Lists.newArrayList();
        if (tagsString != null) {
            String[] tokens = tagsString.split("\\|");
            for (String token : tokens) {
                int idx = token.lastIndexOf(':');
                if (idx != -1) {
                    Tag tag = new Tag(token.substring(0, idx), token.substring(idx + 1));
                    tags.add(tag);
                } else {
                    System.out.println(token);
                }
            }
        }
        return tags;
    }

}

@@ -523,16 +523,73 @@
 0_6.Tag;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -690,16 +690,99 @@
 tils %7B%0A%0A
+    private static final Logger LOGGER = LoggerFactory.getLogger(OSMUtils.class);%0A%0A
     publ
@@ -4240,32 +4240,121 @@
     
-System.out.println(token
+LOGGER.info(%22found tag token '%7B%7D' with no value in tagString '%7B%7D'%22, token,%0A                            tagsString
 );%0A 

replace System.err.println call in OSMUtils by log entry