1 /*******************************************************************************
2 * Copyright (c) 2010, 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 * Patrick Tasse - Initial API and implementation
11 * Bernd Hufmann - Ensure backwards compatibility to Linux Tools
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.views
.filter
;
17 import java
.io
.FileNotFoundException
;
18 import java
.io
.IOException
;
20 import javax
.xml
.parsers
.ParserConfigurationException
;
22 import org
.eclipse
.core
.runtime
.IPath
;
23 import org
.eclipse
.jdt
.annotation
.NonNull
;
24 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.model
.ITmfFilterTreeNode
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.model
.TmfFilterRootNode
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.xml
.TmfFilterXMLParser
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.xml
.TmfFilterXMLWriter
;
29 import org
.xml
.sax
.SAXException
;
32 * Central filter manager
35 * @author Patrick Tasse
37 public class FilterManager
{
39 private static final String SAVED_FILTERS_FILE_NAME
= "saved_filters.xml"; //$NON-NLS-1$
40 private static final String SAVED_FILTERS_PATH_NAME
=
41 Activator
.getDefault().getStateLocation().addTrailingSeparator().append(SAVED_FILTERS_FILE_NAME
).toString();
44 * Legacy path to the XML definitions file (in Linux Tools)
45 * TODO Remove once we feel the transition phase is over.
47 private static final IPath SAVED_FILTERS_FILE_NAME_LEGACY
=
48 Activator
.getDefault().getStateLocation().removeLastSegments(1)
49 .append("org.eclipse.linuxtools.tmf.ui") //$NON-NLS-1$
50 .append(SAVED_FILTERS_FILE_NAME
);
53 private static ITmfFilterTreeNode fRoot
= new TmfFilterRootNode();
56 File defaultFile
= new File(SAVED_FILTERS_PATH_NAME
);
60 * If there is no file at the expected location, check the legacy
63 if (!defaultFile
.exists()) {
64 File legacyFileCore
= SAVED_FILTERS_FILE_NAME_LEGACY
.toFile();
65 if (legacyFileCore
.exists()) {
66 ITmfFilterTreeNode root
= new TmfFilterXMLParser(SAVED_FILTERS_FILE_NAME_LEGACY
.toString()).getTree();
67 setSavedFilters(root
.getChildren());
70 } catch (FileNotFoundException e
) {
71 } catch (SAXException e
) {
72 Activator
.getDefault().logError("Error parsing saved filter xml file: " + SAVED_FILTERS_FILE_NAME_LEGACY
, e
); //$NON-NLS-1$
73 } catch (IOException e
) {
74 Activator
.getDefault().logError("Error parsing saved filter xml file: " + SAVED_FILTERS_FILE_NAME_LEGACY
, e
); //$NON-NLS-1$
78 // Now load the filters from the current location
79 fRoot
= new TmfFilterXMLParser(SAVED_FILTERS_PATH_NAME
).getTree();
80 } catch (FileNotFoundException e
) {
81 } catch (SAXException e
) {
82 Activator
.getDefault().logError("Error parsing saved filter xml file: " + SAVED_FILTERS_PATH_NAME
, e
); //$NON-NLS-1$
83 } catch (IOException e
) {
84 Activator
.getDefault().logError("Error parsing saved filter xml file: " + SAVED_FILTERS_PATH_NAME
, e
); //$NON-NLS-1$
89 * Retrieve the currently saved filters
91 * @return The array of filters
93 public static @NonNull ITmfFilterTreeNode
[] getSavedFilters() {
94 return fRoot
.clone().getChildren();
98 * Set the passed filters as the currently saved ones.
101 * The filters to save
103 public static void setSavedFilters(ITmfFilterTreeNode
[] filters
) {
104 fRoot
= new TmfFilterRootNode();
105 for (ITmfFilterTreeNode filter
: filters
) {
106 fRoot
.addChild(filter
.clone());
109 TmfFilterXMLWriter writerXML
= new TmfFilterXMLWriter(fRoot
);
110 writerXML
.saveTree(SAVED_FILTERS_PATH_NAME
);
111 } catch (ParserConfigurationException e
) {
112 Activator
.getDefault().logError("Error saving filter xml file: " + SAVED_FILTERS_PATH_NAME
, e
); //$NON-NLS-1$