Commit | Line | Data |
---|---|---|
be222f56 | 1 | /******************************************************************************* |
7a0b1e3c | 2 | * Copyright (c) 2010, 2015 Ericsson |
be222f56 PT |
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 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.core.parsers.custom; |
be222f56 | 14 | |
cb1cf0e8 | 15 | import java.io.ByteArrayInputStream; |
be222f56 PT |
16 | import java.text.SimpleDateFormat; |
17 | import java.util.List; | |
18 | ||
7a0b1e3c | 19 | import org.eclipse.jdt.annotation.NonNull; |
2bdf0193 | 20 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; |
cb1cf0e8 MK |
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; | |
be222f56 | 26 | |
a0a88f65 AM |
27 | /** |
28 | * Base class for custom trace definitions. | |
29 | * | |
30 | * @author Patrick Tassé | |
31 | */ | |
be222f56 PT |
32 | public abstract class CustomTraceDefinition { |
33 | ||
a0a88f65 | 34 | /** "set" action */ |
be222f56 | 35 | public static final int ACTION_SET = 0; |
a0a88f65 AM |
36 | |
37 | /** "append" action */ | |
be222f56 | 38 | public static final int ACTION_APPEND = 1; |
a0a88f65 AM |
39 | |
40 | /** "append with separator" action */ | |
be222f56 PT |
41 | public static final int ACTION_APPEND_WITH_SEPARATOR = 2; |
42 | ||
a0a88f65 | 43 | /** Timestamp tag */ |
be222f56 | 44 | public static final String TAG_TIMESTAMP = Messages.CustomTraceDefinition_timestampTag; |
a0a88f65 AM |
45 | |
46 | /** Message tag */ | |
be222f56 | 47 | public static final String TAG_MESSAGE = Messages.CustomTraceDefinition_messageTag; |
a0a88f65 AM |
48 | |
49 | /** "Other" tag */ | |
be222f56 PT |
50 | public static final String TAG_OTHER = Messages.CustomTraceDefinition_otherTag; |
51 | ||
ae09c4ad | 52 | /** Category of this trace definition */ |
332527a4 PT |
53 | public String categoryName; |
54 | ||
a0a88f65 | 55 | /** Name of this trace definition */ |
be222f56 | 56 | public String definitionName; |
a0a88f65 AM |
57 | |
58 | /** List of output columns */ | |
be222f56 | 59 | public List<OutputColumn> outputs; |
a0a88f65 AM |
60 | |
61 | /** Timestamp format */ | |
be222f56 PT |
62 | public String timeStampOutputFormat; |
63 | ||
a0a88f65 AM |
64 | /** |
65 | * Definition of an output column | |
66 | */ | |
be222f56 | 67 | public static class OutputColumn { |
a0a88f65 AM |
68 | |
69 | /** Name of this column */ | |
7a0b1e3c | 70 | public @NonNull String name; |
be222f56 | 71 | |
a0a88f65 AM |
72 | /** |
73 | * Default constructor (empty) | |
74 | */ | |
7a0b1e3c PT |
75 | public OutputColumn() { |
76 | this(""); //$NON-NLS-1$ | |
77 | } | |
be222f56 | 78 | |
a0a88f65 AM |
79 | /** |
80 | * Constructor | |
81 | * | |
82 | * @param name Name of this output column | |
83 | */ | |
7a0b1e3c | 84 | public OutputColumn(@NonNull String name) { |
be222f56 PT |
85 | this.name = name; |
86 | } | |
87 | ||
88 | @Override | |
89 | public String toString() { | |
90 | return name; | |
91 | } | |
92 | } | |
93 | ||
a0a88f65 AM |
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 | */ | |
be222f56 PT |
101 | public String formatTimeStamp(TmfTimestamp timestamp) { |
102 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeStampOutputFormat); | |
103 | return simpleDateFormat.format(timestamp.getValue()); | |
104 | } | |
105 | ||
a0a88f65 AM |
106 | /** |
107 | * Save this custom trace in the default path. | |
108 | */ | |
be222f56 | 109 | public abstract void save(); |
a0a88f65 AM |
110 | |
111 | /** | |
112 | * Save this custom trace in the supplied path. | |
113 | * | |
114 | * @param path | |
115 | * The path to save to | |
116 | */ | |
be222f56 | 117 | public abstract void save(String path); |
cb1cf0e8 MK |
118 | |
119 | /** | |
120 | * Creates a new empty entity resolver | |
121 | * | |
122 | * @return a new entity resolver | |
cb1cf0e8 MK |
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 | |
cb1cf0e8 MK |
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 | } | |
be222f56 | 156 | } |