Skip to content

Commit d6ebd19

Browse files
authored
Merge pull request #19 from moosetechnology/issue-18
modified printing of properties to avoid extra "," for entity with no property
2 parents 81c88f2 + 638d334 commit d6ebd19

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

lib/src/main/java/ch/akuhn/fame/internal/JSONPrettyPrinter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public void beginElement(String name) {
3939
lntabs();
4040
append("\"FM3\":\"");
4141
append(name);
42-
append("\",");
43-
lntabs();
42+
append("\"");
4443
}
4544

4645
public void beginMultivalue(String name) {
@@ -121,9 +120,10 @@ public void reference(String name, int index) {
121120

122121
@Override
123122
public void serial(int index) {
123+
printPropertySeparator();
124+
lntabs();
124125
append("\"id\":");
125126
append(String.valueOf(index));
126-
append(",");
127127
}
128128

129129
public void printEntitySeparator() {

lib/src/main/java/ch/akuhn/fame/internal/JSONPrinter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void beginElement(String name) {
3030
append("{");
3131
append("\"FM3\":\"");
3232
append(name);
33-
append("\",");
33+
append("\"");
3434
}
3535

3636
public void beginMultivalue(String name) {
@@ -104,9 +104,9 @@ public void reference(String name, int index) {
104104

105105
@Override
106106
public void serial(int index) {
107+
printPropertySeparator();
107108
append("\"id\":");
108109
append(String.valueOf(index));
109-
append(",");
110110
}
111111

112112
public void printEntitySeparator() {

lib/src/main/java/ch/akuhn/fame/internal/RepositoryVisitor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ private void handleChildrenProperties(Object each, MetaDescription meta /**, Col
7979
continue;
8080
}*/
8181
if (!values.isEmpty()) {
82+
visitor.printEntitySeparator();
83+
8284
visitor.beginAttribute(property.getName());
8385
if (property.isMultivalued()) {
8486
visitor.beginMultivalue(property.getName());
@@ -131,9 +133,6 @@ private void handleChildrenProperties(Object each, MetaDescription meta /**, Col
131133
visitor.endMultivalue(property.getName());
132134
}
133135
visitor.endAttribute(property.getName());
134-
if (propertiesIterator.hasNext()) {
135-
visitor.printEntitySeparator();
136-
}
137136
}
138137
}
139138
}
Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package ch.akuhn.fame.test;
22

3+
import ch.akuhn.fame.MetaRepository;
34
import ch.akuhn.fame.Tower;
45
import ch.akuhn.fame.internal.JSONPrettyPrinter;
6+
import ch.akuhn.fame.internal.JSONPrinter;
57
import ch.akuhn.fame.parser.InputSource;
68
import junit.framework.TestCase;
79

@@ -10,6 +12,9 @@ public class JSONPrinterTest extends TestCase {
1012
private JSONPrettyPrinter printer;
1113
private Appendable stream;
1214

15+
/** FIXME
16+
* This is not a test but a method to export a meta-model
17+
*/
1318
public void testExportJSON() {
1419
InputSource input = InputSource.fromResource("ch/unibe/fame/resources/lib.mse");
1520
Tower t = new Tower();
@@ -19,6 +24,10 @@ public void testExportJSON() {
1924
System.out.println(stream);
2025
}
2126

27+
private static String removeWhiteSpaces(String input) {
28+
return input.replaceAll("\\s+", "");
29+
}
30+
2231
@Override
2332
public void setUp() throws Exception {
2433
super.setUp();
@@ -28,46 +37,78 @@ public void setUp() throws Exception {
2837

2938
public void testBeginAttributeSimple() {
3039
printer.beginAttribute("hello");
31-
assertEquals(stream.toString(), "\"hello\":");
40+
assertEquals("\"hello\":", stream.toString());
3241
}
3342

3443
public void testPrimitive() {
3544
printer.primitive("value");
36-
assertEquals(stream.toString(), "\"value\"");
45+
assertEquals("\"value\"", stream.toString());
3746
}
3847

3948
public void testPrimitiveWithSpecialCharacter() {
4049
printer.primitive("MySuper\"String");
41-
assertEquals(stream.toString(), "\"MySuper\\\"String\"");
50+
assertEquals("\"MySuper\\\"String\"", stream.toString());
4251
}
4352

4453
public void testPrimitiveWithSpecialCharacterAndActualExample() {
4554
printer.primitive("print(\"Printer \" + name() + \" prints \"+ thePacket.contents(),false)");
46-
assertEquals(stream.toString(), "\"print(\\\"Printer \\\" + name() + \\\" prints \\\"+ thePacket.contents(),false)\"");
55+
assertEquals( "\"print(\\\"Printer \\\" + name() + \\\" prints \\\"+ thePacket.contents(),false)\"", stream.toString());
4756
}
4857

4958
public void testReference() {
5059
printer.reference("hello");
51-
assertEquals(removeWhiteSpaces(stream.toString()), "{\"ref\":\"hello\"}");
60+
assertEquals("{\"ref\":\"hello\"}", removeWhiteSpaces(stream.toString()));
5261
}
5362

5463
public void testReferenceIndex() {
5564
printer.reference(2);
56-
assertEquals(removeWhiteSpaces(stream.toString()), "{\"ref\":2}");
65+
assertEquals("{\"ref\":2}", removeWhiteSpaces(stream.toString()));
5766
}
5867

5968
public void testSerial() {
6069
printer.serial(2);
61-
assertEquals(removeWhiteSpaces(stream.toString()), "\"id\":2,");
70+
assertEquals(",\"id\":2", removeWhiteSpaces(stream.toString()));
6271
}
6372

6473
public void testBeginElement() {
6574
printer.beginElement("Java.Class");
66-
assertEquals(removeWhiteSpaces(stream.toString()), "{\"FM3\":\"Java.Class\",");
75+
assertEquals("{\"FM3\":\"Java.Class\"", removeWhiteSpaces(stream.toString()));
6776
}
6877

78+
public void testEmptyEntityPrettyPrinter() {
79+
String str = "((FM3.Package))";
80+
Tower t = new Tower();
81+
t.getMetamodel().importMSE(str);
82+
MetaRepository repo = t.getMetamodel();
83+
repo.accept( printer);
84+
assertEquals("[{\"FM3\":\"FM3.Package\",\"id\":1}]", removeWhiteSpaces(stream.toString()));
85+
}
6986

70-
private static String removeWhiteSpaces(String input) {
71-
return input.replaceAll("\\s+", "");
87+
public void testEmptyEntityJSONPrinter() {
88+
String str = "((FM3.Package))";
89+
Tower t = new Tower();
90+
t.getMetamodel().importMSE(str);
91+
MetaRepository repo = t.getMetamodel();
92+
repo.accept( new JSONPrinter(stream));
93+
assertEquals("[{\"FM3\":\"FM3.Package\",\"id\":1}]", stream.toString());
94+
}
95+
96+
public void testEntityWithAttributePrettyPrinter() {
97+
String str = "((FM3.Package (name 'Blah')))";
98+
Tower t = new Tower();
99+
t.getMetamodel().importMSE(str);
100+
MetaRepository repo = t.getMetamodel();
101+
repo.accept( printer);
102+
assertEquals("[{\"FM3\":\"FM3.Package\",\"id\":1,\"name\":\"Blah\"}]", removeWhiteSpaces(stream.toString()));
72103
}
104+
105+
public void testEntityWithAttributeJSONPrinter() {
106+
String str = "((FM3.Package (name 'Blah')))";
107+
Tower t = new Tower();
108+
t.getMetamodel().importMSE(str);
109+
MetaRepository repo = t.getMetamodel();
110+
repo.accept( new JSONPrinter(stream));
111+
assertEquals("[{\"FM3\":\"FM3.Package\",\"id\":1,\"name\":\"Blah\"}]", stream.toString());
112+
}
113+
73114
}

0 commit comments

Comments
 (0)