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