tmf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.remote.ui / src / org / eclipse / tracecompass / internal / tmf / remote / ui / wizards / fetch / model / AbstractGenerateManifestOperation.java
1 /*******************************************************************************
2 * Copyright (c) 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 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.AbstractTracePackageOperation;
23 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement;
24 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageFilesElement;
25 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageTraceElement;
26 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
27
28 /**
29 * Abstract operation that generates the manifest based on the content of a
30 * remote node or import package.
31 *
32 * @author Marc-Andre Laperle
33 * @author Bernd Hufmann
34 */
35 public abstract class AbstractGenerateManifestOperation extends AbstractTracePackageOperation {
36
37 /** A pattern to find where to substitute groups in the trace name */
38 protected static final Pattern GROUP_PATTERN = Pattern.compile("\\(group(\\d+)\\)"); //$NON-NLS-1$
39
40 /** Name of metadata file of trace */
41 protected static final String METADATA_FILE_NAME = "metadata"; //$NON-NLS-1$
42 /** Map of pattern to trace element */
43 protected Map<Pattern, TracePackageTraceElement> fTemplatePatternsToTraceElements;
44
45 /**
46 * Constructs a new trace package operation
47 *
48 * @param fileName
49 * the output file name
50 */
51 public AbstractGenerateManifestOperation(String fileName) {
52 super(fileName);
53 }
54
55 /**
56 * Generates regular expression patterns from the template element.
57 *
58 * @param templateElements
59 * input template elements
60 * @return map of generated {@link Pattern} to corresponding
61 * {@link TracePackageFilesElement}
62 */
63 protected Map<Pattern, TracePackageTraceElement> generatePatterns(TracePackageElement[] templateElements) {
64 Map<Pattern, TracePackageTraceElement> templatePatterns = new HashMap<>();
65 for (TracePackageElement templateElement : templateElements) {
66 if (templateElement instanceof TracePackageTraceElement) {
67 TracePackageElement[] children = templateElement.getChildren();
68 if (children != null) {
69 for (TracePackageElement child : children) {
70 if (child instanceof TracePackageFilesElement) {
71 TracePackageFilesElement tracePackageFilesElement = (TracePackageFilesElement) child;
72 Pattern pattern = Pattern.compile(tracePackageFilesElement.getFileName());
73 templatePatterns.put(pattern, (TracePackageTraceElement) templateElement);
74 }
75 }
76 }
77 }
78 }
79 return templatePatterns;
80 }
81
82 /**
83 * Returns a matching pair of {@link Pattern} to corresponding
84 * {@link TracePackageFilesElement} from given path.
85 *
86 * @param fullArchivePath
87 * the input path to match
88 * @return a matching pair of {@link Pattern} to corresponding
89 * {@link TracePackageFilesElement}
90 */
91 protected Entry<Pattern, TracePackageTraceElement> getMatchingTemplateElement(IPath fullArchivePath) {
92 for (Entry<Pattern, TracePackageTraceElement> entry : fTemplatePatternsToTraceElements.entrySet()) {
93 // Check for CTF trace (metadata)
94 if (TmfTraceType.isDirectoryTraceType(entry.getValue().getTraceType())) {
95 if (matchesDirectoryTrace(fullArchivePath, entry)) {
96 return entry;
97 }
98 } else if (entry.getKey().matcher(fullArchivePath.toPortableString()).matches()) {
99 return entry;
100 }
101 }
102
103 return null;
104 }
105
106 /**
107 * Returns whether {@link Pattern} and trace type of
108 * {@link TracePackageFilesElement} matches a directory trace or not.
109 *
110 * @param archivePath
111 * the archive path
112 * @param entry
113 * the map entry of {@link Pattern} to corresponding
114 * {@link TracePackageFilesElement}
115 *
116 * @return <code>true</code> for directory trace else false
117 */
118 protected boolean matchesDirectoryTrace(IPath archivePath, Entry<Pattern, TracePackageTraceElement> entry) {
119 if (archivePath.lastSegment().equals(METADATA_FILE_NAME)) {
120 IPath archiveParentPath = archivePath.removeLastSegments(1);
121 if (entry.getKey().matcher(archiveParentPath.toPortableString()).matches()) {
122 if (TmfTraceType.isDirectoryTraceType(entry.getValue().getTraceType())) {
123 return true;
124 }
125 }
126 }
127 return false;
128 }
129
130 /**
131 * Substitute group patterns inside the trace name with the groups found in
132 * the file name. This allows renaming traces in flexible ways. For example:
133 *
134 * Filename: folder/blah.1.txt Filename pattern: folder/blah.(\d+).txt Trace
135 * name: folder-blah.(group1).txt
136 *
137 * Result: folder-blah.1.txt
138 *
139 * @param traceName
140 * the target trace name that will get it's groups substituted
141 * @param fileNamePattern
142 * the file name pattern that matched the filename and contains
143 * groups to be used in the substitutions
144 * @param fileName
145 * the file name in the archive
146 * @return the resulting String, with the (group#) patterns substituted
147 */
148 protected String substituteGroups(String traceName, Pattern fileNamePattern, String fileName) {
149 String newString = traceName;
150
151 Matcher fileNameMatcher = fileNamePattern.matcher(fileName);
152 fileNameMatcher.find();
153 int groupCount = fileNameMatcher.groupCount();
154 Matcher matcher = GROUP_PATTERN.matcher(newString);
155 while (matcher.find()) {
156 // Found (group#), now get the #
157 if (matcher.groupCount() == 1) {
158 int groupNo = Integer.parseInt(matcher.group(1));
159 if (groupNo <= groupCount) {
160 String substitutedString = newString.substring(0, matcher.start()) + fileNameMatcher.group(groupNo) + newString.substring(matcher.end());
161 if (!substitutedString.equals(newString)) {
162 // Since the string changed, create a new matcher so
163 // that the next match is at a valid position
164 newString = substitutedString;
165 matcher = GROUP_PATTERN.matcher(newString);
166 }
167 }
168 }
169 }
170
171 return newString;
172 }
173 }
This page took 0.047063 seconds and 5 git commands to generate.