mev-4.0.01/source/org/tigr/remote/protocol/parser/SAXResponseHandler.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2 Copyright @ 1999-2003, The Institute for Genomic Research (TIGR).
2 26 Feb 07 jari 3 All rights reserved.
2 26 Feb 07 jari 4 */
2 26 Feb 07 jari 5 /*
2 26 Feb 07 jari 6  * $RCSfile: SAXResponseHandler.java,v $
2 26 Feb 07 jari 7  * $Revision: 1.3 $
2 26 Feb 07 jari 8  * $Date: 2005/03/10 15:28:22 $
2 26 Feb 07 jari 9  * $Author: braistedj $
2 26 Feb 07 jari 10  * $State: Exp $
2 26 Feb 07 jari 11  */
2 26 Feb 07 jari 12 package org.tigr.remote.protocol.parser;
2 26 Feb 07 jari 13
2 26 Feb 07 jari 14 import java.io.BufferedInputStream;
2 26 Feb 07 jari 15 import java.io.BufferedOutputStream;
2 26 Feb 07 jari 16 import java.io.BufferedWriter;
2 26 Feb 07 jari 17 import java.io.File;
2 26 Feb 07 jari 18 import java.io.FileInputStream;
2 26 Feb 07 jari 19 import java.io.FileOutputStream;
2 26 Feb 07 jari 20 import java.io.InputStream;
2 26 Feb 07 jari 21 import java.io.OutputStream;
2 26 Feb 07 jari 22 import java.io.OutputStreamWriter;
2 26 Feb 07 jari 23 import java.io.Writer;
2 26 Feb 07 jari 24
2 26 Feb 07 jari 25 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmEvent;
2 26 Feb 07 jari 26 import org.tigr.remote.protocol.ExecutedJob;
2 26 Feb 07 jari 27 import org.tigr.remote.protocol.Fail;
2 26 Feb 07 jari 28 import org.tigr.remote.protocol.FailedJob;
2 26 Feb 07 jari 29 import org.tigr.remote.protocol.FinishedJob;
2 26 Feb 07 jari 30 import org.tigr.remote.protocol.JobData;
2 26 Feb 07 jari 31 import org.tigr.remote.protocol.Response;
2 26 Feb 07 jari 32 import org.tigr.remote.protocol.SuccessfulJob;
2 26 Feb 07 jari 33 import org.tigr.remote.protocol.util.TempFile;
2 26 Feb 07 jari 34 import org.tigr.util.ConfMap;
2 26 Feb 07 jari 35 import org.xml.sax.Attributes;
2 26 Feb 07 jari 36 import org.xml.sax.SAXException;
2 26 Feb 07 jari 37
2 26 Feb 07 jari 38 import de.mnl.java.io.base64.Base64InputStream;
2 26 Feb 07 jari 39
2 26 Feb 07 jari 40 class SAXResponseHandler extends ResponseHandlerBase {
2 26 Feb 07 jari 41
2 26 Feb 07 jari 42     // General data
2 26 Feb 07 jari 43     private FinishedJob m_job = null;
2 26 Feb 07 jari 44     private Response m_result = new Response( null );
2 26 Feb 07 jari 45     private boolean m_inJobData = false;
2 26 Feb 07 jari 46
2 26 Feb 07 jari 47     // successful-job data
2 26 Feb 07 jari 48     private Writer m_mageStorage = null;
2 26 Feb 07 jari 49     private String m_mageFileName;
2 26 Feb 07 jari 50     // failed job data
2 26 Feb 07 jari 51     private Fail m_fail;
2 26 Feb 07 jari 52     // executed job data
2 26 Feb 07 jari 53     private AlgorithmEvent m_event;
2 26 Feb 07 jari 54     // characters section buffer
2 26 Feb 07 jari 55     private StringBuffer m_chars = new StringBuffer();
2 26 Feb 07 jari 56
2 26 Feb 07 jari 57     /**
2 26 Feb 07 jari 58      * Constructs a <code>SAXResponseHandler</code>
2 26 Feb 07 jari 59      */
2 26 Feb 07 jari 60     public SAXResponseHandler(ConfMap cfg) {
2 26 Feb 07 jari 61         super( cfg );
2 26 Feb 07 jari 62     }
2 26 Feb 07 jari 63
2 26 Feb 07 jari 64     /**
2 26 Feb 07 jari 65      * Document if finished.
2 26 Feb 07 jari 66      */
2 26 Feb 07 jari 67     public void endDocument() {
2 26 Feb 07 jari 68         m_job = null;
2 26 Feb 07 jari 69         m_mageStorage = null;
2 26 Feb 07 jari 70         m_mageFileName = null;
2 26 Feb 07 jari 71         m_fail = null;
2 26 Feb 07 jari 72         m_event = null;
2 26 Feb 07 jari 73         m_chars = null;
2 26 Feb 07 jari 74     }
2 26 Feb 07 jari 75
2 26 Feb 07 jari 76     /**
2 26 Feb 07 jari 77      * An element is started.
2 26 Feb 07 jari 78      */
2 26 Feb 07 jari 79     public void startElement(String uri, String localName, String name, Attributes attrs) throws SAXException {
2 26 Feb 07 jari 80         super.startElement(uri, localName, name, attrs );
2 26 Feb 07 jari 81         try {
2 26 Feb 07 jari 82             m_chars = new StringBuffer();
2 26 Feb 07 jari 83             // Root element
2 26 Feb 07 jari 84             if (name.equals("response" )) {
2 26 Feb 07 jari 85                 // do the response processing
2 26 Feb 07 jari 86             } else
2 26 Feb 07 jari 87                 /* Job Types */
2 26 Feb 07 jari 88                 if (name.equals("successful-job" )) {
2 26 Feb 07 jari 89                 String[] path = {"response","successful-job"};
2 26 Feb 07 jari 90                 m_path.checkFromTopThrow( path );
2 26 Feb 07 jari 91                 String jobId = attrs.getValue("id");
2 26 Feb 07 jari 92                 m_job = new SuccessfulJob( jobId, new JobData( null ) );
2 26 Feb 07 jari 93                 m_result = new Response( m_job );
2 26 Feb 07 jari 94             } else
2 26 Feb 07 jari 95                 if (name.equals("failed-job" )) {
2 26 Feb 07 jari 96                 String[] path = {"response","failed-job"};
2 26 Feb 07 jari 97                 m_path.checkFromTopThrow( path );
2 26 Feb 07 jari 98                 String jobId = attrs.getValue("id");
2 26 Feb 07 jari 99                 m_job = new FailedJob( jobId, null );
2 26 Feb 07 jari 100                 m_result = new Response( m_job );
2 26 Feb 07 jari 101             } else
2 26 Feb 07 jari 102                 if (name.equals("executed-job" )) {
2 26 Feb 07 jari 103                 String[] path = {"response","executed-job"};
2 26 Feb 07 jari 104                 m_path.checkFromTopThrow( path );
2 26 Feb 07 jari 105                 String jobId = attrs.getValue("id");
2 26 Feb 07 jari 106                 m_job = new ExecutedJob( jobId, null );
2 26 Feb 07 jari 107                 m_result = new Response( m_job );
2 26 Feb 07 jari 108             } else
2 26 Feb 07 jari 109                 /* SuccessfulJob descendants */
2 26 Feb 07 jari 110                 if (name.equals("job-data" )) {
2 26 Feb 07 jari 111                 String[] path = {"response","successful-job","job-data"};
2 26 Feb 07 jari 112                 m_path.checkFromTopThrow( path );
2 26 Feb 07 jari 113
2 26 Feb 07 jari 114                 TempFile tmp = new TempFile( m_config );
2 26 Feb 07 jari 115                 m_mageFileName = tmp.getName("MAGE.base64");
2 26 Feb 07 jari 116                 m_mageStorage = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( m_mageFileName ) ) );
2 26 Feb 07 jari 117             } else
2 26 Feb 07 jari 118                 /* FailedJob descendants */
2 26 Feb 07 jari 119                 if (name.equals("fail")) {
2 26 Feb 07 jari 120                 String[] path = {"response","failed-job","fail"};
2 26 Feb 07 jari 121                 m_path.checkFromTopThrow( path );
2 26 Feb 07 jari 122
2 26 Feb 07 jari 123                 String id = attrs.getValue("id");
2 26 Feb 07 jari 124                 m_fail = new Fail( id, null );
2 26 Feb 07 jari 125                 ((FailedJob)m_job).setFail(  m_fail );
2 26 Feb 07 jari 126             } else
2 26 Feb 07 jari 127                 /* ExecutedJob descendants */
2 26 Feb 07 jari 128                 if (name.equals("event")) {
2 26 Feb 07 jari 129                 String[] path = {"response","executed-job","event"};
2 26 Feb 07 jari 130                 m_path.checkFromTopThrow( path );
2 26 Feb 07 jari 131
2 26 Feb 07 jari 132                 String id = attrs.getValue("id");
2 26 Feb 07 jari 133                 int i = 0;
2 26 Feb 07 jari 134                 try {
2 26 Feb 07 jari 135                     i = Integer.parseInt( id );
2 26 Feb 07 jari 136                 } catch (NumberFormatException ex) {
2 26 Feb 07 jari 137                 }
2 26 Feb 07 jari 138                 m_event = new AlgorithmEvent( this, i );
2 26 Feb 07 jari 139                 ((ExecutedJob)m_job).setEvent( m_event );
2 26 Feb 07 jari 140             } else
2 26 Feb 07 jari 141                 if (name.equals("progress")) {
2 26 Feb 07 jari 142                 String[] path = {"response","executed-job","event","progress"};
2 26 Feb 07 jari 143                 m_path.checkFromTopThrow( path );
2 26 Feb 07 jari 144
2 26 Feb 07 jari 145                 // do the progress processing
2 26 Feb 07 jari 146             } else
2 26 Feb 07 jari 147                 if (name.equals("float-value")) {
2 26 Feb 07 jari 148                 String[] path = {"response","executed-job","event","float-value"};
2 26 Feb 07 jari 149                 m_path.checkFromTopThrow( path );
2 26 Feb 07 jari 150
2 26 Feb 07 jari 151                 // do the float value processing
2 26 Feb 07 jari 152             } else
2 26 Feb 07 jari 153                 if (name.equals("description")) {
2 26 Feb 07 jari 154                 String[] path = {"response","executed-job","event","description"};
2 26 Feb 07 jari 155                 m_path.checkFromTopThrow( path );
2 26 Feb 07 jari 156
2 26 Feb 07 jari 157                 // do the description processing
2 26 Feb 07 jari 158             } else throw new Exception("Unexpected element: " + name );
2 26 Feb 07 jari 159         } catch (Exception ex) {
2 26 Feb 07 jari 160             ex.printStackTrace( System.out );
2 26 Feb 07 jari 161             processError( ex );
2 26 Feb 07 jari 162         }
2 26 Feb 07 jari 163     }
2 26 Feb 07 jari 164
2 26 Feb 07 jari 165     /**
2 26 Feb 07 jari 166      * An element is finished.
2 26 Feb 07 jari 167      */
2 26 Feb 07 jari 168     public void endElement(String uri, String localName, String name) throws SAXException {
2 26 Feb 07 jari 169         try {
2 26 Feb 07 jari 170             /* FailedJob descendants */
2 26 Feb 07 jari 171             if (name.equals("fail")) {
2 26 Feb 07 jari 172                 m_fail.setDescription( m_chars.toString() );
2 26 Feb 07 jari 173             }
2 26 Feb 07 jari 174             /* SuccessfulJob descendants */
2 26 Feb 07 jari 175             else
2 26 Feb 07 jari 176                 if (name.equals("job-data")) {
2 26 Feb 07 jari 177                 boolean keepFiles = m_config.getBoolean("remote.debug.keep-response-files", false);
2 26 Feb 07 jari 178                 m_mageStorage.close();
2 26 Feb 07 jari 179                 m_mageStorage = null;
2 26 Feb 07 jari 180                 TempFile tmp = new TempFile( m_config );
2 26 Feb 07 jari 181                 String fname = tmp.getName("MAGE");
2 26 Feb 07 jari 182                 InputStream in = new Base64InputStream(new BufferedInputStream(new FileInputStream(m_mageFileName)));
2 26 Feb 07 jari 183                 OutputStream out = new BufferedOutputStream(new FileOutputStream(fname));
2 26 Feb 07 jari 184                 byte[] b = new byte[1024*100];
2 26 Feb 07 jari 185                 int cnt;
2 26 Feb 07 jari 186                 while ((cnt = in.read (b)) >= 0) {
2 26 Feb 07 jari 187                     out.write(b, 0, cnt);
2 26 Feb 07 jari 188                 }
2 26 Feb 07 jari 189                 in.close();
2 26 Feb 07 jari 190                 out.close();
2 26 Feb 07 jari 191                 in = null;
2 26 Feb 07 jari 192                 out = null;
2 26 Feb 07 jari 193                 if (! keepFiles)
2 26 Feb 07 jari 194                     new File( m_mageFileName ).delete();
2 26 Feb 07 jari 195                 XMLMAGEParser parser = new XMLMAGEParser( m_config );
2 26 Feb 07 jari 196                 parser.parse( fname );
2 26 Feb 07 jari 197                 ((SuccessfulJob)m_job).getData().setData(  parser.getResult()  );
2 26 Feb 07 jari 198                 if (! keepFiles)
2 26 Feb 07 jari 199                     new File( fname ).delete();
2 26 Feb 07 jari 200             }
2 26 Feb 07 jari 201             /* ExecutedJob descendants */
2 26 Feb 07 jari 202             else
2 26 Feb 07 jari 203                 if (name.equals("progress")) {
2 26 Feb 07 jari 204                 String str = m_chars.toString();
2 26 Feb 07 jari 205                 try {
2 26 Feb 07 jari 206                     int i = Integer.parseInt( str );
2 26 Feb 07 jari 207                     m_event.setIntValue( i );
2 26 Feb 07 jari 208                 } catch (NumberFormatException ex) {
2 26 Feb 07 jari 209                     throw new Exception("invalid progress integer value: " + str );
2 26 Feb 07 jari 210                 }
2 26 Feb 07 jari 211             } else
2 26 Feb 07 jari 212                 if (name.equals("float-value")) {
2 26 Feb 07 jari 213                 String str = m_chars.toString();
2 26 Feb 07 jari 214                 try {
2 26 Feb 07 jari 215                     float i = Float.parseFloat( str );
2 26 Feb 07 jari 216                     m_event.setFloatValue( i );
2 26 Feb 07 jari 217                 } catch (NumberFormatException ex) {
2 26 Feb 07 jari 218                     throw new Exception("invalid float value: " + str );
2 26 Feb 07 jari 219                 }
2 26 Feb 07 jari 220             } else
2 26 Feb 07 jari 221                 if (name.equals("description")) {
2 26 Feb 07 jari 222                 m_event.setDescription( m_chars.toString() );
2 26 Feb 07 jari 223             }
2 26 Feb 07 jari 224         } catch (Exception ex) {
2 26 Feb 07 jari 225             processError(ex);
2 26 Feb 07 jari 226         } finally {
2 26 Feb 07 jari 227             super.endElement( uri, localName, name );
2 26 Feb 07 jari 228         }
2 26 Feb 07 jari 229     }
2 26 Feb 07 jari 230
2 26 Feb 07 jari 231     /**
2 26 Feb 07 jari 232      * Invoked to handle a chunk of characters.
2 26 Feb 07 jari 233      */
2 26 Feb 07 jari 234     public void characters(char ch[], int start, int length) throws SAXException {
2 26 Feb 07 jari 235         try {
2 26 Feb 07 jari 236             if (getCurrentElement().equals("job-data")) {
2 26 Feb 07 jari 237                 m_mageStorage.write( ch, start, length );
2 26 Feb 07 jari 238             } else {
2 26 Feb 07 jari 239                 m_chars.append( ch, start, length );
2 26 Feb 07 jari 240             }
2 26 Feb 07 jari 241         } catch (Exception ex) {
2 26 Feb 07 jari 242             processError( ex );
2 26 Feb 07 jari 243         }
2 26 Feb 07 jari 244     }
2 26 Feb 07 jari 245
2 26 Feb 07 jari 246     /**
2 26 Feb 07 jari 247      * Ignore the ignorable.
2 26 Feb 07 jari 248      */
2 26 Feb 07 jari 249     public void ignorableWhitespace(char ch[], int start, int length) {}
2 26 Feb 07 jari 250
2 26 Feb 07 jari 251     /**
2 26 Feb 07 jari 252      * Returns the build result.
2 26 Feb 07 jari 253      */
2 26 Feb 07 jari 254     public Response getResponse() { return m_result;}
2 26 Feb 07 jari 255
2 26 Feb 07 jari 256 }
2 26 Feb 07 jari 257