View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.remote.file;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.InputStreamReader;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.NoSuchElementException;
24  
25  import org.jdiagnose.DefaultDiagnosticMessage;
26  import org.jdiagnose.DiagnosticException;
27  import org.jdiagnose.remote.DefaultRemoteResult;
28  import org.jdiagnose.remote.DefaultResultInfo;
29  import org.jdiagnose.runtime.ResultState;
30  
31  /***
32   * @author jmccrindle
33   */
34  public class CsvResultImporter implements ResultImporter {
35  
36      private CsvParser parser = null;
37  
38      public Iterator getResults(InputStream in) throws DiagnosticException {
39          try {
40              final Iterator lineIterator = parser
41                      .parse(new InputStreamReader(in));
42              return new Iterator() {
43                  public void remove() {
44                      throw new UnsupportedOperationException();
45                  }
46  
47                  public boolean hasNext() {
48                      return lineIterator.hasNext();
49                  }
50  
51                  public Object next() {
52                      if (!hasNext()) {
53                          throw new NoSuchElementException();
54                      }
55  
56                      List line = (List) lineIterator.next();
57  
58                      String guid = (String) line.get(0);
59                      String agent = (String) line.get(1);
60                      String host = (String) line.get(2);
61                      String sequenceNumber = (String) line.get(3);
62  
63                      String name = (String) line.get(4);
64                      String state = (String) line.get(5);
65                      String startTime = (String) line.get(6);
66                      String finishTime = (String) line.get(7);
67                      String duration = (String) line.get(8);
68                      String message = (String) line.get(9);
69                      String summary = (String) line.get(10);
70  
71                      DefaultResultInfo resultInfo = new DefaultResultInfo(name,
72                              ResultState.getEnum(state), Long
73                                      .parseLong(duration),
74                              new DefaultDiagnosticMessage(message, summary),
75                              Long.parseLong(startTime), Long
76                                      .parseLong(finishTime));
77                      
78                      DefaultRemoteResult remoteResult = new DefaultRemoteResult(guid, resultInfo, agent, host, Long.parseLong(sequenceNumber));
79  
80                      return remoteResult;
81                  }
82              };
83          } catch (IOException e) {
84              throw new DiagnosticException(e);
85          }
86      }
87  
88      public void setParser(CsvParser parser) {
89          this.parser = parser;
90      }
91  }