[HepData-svn] r1905 - trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Tue Nov 10 16:21:14 GMT 2015


Author: watt
Date: Tue Nov 10 16:21:14 2015
New Revision: 1905

Log:
YAML formatter: write headers and qualifiers even if no data points

Modified:
   trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java

Modified: trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java
==============================================================================
--- trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java	Mon Nov  9 16:56:03 2015	(r1904)
+++ trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java	Tue Nov 10 16:21:14 2015	(r1905)
@@ -330,271 +330,283 @@
 
 	//s.append("name: 'Table "+ds.getId()+"'\n");
         s.append("independent_variables:\n");
-	if (npoints > 0) {
-	    for (XAxis xax : ds.getXAxes()){
-	    	String name=xax.getHeader();
-		    String unit="";
-		    if(xax.getHeader().contains(" IN ")){
-		        name=xax.getHeader().substring(0,xax.getHeader().indexOf(" IN ")).trim();
-		        unit=xax.getHeader().substring(xax.getHeader().indexOf(" IN ")+4).trim();
-		    } else if (xax.getHeader().contains(" in ")) {
-		        name=xax.getHeader().substring(0,xax.getHeader().indexOf(" in ")).trim();
-		        unit=xax.getHeader().substring(xax.getHeader().indexOf(" in ")+4).trim();
-		    }
-		    s.append(sp+"header: {name: '"+name.replaceAll("'","''")+"'");
-		    if(!unit.equals("")){ s.append(", units: '"+unit+"'"); }
-	    	s.append("}\n");
-		    s.append(s4+"values:\n");
-		    for (int ip=1; ip<npoints+1; ip++){
-                Bin bin = xax.getBin(ip);
-                if(bin == null) {
-                    s.append(s4+sp+"{value: '-'}\n");
-                }
-                else {
-		            boolean first=true;
-		            s.append(s4+sp+"{");
+	//if (npoints > 0) {
+	for (XAxis xax : ds.getXAxes()){
+	    String name=xax.getHeader();
+	    String unit="";
+	    if(xax.getHeader().contains(" IN ")){
+		name=xax.getHeader().substring(0,xax.getHeader().indexOf(" IN ")).trim();
+		unit=xax.getHeader().substring(xax.getHeader().indexOf(" IN ")+4).trim();
+	    } else if (xax.getHeader().contains(" in ")) {
+		name=xax.getHeader().substring(0,xax.getHeader().indexOf(" in ")).trim();
+		unit=xax.getHeader().substring(xax.getHeader().indexOf(" in ")+4).trim();
+	    }
+	    s.append(sp+"header: {name: '"+name.replaceAll("'","''")+"'");
+	    if(!unit.equals("")){ s.append(", units: '"+unit+"'"); }
+	    s.append("}\n");
+	    if (npoints > 0) {
+		s.append(s4+"values:\n");
+	    } else {
+		s.append(s4+"values: []\n");
+	    }
+	    for (int ip=1; ip<npoints+1; ip++){
+		Bin bin = xax.getBin(ip);
+		if(bin == null) {
+		    s.append(s4+sp+"{value: '-'}\n");
+		}
+		else {
+		    boolean first=true;
+		    s.append(s4+sp+"{");
 		    //         if(bin.getId() != null ) { 
 		    //             s.append("id: "+bin.getId()); 
 		    //             first=false; 
 		    //         }
                 
-		            boolean asymmfocus = false;
-		            if (bin.getLowValue() != null && bin.getHighValue() != null && bin.getFocus() != null) {
-			            double diff = bin.getFocus() - (bin.getLowValue() + bin.getHighValue()) / 2.0;
-			            if (Math.abs(diff/bin.getFocus()) > 1E-6) { asymmfocus = true; }
-		            }
-		            boolean haswidth = false;
-		            if (bin.getLowValue() != null && bin.getHighValue() != null) {
-			            double diff = bin.getHighValue() - bin.getLowValue();
-			            double mean = (bin.getLowValue() + bin.getHighValue()) / 2.0;
-			            if((Math.abs(diff/mean) > 1E-6) || (mean == 0.0 && Math.abs(diff) > 1E-6)){ haswidth = true;}
-		            }
+		    boolean asymmfocus = false;
+		    if (bin.getLowValue() != null && bin.getHighValue() != null && bin.getFocus() != null) {
+			double diff = bin.getFocus() - (bin.getLowValue() + bin.getHighValue()) / 2.0;
+			if (Math.abs(diff/bin.getFocus()) > 1E-6) { asymmfocus = true; }
+		    }
+		    boolean haswidth = false;
+		    if (bin.getLowValue() != null && bin.getHighValue() != null) {
+			double diff = bin.getHighValue() - bin.getLowValue();
+			double mean = (bin.getLowValue() + bin.getHighValue()) / 2.0;
+			if((Math.abs(diff/mean) > 1E-6) || (mean == 0.0 && Math.abs(diff) > 1E-6)){ haswidth = true;}
+		    }
 
-		            if(bin.getRelation() != null && bin.getRelation() != Relation.EQUALS ) { 
-			            if(!first) { s.append(", "); } 
-			            s.append("value: '"+bin.getRelation()+" ");
-			            if (bin.getLowValue() != null) {
-			                s.append(bin.getLowValue());
-			            } else if (bin.getHighValue() != null) {
-			                s.append(bin.getHighValue());
-			            }
-			            s.append("'");
-			            first=false; 
-		            }
-		            else if(bin.getFocus() != null) {
-			            if(asymmfocus || !haswidth) { 
-			                if(!first) {s.append(", "); } 
-			                if(bin.getFocusLength()!=null) { 
-				               s.append("value: "+forms(bin.getFocus(),bin.getFocusLength())); 
-			                } else {
-				                s.append("value: "+bin.getFocus()); 
-			                }
-			                first=false; 
-			            }
-		            }
-		            if(bin.getLowValue() != null && haswidth) {
-			            if(!first) { s.append(", "); } 
-			            if(bin.getLowValueLength()!=null) { 
-			                s.append("low: "+forms(bin.getLowValue(),bin.getLowValueLength())); 
-			            } else {
-			                s.append("low: "+bin.getLowValue()); 
-			            }
-			            first=false; 
-		            }
-		            if(bin.getHighValue() != null && haswidth) {
-			            if(!first) {s.append(", "); } 
-			            if(bin.getHighValueLength()!=null) { 
-			                s.append("high: "+forms(bin.getHighValue(),bin.getHighValueLength())); 
-			            } else {
-			                s.append("high: "+bin.getHighValue()); 
-			            }
-			            first=false; 
-		            }
-		            if(bin.getDescription() != null ) {  
-			            if(!first) { s.append(", "); } 
-			            s.append("value: '"+bin.getDescription().replaceAll("'","''")+"'");  
-			            first=false;
-		            }
-		            if (bin.getDescription() == null && bin.getFocus() == null && bin.getLowValue() == null && bin.getHighValue() == null) {
-			            s.append("value: '-'");
-		            }
-		            if(bin.getId() < xax.getBins().size()) {
-			            s.append("}");
-		            }
-		            else{s.append("}");}  
-		            s.append("\n");
-		         }
+		    if(bin.getRelation() != null && bin.getRelation() != Relation.EQUALS ) { 
+			if(!first) { s.append(", "); } 
+			s.append("value: '"+bin.getRelation()+" ");
+			if (bin.getLowValue() != null) {
+			    s.append(bin.getLowValue());
+			} else if (bin.getHighValue() != null) {
+			    s.append(bin.getHighValue());
+			}
+			s.append("'");
+			first=false; 
+		    }
+		    else if(bin.getFocus() != null) {
+			if(asymmfocus || !haswidth) { 
+			    if(!first) {s.append(", "); } 
+			    if(bin.getFocusLength()!=null) { 
+				s.append("value: "+forms(bin.getFocus(),bin.getFocusLength())); 
+			    } else {
+				s.append("value: "+bin.getFocus()); 
+			    }
+			    first=false; 
+			}
+		    }
+		    if(bin.getLowValue() != null && haswidth) {
+			if(!first) { s.append(", "); } 
+			if(bin.getLowValueLength()!=null) { 
+			    s.append("low: "+forms(bin.getLowValue(),bin.getLowValueLength())); 
+			} else {
+			    s.append("low: "+bin.getLowValue()); 
+			}
+			first=false; 
+		    }
+		    if(bin.getHighValue() != null && haswidth) {
+			if(!first) {s.append(", "); } 
+			if(bin.getHighValueLength()!=null) { 
+			    s.append("high: "+forms(bin.getHighValue(),bin.getHighValueLength())); 
+			} else {
+			    s.append("high: "+bin.getHighValue()); 
+			}
+			first=false; 
 		    }
+		    if(bin.getDescription() != null ) {  
+			if(!first) { s.append(", "); } 
+			s.append("value: '"+bin.getDescription().replaceAll("'","''")+"'");  
+			first=false;
+		    }
+		    if (bin.getDescription() == null && bin.getFocus() == null && bin.getLowValue() == null && bin.getHighValue() == null) {
+			s.append("value: '-'");
+		    }
+		    if(bin.getId() < xax.getBins().size()) {
+			s.append("}");
+		    }
+		    else{s.append("}");}  
+		    s.append("\n");
+		}
 	    }
 	}
+	//}
 
         s.append("dependent_variables:\n");
 
-	if (npoints > 0) {
-	    for (YAxis yax : ds.getYAxes()){
-		String name=yax.getHeader();
-		String unit="";
-		if(yax.getHeader().contains(" IN ")){
-		    name=yax.getHeader().substring(0,yax.getHeader().indexOf(" IN ")).trim();
-		    unit=yax.getHeader().substring(yax.getHeader().indexOf(" IN ")+4).trim();
-		} else if (yax.getHeader().contains(" in ")) {
-		    name=yax.getHeader().substring(0,yax.getHeader().indexOf(" in ")).trim();
-		    unit=yax.getHeader().substring(yax.getHeader().indexOf(" in ")+4).trim();
-		}
-		s.append(sp+"header: {name: '"+name.replaceAll("'","''")+"'");
-		if(!unit.equals("")){ s.append(", units: '"+unit+"'"); }
-		s.append("}\n");
-		boolean first=true;
-                List <String> storelist = new ArrayList();
-		for (Property prop : yax.getProperties()){
-                    StringBuffer stemp = new StringBuffer();
-		    if(first) { s.append(s4+"qualifiers:\n"); }
-		    first=false;
+	//if (npoints > 0) {
+	for (YAxis yax : ds.getYAxes()){
+	    String name=yax.getHeader();
+	    String unit="";
+	    if(yax.getHeader().contains(" IN ")){
+		name=yax.getHeader().substring(0,yax.getHeader().indexOf(" IN ")).trim();
+		unit=yax.getHeader().substring(yax.getHeader().indexOf(" IN ")+4).trim();
+	    } else if (yax.getHeader().contains(" in ")) {
+		name=yax.getHeader().substring(0,yax.getHeader().indexOf(" in ")).trim();
+		unit=yax.getHeader().substring(yax.getHeader().indexOf(" in ")+4).trim();
+	    }
+	    s.append(sp+"header: {name: '"+name.replaceAll("'","''")+"'");
+	    if(!unit.equals("")){ s.append(", units: '"+unit+"'"); }
+	    s.append("}\n");
+	    boolean first=true;
+	    List <String> storelist = new ArrayList();
+	    for (Property prop : yax.getProperties()){
+		StringBuffer stemp = new StringBuffer();
+		if(first) { s.append(s4+"qualifiers:\n"); }
+		first=false;
 
-		    stemp.append(s4+sp+"{name: '"+prop.getName().replaceAll("'","''").replaceFirst("\\.","zzzz")+"'");
+		stemp.append(s4+sp+"{name: '"+prop.getName().replaceAll("'","''").replaceFirst("\\.","zzzz")+"'");
 
-		    if(prop.getFocus()!=null || prop.getLowValue()!=null || prop.getHighValue()!=null){
-			stemp.append(", value: '");
-		    }
-		    if (prop.getFocus() != null){
-			stemp.append(prop.getFocus());
-		    }
+		if(prop.getFocus()!=null || prop.getLowValue()!=null || prop.getHighValue()!=null){
+		    stemp.append(", value: '");
+		}
+		if (prop.getFocus() != null){
+		    stemp.append(prop.getFocus());
+		}
                 
-		    if (prop.getLowValue() != null && prop.getHighValue() != null &&
-			prop.getLowValue().equals(prop.getHighValue())) {
-			stemp.append(prop.getLowValue());
-		    } else {
-			if(prop.getFocus() != null) { stemp.append(" ("); }
-			stemp.append(prop.getLowValue());
-			stemp.append("-");
-			stemp.append(prop.getHighValue());
-			if(prop.getFocus() != null) { stemp.append(")"); }
-		    }
-		    if(prop.getFocus()!=null || prop.getLowValue()!=null || prop.getHighValue()!=null){
-			stemp.append("'");
-		    }
+		if (prop.getLowValue() != null && prop.getHighValue() != null &&
+		    prop.getLowValue().equals(prop.getHighValue())) {
+		    stemp.append(prop.getLowValue());
+		} else {
+		    if(prop.getFocus() != null) { stemp.append(" ("); }
+		    stemp.append(prop.getLowValue());
+		    stemp.append("-");
+		    stemp.append(prop.getHighValue());
+		    if(prop.getFocus() != null) { stemp.append(")"); }
+		}
+		if(prop.getFocus()!=null || prop.getLowValue()!=null || prop.getHighValue()!=null){
+		    stemp.append("'");
+		}
                     
-		    if (prop.getUnit().toString().equals("") || !prop.getUnit().isDimensionless()) {
-			stemp.append(", units: '");
-			stemp.append(prop.getUnit().toString()+"'");
-		    }
+		if (prop.getUnit().toString().equals("") || !prop.getUnit().isDimensionless()) {
+		    stemp.append(", units: '");
+		    stemp.append(prop.getUnit().toString()+"'");
+		}
                   
-		    stemp.append("}\n");
-                    storelist.add(stemp.toString());
-                }
-		for (String comment : yax.getComments()){
-                    StringBuffer stemp = new StringBuffer();
+		stemp.append("}\n");
+		storelist.add(stemp.toString());
+	    }
+	    for (String comment : yax.getComments()){
+		StringBuffer stemp = new StringBuffer();
+		if(first) { s.append(s4+"qualifiers:\n"); }
+		first=false;
+		unit="";
+		String type=comment.substring(0,comment.indexOf(":")-1).trim();
+		String value=comment.substring(comment.indexOf(":")+1).trim();
+		//                String type="SYS";
+		//                String value=comment;
+		//                if(comment.indexOf(":")>-1) {
+		//                    type=comment.substring(0,comment.indexOf(":")-1).trim();
+		//                    value=comment.substring(comment.indexOf(":")+1).trim();
+		//                }
+		if(type.contains(" IN ")){
+		    unit=type.substring(type.indexOf(" IN ")+4).trim();
+		    type=type.substring(0,type.indexOf(" IN ")).trim();
+		} else if (type.contains(" in ")) {
+		    unit=type.substring(type.indexOf(" in ")+4).trim();
+		    type=type.substring(0,type.indexOf(" in ")).trim();
+		}
+		stemp.append(s4+sp+"{name: '"+type.replaceAll("'","''").replaceFirst("\\.","zzzz")+"'");
+		stemp.append(", value: '"+ value.replaceAll("'","''")+"'"); 
+		//                if(type.equals("SYS") && value.indexOf("//")>-1){
+		//                    String comments[] = value.replaceAll("////","//").split("//");
+		//                    for( int i=0; i<comments.length; i++) { 
+		//                        s.append(s4+sp+"{name: '"+type.replaceAll("'","''")+"'");
+		//                        s.append(", value: '"+ comments[i].replaceAll("'","''")+"'"); 
+		//                        if(i!=comments.length-1) { s.append("}\n"); }
+		//                    }   
+		//                } 
+		//                else { 
+		//                    s.append(s4+sp+"{name: '"+type.replaceAll("'","''")+"'");
+		//                    s.append(", value: '"+ value.replaceAll("'","''")+"'"); 
+		//                } 
+		if(!unit.equals("")){
+		    stemp.append(", units: '"+ unit+"'");
+		}
+		stemp.append("}\n");
+		storelist.add(stemp.toString());
+	    }
+	    Collections.sort(storelist);
+	    for (int i=0; i<storelist.size(); i++){
+		s.append(storelist.get(i).replaceFirst("zzzz",""));
+	    }         
+
+	    for(Uncertainty error : ds.getErrors()){
+		if(error.getPlus()==0.0){
 		    if(first) { s.append(s4+"qualifiers:\n"); }
 		    first=false;
-		    unit="";
-		    String type=comment.substring(0,comment.indexOf(":")-1).trim();
-		    String value=comment.substring(comment.indexOf(":")+1).trim();
-		    //                String type="SYS";
-		    //                String value=comment;
-		    //                if(comment.indexOf(":")>-1) {
-		    //                    type=comment.substring(0,comment.indexOf(":")-1).trim();
-		    //                    value=comment.substring(comment.indexOf(":")+1).trim();
-		    //                }
-		    if(type.contains(" IN ")){
-			unit=type.substring(type.indexOf(" IN ")+4).trim();
-			type=type.substring(0,type.indexOf(" IN ")).trim();
-		    } else if (type.contains(" in ")) {
-			unit=type.substring(type.indexOf(" in ")+4).trim();
-			type=type.substring(0,type.indexOf(" in ")).trim();
-		    }
-		    stemp.append(s4+sp+"{name: '"+type.replaceAll("'","''").replaceFirst("\\.","zzzz")+"'");
-		    stemp.append(", value: '"+ value.replaceAll("'","''")+"'"); 
-		    //                if(type.equals("SYS") && value.indexOf("//")>-1){
-		    //                    String comments[] = value.replaceAll("////","//").split("//");
-		    //                    for( int i=0; i<comments.length; i++) { 
-		    //                        s.append(s4+sp+"{name: '"+type.replaceAll("'","''")+"'");
-		    //                        s.append(", value: '"+ comments[i].replaceAll("'","''")+"'"); 
-		    //                        if(i!=comments.length-1) { s.append("}\n"); }
-		    //                    }   
-		    //                } 
-		    //                else { 
-		    //                    s.append(s4+sp+"{name: '"+type.replaceAll("'","''")+"'");
-		    //                    s.append(", value: '"+ value.replaceAll("'","''")+"'"); 
-		    //                } 
-		    if(!unit.equals("")){
-			stemp.append(", units: '"+ unit+"'");
-		    }
-		    stemp.append("}\n");
-                    storelist.add(stemp.toString());
-		}
-                Collections.sort(storelist);
-                for (int i=0; i<storelist.size(); i++){
-                    s.append(storelist.get(i).replaceFirst("zzzz",""));
-                }         
-
-		for(Uncertainty error : ds.getErrors()){
-		    if(error.getPlus()==0.0){
-			s.append(s4+sp+"{name: 'SYS', value: '"+error.getComment()+"'}\n");
-		    }
+		    s.append(s4+sp+"{name: 'SYS', value: '"+error.getComment()+"'}\n");
 		}
-		for(Uncertainty error : yax.getErrors()){
-		    if(error.getPlus()==0.0){
-			s.append(s4+sp+"{name: 'SYS', value: '"+error.getComment()+"'}\n");
-		    }
+	    }
+	    for(Uncertainty error : yax.getErrors()){
+		if(error.getPlus()==0.0){
+		    if(first) { s.append(s4+"qualifiers:\n"); }
+		    first=false;
+		    s.append(s4+sp+"{name: 'SYS', value: '"+error.getComment()+"'}\n");
 		}
+	    }
 
+	    if (npoints > 0) {
 		s.append(s4+"values:\n");
+	    } else {
+		s.append(s4+"values: []\n");
+	    }
 
-		for (int ip=1; ip<npoints+1; ip++){
-		    Point point = yax.getPoint(ip);
-		    if(point==null){
-			//   s.append(s4+sp+"id: "+ip+"\n");
-			//   s.append(s4+s4+"value: '-'\n");
-			s.append(s4+sp+"value: '-'\n");
-		    }
-		    else{            
-			//    s.append(s4+sp+"id: "+point.getId()+"\n");
-			//    s.append(s4+s4+"value: ");
-			s.append(s4+sp+"value: ");
-			if(point.getRelation() != Relation.EQUALS){
-			    s.append("'"+point.getRelation());
-			}
-			//                    s.append(point.getValue()+" "+point.getValueLength()+"\n");
-			if (point.getDescription() != null) {
-			    if(point.getRelation() == Relation.EQUALS){
-				s.append("'");
-			    }
-			    s.append(point.getDescription().replaceAll("'","''")+"'");
-			} else {
-			    s.append(point.getValue());
-			}
-			if (point.getRelation() != Relation.EQUALS) {
+	    for (int ip=1; ip<npoints+1; ip++){
+		Point point = yax.getPoint(ip);
+		if(point==null){
+		    //   s.append(s4+sp+"id: "+ip+"\n");
+		    //   s.append(s4+s4+"value: '-'\n");
+		    s.append(s4+sp+"value: '-'\n");
+		}
+		else{            
+		    //    s.append(s4+sp+"id: "+point.getId()+"\n");
+		    //    s.append(s4+s4+"value: ");
+		    s.append(s4+sp+"value: ");
+		    if(point.getRelation() != Relation.EQUALS){
+			s.append("'"+point.getRelation());
+		    }
+		    //                    s.append(point.getValue()+" "+point.getValueLength()+"\n");
+		    if (point.getDescription() != null) {
+			if(point.getRelation() == Relation.EQUALS){
 			    s.append("'");
 			}
-			s.append("\n");
-                        boolean firstError=true;
-//			if (point.getErrors().size() > 0
-//			    || yax.getErrors().size() > 0
-//			    || ds.getErrors().size() > 0) {
-//			    s.append(s4+s4+"errors:\n");
-//			}
-			if(point.getErrors().size()>0){
-			    for(Uncertainty error : point.getErrors()){
-                                if(firstError) { s.append(s4+s4+"errors:\n"); firstError=false; }
-				s.append(_formatError(error,"point"));
-			    }
+			s.append(point.getDescription().replaceAll("'","''")+"'");
+		    } else {
+			s.append(point.getValue());
+		    }
+		    if (point.getRelation() != Relation.EQUALS) {
+			s.append("'");
+		    }
+		    s.append("\n");
+		    boolean firstError=true;
+		    //			if (point.getErrors().size() > 0
+		    //			    || yax.getErrors().size() > 0
+		    //			    || ds.getErrors().size() > 0) {
+		    //			    s.append(s4+s4+"errors:\n");
+		    //			}
+		    if(point.getErrors().size()>0){
+			for(Uncertainty error : point.getErrors()){
+			    if(firstError) { s.append(s4+s4+"errors:\n"); firstError=false; }
+			    s.append(_formatError(error,"point"));
 			}
-			if(yax.getErrors().size()>0){
-			    for(Uncertainty error : yax.getErrors()){
-                                if(firstError && error.getPlus()!=0.0) { s.append(s4+s4+"errors:\n"); firstError=false; }
-				if(error.getPlus()!=0.0) s.append(_formatError(error,"axis"));
-			    }
+		    }
+		    if(yax.getErrors().size()>0){
+			for(Uncertainty error : yax.getErrors()){
+			    if(firstError && error.getPlus()!=0.0) { s.append(s4+s4+"errors:\n"); firstError=false; }
+			    if(error.getPlus()!=0.0) s.append(_formatError(error,"axis"));
 			}
-			if(ds.getErrors().size()>0){
-			    for(Uncertainty error : ds.getErrors()){
-                                if(firstError && error.getPlus()!=0.0) { s.append(s4+s4+"errors:\n"); firstError=false; }
-				if(error.getPlus()!=0.0) s.append(_formatError(error,"dataset"));
-			    }
+		    }
+		    if(ds.getErrors().size()>0){
+			for(Uncertainty error : ds.getErrors()){
+			    if(firstError && error.getPlus()!=0.0) { s.append(s4+s4+"errors:\n"); firstError=false; }
+			    if(error.getPlus()!=0.0) s.append(_formatError(error,"dataset"));
 			}
 		    }
 		}
 	    }
 	}
+	//}
         return s.toString();
     }
 


More information about the HepData-svn mailing list