diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2014-08-02 10:28:25 +0200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2014-08-02 10:28:25 +0200 |
commit | 53486c0fe1bad6adb2324bd596e7528dcec82d26 (patch) | |
tree | b1b1b38cf8ad81f10e9eaf0a6b133c854384ea23 | |
parent | e77e0e4de5c2c424df8aa6663f986e85e1190db1 (diff) | |
download | traccar-server-53486c0fe1bad6adb2324bd596e7528dcec82d26.tar.gz traccar-server-53486c0fe1bad6adb2324bd596e7528dcec82d26.tar.bz2 traccar-server-53486c0fe1bad6adb2324bd596e7528dcec82d26.zip |
Improve extended formatter
-rw-r--r-- | src/org/traccar/model/ExtendedInfoFormatter.java | 33 | ||||
-rw-r--r-- | test/org/traccar/model/ExtendedInfoFormatterTest.java | 20 |
2 files changed, 43 insertions, 10 deletions
diff --git a/src/org/traccar/model/ExtendedInfoFormatter.java b/src/org/traccar/model/ExtendedInfoFormatter.java index 99966569e..31aac7672 100644 --- a/src/org/traccar/model/ExtendedInfoFormatter.java +++ b/src/org/traccar/model/ExtendedInfoFormatter.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 Anton Tananaev (anton.tananaev@gmail.com) + * 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. @@ -15,6 +15,9 @@ */ package org.traccar.model; +import java.util.LinkedHashMap; +import java.util.Map; + /** * Format extended tracker status */ @@ -22,12 +25,11 @@ public class ExtendedInfoFormatter { private static final String rootNode = "info"; - private StringBuilder data; - + private final Map<String, Object> data; + public ExtendedInfoFormatter(String protocol) { - data = new StringBuilder(); - data.append("<").append(rootNode).append(">"); - data.append("<protocol>").append(protocol).append("</protocol>"); + data = new LinkedHashMap<String, Object>(); + data.put("protocol", protocol); } public void set(String key, Object value) { @@ -37,15 +39,26 @@ public class ExtendedInfoFormatter { return; } - data.append("<").append(key).append(">"); - data.append(value); - data.append("</").append(key).append(">"); + data.put(key, value); } } @Override public String toString() { - return data.toString() + "</" + rootNode + ">"; + 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(); } } diff --git a/test/org/traccar/model/ExtendedInfoFormatterTest.java b/test/org/traccar/model/ExtendedInfoFormatterTest.java new file mode 100644 index 000000000..31161e9e7 --- /dev/null +++ b/test/org/traccar/model/ExtendedInfoFormatterTest.java @@ -0,0 +1,20 @@ +package org.traccar.model; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class ExtendedInfoFormatterTest { + + @Test + public void testToString() throws Exception { + + ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("test"); + extendedInfo.set("a", "1"); + extendedInfo.set("b", "2"); + extendedInfo.set("a", "3"); + + assertEquals(extendedInfo.toString(), "<info><protocol>test</protocol><a>3</a><b>2</b></info>"); + + } + +} |