2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.report;
20 import gov.nasa.jpf.Config;
22 import java.io.PrintWriter;
23 import java.text.DateFormat;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.List;
29 * abstract base for all format specific publishers. Note that this interface
30 * also has to work for non-stream based reporting, i.e. within Eclipse
31 * (we don't want to re-parse from text reports there)
33 public abstract class Publisher {
36 public static final int START = 1;
37 public static final int TRANSITION = 2;
38 public static final int PROBE = 3;
39 public static final int CONSTRAINT = 4;
40 public static final int PROPERTY_VIOLATION = 5;
41 public static final int FINISHED = 6;
43 protected Config conf;
44 protected Reporter reporter; // our master
46 protected String[] startItems = {};
47 protected String[] transitionItems = {};
48 protected String[] propertyViolationItems = {};
49 protected String[] constraintItems = {};
50 protected String[] finishedItems = {};
51 protected String[] probeItems = {};
53 DateFormat dtgFormatter = DateFormat.getDateTimeInstance(DateFormat.SHORT,
56 ArrayList<PublisherExtension> extensions;
59 * to be initialized in openChannel
60 * NOTE - not all publishers need to have one
62 protected PrintWriter out;
64 public PrintWriter getOut () {
68 protected Publisher (Config conf, Reporter reporter){
70 this.reporter = reporter;
75 public void setItems (int category, String[] newTopics){
78 startItems = newTopics; break;
80 probeItems = newTopics; break;
82 transitionItems = newTopics; break;
84 constraintItems = newTopics; break;
85 case PROPERTY_VIOLATION:
86 propertyViolationItems = newTopics; break;
88 finishedItems = newTopics; break;
90 Reporter.log.warning("unknown publisher topic: " + category);
94 public abstract String getName();
96 protected void setTopicItems () {
97 setTopicItems(getName());
100 protected void setTopicItems (String name) {
101 String prefix = "report." + name;
102 startItems = conf.getStringArray(prefix + ".start", startItems);
103 transitionItems = conf.getStringArray(prefix + ".transition", transitionItems);
104 probeItems = conf.getStringArray(prefix + ".probe", transitionItems);
105 propertyViolationItems = conf.getStringArray(prefix + ".property_violation", propertyViolationItems);
106 constraintItems = conf.getStringArray(prefix + ".constraint", constraintItems);
107 finishedItems = conf.getStringArray(prefix + ".finished", finishedItems);
110 public void addExtension (PublisherExtension ext) {
111 if (extensions == null) {
112 extensions = new ArrayList<PublisherExtension>();
117 // <2do> should not be a list we can add to
118 private static final List<PublisherExtension> EMPTY_LIST = new ArrayList<PublisherExtension>(0);
120 public List<PublisherExtension> getExtensions(){
121 if (extensions != null){
124 return EMPTY_LIST; // make life easier for callers
128 public String getLastErrorId() {
129 return reporter.getCurrentErrorId();
132 public boolean hasTopic (String topic) {
133 for (String s : startItems) {
134 if (s.equalsIgnoreCase(topic)){
138 for (String s : transitionItems) {
139 if (s.equalsIgnoreCase(topic)){
143 for (String s : constraintItems) {
144 if (s.equalsIgnoreCase(topic)){
148 for (String s : propertyViolationItems) {
149 if (s.equalsIgnoreCase(topic)){
153 for (String s : finishedItems) {
154 if (s.equalsIgnoreCase(topic)){
162 public String formatDTG (Date date) {
163 return dtgFormatter.format(date);
167 static public String _formatHMS (long t) {
168 t /= 1000; // convert to sec
172 long m = (t % 3600) / 60;
174 StringBuilder sb = new StringBuilder();
187 return sb.toString();
191 static char[] tBuf = { '0', '0', ':', '0', '0', ':', '0', '0' };
193 static synchronized public String formatHMS (long t) {
194 int h = (int) (t / 3600000);
195 int m = (int) ((t / 60000) % 60);
196 int s = (int) ((t / 1000) % 60);
198 tBuf[0] = (char) ('0' + (h / 10));
199 tBuf[1] = (char) ('0' + (h % 10));
201 tBuf[3] = (char) ('0' + (m / 10));
202 tBuf[4] = (char) ('0' + (m % 10));
204 tBuf[6] = (char) ('0' + (s / 10));
205 tBuf[7] = (char) ('0' + (s % 10));
207 return new String(tBuf);
210 public String getReportFileName (String key) {
211 String fname = conf.getString(key);
213 fname = conf.getString("report.file");
222 public void publishTopicStart (String topic) {
223 // to be done by subclasses
226 public void publishTopicEnd (String topic) {
227 // to be done by subclasses
230 public boolean hasToReportStatistics() {
231 for (String s : finishedItems) {
232 if ("statistics".equalsIgnoreCase(s)){
239 //--- open/close streams etc
240 protected void openChannel(){}
241 protected void closeChannel(){}
243 //--- if you have different preferences about when to report things, override those
244 public void publishStart() {
245 for (String item : startItems) {
246 if ("jpf".equalsIgnoreCase(item)){
248 } else if ("platform".equalsIgnoreCase(item)){
250 } else if ("user".equalsIgnoreCase(item)) {
252 } else if ("dtg".equalsIgnoreCase(item)) {
254 } else if ("config".equalsIgnoreCase(item)){
256 } else if ("sut".equalsIgnoreCase(item)){
261 if (extensions != null) {
262 for (PublisherExtension e : extensions) {
263 e.publishStart(this);
268 public void publishTransition() {
269 for (String topic : transitionItems) {
270 if ("statistics".equalsIgnoreCase(topic)){
275 if (extensions != null) {
276 for (PublisherExtension e : extensions) {
277 e.publishTransition(this);
282 public void publishConstraintHit() {
283 for (String item : constraintItems) {
284 if ("constraint".equalsIgnoreCase(item)) {
286 } else if ("trace".equalsIgnoreCase(item)){
288 } else if ("snapshot".equalsIgnoreCase(item)){
290 } else if ("output".equalsIgnoreCase(item)){
292 } else if ("statistics".equalsIgnoreCase(item)){
293 publishStatistics(); // not sure if that is good for anything
297 if (extensions != null) {
298 for (PublisherExtension e : extensions) {
299 e.publishConstraintHit(this);
304 public void publishProbe(){
305 for (String topic : probeItems) {
306 if ("statistics".equalsIgnoreCase(topic)){
311 if (extensions != null) {
312 for (PublisherExtension e : extensions) {
313 e.publishProbe(this);
318 public void publishPropertyViolation() {
320 for (String topic : propertyViolationItems) {
321 if ("error".equalsIgnoreCase(topic)) {
323 } else if ("trace".equalsIgnoreCase(topic)){
325 } else if ("snapshot".equalsIgnoreCase(topic)){
327 } else if ("output".equalsIgnoreCase(topic)){
329 } else if ("statistics".equalsIgnoreCase(topic)){
330 publishStatistics(); // not sure if that is good for anything
334 if (extensions != null) {
335 for (PublisherExtension e : extensions) {
336 e.publishPropertyViolation(this);
342 public void publishFinished() {
343 if (extensions != null) {
344 for (PublisherExtension e : extensions) {
345 e.publishFinished(this);
349 for (String topic : finishedItems) {
350 if ("result".equalsIgnoreCase(topic)){
352 } else if ("statistics".equalsIgnoreCase(topic)){
358 protected void publishProlog() {} // XML headers etc
359 protected void publishEpilog() {} // likewise at the end
361 //--- our standard topics (only placeholders here)
362 protected void publishJPF() {}
363 protected void publishJPFConfig() {}
364 protected void publishPlatform() {}
365 protected void publishUser() {}
366 protected void publishDTG() {}
367 protected void publishJava() {}
368 protected void publishSuT() {}
369 protected void publishResult() {}
370 protected void publishError() {}
371 protected void publishConstraint() {}
372 protected void publishTrace() {}
373 protected void publishOutput() {}
374 protected void publishSnapshot() {}
375 protected void publishStatistics() {}
377 //--- internal helpers