1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.model
;
15 import java
.util
.HashMap
;
17 import java
.util
.Map
.Entry
;
18 import java
.util
.regex
.Matcher
;
19 import java
.util
.regex
.Pattern
;
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
;
29 * Abstract operation that generates the manifest based on the content of a
30 * remote node or import package.
32 * @author Marc-Andre Laperle
33 * @author Bernd Hufmann
35 public abstract class AbstractGenerateManifestOperation
extends AbstractTracePackageOperation
{
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$
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
;
46 * Constructs a new trace package operation
49 * the output file name
51 public AbstractGenerateManifestOperation(String fileName
) {
56 * Generates regular expression patterns from the template element.
58 * @param templateElements
59 * input template elements
60 * @return map of generated {@link Pattern} to corresponding
61 * {@link TracePackageFilesElement}
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
);
79 return templatePatterns
;
83 * Returns a matching pair of {@link Pattern} to corresponding
84 * {@link TracePackageFilesElement} from given path.
86 * @param fullArchivePath
87 * the input path to match
88 * @return a matching pair of {@link Pattern} to corresponding
89 * {@link TracePackageFilesElement}
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
)) {
98 } else if (entry
.getKey().matcher(fullArchivePath
.toPortableString()).matches()) {
107 * Returns whether {@link Pattern} and trace type of
108 * {@link TracePackageFilesElement} matches a directory trace or not.
113 * the map entry of {@link Pattern} to corresponding
114 * {@link TracePackageFilesElement}
116 * @return <code>true</code> for directory trace else false
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())) {
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:
134 * Filename: folder/blah.1.txt Filename pattern: folder/blah.(\d+).txt Trace
135 * name: folder-blah.(group1).txt
137 * Result: folder-blah.1.txt
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
145 * the file name in the archive
146 * @return the resulting String, with the (group#) patterns substituted
148 protected String
substituteGroups(String traceName
, Pattern fileNamePattern
, String fileName
) {
149 String newString
= traceName
;
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
);