trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/engine/preferences/XMLFilterConfigurationExporter.java
changeset 11 5b9d4d8641ce
equal deleted inserted replaced
10:ed1c9f64298a 11:5b9d4d8641ce
       
     1 /*
       
     2  * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  *
       
    16  * Exports Filter configuration to XML file
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.engine.preferences;
       
    20 
       
    21 import java.util.List;
       
    22 
       
    23 import javax.xml.transform.stream.StreamResult;
       
    24 
       
    25 import org.w3c.dom.Attr;
       
    26 import org.w3c.dom.Document;
       
    27 import org.w3c.dom.NamedNodeMap;
       
    28 import org.w3c.dom.Node;
       
    29 import org.w3c.dom.NodeList;
       
    30 
       
    31 import com.nokia.traceviewer.dialog.treeitem.FilterTreeComponentItem;
       
    32 import com.nokia.traceviewer.dialog.treeitem.FilterTreeItem;
       
    33 import com.nokia.traceviewer.dialog.treeitem.FilterTreeTextItem;
       
    34 import com.nokia.traceviewer.dialog.treeitem.TreeItem;
       
    35 import com.nokia.traceviewer.engine.TraceViewerGlobals;
       
    36 import com.nokia.traceviewer.engine.dataprocessor.FilterRuleObject;
       
    37 import com.nokia.traceviewer.engine.dataprocessor.FilterRuleSet;
       
    38 
       
    39 /**
       
    40  * Exports Filter configuration to XML file
       
    41  * 
       
    42  */
       
    43 public final class XMLFilterConfigurationExporter extends
       
    44 		XMLBaseConfigurationExporter {
       
    45 
       
    46 	/**
       
    47 	 * Constructor
       
    48 	 * 
       
    49 	 * @param root
       
    50 	 *            root of the Filter elements
       
    51 	 * @param configurationFileName
       
    52 	 *            file name where to export data
       
    53 	 * @param pathRelative
       
    54 	 *            tells is the path relative
       
    55 	 */
       
    56 	public XMLFilterConfigurationExporter(TreeItem root,
       
    57 			String configurationFileName, boolean pathRelative) {
       
    58 		super(root, configurationFileName, pathRelative);
       
    59 	}
       
    60 
       
    61 	/**
       
    62 	 * Exports data
       
    63 	 */
       
    64 	public void export() {
       
    65 		Document doc = getDocument();
       
    66 		Node parent;
       
    67 
       
    68 		// Get root node of filter rules
       
    69 		NodeList list = doc.getElementsByTagName(FILTER_TAG);
       
    70 		if (list.getLength() == 0) {
       
    71 			parent = doc.createElement(FILTER_TAG);
       
    72 
       
    73 			// Get the main configurations tag
       
    74 			NodeList list2 = doc.getElementsByTagName(CONFIGURATIONS_TAG);
       
    75 			Node mainRoot = list2.item(0);
       
    76 			mainRoot.appendChild(parent);
       
    77 		} else {
       
    78 			parent = list.item(0);
       
    79 		}
       
    80 
       
    81 		// Delete old filter information
       
    82 		NodeList filterChilds = parent.getChildNodes();
       
    83 		int len = filterChilds.getLength();
       
    84 		for (int i = 0; i < len; i++) {
       
    85 			parent.removeChild(filterChilds.item(0));
       
    86 		}
       
    87 
       
    88 		// Add Type, Action and AdvancedFilter attributes
       
    89 		addCommonAttributes(doc, parent);
       
    90 
       
    91 		// Process through the tree
       
    92 		for (int i = 0; i < root.getChildren().length; i++) {
       
    93 			processChild((FilterTreeItem) root.getChildren()[i], doc, parent);
       
    94 		}
       
    95 
       
    96 		// Get result from document
       
    97 		StreamResult result = getResultFromDocument(doc);
       
    98 
       
    99 		// Print out
       
   100 		String xmlString = result.getWriter().toString();
       
   101 		writeFile(xmlString);
       
   102 	}
       
   103 
       
   104 	/**
       
   105 	 * Adds common attributes to the XML
       
   106 	 * 
       
   107 	 * @param doc
       
   108 	 *            document
       
   109 	 * @param parent
       
   110 	 *            parent node
       
   111 	 */
       
   112 	private void addCommonAttributes(Document doc, Node parent) {
       
   113 		// Get attributes
       
   114 		NamedNodeMap attr = parent.getAttributes();
       
   115 
       
   116 		// Type attribute
       
   117 		Attr type = doc.createAttribute(TYPE_TAG);
       
   118 		String operator = AND_TAG;
       
   119 		if (TraceViewerGlobals.getTraceViewer().getDataProcessorAccess()
       
   120 				.getFilterProcessor().isLogicalOrInUse()) {
       
   121 			operator = OR_TAG;
       
   122 		}
       
   123 		type.setValue(operator);
       
   124 		attr.setNamedItem(type);
       
   125 
       
   126 		// Action attribute
       
   127 		Attr action = doc.createAttribute(ACTION_TAG);
       
   128 		String actionStr = HIDE_TAG;
       
   129 		if (TraceViewerGlobals.getTraceViewer().getDataProcessorAccess()
       
   130 				.getFilterProcessor().isShowTracesContainingRule()) {
       
   131 			actionStr = SHOW_TAG;
       
   132 		}
       
   133 		action.setValue(actionStr);
       
   134 		attr.setNamedItem(action);
       
   135 
       
   136 		// Advanced filter attribute
       
   137 		Attr string = doc.createAttribute(ADVANCEDFILTER_TAG);
       
   138 		string.setValue(TraceViewerGlobals.getTraceViewer()
       
   139 				.getDataProcessorAccess().getFilterProcessor()
       
   140 				.getAdvancedFilterString());
       
   141 		attr.setNamedItem(string);
       
   142 	}
       
   143 
       
   144 	/**
       
   145 	 * Process child
       
   146 	 * 
       
   147 	 * @param item
       
   148 	 *            child to process
       
   149 	 * @param doc
       
   150 	 *            document
       
   151 	 * @param root
       
   152 	 *            node to insert this child to
       
   153 	 */
       
   154 	private void processChild(FilterTreeItem item, Document doc, Node root) {
       
   155 
       
   156 		// Process through the tree
       
   157 		Node newItem = null;
       
   158 		// Group rule
       
   159 		if (item.getRule() == FilterTreeItem.Rule.GROUP) {
       
   160 			newItem = doc.createElement(GROUP_TAG);
       
   161 			// Create a configuration
       
   162 		} else {
       
   163 			newItem = doc.createElement(CONFIGURATION_TAG);
       
   164 		}
       
   165 
       
   166 		// Get attributes
       
   167 		NamedNodeMap itemAttributes = newItem.getAttributes();
       
   168 
       
   169 		// Set name
       
   170 		Attr name = doc.createAttribute(NAME_TAG);
       
   171 		name.setValue(item.getName());
       
   172 		itemAttributes.setNamedItem(name);
       
   173 
       
   174 		// Set rule if not group item
       
   175 		if (item.getRule() != FilterTreeItem.Rule.GROUP) {
       
   176 			Attr rule = doc.createAttribute(RULE_TAG);
       
   177 			if (item.getRule() == FilterTreeItem.Rule.TEXT_RULE) {
       
   178 				rule.setValue(TEXTRULE_TAG);
       
   179 			} else if (item.getRule() == FilterTreeItem.Rule.COMPONENT_RULE) {
       
   180 				rule.setValue(COMPONENTRULE_TAG);
       
   181 			} else {
       
   182 				String unknownText = Messages
       
   183 						.getString("XMLFilterConfigurationExporter.UnknownText"); //$NON-NLS-1$
       
   184 				rule.setValue(unknownText);
       
   185 			}
       
   186 			itemAttributes.setNamedItem(rule);
       
   187 
       
   188 			// Add enabled attribute
       
   189 			Attr enabled = doc.createAttribute(ENABLED_TAG);
       
   190 			boolean checked = isChecked(item);
       
   191 			if (checked) {
       
   192 				enabled.setValue(YES_TAG);
       
   193 			} else {
       
   194 				enabled.setValue(NO_TAG);
       
   195 			}
       
   196 			itemAttributes.setNamedItem(enabled);
       
   197 		}
       
   198 
       
   199 		// Set as child to parent
       
   200 		root.appendChild(newItem);
       
   201 
       
   202 		// Set nodes when text rule
       
   203 		if (item.getRule() == FilterTreeItem.Rule.TEXT_RULE) {
       
   204 			FilterTreeTextItem textItem = (FilterTreeTextItem) item;
       
   205 
       
   206 			// Text
       
   207 			Node text = doc.createElement(TEXT_TAG);
       
   208 			text.setTextContent(textItem.getText());
       
   209 			newItem.appendChild(text);
       
   210 
       
   211 			// Match case
       
   212 			Node matchCase = doc.createElement(MATCHCASE_TAG);
       
   213 			if (textItem.isMatchCase()) {
       
   214 				matchCase.setTextContent(YES_TAG);
       
   215 			} else {
       
   216 				matchCase.setTextContent(NO_TAG);
       
   217 			}
       
   218 			newItem.appendChild(matchCase);
       
   219 
       
   220 			// Component rule
       
   221 		} else if (item.getRule() == FilterTreeItem.Rule.COMPONENT_RULE) {
       
   222 			FilterTreeComponentItem componentItem = (FilterTreeComponentItem) item;
       
   223 
       
   224 			// Component ID
       
   225 			Node component = doc.createElement(COMPONENTID_TAG);
       
   226 			component.setTextContent(String.valueOf(componentItem
       
   227 					.getComponentId()));
       
   228 			newItem.appendChild(component);
       
   229 
       
   230 			// Group ID
       
   231 			Node group = doc.createElement(GROUPID_TAG);
       
   232 			group.setTextContent(String.valueOf(componentItem.getGroupId()));
       
   233 			newItem.appendChild(group);
       
   234 		}
       
   235 
       
   236 		// Loop through own children
       
   237 		for (int i = 0; i < item.getChildren().length; i++) {
       
   238 			processChild((FilterTreeItem) item.getChildren()[i], doc, newItem);
       
   239 		}
       
   240 	}
       
   241 
       
   242 	/**
       
   243 	 * Checks it the given item is checked in the filter dialog
       
   244 	 * 
       
   245 	 * @param item
       
   246 	 *            item to be checked
       
   247 	 * @return true if the given item is checked
       
   248 	 */
       
   249 	private boolean isChecked(FilterTreeItem item) {
       
   250 		boolean checked = false;
       
   251 
       
   252 		List<FilterRuleObject> rules = TraceViewerGlobals.getTraceViewer()
       
   253 				.getDataProcessorAccess().getFilterProcessor().getFilterRules()
       
   254 				.getFilterRules();
       
   255 
       
   256 		// First object must be rule set
       
   257 		if (!rules.isEmpty() && rules.get(0) instanceof FilterRuleSet) {
       
   258 			if (((FilterRuleSet) rules.get(0)).getFilterRules().contains(item)) {
       
   259 				checked = true;
       
   260 			}
       
   261 		}
       
   262 
       
   263 		return checked;
       
   264 	}
       
   265 }