From 287120a0c892cda1dffaf856e5d9adf740b00a6f Mon Sep 17 00:00:00 2001 From: "AMAIISDOM\\kyakkala" Date: Thu, 22 Mar 2018 15:34:01 +0530 Subject: [PATCH 1/4] adding formatter to convert decimal exponent notation to number. e.g., 1.234567067801245E10 to 12345670678.0124 --- .../com/googlecode/protobuf/format/XmlFormat.java | 15 +++++++++------ .../googlecode/protobuf/format/XmlFormatTest.java | 7 +++++++ .../xml_format_without_exponent_notation.txt | 1 + src/test/resources/proto/issue.proto | 0 4 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java create mode 100644 src/test/resources/expectations/xmlFormatTest/xml_format_without_exponent_notation.txt create mode 100644 src/test/resources/proto/issue.proto diff --git a/src/main/java/com/googlecode/protobuf/format/XmlFormat.java b/src/main/java/com/googlecode/protobuf/format/XmlFormat.java index 336b92a..bdc441b 100644 --- a/src/main/java/com/googlecode/protobuf/format/XmlFormat.java +++ b/src/main/java/com/googlecode/protobuf/format/XmlFormat.java @@ -31,6 +31,9 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT import java.io.IOException; import java.math.BigInteger; +import java.text.DecimalFormat; +import java.text.Format; +import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; @@ -38,11 +41,7 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT import java.util.regex.Pattern; import java.nio.CharBuffer; -import com.google.protobuf.ByteString; -import com.google.protobuf.Descriptors; -import com.google.protobuf.ExtensionRegistry; -import com.google.protobuf.Message; -import com.google.protobuf.UnknownFieldSet; +import com.google.protobuf.*; import com.google.protobuf.Descriptors.EnumValueDescriptor; import com.google.protobuf.Descriptors.FieldDescriptor; import static com.googlecode.protobuf.format.util.TextUtils.*; @@ -62,6 +61,7 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */ public final class XmlFormat extends AbstractCharBasedFormatter { + public Map formatter = new HashMap(); /** * Outputs a textual representation of the Protocol Message supplied into the parameter output. * (This representation is the new version of the classic "ProtocolPrinter" output from the @@ -164,7 +164,10 @@ private void printFieldValue(FieldDescriptor field, Object value, XmlGenerator g case DOUBLE: case BOOL: // Good old toString() does what we want for these types. - generator.print(value.toString()); + if(formatter.containsKey(field.getType())) + generator.print(formatter.get(field.getType()).format(value)); + else + generator.print(value.toString()); break; case UINT32: diff --git a/src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java b/src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java new file mode 100644 index 0000000..959c982 --- /dev/null +++ b/src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java @@ -0,0 +1,7 @@ +package com.googlecode.protobuf.format; + +/** + * Created by kyakkala on 3/22/2018. + */ +public class XmlFormatTest { +} diff --git a/src/test/resources/expectations/xmlFormatTest/xml_format_without_exponent_notation.txt b/src/test/resources/expectations/xmlFormatTest/xml_format_without_exponent_notation.txt new file mode 100644 index 0000000..0fc03d9 --- /dev/null +++ b/src/test/resources/expectations/xmlFormatTest/xml_format_without_exponent_notation.txt @@ -0,0 +1 @@ +12345670678.0124 \ No newline at end of file diff --git a/src/test/resources/proto/issue.proto b/src/test/resources/proto/issue.proto new file mode 100644 index 0000000..e69de29 From fe4af5b96c17d20f27bce968941a3ce733159201 Mon Sep 17 00:00:00 2001 From: "AMAIISDOM\\kyakkala" Date: Thu, 22 Mar 2018 15:40:54 +0530 Subject: [PATCH 2/4] adding test case with decimalformat Signed-off-by: AMAIISDOM\kyakkala --- .../protobuf/format/XmlFormatTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java b/src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java index 959c982..86ba491 100644 --- a/src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java +++ b/src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java @@ -1,7 +1,37 @@ package com.googlecode.protobuf.format; +import com.google.protobuf.Descriptors; +import com.googlecode.protobuf.format.test.Issue; +import org.testng.annotations.Test; +import org.testng.reporters.Files; + +import java.io.IOException; +import java.text.DecimalFormat; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + /** * Created by kyakkala on 3/22/2018. */ public class XmlFormatTest { + + @Test + public void testDecimalFormat() throws IOException { + + Issue.MsgWithFields msg = Issue.MsgWithFields.newBuilder() + .setDecmialField(12345670678.01245) + .build(); + XmlFormat xmlFormat = new XmlFormat(); + + DecimalFormat decimalFormat = new DecimalFormat("#"); + decimalFormat.setMaximumFractionDigits(4); + xmlFormat.formatter.put(Descriptors.FieldDescriptor.Type.DOUBLE, decimalFormat); + + assertThat(xmlFormat.printToString(msg).toString(), + is(Files.readFile(XmlFormatTest.class + .getResourceAsStream( + "/expectations/xmlFormatTest/xml_format_without_exponent_notation.txt")).trim())); + + } } From e854d8362572cf83c5f254795e24e99cc2dfb63d Mon Sep 17 00:00:00 2001 From: "AMAIISDOM\\kyakkala" Date: Mon, 26 Mar 2018 17:18:24 +0530 Subject: [PATCH 3/4] changing package name --- src/test/resources/proto/issue.proto | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/resources/proto/issue.proto b/src/test/resources/proto/issue.proto index e69de29..9947c4b 100644 --- a/src/test/resources/proto/issue.proto +++ b/src/test/resources/proto/issue.proto @@ -0,0 +1,9 @@ +package issue; + +option optimize_for = SPEED; + +option java_package = "com.googlecode.protobuf.format.test"; + +message MsgWithFields { + optional double decmialField = 1; +} \ No newline at end of file From c0d14fcf23ba09a8a5b93daf111b022a92e60a21 Mon Sep 17 00:00:00 2001 From: "AMAIISDOM\\kyakkala" Date: Tue, 27 Mar 2018 11:04:29 +0530 Subject: [PATCH 4/4] changing package name --- .../com/googlecode/protobuf/format/XmlFormatTest.java | 6 +++--- .../xml_format_without_exponent_notation.txt | 2 +- src/test/resources/proto/issue.proto | 9 --------- src/test/resources/proto/issue23.proto | 1 + 4 files changed, 5 insertions(+), 13 deletions(-) delete mode 100644 src/test/resources/proto/issue.proto diff --git a/src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java b/src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java index 86ba491..a13a126 100644 --- a/src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java +++ b/src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java @@ -1,7 +1,7 @@ package com.googlecode.protobuf.format; import com.google.protobuf.Descriptors; -import com.googlecode.protobuf.format.test.Issue; +import com.googlecode.protobuf.format.issue23.Issue23; import org.testng.annotations.Test; import org.testng.reporters.Files; @@ -19,8 +19,8 @@ public class XmlFormatTest { @Test public void testDecimalFormat() throws IOException { - Issue.MsgWithFields msg = Issue.MsgWithFields.newBuilder() - .setDecmialField(12345670678.01245) + Issue23.MsgWithUnknownFields msg = Issue23.MsgWithUnknownFields.newBuilder() + .setLeaf4(12345670678.01245) .build(); XmlFormat xmlFormat = new XmlFormat(); diff --git a/src/test/resources/expectations/xmlFormatTest/xml_format_without_exponent_notation.txt b/src/test/resources/expectations/xmlFormatTest/xml_format_without_exponent_notation.txt index 0fc03d9..ad83508 100644 --- a/src/test/resources/expectations/xmlFormatTest/xml_format_without_exponent_notation.txt +++ b/src/test/resources/expectations/xmlFormatTest/xml_format_without_exponent_notation.txt @@ -1 +1 @@ -12345670678.0124 \ No newline at end of file +12345670678.0124 \ No newline at end of file diff --git a/src/test/resources/proto/issue.proto b/src/test/resources/proto/issue.proto deleted file mode 100644 index 9947c4b..0000000 --- a/src/test/resources/proto/issue.proto +++ /dev/null @@ -1,9 +0,0 @@ -package issue; - -option optimize_for = SPEED; - -option java_package = "com.googlecode.protobuf.format.test"; - -message MsgWithFields { - optional double decmialField = 1; -} \ No newline at end of file diff --git a/src/test/resources/proto/issue23.proto b/src/test/resources/proto/issue23.proto index 422bb59..f9d5428 100644 --- a/src/test/resources/proto/issue23.proto +++ b/src/test/resources/proto/issue23.proto @@ -8,4 +8,5 @@ message MsgWithUnknownFields { optional string leaf1 = 1; optional int32 leaf2 = 2; repeated int32 leaf3 = 3; + optional double leaf4 = 4; }