|
[HepData-svn] r1852 - trunk/hepdata-webapp/src/main/java/cedar/hepdata/formatsblackhole at projects.hepforge.org blackhole at projects.hepforge.orgMon Mar 30 12:40:49 BST 2015
Author: whalley Date: Mon Mar 30 12:40:49 2015 New Revision: 1852 Log: substantial update to Json formatter Modified: trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/JsonFormatter.java Modified: trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/JsonFormatter.java ============================================================================== --- trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/JsonFormatter.java Fri Mar 27 16:09:40 2015 (r1851) +++ trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/JsonFormatter.java Mon Mar 30 12:40:49 2015 (r1852) @@ -13,7 +13,6 @@ import java.io.*; import org.antlr.stringtemplate.*; - import com.Ostermiller.util.SignificantFigures; @@ -23,7 +22,7 @@ StringBuffer s = new StringBuffer(); String s4=" "; if(p == null) return null; - s.append("\"record\": {\n"); + s.append("{\n"); s.append(_headerXML(p)); @@ -71,9 +70,15 @@ } for (Dataset ds : p.getDatasets()){ - s.append(s4+"\"dataset\": {\n"); + if(ds.getId() == 1) { + s.append(s4+"\"datasets\": [\n"); + } s.append(_metadataXML(ds)); - s.append(s4+"}\n"); + if(ds.getId() == p.getDatasets().size()) { + s.append(s4+"}]\n"); + } else { + s.append(s4+"},\n"); + } } s.append(_footerXML()); @@ -92,10 +97,10 @@ public static String _headerXML(Paper p) { String s4=" "; StringBuffer s = new StringBuffer(); - s.append(s4+"\"inspireid\": \""+p.getInspireId()+"\"\n"); - if(p.getArchive()!=null) { s.append(s4+"\"archive\": \""+p.getArchive().replaceAll("ARXIV","arXiv")+"\"\n"); } - s.append(s4+"\"dateupdated\": \""+p.getDateUpdated()+"\"\n"); - s.append(s4+"\"collaboration\": \""+p.getInformalName()+"\"\n"); + s.append(s4+"\"inspireid\": \""+p.getInspireId()+"\",\n"); + if(p.getArchive()!=null) { s.append(s4+"\"archive\": \""+p.getArchive().replaceAll("ARXIV","arXiv")+"\",\n"); } + s.append(s4+"\"dateupdated\": \""+p.getDateUpdated()+"\",\n"); + s.append(s4+"\"collaboration\": \""+p.getInformalName()+"\",\n"); for (String comment : p.getComments()){ s.append(" \"comment\": \""+comment+"\",\n"); } @@ -118,69 +123,96 @@ for (String ct : ds.getComments()){ if(ct.startsWith("Location:")) { location = ct.replaceFirst("Location:",""); - s.append(s4+s4+"\"label\": \""+ct.replaceFirst("Location:","Data from")+" from: "+p.getTitle()+"\"\n"); + s.append(s4+s4+"{\"label\": \""+ct.replaceFirst("Location:","Data from")+" from: "+p.getTitle()+"\",\n"); } } for (String ct : ds.getComments()){ if(!ct.startsWith("Location:")) { - s.append(s4+s4+"\"comment\": \""+ct.replaceFirst("VERBATIM","").replaceAll(">",">").replaceAll("<","<")+"\"\n"); + s.append(s4+s4+"\"comment\": \""+ct.replaceFirst("VERBATIM","").replaceAll(">",">").replaceAll("<","<")+"\",\n"); } } for (String dsreac : ds.getDsReactions()){ - s.append(s4+s4+"\"reackey\": \""+dsreac+"\"\n"); + s.append(s4+s4+"\"reackey\": \""+dsreac+"\",\n"); } for (String dsobs : ds.getDsObservables()){ - s.append(s4+s4+"\"obskey\": \""+dsobs+"\"\n"); + s.append(s4+s4+"\"obskey\": \""+dsobs+"\",\n"); } for (String dsplab : ds.getDsPlabs()){ - s.append(s4+s4+"\"plabkey\": \""+dsplab+"\"\n"); + s.append(s4+s4+"\"plabkey\": \""+dsplab+"\",\n"); } + s.append(s4+s4+"\"xaxes\": [\n"); for (XAxis xax : ds.getXAxes()){ - s.append(s4+s4+"\"xaxis\": {\n"); - s.append(s4+s4+s4+"\"xheader\": \""+xax.getHeader()+"\"\n"); - for (Bin bin : xax.getBins()){ - s.append(s4+s4+s4+"\"bin\": {\n"); - s.append(s4+s4+s4+s4+"\"id\": \""+bin.getId()+"\"\n"); - s.append(s4+s4+s4+s4+"\"relation\": \""+bin.getRelation()+"\"\n"); - s.append(s4+s4+s4+s4+"\"focus\": \""+bin.getFocus()+"\"\n"); - s.append(s4+s4+s4+s4+"\"high\": \""+bin.getHighValue()+"\"\n"); - s.append(s4+s4+s4+s4+"\"low\": \""+bin.getLowValue()+"\"\n"); - if(bin.getDescription() != null ) { s.append(s4+s4+s4+s4+"\"description\": \""+bin.getDescription()+"\"\n"); } + s.append(s4+s4+s4+"{\"xheader\": \""+xax.getHeader()+"\", \n"); + s.append(s4+s4+s4+"\"bins\": [\n"); + for (Bin bin : xax.getBins()){ + s.append(s4+s4+s4+s4+"{\"id\": \""+bin.getId()+"\", "); + if(bin.getDescription() != null ) { s.append("\"description\": \""+bin.getDescription()+"\", \n"); } + s.append("\"relation\": \""+bin.getRelation()+"\", "); + s.append("\"focus\": \""+bin.getFocus()+"\", "); + s.append("\"high\": \""+bin.getHighValue()+"\", "); + s.append("\"low\": \""+bin.getLowValue()+"\""); + if(bin.getId() < xax.getBins().size()) { + s.append("},\n"); + } else{ + // s.append("}] * end of bin\n"); + s.append("}]\n"); + } + } + if(xax.getId() < ds.getXAxes().size()){ + s.append(s4+s4+s4+"},\n"); + } else { s.append(s4+s4+s4+"}\n"); } - s.append(s4+s4+"}\n"); } + s.append(s4+s4+"],\n"); + s.append(s4+s4+"\"yaxes\": [\n"); for (YAxis yax : ds.getYAxes()){ - s.append(s4+s4+"\"yaxis\": {\n"); - s.append(s4+s4+s4+"\"yheader\": \""+yax.getHeader()+"\"\n"); + s.append(s4+s4+s4+"{\"yheader\": \""+yax.getHeader()+"\", \n"); for (Property prop : yax.getProperties()){ - s.append(s4+s4+s4+"\"property\": \""+prop.getFull()+"\"\n"); + s.append(s4+s4+s4+"\"property\": \""+prop.getFull()+"\", \n"); } for (String comment : yax.getComments()){ - s.append(s4+s4+s4+"\"comment\": \""+comment+"\"\n"); + s.append(s4+s4+s4+"\"comment\": \""+comment+"\", \n"); } + s.append(s4+s4+s4+"\"points\": [\n"); for (Point point : yax.getPoints()){ - s.append(s4+s4+s4+"\"point\": {\n"); - s.append(s4+s4+s4+s4+"\"id\": \""+point.getId()+"\"\n"); - s.append(s4+s4+s4+s4+"\"value\": \""+point.getValue()+"\"\n"); - for(PointError error : point.getErrors()){ - s.append(s4+s4+s4+s4+"\"error\": {\n"); - s.append(s4+s4+s4+s4+s4+"\"plus\": \""+error.getPlus()+"\"\n"); - s.append(s4+s4+s4+s4+s4+"\"minus\": \""+error.getMinus()+"\"\n"); - s.append(s4+s4+s4+s4+s4+"\"norm\": \""+error.getNormType()+"\"\n"); - s.append(s4+s4+s4+s4+s4+"\"type\": \""+error.getSourceType()+"\"\n"); - - s.append(s4+s4+s4+s4+"}\n"); + s.append(s4+s4+s4+s4+"{\"id\": \""+point.getId()+"\", "); + s.append("\"value\": \""+point.getValue()+"\""); + if(point.getErrors().size()>0){ + s.append(", "); + s.append("\"errors\": [ "); + boolean first = true; + for(PointError error : point.getErrors()){ + if(!first) { s.append(s4+s4+s4+s4+s4+s4+s4+s4+s4); } + first = false; + s.append("{\"plus\": \""+error.getPlus()+"\", "); + s.append("\"minus\": \""+error.getMinus()+"\", "); + s.append("\"norm\": \""+error.getNormType()+"\", "); + s.append("\"type\": \""+error.getSourceType()+"\"}"); + if(error.getId() < point.getErrors().size()) { + s.append(",\n"); + } else { + s.append("]\n"); + } + } } + if(point.getId() < yax.getPoints().size()){ + s.append(s4+s4+s4+s4+"},\n"); + } else { + s.append(s4+s4+s4+s4+"}]\n"); + } + } + if(yax.getId() < ds.getYAxes().size()){ + s.append(s4+s4+s4+"},\n"); + } else { s.append(s4+s4+s4+"}\n"); } - - s.append(s4+s4+"}\n"); } + s.append(s4+s4+"]\n"); return s.toString(); } }
More information about the HepData-svn mailing list |