Skip to main content
summaryrefslogtreecommitdiffstats
blob: ae0ff8c1ec233840ae8b310c650030a149dc114d (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
/*******************************************************************************
 * 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.framework.ui.skynet.results.html;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.framework.jdk.core.util.AHTML;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.ui.skynet.SkynetGuiPlugin;
import org.eclipse.osee.framework.ui.skynet.results.XResultData;
import org.eclipse.osee.framework.ui.skynet.widgets.dialog.Dialogs;
import org.eclipse.swt.program.Program;

/**
 * @author Donald G. Dunne
 */
public class XResultPage {

   private String title;
   private String html;
   private final String id; // Used to add and remove to menu item
   private String manipulatedHtml;
   private Set<Manipulations> manipulations = new HashSet<Manipulations>();
   private int numWarnings = Integer.MAX_VALUE;
   private int numErrors = Integer.MAX_VALUE;
   public static enum Manipulations {
      NONE, // 
      HRID_CMD_HYPER,
      // Replace all HRID strings with hyperlinks; ATS=<hrid> opens Action editor
      // ART=<hrid> opens Artifact editor, BOTH=<hrid> allows either
      ERROR_RED, // Make all "Error" strings red
      WARNING_YELLOW, // Make all "Warning" strings yellow
      CONVERT_NEWLINES, // Convert all \n to <br>
      HTML_MANIPULATIONS, // Do all except converting newlines
      RAW_HTML, // Just display in simple html page
      ERROR_WARNING_HEADER, // Shows Errors: 4 Warnings: 23 count at top of page
      ALL
   };
   public enum HyperType {
      ATS,
      ART,
      BOTH
   };

   /**
    * Create and display result page with all Manipulations available
    */
   public XResultPage(String title, String text) {
      this(title, text, Manipulations.ALL);
   }

   /**
    * Create and display result page with given Manipulations
    * 
    * @param title title of the page (include date/time due or something unique due to multi-page view of results)
    * @param html html to display (minus manipulations). this html MUST already handle new lines (eg
    * text.replaceAll("\n",AHTML.newLine())) or use the CONVERT_NEWLINES manipultion
    * @param manipulations manipulations desired for the input HTML
    */
   public XResultPage(String title, String html, Manipulations... manipulations) {
      super();
      this.title = title;
      this.html = html;
      id = GUID.create();
      for (Manipulations man : manipulations) {
         if (man == Manipulations.ALL) {
            this.manipulations.add(Manipulations.HRID_CMD_HYPER);
            this.manipulations.add(Manipulations.ERROR_RED);
            this.manipulations.add(Manipulations.CONVERT_NEWLINES);
            this.manipulations.add(Manipulations.WARNING_YELLOW);
         } else if (man == Manipulations.HTML_MANIPULATIONS) {
            this.manipulations.add(Manipulations.HRID_CMD_HYPER);
            this.manipulations.add(Manipulations.ERROR_RED);
            this.manipulations.add(Manipulations.WARNING_YELLOW);
         } else {
            this.manipulations.add(man);
         }
      }
   }

   public int getNumWarnings() {
      if (numWarnings != Integer.MAX_VALUE) {
         return numWarnings;
      }
      if (manipulations.contains(Manipulations.WARNING_YELLOW)) {
         numWarnings = Lib.numOccurances(html, "Warning:");
      }
      if (numWarnings == Integer.MAX_VALUE) {
         return 0;
      }
      return numWarnings;
   }

   public int getNumErrors() {
      if (numErrors != Integer.MAX_VALUE) {
         return numErrors;
      }
      if (manipulations.contains(Manipulations.ERROR_RED)) {
         numErrors = Lib.numOccurances(html, "Error:");
      }
      if (numErrors == Integer.MAX_VALUE) {
         return 0;
      }
      return numErrors;
   }

   public static String getCmdValue(HyperType type, String hrid) {
      return String.format("%s=%s", type.name(), hrid);
   }

   /**
    * @param type
    * @param name that will show hyperlinked
    * @param hrid value that will be returned upon selection of hyperlink
    * @return cmd value to put in HTML for processing by result page
    */
   public static String getCmdValue(HyperType type, String name, String hrid) {
      return String.format("%s=%s:%s", type.name(), name, hrid);
   }

   public String getId() {
      return id;
   }

   public String getErrorWarningHtml() {
      return String.format("<b>Errors</b>: %d  <b>Warnings</b>: %d<br><br>", getNumErrors(), getNumWarnings());
   }

   public String getManipulatedHtml() {
      return getManipulatedHtml(manipulations);
   }

   public String getManipulatedHtml(Collection<Manipulations> manipulations) {
      if (manipulatedHtml == null) {
         String str =
            (manipulations.contains(Manipulations.ERROR_WARNING_HEADER) ? getErrorWarningHtml() : "") + getText();
         if (manipulations.contains(Manipulations.RAW_HTML)) {
            str = AHTML.simplePage(str);
         } else {
            if (manipulations.contains(Manipulations.CONVERT_NEWLINES)) {
               str = str.replaceAll("\n", AHTML.newline());
            }
            if (manipulations.contains(Manipulations.HRID_CMD_HYPER)) {
               // System.err.println("match " + line);
               // Match getText so it doesn't mess up replace
               // Retireve all ATS=WPN_PAGE:HSRID matches
               Matcher m = Pattern.compile("([A-Z]{3,4})=(.*?):([A-Z0-9]{5})").matcher(str);
               Set<String> cmdNameHrids = new HashSet<String>();
               while (m.find()) {
                  cmdNameHrids.add(m.group());
               }
               // Retrieve all ATS=Name:HRSID matches and replace with hyperlinking
               for (String cmdNameHrid : cmdNameHrids) {
                  String value = cmdNameHrid;
                  value = value.replaceAll("^.*?=", "");
                  String name = value;
                  name = name.replaceAll(":.*$", "");
                  String hrid = value;
                  hrid = hrid.replaceAll("^.*:", "");
                  if (cmdNameHrid.startsWith(HyperType.BOTH.name())) {
                     String replaceStr = hrid + " (" + XResultData.getHyperlinkForAction("ATS-" + name, hrid);
                     replaceStr += "  " + XResultData.getHyperlinkForArtifactEditor("AE-" + name, hrid);
                     replaceStr += ")";
                     str = str.replaceAll(cmdNameHrid, replaceStr);
                  } else if (cmdNameHrid.startsWith(HyperType.ATS.name())) {
                     str = str.replaceAll(cmdNameHrid, XResultData.getHyperlinkForAction(name, hrid));
                  } else if (cmdNameHrid.startsWith(HyperType.ART.name())) {
                     str = str.replaceAll(cmdNameHrid, XResultData.getHyperlinkForArtifactEditor(name, hrid));
                  }
               }
               // Retrieve all ATS=HRSID matches and replace with hyperlinking
               m = Pattern.compile("([A-Z]{3,4})=([A-Z0-9]{5})").matcher(str);
               Set<String> cmdHrids = new HashSet<String>();
               while (m.find()) {
                  cmdHrids.add(m.group());
               }
               for (String cmdHrid : cmdHrids) {
                  String hrid = cmdHrid;
                  hrid = hrid.replaceAll("^.*?=", "");
                  if (cmdHrid.startsWith(HyperType.BOTH.name())) {
                     String replaceStr = hrid + " (" + XResultData.getHyperlinkForAction("ATS", hrid);
                     replaceStr += "  " + XResultData.getHyperlinkForArtifactEditor("AE", hrid);
                     replaceStr += ")";
                     str = str.replaceAll(cmdHrid, replaceStr);
                  } else if (cmdHrid.startsWith(HyperType.ATS.name())) {
                     str = str.replaceAll(cmdHrid, XResultData.getHyperlinkForAction(hrid, hrid));
                  } else if (cmdHrid.startsWith(HyperType.ART.name())) {
                     str = str.replaceAll(cmdHrid, XResultData.getHyperlinkForArtifactEditor(hrid, hrid));
                  }
               }
            }
            if (manipulations.contains(Manipulations.ERROR_RED)) {
               str = str.replaceAll("Error:", AHTML.color("red", "Error:"));
            }
            if (manipulations.contains(Manipulations.WARNING_YELLOW)) {
               str = str.replaceAll("Warning:", AHTML.color("orange", "Warning:"));
            }
         }
         manipulatedHtml = str;
      }
      return manipulatedHtml;
   }

   public String getText() {
      return html;
   }

   public void setHtml(String html) {
      this.html = html;
   }

   public String getTitle() {
      return title;
   }

   public void setTitle(String title) {
      this.title = title;
   }

   public Set<Manipulations> getManipulations() {
      return manipulations;
   }

   public void setManipulations(Set<Manipulations> manipulations) {
      this.manipulations = manipulations;
   }

   public void handleExport() {
      Dialogs.exportHtmlTableDialog(title, html, true);
   }

   public void saveToFile() {
      saveToFile(null);
   }

   public void saveToFile(String filename) {
      if (manipulatedHtml == null) {
         getManipulatedHtml();
      }
      if (filename == null) {
         Dialogs.saveHtmlDialog(manipulatedHtml, true);
      } else {
         try {
            Lib.writeStringToFile(manipulatedHtml, new File(filename));
         } catch (IOException ex) {
            OseeLog.log(SkynetGuiPlugin.class, OseeLevel.SEVERE_POPUP, ex);
         }
         Program.launch(filename);
      }
   }

}

Back to the top