00001 package cedar.hepdata.webapp.pages;
00002
00003
00004 import cedar.hepdata.model.*;
00005 import cedar.hepdata.xml.*;
00006 import cedar.hepdata.util.*;
00007 import cedar.hepdata.db.*;
00008
00009 import java.io.*;
00010 import java.util.*;
00011 import javax.servlet.*;
00012 import javax.servlet.http.*;
00013
00014 import java.text.*;
00015 import java.lang.*;
00016
00017 import org.exolab.castor.xml.Unmarshaller;
00018
00019
00025 public class DisplayPaperFromXml extends HttpServlet {
00026
00028 public String getServletInfo() {
00029 return "Test servlet. Do not use.";
00030 }
00031
00032
00034 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
00035 response.setContentType("text/html");
00036 PrintWriter out = response.getWriter();
00037
00038 out.println("<h1>Shape XML demo (XML)</h1>");
00039 out.println("<p>Reading in data from XML file !!!</p>");
00040
00041 out.println(request.getParameter("irn"));
00042 String irn = request.getParameter("irn");
00043 int spiresid = Integer.parseInt(irn);
00044
00045 String dataset = null;
00046 String yaxis = null;
00047 dataset = request.getParameter("dataset");
00048 yaxis = request.getParameter("yaxis");
00049
00050 Paper p = null;
00051 try {
00052
00053 Data data = XMLUtils.getDataFromHepML(new File("/home/whalley/hd/hepdata-migration/shapes-hepml/"+ irn + ".hep.xml"));
00054 if (data != null) p = data.getFirstPaper();
00056 if (p == null) throw new Exception("Null paper");
00057
00058 this.displayHeader(p,out);
00059 if(dataset == null){
00060 this.displayDatasetError(p,out,String.valueOf(1));
00061 }
00062 else{
00063 this.displayDatasetError(p,out,dataset);
00064 }
00065 if(dataset == null){
00066 this.displayDatasets(p,out);
00067 }
00068 else if(yaxis == null){
00069 this.displayDataset(p,out,dataset);
00070 }
00071 else {
00072 this.displayDataset(p,out,dataset,yaxis);
00073 }
00074 }
00075 catch (Exception e) {
00076 out.println("Exception: " + e.toString());
00077 out.println("<pre>");
00078 e.printStackTrace(out);
00079 out.println("</pre>");
00080 return;
00081 }
00082 }
00083
00085 private void displayDatasets(Paper p,PrintWriter out){
00086 int nds = 0;
00087 for ( Dataset d : p.getDatasets() ) {
00088 nds++;
00089 String dataset = String.valueOf(nds);
00090 this.displayDataset(p,out,dataset);
00091 }
00092 }
00093
00095 private void displayDataset(Paper p,PrintWriter out,String dataset){
00096 int ds = Integer.parseInt(dataset);
00097 Dataset d = p.getDataset(ds);
00098 int ya=0;
00099 for (YAxis y : d.getYAxes()){
00100 ya++;
00101 String yaxis = String.valueOf(ya);
00102 this.displayDataset(p,out,dataset,yaxis);
00103 }
00104 }
00105
00107 private void displayDataset(Paper p,PrintWriter out,String dataset,String yaxis){
00108 int ds = Integer.parseInt(dataset);
00109 int ya = Integer.parseInt(yaxis);
00110 Dataset d = p.getDataset(ds);
00111 YAxis y = d.getYAxis(ya);
00112
00113
00114 int nx = d.getXAxes().size();
00115 int npoints = 0;
00116 for (XAxis x : d.getXAxes()){npoints = x.getBins().size();}
00117
00118
00120
00121
00122 for (AxisError ae : y.getErrors() ) {
00123 out.println("\nAxis Error");
00124 if (ae.getComment() != null) { out.println(ae.getComment() ); }
00125 if (ae.getPlus() != null) { out.println(ae.getPlus() ); }
00126 if (ae.getMinus() != null) { out.println(ae.getMinus() ); }
00127 if (ae.getSourceType() != null) { out.println(ae.getSourceType() ); }
00128 if (ae.getNormType() != null) { out.println(ae.getNormType() ); }
00129 }
00130
00131
00132 out.println("<table border=1>");
00133 int nspan = nx+1;
00134 out.println("<th colspan=" + nspan + ">Dataset " + ds + " yaxis " + ya + "</th></tr><tr>");
00135 for ( Reaction reac : y.getReactions() ){
00136 out.println("<th colspan=" + nspan +">");
00137
00138 for ( Particle part : reac.getInitialState() ){
00139 out.println(part.getName() + " ");
00140 }
00141 out.println("-->");
00142 for ( Particle part : reac.getFinalState() ){
00143 if (part.getMultRelation() != Relation.EQUALS) {out.println( part.getMultRelation() );}
00144 if (part.getMultiplicity() > 1) {out.println( part.getMultiplicity() );}
00145 out.println(part.getName() + " ");
00146 }
00147 out.println("</th></tr>");
00148 }
00149
00150
00151 for (String comment : y.getComments()){
00152 out.println("<tr>");
00153 out.println("<th colspan=" + nspan +">" + comment + "</th></tr>");
00154 }
00155
00156
00157 String sobs = y.getObservable();
00158 out.println("<th colspan=" + nspan + ">" + "obs:"+ sobs + "</th></tr>");
00159
00160
00161 for ( Property prop: y.getProperties() ){
00162 out.println("<tr>");
00163 out.println("<th colspan=" + nspan +">");
00164 out.println( prop.getName() + " : ");
00165 if (prop.getLowValue() != null && prop.getHighValue() != null &&
00166 prop.getLowValue().equals(prop.getHighValue())) {
00167 out.println(prop.getLowValue());
00168 }
00169 else{out.println(prop.getLowValue() + " TO " + prop.getHighValue());}
00170 if (!prop.getUnit().toString().equals("num")){
00171 out.println(" " + prop.getUnit().toString());
00172 }
00173 out.println("</th>" ) ;
00174 }
00175
00176
00177 out.println("<tr>");
00178 for (XAxis x : d.getXAxes()){
00179 out.println("<th>" + x.getHeader() + "</th>");
00180 }
00181 out.println("<th>" + y.getHeader() + "</th>");
00182 out.println("</tr>");
00183
00184
00185
00186 Double f = null;
00187 Double high = null;
00188 Double low = null;
00189 out.println("<tr>");
00190 for (int ix=1 ; ix<=nx; ix++){
00191 XAxis xaxis = d.getXAxis(ix);
00192 out.println("<td><pre>");
00193 for (int ip=1 ; ip<=npoints; ip++){
00194 for (Bin b : xaxis.getBins()){
00195 if(b.getId() == ip) {
00196 low = b.getLowValue();
00197 high = b.getHighValue();
00198 f = b.getFocus();
00199 if(low != null){
00200 out.println(forms(high,8,out) + " TO " + forms(low,8,out));
00201 }
00202 else{
00203 out.println(forms(f,8,out));
00204 }
00205 }
00206 }
00207 }
00208 out.println("</pre></td>");
00209 }
00210
00211
00212
00213 StringBuffer s = new StringBuffer();
00214 s.append("<td><pre>");
00215 for (int ip=1 ; ip<=npoints; ip++){
00216 try {
00217 Point pt2 = y.getPoint(ip);
00218 s.append(forms(pt2.getValue(),15,out));
00219 for ( Uncertainty e : pt2.getErrors() ){
00220 if( e.getPlus().equals(e.getMinus()) ) {
00221 s.append(" +- " + forms(e.getPlus(),15,out) );
00222 }
00223 else{
00224 s.append(" +" + forms(e.getPlus(),15,out) + ",-" + forms(e.getMinus(),15,out) );
00225 }
00226 if ( e.getNormType() == ErrorNorm.PCT ) {
00227 s.append(" (" + e.getNormType().toString() + ")");
00228 }
00229 }
00230 s.append("<br>");
00231 }
00232 catch (Exception e) {
00233 s.append(" - <br>");
00234 }
00235 }
00236 s.append("</pre></td>");
00237 out.println(s);
00238 out.println("</tr>");
00239 out.println("</table>");
00240 }
00241
00243 private void displayHeader(Paper p,PrintWriter out){
00244 StringBuffer s = new StringBuffer();
00245 out.println("<pre>");
00246 out.println("irn = " + p.getSpiresId());
00247 out.println("red = " + p.getRedId());
00248 int icom = 0;
00249 for ( String comment : p.getComments() ){
00250 icom++;
00251 out.println(icom + ":" + comment);
00252 }
00253 int imod = 0;
00254 for ( Modification modification : p.getModifications() ){
00255 imod++;
00256 if(imod == 1){out.println("Created on " + modification.getTimestamp() + " by " + modification.getModifier());}
00257 else {out.println("Modified on " + modification.getTimestamp() + " by " + modification.getModifier());}
00258 }
00259 for (String auth: p.getAuthors() ) {
00260 out.println("author: " + auth);
00261 }
00262 out.println("Title:" + p.getTitle());
00263
00264 out.println("Number of references are: " + p.getReferences().size());
00265
00266 for (Reference ref : p.getReferences() ) {
00267 out.println("Reference:" + ref.getDescription());
00268 }
00269 for (Experiment exp : p.getExperiments() ) {
00270 out.println("Experiment:" + exp.getName());
00271 out.println("Laboratory:" + exp.getLaboratory());
00272 out.println("InformalName:" + exp.getInformalName());
00273 }
00274 }
00275
00277 private void displayDatasetError(Paper p,PrintWriter out,String dataset){
00278 int ds = Integer.parseInt(dataset);
00279 Dataset d = p.getDataset(ds);
00280 for (DatasetError de : d.getErrors() ) {
00281 out.println("\nDataset Error");
00282 if (de.getComment() != null) { out.println(de.getComment() ); }
00283 if (de.getPlus() != null) { out.println(de.getPlus() ); }
00284 if (de.getMinus() != null) { out.println(de.getMinus() ); }
00285 if (de.getSourceType() != null) { out.println(de.getSourceType() ); }
00286 if (de.getNormType() != null) { out.println(de.getNormType() ); }
00287 }
00288 }
00290 private String forms(Double value,PrintWriter out){
00291 int width = 20;
00292 return this.forms(value,width,out);
00293
00294 }
00295
00296 private String forms(Double value,int width,PrintWriter out){
00297 int position = width/2;
00298 return this.forms(value,width,position,out);
00299 }
00300
00301 private String forms(Double value,int width, int position,PrintWriter out){
00302 String form = "##############.###############################";
00303 DecimalFormat myFormatter = new DecimalFormat(form);
00304 String sval = myFormatter.format(value);
00305 String sout = " ";
00306 for (int i=1 ; i<width; i++){
00307 sout = sout.concat(" ");
00308 }
00309 int lenval = sval.length();
00310 int dotpos = sval.indexOf(".");
00311 String before = sval;
00312 String after = "";
00313 if(dotpos >= 0){
00314 before = sval.substring(0,dotpos);
00315 after = sval.substring(dotpos+1,lenval);
00316 }
00317 int lenbefore = before.length();
00318 int lenafter = after.length();
00319 int lenout = sout.length();
00320 StringBuffer bout = new StringBuffer(sout);
00321 int lstart = 1;
00322 int l1 = lstart+lenbefore;
00323 int l2 = l1;
00324 int l3 = l2+1;
00325 int l4 = l3+lenafter;
00326
00327 bout.replace(lstart,l1,before);
00328 bout.replace(l2,l2,".");
00329 bout.replace(l3,l4,after);
00330 sout = bout.toString();
00331
00332 return sout;
00333 }
00334 }