diff --git a/examples/omwg/total.xml b/examples/omwg/total.xml index eeb79e094fe52a15f08940826ed1bae2dea14330..50af29c5aef828eb6f4152b35a59b640cd0e04bb 100644 --- a/examples/omwg/total.xml +++ b/examples/omwg/total.xml @@ -590,9 +590,9 @@ <edoal:PropertyDomainRestriction> <edoal:class><edoal:Class rdf:about="&wine;Bordeaux1"/></edoal:class> </edoal:PropertyDomainRestriction> - <!--edoal:PropertyTypeRestriction> - <edoal:type><edoal:Datatype rdf:about="&wine;Bordeaux2"/></edoal:type> - </edoal:PropertyTypeRestriction--> + <edoal:PropertyTypeRestriction> + <edoal:datatype><edoal:Datatype rdf:about="&edoal;MyTypett"/></edoal:datatype> + </edoal:PropertyTypeRestriction> <edoal:PropertyValueRestriction> <edoal:comparator rdf:resource="http://www.w3.org/2001/XMLSchema#equals"/> <edoal:value><edoal:Literal edoal:string="Bordeaux"/></edoal:value> @@ -612,11 +612,12 @@ </edoal:and> </edoal:Class> </edoal:class> - </edoal:PropertyDomainRestriction> - <!--edoal:PropertyValueRestriction> - <edoal:comparator rdf:resource="http://www.w3.org/2001/XMLSchema#equals"/> + </edoal:PropertyDomainRestriction> + <!-- not fixed --> + <edoal:PropertyValueRestriction> + <edoal:comparator rdf:resource="http://www.w3.org/2001/XMLSchema#equals"/> <edoal:value><edoal:Apply edoal:operator="http://example.org/gensym"/></edoal:value> - </edoal:PropertyValueRestriction--> + </edoal:PropertyValueRestriction> </edoal:and> </edoal:Property> </entity2> diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/JSONRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/JSONRendererVisitor.java index a61bb29b5b93fdc55f7def7f388a0f21ef6821be..8d372f1e05876f09b14a637cae50c777164ee0e4 100644 --- a/src/fr/inrialpes/exmo/align/impl/renderer/JSONRendererVisitor.java +++ b/src/fr/inrialpes/exmo/align/impl/renderer/JSONRendererVisitor.java @@ -89,17 +89,11 @@ import fr.inrialpes.exmo.align.impl.edoal.EDOALVisitor; * @version $Id$ */ -// TODO: JE2012 -// - check "," in enumerations (not enough) and in structure (too many) -// - add test PropertyTypeRestriction -// - check namespace at the begining - public class JSONRendererVisitor extends IndentedRendererVisitor implements AlignmentVisitor,EDOALVisitor { Alignment alignment = null; Cell cell = null; Hashtable<String,String> nslist = null; - boolean embedded = false; // if the output is XML embeded in a structure // We do not want a default namespace here private static Namespace DEF = Namespace.NONE; @@ -108,12 +102,9 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig public JSONRendererVisitor( PrintWriter writer ){ super( writer ); - //NL = System.getProperty("line.separator"); } public void init( Properties p ) { - if ( p.getProperty( "embedded" ) != null - && !p.getProperty( "embedded" ).equals("") ) embedded = true; if ( p.getProperty( "indent" ) != null ) INDENT = p.getProperty( "indent" ); if ( p.getProperty( "newline" ) != null ) @@ -151,23 +142,22 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig extensionString += INDENT+"\""+label+"\" : \""+((BasicAlignment)align).getXNamespace( label )+"\","+NL; } } - writer.print("{ class : \""+align.getClass().getName()+"\","+NL ); + writer.print("{ \"@type\" : \""+SyntaxElement.ALIGNMENT.print(DEF)+"\","+NL ); increaseIndent(); - indentedOutputln("@context : {"); + indentedOutputln("\"@context\" : {"); increaseIndent(); for ( Enumeration e = nslist.keys() ; e.hasMoreElements(); ) { String k = (String)e.nextElement(); - indentedOutputln(nslist.get(k)+" : \""+k+"\","); + indentedOutputln("\""+nslist.get(k)+"\" : \""+k+"\","); } // Not sure that this is fully correct - indentedOutputln("\""+SyntaxElement.MEASURE.print(DEF)+"\" : { \"@type\" : \"xsd:float\" },"); + indentedOutputln("\""+SyntaxElement.MEASURE.print(DEF)+"\" : { \"@type\" : \"xsd:float\" }"); decreaseIndent(); indentedOutputln("},"); - indentedOutputln("@type : \""+SyntaxElement.ALIGNMENT.print(DEF)+"\"," ); //?? String idext = align.getExtension( Namespace.ALIGNMENT.uri, Annotations.ID ); if ( idext != null ) { //indentedOutputln("\"rdf:about\" : \""+idext+"\","); - indentedOutputln("@id : \""+idext+"\","); + indentedOutputln("\"@id\" : \""+idext+"\","); } if ( alignment.getLevel().startsWith("2EDOALPattern") ) isPattern = true; indentedOutputln( "\""+SyntaxElement.LEVEL.print(DEF)+"\" : \""+align.getLevel()+"\","); @@ -193,10 +183,12 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig decreaseIndent(); indentedOutputln( "\""+SyntaxElement.MAP.print(DEF)+"\" : [" ); increaseIndent(); + boolean first = true; for( Cell c : align ){ + if ( first ) { first = false; } else { writer.print(","+NL); } c.accept( this ); - writer.print( ","+NL ); }; + writer.print(NL); decreaseIndent(); indentedOutputln( "]" ); decreaseIndent(); @@ -204,10 +196,10 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig } private void printBasicOntology ( URI u, URI f ) { - indentedOutput("{ class : \""+SyntaxElement.ONTOLOGY.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.ONTOLOGY.print(DEF)+"\","+NL); increaseIndent(); //indentedOutput("\rdf:about\" : \""+u+"\","+NL); - indentedOutput("@id : \""+u+"\","+NL); + indentedOutput("\"@id\" : \""+u+"\","+NL); if ( f != null ) { indentedOutput("\""+SyntaxElement.LOCATION.print(DEF)+"\" : \""+f+"\","+NL); } else { @@ -220,10 +212,10 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig public void printOntology( Ontology onto ) { URI u = onto.getURI(); URI f = onto.getFile(); - indentedOutputln("{ class : \""+onto.getClass().getName()+"\","); + indentedOutputln("{ \"@type\" : \""+SyntaxElement.ONTOLOGY.print(DEF)+"\","); increaseIndent(); //indentedOutput("\"rdf:about\" : \""+u+"\","+NL); - indentedOutput("@id : \""+u+"\","+NL); + indentedOutput("\"@id\" : \""+u+"\","+NL); if ( f != null ) { indentedOutput("\""+SyntaxElement.LOCATION.print(DEF)+"\" : \""+f+"\""); } else { @@ -233,7 +225,7 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig writer.print(","+NL); indentedOutputln("\""+SyntaxElement.FORMATT.print(DEF)+"\" : "); increaseIndent(); - indentedOutputln("{ class : \""+SyntaxElement.FORMALISM.print(DEF)+"\"," ); + indentedOutputln("{ \"@type\" : \""+SyntaxElement.FORMALISM.print(DEF)+"\"," ); increaseIndent(); indentedOutputln("\""+SyntaxElement.NAME.print(DEF)+"\" : \""+onto.getFormalism()+"\","); indentedOutputln("\""+SyntaxElement.URI.print()+"\" : \""+onto.getFormURI()+"\""); @@ -245,7 +237,6 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig indentedOutput("}"); } - // TODO (EDOAL-PART) public void visit( Cell cell ) throws AlignmentException { if ( subsumedInvocableMethod( this, cell, Cell.class ) ) return; // default behaviour @@ -254,11 +245,11 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig URI u2 = cell.getObject2AsURI(alignment); if ( ( u1 != null && u2 != null) || alignment.getLevel().startsWith("2EDOAL") ){ //expensive test - indentedOutputln("{ class : \""+cell.getClass().getName()+"\","); + indentedOutputln("{ \"@type\" : \""+SyntaxElement.CELL.print(DEF)+"\","); increaseIndent(); if ( cell.getId() != null && !cell.getId().equals("") ){ //indentedOutputln("\"rdf:about\" : \""+cell.getId()+"\","); - indentedOutputln("@id : \""+cell.getId()+"\","); + indentedOutputln("\"@id\" : \""+cell.getId()+"\","); } if ( alignment.getLevel().startsWith("2EDOAL") ) { indentedOutputln("\""+SyntaxElement.ENTITY1.print(DEF)+"\" : "); @@ -279,7 +270,7 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig increaseIndent(); transf.accept( this ); decreaseIndent(); - writer.print(NL); + writer.print(","+NL); } } } @@ -290,17 +281,21 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig indentedOutput("\""+SyntaxElement.RULE_RELATION.print(DEF)+"\" : \""); cell.getRelation().accept( this ); writer.print("\","+NL); - indentedOutputln("\""+SyntaxElement.MEASURE.print(DEF)+"\" : \""+cell.getStrength()+"\","); + indentedOutput("\""+SyntaxElement.MEASURE.print(DEF)+"\" : \""+cell.getStrength()+"\""); if ( cell.getSemantics() != null && !cell.getSemantics().equals("") && - !cell.getSemantics().equals("first-order") ) - indentedOutputln("\""+SyntaxElement.SEMANTICS.print(DEF)+"\" : \""+cell.getSemantics()+"\","); + !cell.getSemantics().equals("first-order") ) { + writer.print(","+NL); + indentedOutput("\""+SyntaxElement.SEMANTICS.print(DEF)+"\" : \""+cell.getSemantics()+"\""); + } if ( cell.getExtensions() != null ) { for ( String[] ext : cell.getExtensions() ){ - indentedOutputln(ext[1]+" : \""+ext[2]+"\","+NL); + writer.print(","+NL); + indentedOutputln(ext[1]+" : \""+ext[2]+"\""); } } decreaseIndent(); + writer.print(NL); indentedOutput("}"); } } @@ -316,35 +311,40 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig public void renderVariables( Expression expr ) { if ( expr.getVariable() != null ) { - indentedOutputln("\""+SyntaxElement.VAR.print(DEF)+"\" : \""+expr.getVariable().name()+"\","); + writer.print(","+NL); + indentedOutputln("\""+SyntaxElement.VAR.print(DEF)+"\" : \""+expr.getVariable().name()+"\""); } } public void visit( final ClassId e ) throws AlignmentException { - indentedOutput("{ class : \""+SyntaxElement.CLASS_EXPR.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.CLASS_EXPR.print(DEF)+"\""); increaseIndent(); if ( e.getURI() != null ){ //indentedOutput("\rdf:about\" : \""+u+"\","+NL); - indentedOutput("@id : \""+e.getURI()+"\","+NL); + writer.print(","+NL); + indentedOutput("\"@id\" : \""+e.getURI()+"\""); } if ( isPattern ) renderVariables( e ); decreaseIndent(); + writer.print(NL); indentedOutput("}"); } public void visit( final ClassConstruction e ) throws AlignmentException { final Constructor op = e.getOperator(); String sop = SyntaxElement.getElement( op ).print(DEF) ; - indentedOutput("{ class : \""+SyntaxElement.CLASS_EXPR.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.CLASS_EXPR.print(DEF)+"\""); increaseIndent(); if ( isPattern ) renderVariables( e ); + writer.print(","+NL); indentedOutput("\""+sop+"\" : ["+NL); increaseIndent(); + boolean first = true; for ( final ClassExpression ce : e.getComponents() ) { - writer.print(linePrefix); + if ( first ) { first = false; } else { writer.print(","+NL); } ce.accept( this ); - writer.print(NL); } + writer.print(NL); decreaseIndent(); indentedOutputln("]"); decreaseIndent(); @@ -352,10 +352,11 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig } public void visit( final ClassValueRestriction c ) throws AlignmentException { - indentedOutput("{ class : \""+SyntaxElement.VALUE_COND.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.VALUE_COND.print(DEF)+"\""); increaseIndent(); if ( isPattern ) renderVariables( c ); - indentedOutput("\""+SyntaxElement.ONPROPERTY.print(DEF)+"\" : "+NL); + writer.print(","+NL); + indentedOutputln("\""+SyntaxElement.ONPROPERTY.print(DEF)+"\" : "); increaseIndent(); c.getRestrictionPath().accept( this ); decreaseIndent(); @@ -371,10 +372,11 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig } public void visit( final ClassTypeRestriction c ) throws AlignmentException { - indentedOutput("{ class : \""+SyntaxElement.TYPE_COND.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.TYPE_COND.print(DEF)+"\""); increaseIndent(); if ( isPattern ) renderVariables( c ); - indentedOutput("\""+SyntaxElement.ONPROPERTY.print(DEF)+"\" : "+NL); + writer.print(","+NL); + indentedOutputln("\""+SyntaxElement.ONPROPERTY.print(DEF)+"\" : "); increaseIndent(); c.getRestrictionPath().accept( this ); writer.print(","+NL); @@ -386,10 +388,11 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig } public void visit( final ClassDomainRestriction c ) throws AlignmentException { - indentedOutput("{ class : \""+SyntaxElement.DOMAIN_RESTRICTION.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.DOMAIN_RESTRICTION.print(DEF)+"\""); increaseIndent(); if ( isPattern ) renderVariables( c ); - indentedOutput("\""+SyntaxElement.ONPROPERTY.print(DEF)+"\" : "+NL); + writer.print(","+NL); + indentedOutputln("\""+SyntaxElement.ONPROPERTY.print(DEF)+"\" : "); increaseIndent(); c.getRestrictionPath().accept( this ); decreaseIndent(); @@ -408,10 +411,11 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig } public void visit( final ClassOccurenceRestriction c ) throws AlignmentException { - indentedOutput("{ class : \""+SyntaxElement.OCCURENCE_COND.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.OCCURENCE_COND.print(DEF)+"\""); increaseIndent(); if ( isPattern ) renderVariables( c ); - indentedOutput("\""+SyntaxElement.ONPROPERTY.print(DEF)+"\" : "+NL); + writer.print(","+NL); + indentedOutputln("\""+SyntaxElement.ONPROPERTY.print(DEF)+"\" : "); increaseIndent(); c.getRestrictionPath().accept( this ); decreaseIndent(); @@ -419,7 +423,7 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig indentedOutput("\""+SyntaxElement.COMPARATOR.print(DEF)+"\" : \""+c.getComparator().getURI()+"\","+NL); indentedOutput("\""+SyntaxElement.VALUE.print(DEF)+"\" : "+NL); increaseIndent(); - indentedOutputln("{ class : \""+SyntaxElement.LITERAL.print(DEF)+"\","); + indentedOutputln("{ \"@type\" : \""+SyntaxElement.LITERAL.print(DEF)+"\","); increaseIndent(); indentedOutputln("\""+SyntaxElement.ETYPE.print(DEF)+"\" : \"xsd:int\","); indentedOutputln("\""+SyntaxElement.STRING.print(DEF)+"\" : \""+c.getOccurence()+"\""); @@ -431,13 +435,15 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig } public void visit(final PropertyId e) throws AlignmentException { - indentedOutput("{ class : \""+SyntaxElement.PROPERTY_EXPR.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.PROPERTY_EXPR.print(DEF)+"\""); increaseIndent(); if ( e.getURI() != null ){ //indentedOutput("\rdf:about\" : \""+u+"\","+NL); - indentedOutput("@id : \""+e.getURI()+"\","+NL); + writer.print(","+NL); + indentedOutput("\"@id\" : \""+e.getURI()+"\""); } if ( isPattern ) renderVariables( e ); + writer.print(NL); decreaseIndent(); indentedOutput("}"); } @@ -445,16 +451,19 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig public void visit(final PropertyConstruction e) throws AlignmentException { final Constructor op = e.getOperator(); String sop = SyntaxElement.getElement( op ).print(DEF) ; - indentedOutput("{ class : \""+SyntaxElement.PROPERTY_EXPR.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.PROPERTY_EXPR.print(DEF)+"\""); increaseIndent(); if ( isPattern ) renderVariables( e ); + writer.print(","+NL); indentedOutput("\""+sop+"\" : ["+NL); increaseIndent(); + boolean first = true; for ( final PathExpression pe : e.getComponents() ) { + if ( first ) { first = false; } else { writer.print(","+NL); } writer.print(linePrefix); pe.accept( this ); - writer.print(NL); } + writer.print(NL); decreaseIndent(); indentedOutputln("]"); decreaseIndent(); @@ -462,9 +471,10 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig } public void visit(final PropertyValueRestriction c) throws AlignmentException { - indentedOutput("{ class : \""+SyntaxElement.PROPERTY_VALUE_COND.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.PROPERTY_VALUE_COND.print(DEF)+"\""); increaseIndent(); if ( isPattern ) renderVariables( c ); + writer.print(","+NL); indentedOutput("\""+SyntaxElement.COMPARATOR.print(DEF)+"\" : \""+c.getComparator().getURI()+"\","+NL); indentedOutput("\""+SyntaxElement.VALUE.print(DEF)+"\" : "+NL); increaseIndent(); @@ -476,9 +486,10 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig } public void visit(final PropertyDomainRestriction c) throws AlignmentException { - indentedOutput("{ class : \""+SyntaxElement.PROPERTY_DOMAIN_COND.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.PROPERTY_DOMAIN_COND.print(DEF)+"\""); increaseIndent(); if ( isPattern ) renderVariables( c ); + writer.print(","+NL); indentedOutput("\""+SyntaxElement.TOCLASS.print(DEF)+"\" : "+NL); increaseIndent(); c.getDomain().accept( this ); @@ -489,9 +500,10 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig } public void visit(final PropertyTypeRestriction c) throws AlignmentException { - indentedOutput("{ class : \""+SyntaxElement.PROPERTY_TYPE_COND.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.PROPERTY_TYPE_COND.print(DEF)+"\""); increaseIndent(); if ( isPattern ) renderVariables( c ); + writer.print(","+NL); c.getType().accept( this ); writer.print(NL); decreaseIndent(); @@ -499,13 +511,15 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig } public void visit( final RelationId e ) throws AlignmentException { - indentedOutput("{ class : \""+SyntaxElement.RELATION_EXPR.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.RELATION_EXPR.print(DEF)+"\""); increaseIndent(); if ( e.getURI() != null ){ - //indentedOutput("\rdf:about\" : \""+u+"\","+NL); - indentedOutput("@id : \""+e.getURI()+"\","+NL); + writer.print(","+NL); + //indentedOutput("\rdf:about\" : \""+u+"\""); + indentedOutput("\"@id\" : \""+e.getURI()+"\""); } if ( isPattern ) renderVariables( e ); + writer.print(NL); decreaseIndent(); indentedOutput("}"); } @@ -513,16 +527,18 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig public void visit( final RelationConstruction e ) throws AlignmentException { final Constructor op = e.getOperator(); String sop = SyntaxElement.getElement( op ).print(DEF) ; - indentedOutput("{ class : \""+SyntaxElement.RELATION_EXPR.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.RELATION_EXPR.print(DEF)+"\""); increaseIndent(); if ( isPattern ) renderVariables( e ); + writer.print(","+NL); indentedOutput("\""+sop+"\" : ["+NL); increaseIndent(); + boolean first = true; for ( final PathExpression re : e.getComponents() ) { - writer.print(linePrefix); + if ( first ) { first = false; } else { writer.print(","+NL); } re.accept( this ); - writer.print(NL); } + writer.print(NL); decreaseIndent(); indentedOutputln("]"); decreaseIndent(); @@ -530,9 +546,10 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig } public void visit(final RelationCoDomainRestriction c) throws AlignmentException { - indentedOutput("{ class : \""+SyntaxElement.RELATION_CODOMAIN_COND.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.RELATION_CODOMAIN_COND.print(DEF)+"\""); increaseIndent(); if ( isPattern ) renderVariables( c ); + writer.print(","+NL); indentedOutput("\""+SyntaxElement.TOCLASS.print(DEF)+"\" : "+NL); increaseIndent(); c.getCoDomain().accept( this ); @@ -543,9 +560,10 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig } public void visit(final RelationDomainRestriction c) throws AlignmentException { - indentedOutput("{ class : \""+SyntaxElement.RELATION_DOMAIN_COND.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.RELATION_DOMAIN_COND.print(DEF)+"\""); increaseIndent(); if ( isPattern ) renderVariables( c ); + writer.print(","+NL); indentedOutput("\""+SyntaxElement.TOCLASS.print(DEF)+"\" : "+NL); increaseIndent(); c.getDomain().accept( this ); @@ -556,39 +574,44 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig } public void visit( final InstanceId e ) throws AlignmentException { - indentedOutput("{ class : \""+SyntaxElement.INSTANCE_EXPR.print(DEF)+"\","+NL); + indentedOutput("{ \"@type\" : \""+SyntaxElement.INSTANCE_EXPR.print(DEF)+"\""); increaseIndent(); if ( e.getURI() != null ){ - //indentedOutput("\rdf:about\" : \""+u+"\","+NL); - indentedOutput("@id : \""+e.getURI()+"\","+NL); + //indentedOutput("\rdf:about\" : \""+u+"\""); + writer.print(","+NL); + indentedOutput("\"@id\" : \""+e.getURI()+"\""); } if ( isPattern ) renderVariables( e ); + writer.print(NL); decreaseIndent(); indentedOutput("}"); } public void visit( final Value e ) throws AlignmentException { - indentedOutputln("{ class : \""+SyntaxElement.LITERAL.print(DEF)+"\","); + indentedOutput("{ \"@type\" : \""+SyntaxElement.LITERAL.print(DEF)+"\""); increaseIndent(); if ( e.getType() != null ) { - indentedOutputln("\""+SyntaxElement.ETYPE.print(DEF)+"\" : \""+e.getType()+"\","); + writer.print(","+NL); + indentedOutput("\""+SyntaxElement.ETYPE.print(DEF)+"\" : \""+e.getType()+"\""); } + writer.print(","+NL); indentedOutputln("\""+SyntaxElement.STRING.print(DEF)+"\" : \""+e.getValue()+"\""); decreaseIndent(); indentedOutput("}"); } public void visit( final Apply e ) throws AlignmentException { - indentedOutputln("{ class : \""+SyntaxElement.APPLY.print(DEF)+"\","); + indentedOutputln("{ \"@type\" : \""+SyntaxElement.APPLY.print(DEF)+"\","); increaseIndent(); indentedOutputln("\""+SyntaxElement.OPERATOR.print(DEF)+"\" : \""+e.getOperation()+"\","); indentedOutputln("\""+SyntaxElement.ARGUMENTS.print(DEF)+"\" : ["); increaseIndent(); + boolean first = true; for ( final ValueExpression ve : e.getArguments() ) { - writer.print(linePrefix); + if ( first ) { first = false; } else { writer.print(","+NL); } ve.accept( this ); - writer.print(","+NL); } + writer.print(NL); decreaseIndent(); indentedOutputln("]"); decreaseIndent(); @@ -596,7 +619,7 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig } public void visit( final Transformation transf ) throws AlignmentException { - indentedOutputln("{ class : \""+SyntaxElement.TRANSF.print(DEF)+"\""); + indentedOutputln("{ \"@type\" : \""+SyntaxElement.TRANSF.print(DEF)+"\","); increaseIndent(); indentedOutputln("\""+SyntaxElement.TRDIR.print(DEF)+"\" : \""+transf.getType()+"\","); indentedOutputln("\""+SyntaxElement.TRENT1.print(DEF)+"\" : "); @@ -616,9 +639,9 @@ public class JSONRendererVisitor extends IndentedRendererVisitor implements Alig public void visit( final Datatype e ) throws AlignmentException { indentedOutputln("\""+SyntaxElement.EDATATYPE.print(DEF)+"\" : "); increaseIndent(); - indentedOutput("{ class : \""+SyntaxElement.DATATYPE.print(DEF)+"\","+NL); + indentedOutputln("{ \"@type\" : \""+SyntaxElement.DATATYPE.print(DEF)+"\","); increaseIndent(); - indentedOutput("@type : \""+e.getType()+"\" }"); + indentedOutput("\"@id\" : \""+e.getType()+"\" }"); decreaseIndent(); decreaseIndent(); } diff --git a/test/src/EDOALExportTest.java b/test/src/EDOALExportTest.java index c6104f8d5f5ca3781ee296599736b0a9c0fd36f2..2df3e913876b82b476912ae9e00cc59e75030852 100644 --- a/test/src/EDOALExportTest.java +++ b/test/src/EDOALExportTest.java @@ -32,6 +32,7 @@ import org.semanticweb.owl.align.AlignmentException; import org.semanticweb.owl.align.Alignment; import fr.inrialpes.exmo.align.impl.renderer.RDFRendererVisitor; +import fr.inrialpes.exmo.align.impl.renderer.JSONRendererVisitor; import fr.inrialpes.exmo.align.impl.renderer.OWLAxiomsRendererVisitor; import fr.inrialpes.exmo.align.impl.Annotations; import fr.inrialpes.exmo.align.impl.Namespace; @@ -157,6 +158,26 @@ public class EDOALExportTest { assertEquals( str1.length(), 11623 ); } + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"testOWLRendering0"}) + public void testJSONRendering() throws Exception { + AlignmentParser aparser = new AlignmentParser( 0 ); + aparser.initAlignment( null ); + Alignment alignment = aparser.parse( "file:examples/omwg/total.xml" ); + assertNotNull( alignment ); + // Print it in a string + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + PrintWriter writer = new PrintWriter ( + new BufferedWriter( + new OutputStreamWriter( stream, "UTF-8" )), true); + AlignmentVisitor renderer = new JSONRendererVisitor( writer ); + alignment.render( renderer ); + writer.flush(); + writer.close(); + String str1 = stream.toString(); + //System.err.println(str1); + assertEquals( str1.length(), 36106 ); + } + @Test(groups = { "full", "omwg", "raw" }, dependsOnMethods = {"setUp"}) public void testExportPath() throws Exception { /* diff --git a/test/src/RendererTest.java b/test/src/RendererTest.java index 50589ef5ad1f6bdc01b30553135bfd5b23b4b27b..d9215265c4b0bf29f7b7cdd62d6a662439cb263e 100644 --- a/test/src/RendererTest.java +++ b/test/src/RendererTest.java @@ -1,7 +1,7 @@ /* * $Id$ * - * Copyright (C) INRIA, 2009-2011 + * Copyright (C) INRIA, 2009-2012 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -36,6 +36,7 @@ import fr.inrialpes.exmo.align.impl.ObjectAlignment; import fr.inrialpes.exmo.align.impl.renderer.RDFRendererVisitor; import fr.inrialpes.exmo.align.impl.renderer.COWLMappingRendererVisitor; import fr.inrialpes.exmo.align.impl.renderer.HTMLRendererVisitor; +import fr.inrialpes.exmo.align.impl.renderer.JSONRendererVisitor; import fr.inrialpes.exmo.align.impl.renderer.OWLAxiomsRendererVisitor; import fr.inrialpes.exmo.align.impl.renderer.SEKTMappingRendererVisitor; import fr.inrialpes.exmo.align.impl.renderer.SKOSRendererVisitor; @@ -209,6 +210,20 @@ public class RendererTest { assertTrue( valueSimilarTo( stream.toString().length(), 13589 ), "Rendered differently: expected "+13589+" but was "+stream.toString().length() ); } + @Test(groups = { "full", "impl", "raw" }) + public void JSONrenderer() throws Exception { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + PrintWriter writer = new PrintWriter ( + new BufferedWriter( + new OutputStreamWriter( stream, "UTF-8" )), true); + AlignmentVisitor renderer = new JSONRendererVisitor( writer ); + alignment.render( renderer ); + writer.flush(); + writer.close(); + //System.err.println( stream.toString() ); + assertTrue( valueSimilarTo( stream.toString().length(), 8414 ), "Rendered differently: expected "+8414+" but was "+stream.toString().length() ); + } + @Test(groups = { "full", "impl", "raw" }) public void XMLMetadatarenderer() throws Exception { ByteArrayOutputStream stream = new ByteArrayOutputStream();