Skip to content

Commit

Permalink
Merge pull request #2 from moosetechnology/suport-json-printer
Browse files Browse the repository at this point in the history
Add JSONPrinter
  • Loading branch information
badetitou authored Aug 11, 2021
2 parents 4aaea44 + c7baa15 commit 02091f9
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 54 deletions.
72 changes: 72 additions & 0 deletions samples/ch/unibe/fame/resources/lib.mse
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
(
(FM3.Package (id: 1)
(name 'LIB')
(classes
(FM3.Class (id: 2)
(name 'Person')
(abstract false)
(package (ref: 1))
(superclass (ref: Object))
(traits (ref: 3))
(properties
(FM3.Property (id: 4)
(name 'name')
(class (ref: 2))
(container false)
(derived false)
(multivalued false)
(type (ref: String)))
(FM3.Property (id: 5)
(name 'books')
(class (ref: 2))
(container false)
(derived false)
(multivalued true)
(opposite (ref: 6))
(type (ref: 7)))))
(FM3.Class (id: 8)
(name 'Library')
(abstract false)
(package (ref: 1))
(superclass (ref: Object))
(traits (ref: 3))
(properties
(FM3.Property (id: 9)
(name 'books')
(class (ref: 8))
(container false)
(derived false)
(multivalued true)
(type (ref: 7)))
(FM3.Property (id: 10)
(name 'librarian')
(class (ref: 8))
(container false)
(derived false)
(multivalued false)
(type (ref: 2)))))
(FM3.Trait (id: 3)
(name 'TNamed')
(package (ref: 1)))
(FM3.Class (id: 7)
(name 'Book')
(abstract false)
(package (ref: 1))
(superclass (ref: Object))
(traits (ref: 3))
(properties
(FM3.Property (id: 6)
(name 'authors')
(class (ref: 7))
(container false)
(derived false)
(multivalued true)
(opposite (ref: 5))
(type (ref: 2)))
(FM3.Property (id: 11)
(name 'title')
(class (ref: 7))
(container false)
(derived false)
(multivalued false)
(type (ref: String))))))))
9 changes: 9 additions & 0 deletions src/ch/akuhn/fame/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import ch.akuhn.fame.MetaRepository.ClassNotMetadescribedException;
import ch.akuhn.fame.fm3.MetaDescription;
import ch.akuhn.fame.fm3.PropertyDescription;
import ch.akuhn.fame.internal.JSONPrinter;
import ch.akuhn.fame.internal.MSEPrinter;
import ch.akuhn.fame.internal.RepositoryVisitor;
import ch.akuhn.fame.parser.Importer;
Expand Down Expand Up @@ -225,6 +226,14 @@ public void exportMSE(Appendable stream) {
this.accept(new MSEPrinter(stream));
}

public void exportJSONFile(String filename) {
this.exportJSON(Files.openWrite(filename));
}

public void exportJSON(Appendable stream) {
this.accept(new JSONPrinter(stream));
}

public Collection getElements() {
return elements;
}
Expand Down
135 changes: 135 additions & 0 deletions src/ch/akuhn/fame/internal/JSONPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package ch.akuhn.fame.internal;


/**
* This class a helps printing model in JSON
*
* @author Sabri.BENBRAHIM, Benoit "badetitou" VERHAEGHE
*/
public class JSONPrinter extends AbstractPrintClient {

/**
* Constructor
*
* @param stream in which we have to write
*/
public JSONPrinter(Appendable stream) {
super(stream);
}

@Override
public void beginAttribute(String name) {
lntabs();
append("\"");
append(name);
append("\"");
append(":");

}

@Override
public void beginDocument() {
append("[");
}

@Override
public void beginElement(String name) {
this.indentation++;
append("{");
lntabs();
append("\"FM3\":\"");
append(name);
append("\",");
lntabs();
}

public void beginMultivalue(String name) {
append("[");
}

@Override
public void directive(String name, String... params) {
}

@Override
public void endAttribute(String name) {
}

@Override
public void endDocument() {
lntabs();
append("]");
close();
}

@Override
public void endElement(String name) {
lntabs();
append("}");
this.indentation--;
}

public void endMultivalue(String name) {
append("]");
}

@Override
public void primitive(Object value) {
append('"');
if (value.getClass() == String.class){
for (char c : ((String) value).toCharArray()) {
if (c == '"') {
append('\\');
} else if (c == '\\') {
append('\\');
}
append(c);
}
} else {
append(value.toString());
}
append('"');
}

@Override
public void reference(int index) {
append(" ");
append("{");
append("\"ref\":");
append(" ");
append(String.valueOf(index));
append("}");
}

@Override
public void reference(String name) {
append(" ");
append("{");
append("\"ref\":");
append(" ");
append("\"");
append(name);
append("\"");
append("}");
}

@Override
public void reference(String name, int index) {
}

@Override
public void serial(int index) {
append("\"id\":");
append(String.valueOf(index));
append(",");
}

public void printEntitySeparator() {
append(",");
}

public void printPropertySeparator() {
append(",");
}

}
Loading

0 comments on commit 02091f9

Please sign in to comment.