/*
 * Copyright 2013 - 2014 Anton Tananaev (anton.tananaev@gmail.com)
 *
 * 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 org.traccar.model;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Format extended tracker status
 */
public class ExtendedInfoFormatter {

    private static final String rootNode = "info";
    
    private final Map<String, Object> data;
    
    public ExtendedInfoFormatter(String protocol) {
        data = new LinkedHashMap<String, Object>();
        data.put("protocol", protocol);
    }

    public void set(String key, Object value) {
        if (value != null) {
            // Exclude empty strings
            if ((value instanceof String) && ((String) value).isEmpty()) {
                return;
            }
            
            data.put(key, value);
        }
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        
        result.append("<").append(rootNode).append(">");
        
        for (Map.Entry<String, Object> entry : data.entrySet()) {
         
            result.append("<").append(entry.getKey()).append(">");
            result.append(entry.getValue());
            result.append("</").append(entry.getKey()).append(">");
        }

        result.append("</").append(rootNode).append(">");
        
        return result.toString();
    }

}

@@ -653,16 +653,43 @@
 model;%0A%0A
+import java.util.Iterator;%0A
 import j
@@ -712,16 +712,16 @@
 ashMap;%0A
-
 import j
@@ -1365,33 +1365,24 @@
   %7D%0A
-%0A
     
-@Override%0A    public
+%0A    private
  Str
@@ -1387,16 +1387,19 @@
 tring to
+Xml
 String()
@@ -1854,32 +1854,32 @@
 (%22%3E%22);%0A        %0A
-
         return r
@@ -1901,12 +1901,768 @@
 );%0A    %7D
+%0A    %0A    private String toJsonString() %7B%0A        StringBuilder result = new StringBuilder();%0A        %0A        result.append(%22%7B%22);%0A        %0A        Iterator%3CMap.Entry%3CString, Object%3E %3E i = data.entrySet().iterator();%0A        %0A        while (i.hasNext()) %7B%0A            Map.Entry%3CString, Object%3E entry = i.next();%0A            %0A            result.append('%22').append(entry.getKey()).append('%22');%0A            result.append(':');%0A            result.append('%22').append(entry.getValue()).append('%22');%0A            %0A            if (i.hasNext()) %7B%0A                result.append(',');%0A            %7D%0A        %7D%0A%0A        result.append(%22%7D%22);%0A        %0A        return result.toString();%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A        return toXmlString();%0A    %7D
 %0A%0A%7D%0A

Add JSON formatting method