93ef98ec644f687f0c741d0f827a73d57563f268
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / parsers / custom / CustomTraceDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 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 /** Category of this trace definition */
53 public String categoryName;
54
55 /** Name of this trace definition */
56 public String definitionName;
57
58 /** List of output columns */
59 public List<OutputColumn> outputs;
60
61 /** Timestamp format */
62 public String timeStampOutputFormat;
63
64 /**
65 * Definition of an output column
66 */
67 public static class OutputColumn {
68
69 /** Name of this column */
70 public @NonNull String name;
71
72 /**
73 * Default constructor (empty)
74 */
75 public OutputColumn() {
76 this(""); //$NON-NLS-1$
77 }
78
79 /**
80 * Constructor
81 *
82 * @param name Name of this output column
83 */
84 public OutputColumn(@NonNull String name) {
85 this.name = name;
86 }
87
88 @Override
89 public String toString() {
90 return name;
91 }
92 }
93
94 /**
95 * Format a timestamp in this trace's current time stamp format.
96 *
97 * @param timestamp
98 * The timestamp to format
99 * @return The same timestamp as a formatted string
100 */
101 public String formatTimeStamp(TmfTimestamp timestamp) {
102 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeStampOutputFormat);
103 return simpleDateFormat.format(timestamp.getValue());
104 }
105
106 /**
107 * Save this custom trace in the default path.
108 */
109 public abstract void save();
110
111 /**
112 * Save this custom trace in the supplied path.
113 *
114 * @param path
115 * The path to save to
116 */
117 public abstract void save(String path);
118
119 /**
120 * Creates a new empty entity resolver
121 *
122 * @return a new entity resolver
123 */
124 protected static EntityResolver createEmptyEntityResolver() {
125 return new EntityResolver() {
126 @Override
127 public InputSource resolveEntity(String publicId, String systemId) {
128 String empty = ""; //$NON-NLS-1$
129 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
130 return new InputSource(bais);
131 }
132 };
133 }
134
135 /**
136 * Creates an error handler for parse exceptions
137 *
138 * @return a new error handler
139 */
140 protected static ErrorHandler createErrorHandler() {
141 return new ErrorHandler() {
142 @Override
143 public void error(SAXParseException saxparseexception) throws SAXException {
144 }
145
146 @Override
147 public void warning(SAXParseException saxparseexception) throws SAXException {
148 }
149
150 @Override
151 public void fatalError(SAXParseException saxparseexception) throws SAXException {
152 throw saxparseexception;
153 }
154 };
155 }
156 }
This page took 0.063633 seconds and 4 git commands to generate.