Skip to main content
summaryrefslogtreecommitdiffstats
blob: 72b6ccbff8b74ad80750b8c01e9cd6921fbd43a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.ote.ui.markers;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.logging.Level;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.plugin.core.util.IExceptionableRunnable;
import org.eclipse.osee.framework.ui.ws.AWorkspace;
import org.eclipse.osee.ote.core.framework.saxparse.IBaseSaxElementListener;
import org.eclipse.osee.ote.core.framework.saxparse.OteSaxHandler;
import org.eclipse.osee.ote.core.framework.saxparse.elements.StacktraceData;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * @author Andrew M. Finkbeiner
 */
public class ProcessOutfileSax implements IExceptionableRunnable {

   private final IFile file;

   private static final int _1_MB = 1048576;
   private static final int _20_MB = _1_MB * 20;

   private final List<TestPointData> testPointDatas = new ArrayList<TestPointData>();
   private TestPointData currentData = null;
   private CheckPointData currentCheckPoint = null;
   protected StackTraceCollection currentStackTrace;

   public ProcessOutfileSax(IFile file) {
      this.file = file;
   }

   @Override
   public IStatus run(IProgressMonitor monitor) throws Exception {
      File outfile = AWorkspace.iFileToFile(file);
      if (outfile.length() > _20_MB) {
         OseeLog.logf(MarkerPlugin.class, Level.WARNING, 
            "%s has a length of [%d], the max size processed is [%d].", file.getName(), outfile.length(), _20_MB);
         return Status.OK_STATUS;
      }
      if (!file.isSynchronized(0)) {
         OseeLog.logf(MarkerPlugin.class, Level.WARNING, "%s is not synchronized.", file.getName());
         file.refreshLocal(0, monitor);
      }

      monitor.setTaskName(String.format("Computing overview information for [%s].", file.getName()));

//      InputStream contents = file.getContents();
      
      boolean hadParseException = false;
      int numberOfTries = 0;
      do{
         // Using this because IFile was acting very flaky for this.
         FileInputStream contents = new FileInputStream(outfile);

         try {
            hadParseException = false;
            parseContents(contents);
         } catch (Exception ex) {
            hadParseException = true;
            System.out.println("Had EXCEPTION HERE FROM parseContents()!!! numberOfTries = " + numberOfTries);
         }
         finally {
            numberOfTries++;
            Thread.sleep(1000);
         }
      } while(hadParseException && (numberOfTries < 5));
      if (numberOfTries > 1){
         FileInputStream contents = new FileInputStream(outfile);
         parseContents(contents);
      }
      
      
      OteMarkerHelper helper = new OteMarkerHelper(this.testPointDatas);
      MarkerPlugin.updateMarkerInfo(file, helper.getMarkers());

      return Status.OK_STATUS;
   }

   private void parseContents(InputStream contents) throws SAXException, Exception, SAXNotRecognizedException, SAXNotSupportedException, IOException {
      XMLReader xmlReader = XMLReaderFactory.createXMLReader();
      OteSaxHandler handler = new OteSaxHandler();
      xmlReader.setContentHandler(handler);
      xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler); // This is the important part

      final Stack<String> elementStack = new Stack<String>();
      handler.getHandler("*").addListener(new IBaseSaxElementListener() {

         @Override
         public void onStartElement(Object obj) {
            elementStack.push((String) obj);
         }

         @Override
         public void onEndElement(Object obj) {
            elementStack.pop();
         }
      });

      handler.getHandler("TestPoint").addListener(new IBaseSaxElementListener() {
         @Override
         public void onEndElement(Object obj) {
        	if (currentData != null && currentData.isFailed()) {
               testPointDatas.add(currentData);
            }
            currentData = null;
         }

         @Override
         public void onStartElement(Object obj) {
            currentData = new TestPointData();
         }
      });
      handler.getHandler("CheckPoint").addListener(new IBaseSaxElementListener() {
         @Override
         public void onEndElement(Object obj) {
            if (currentCheckPoint.isFailed()) {
               currentData.add(currentCheckPoint);
            }
            currentCheckPoint = null;
         }

         @Override
         public void onStartElement(Object obj) {
            if (currentData != null) {
               currentCheckPoint = new CheckPointData();
            }
         }
      });

      handler.getHandler("Result").addListener(new IBaseSaxElementListener() {
         @Override
         public void onEndElement(Object obj) {
            boolean failed = false;
            if ("FAILED".equals(obj)) {
               failed = true;
            }

            if (currentCheckPoint != null) {
               currentCheckPoint.setFailed(failed);
            } else if (currentData != null && elementStack.peek().equals("TestPoint")) {
               currentData.setFailed(failed);
            }
         }

         @Override
         public void onStartElement(Object obj) {
         }
      });

      handler.getHandler("TestPointName").addListener(new IBaseSaxElementListener() {
         @Override
         public void onEndElement(Object obj) {
            if (currentCheckPoint != null) {
               currentCheckPoint.setName(obj.toString());
            }
         }

         @Override
         public void onStartElement(Object obj) {
         }
      });
      handler.getHandler("Expected").addListener(new IBaseSaxElementListener() {
         @Override
         public void onEndElement(Object obj) {
            if (currentCheckPoint != null) {
               currentCheckPoint.setExpected(spaceProcessing(obj.toString()));
            }
         }

         @Override
         public void onStartElement(Object obj) {
         }
      });
      handler.getHandler("Actual").addListener(new IBaseSaxElementListener() {
         @Override
         public void onEndElement(Object obj) {
            if (currentCheckPoint != null) {
               currentCheckPoint.setActual(spaceProcessing(obj.toString()));
            }
         }

         @Override
         public void onStartElement(Object obj) {
         }
      });
      handler.getHandler("Number").addListener(new IBaseSaxElementListener() {
         @Override
         public void onEndElement(Object obj) {
            if (currentData != null) {
               currentData.setNumber(obj.toString());
            }
         }

         @Override
         public void onStartElement(Object obj) {
         }
      });
      handler.getHandler("Location").addListener(new IBaseSaxElementListener() {
         @Override
         public void onEndElement(Object obj) {
            if (currentStackTrace != null) {
               currentData.setStackTrace(currentStackTrace);
               currentStackTrace = null;
            }
         }

         @Override
         public void onStartElement(Object obj) {
            if (currentData != null) {
               currentStackTrace = new StackTraceCollection();
            }
         }
      });
      handler.getHandler("Stacktrace").addListener(new IBaseSaxElementListener() {
         @Override
         public void onEndElement(Object obj) {
         }

         @Override
         public void onStartElement(Object obj) {
            if (currentStackTrace != null) {
               currentStackTrace.addTrace((StacktraceData) obj);
            }
         }
      });

      xmlReader.parse(new InputSource(contents));
   }

   private String spaceProcessing(String expected) {
      int index = 0, preCount = 0, postCount = 0;
      while (index < expected.length() && expected.charAt(index) == ' ') {
         index++;
         preCount++;
      }
      index = expected.length() - 1;
      while (index >= 0 && expected.charAt(index) == ' ') {
         index--;
         postCount++;
      }

      expected = expected.trim();
      if (preCount > 0) {
         expected = String.format("#sp%d#%s", preCount, expected);
      }
      if (postCount > 0) {
         expected = String.format("%s#sp%d#", expected, postCount);
      }

      return expected;
   }
}

Back to the top