d2b9935b591c6c63048079ac92cd0323c428ccb8
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / parsers / custom / CustomTraceDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.parsers.custom;
14
15 import java.io.ByteArrayInputStream;
16 import java.text.SimpleDateFormat;
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
21 import org.xml.sax.EntityResolver;
22 import org.xml.sax.ErrorHandler;
23 import org.xml.sax.InputSource;
24 import org.xml.sax.SAXException;
25 import org.xml.sax.SAXParseException;
26
27 /**
28 * Base class for custom trace definitions.
29 *
30 * @author Patrick Tassé
31 */
32 public abstract class CustomTraceDefinition {
33
34 /** "set" action */
35 public static final int ACTION_SET = 0;
36
37 /** "append" action */
38 public static final int ACTION_APPEND = 1;
39
40 /** "append with separator" action */
41 public static final int ACTION_APPEND_WITH_SEPARATOR = 2;
42
43 /** Timestamp tag */
44 public static final String TAG_TIMESTAMP = Messages.CustomTraceDefinition_timestampTag;
45
46 /** Message tag */
47 public static final String TAG_MESSAGE = Messages.CustomTraceDefinition_messageTag;
48
49 /** "Other" tag */
50 public static final String TAG_OTHER = Messages.CustomTraceDefinition_otherTag;
51
52 /** Event type tag
53 * @since 2.1*/
54 public static final String TAG_EVENT_TYPE = Messages.CustomTraceDefinition_eventTypeTag;
55
56 /** Category of this trace definition */
57 public String categoryName;
58
59 /** Name of this trace definition */
60 public String definitionName;
61
62 /** List of output columns */
63 public List<OutputColumn> outputs;
64
65 /** Timestamp format */
66 public String timeStampOutputFormat;
67
68 /**
69 * Definition of an output column
70 */
71 public static class OutputColumn {
72
73 /** Name of this column */
74 public @NonNull String name;
75
76 /**
77 * Default constructor (empty)
78 */
79 public OutputColumn() {
80 this(""); //$NON-NLS-1$
81 }
82
83 /**
84 * Constructor
85 *
86 * @param name Name of this output column
87 */
88 public OutputColumn(@NonNull String name) {
89 this.name = name;
90 }
91
92 @Override
93 public String toString() {
94 return name;
95 }
96 }
97
98 /**
99 * Format a timestamp in this trace's current time stamp format.
100 *
101 * @param timestamp
102 * The timestamp to format
103 * @return The same timestamp as a formatted string
104 */
105 public String formatTimeStamp(TmfTimestamp timestamp) {
106 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeStampOutputFormat);
107 return simpleDateFormat.format(timestamp.getValue());
108 }
109
110 /**
111 * Save this custom trace in the default path.
112 */
113 public abstract void save();
114
115 /**
116 * Save this custom trace in the supplied path.
117 *
118 * @param path
119 * The path to save to
120 */
121 public abstract void save(String path);
122
123 /**
124 * Creates a new empty entity resolver
125 *
126 * @return a new entity resolver
127 */
128 protected static EntityResolver createEmptyEntityResolver() {
129 return new EntityResolver() {
130 @Override
131 public InputSource resolveEntity(String publicId, String systemId) {
132 String empty = ""; //$NON-NLS-1$
133 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
134 return new InputSource(bais);
135 }
136 };
137 }
138
139 /**
140 * Creates an error handler for parse exceptions
141 *
142 * @return a new error handler
143 */
144 protected static ErrorHandler createErrorHandler() {
145 return new ErrorHandler() {
146 @Override
147 public void error(SAXParseException saxparseexception) throws SAXException {
148 }
149
150 @Override
151 public void warning(SAXParseException saxparseexception) throws SAXException {
152 }
153
154 @Override
155 public void fatalError(SAXParseException saxparseexception) throws SAXException {
156 throw saxparseexception;
157 }
158 };
159 }
160 }
This page took 0.036018 seconds and 4 git commands to generate.