trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/engine/preferences/XMLLineCountConfigurationImporter.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  * Imports LineCountRule configuration from XML file
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.engine.preferences;
       
    20 
       
    21 import java.io.File;
       
    22 
       
    23 import org.w3c.dom.Attr;
       
    24 import org.w3c.dom.Document;
       
    25 import org.w3c.dom.Element;
       
    26 import org.w3c.dom.NamedNodeMap;
       
    27 import org.w3c.dom.Node;
       
    28 import org.w3c.dom.NodeList;
       
    29 
       
    30 import com.nokia.traceviewer.dialog.BasePropertyDialog;
       
    31 import com.nokia.traceviewer.dialog.treeitem.LineCountTreeBaseItem;
       
    32 import com.nokia.traceviewer.dialog.treeitem.LineCountTreeComponentItem;
       
    33 import com.nokia.traceviewer.dialog.treeitem.LineCountTreeItem;
       
    34 import com.nokia.traceviewer.dialog.treeitem.LineCountTreeTextItem;
       
    35 import com.nokia.traceviewer.dialog.treeitem.TreeItem;
       
    36 import com.nokia.traceviewer.dialog.treeitem.TreeItemListener;
       
    37 import com.nokia.traceviewer.engine.TraceViewerGlobals;
       
    38 
       
    39 /**
       
    40  * Imports LineCountRule configuration from XML file
       
    41  */
       
    42 public final class XMLLineCountConfigurationImporter extends
       
    43 		XMLBaseConfigurationImporter {
       
    44 
       
    45 	/**
       
    46 	 * Constructor
       
    47 	 * 
       
    48 	 * @param root
       
    49 	 *            root of the LineCount elements
       
    50 	 * @param configurationFileName
       
    51 	 *            file name where to export data
       
    52 	 * @param pathRelative
       
    53 	 *            tells is the path relative
       
    54 	 */
       
    55 	public XMLLineCountConfigurationImporter(TreeItem root,
       
    56 			String configurationFileName, boolean pathRelative) {
       
    57 		super(root, configurationFileName, pathRelative);
       
    58 	}
       
    59 
       
    60 	/**
       
    61 	 * Imports data
       
    62 	 */
       
    63 	public void importData() {
       
    64 		File file = new File(filePath);
       
    65 		if (file.exists()) {
       
    66 			Document doc = getDocument();
       
    67 
       
    68 			if (doc != null) {
       
    69 
       
    70 				// Remove all children from the tree item
       
    71 				Object[] oldRules = root.getChildren();
       
    72 				for (int i = 0; i < oldRules.length; i++) {
       
    73 					root.removeChild((LineCountTreeItem) oldRules[i]);
       
    74 				}
       
    75 
       
    76 				// Delete old rules from the processor
       
    77 				TraceViewerGlobals.getTraceViewer().getDataProcessorAccess()
       
    78 						.getLineCountProcessor().getLineCountItems().clear();
       
    79 				TraceViewerGlobals.getTraceViewer().getDataProcessorAccess()
       
    80 						.getLineCountProcessor().getTextRules().clear();
       
    81 				TraceViewerGlobals.getTraceViewer().getDataProcessorAccess()
       
    82 						.getLineCountProcessor().getComponentRules().clear();
       
    83 
       
    84 				// Get lineCount node from the XML file
       
    85 				NodeList list = doc.getElementsByTagName(LINECOUNT_TAG);
       
    86 				Node parent = list.item(0);
       
    87 
       
    88 				// Import rules
       
    89 				if (parent != null) {
       
    90 					NodeList children = parent.getChildNodes();
       
    91 					for (int i = 0; i < children.getLength(); i++) {
       
    92 						if (children.item(i) instanceof Element) {
       
    93 							processChild(children.item(i), root);
       
    94 						}
       
    95 					}
       
    96 				}
       
    97 			}
       
    98 		}
       
    99 	}
       
   100 
       
   101 	/**
       
   102 	 * Import this Line Count rule to root
       
   103 	 * 
       
   104 	 * @param node
       
   105 	 *            Line Count node to import
       
   106 	 * @param root
       
   107 	 *            Root node of lineCount dialog
       
   108 	 */
       
   109 	private void processChild(Node node, TreeItem root) {
       
   110 		String name = null;
       
   111 		NamedNodeMap itemAttributes = node.getAttributes();
       
   112 
       
   113 		// Get name
       
   114 		Attr nameAttr = (Attr) itemAttributes.getNamedItem(NAME_TAG);
       
   115 		name = nameAttr.getValue();
       
   116 
       
   117 		// Group item
       
   118 		TreeItem newItem = null;
       
   119 		if (node.getNodeName().equals(GROUP_TAG)) {
       
   120 			newItem = new LineCountTreeBaseItem(TraceViewerGlobals
       
   121 					.getTraceViewer().getDataProcessorAccess()
       
   122 					.getLineCountProcessor().getTreeItemListener(), root, name,
       
   123 					LineCountTreeItem.Rule.GROUP);
       
   124 
       
   125 			// This is for normal rules
       
   126 		} else if (node.getNodeName().equals(CONFIGURATION_TAG)) {
       
   127 
       
   128 			// Get tree item listener from the processor
       
   129 			TreeItemListener listener = TraceViewerGlobals.getTraceViewer()
       
   130 					.getDataProcessorAccess().getLineCountProcessor()
       
   131 					.getTreeItemListener();
       
   132 
       
   133 			// Get rule
       
   134 			Attr ruleAttr = (Attr) itemAttributes.getNamedItem(RULE_TAG);
       
   135 			String rule = ruleAttr.getValue();
       
   136 
       
   137 			if (rule.equals(TEXTRULE_TAG)) {
       
   138 				newItem = processTextRule(node, root, name, listener);
       
   139 			} else if (rule.equals(COMPONENTRULE_TAG)) {
       
   140 				newItem = processComponentRule(node, root, name, listener);
       
   141 			}
       
   142 
       
   143 			// Check if the rules should be kept over boot
       
   144 			Attr enabledAttr = (Attr) itemAttributes.getNamedItem(ENABLED_TAG);
       
   145 			if (enabledAttr != null) {
       
   146 				String enabled = enabledAttr.getValue();
       
   147 				if (enabled.equals(YES_TAG)) {
       
   148 					TraceViewerGlobals.getTraceViewer()
       
   149 							.getDataProcessorAccess().getLineCountProcessor()
       
   150 							.enableRule((LineCountTreeItem) newItem);
       
   151 				}
       
   152 			}
       
   153 
       
   154 		} else {
       
   155 			// Unknown rule, shouldn't come here
       
   156 		}
       
   157 
       
   158 		// Add the child to the parent node
       
   159 		if (newItem != null) {
       
   160 			root.addChild(newItem);
       
   161 		}
       
   162 
       
   163 		// If node was group, process it's children
       
   164 		if (node.getNodeName().equals(GROUP_TAG)) {
       
   165 			// Get children
       
   166 			NodeList children = node.getChildNodes();
       
   167 			for (int i = 0; i < children.getLength(); i++) {
       
   168 				if (children.item(i) instanceof Element) {
       
   169 					processChild(children.item(i), newItem);
       
   170 				}
       
   171 			}
       
   172 		}
       
   173 
       
   174 	}
       
   175 
       
   176 	/**
       
   177 	 * Process text LineCount rule
       
   178 	 * 
       
   179 	 * @param node
       
   180 	 *            node to process
       
   181 	 * @param root
       
   182 	 *            root node
       
   183 	 * @param name
       
   184 	 *            name for the rule
       
   185 	 * @param listener
       
   186 	 *            TreeItem Listener
       
   187 	 * @return the newly created text item
       
   188 	 */
       
   189 	private TreeItem processTextRule(Node node, TreeItem root, String name,
       
   190 			TreeItemListener listener) {
       
   191 		TreeItem newItem;
       
   192 		String text = null;
       
   193 		boolean matchCase = false;
       
   194 		NodeList textProperties = node.getChildNodes();
       
   195 		for (int i = 0; i < textProperties.getLength(); i++) {
       
   196 			if (textProperties.item(i) instanceof Element) {
       
   197 				if (textProperties.item(i).getNodeName().equals(TEXT_TAG)) {
       
   198 					text = textProperties.item(i).getTextContent();
       
   199 				} else if (textProperties.item(i).getNodeName().equals(
       
   200 						MATCHCASE_TAG)) {
       
   201 					if (textProperties.item(i).getTextContent().equals(NO_TAG)) {
       
   202 						matchCase = false;
       
   203 					} else {
       
   204 						matchCase = true;
       
   205 					}
       
   206 				}
       
   207 			}
       
   208 		}
       
   209 
       
   210 		// Create new item
       
   211 		newItem = new LineCountTreeTextItem(listener, root, name,
       
   212 				LineCountTreeItem.Rule.TEXT_RULE, text, matchCase);
       
   213 		return newItem;
       
   214 	}
       
   215 
       
   216 	/**
       
   217 	 * Process component LineCount rule
       
   218 	 * 
       
   219 	 * @param node
       
   220 	 *            node to process
       
   221 	 * @param root
       
   222 	 *            root node
       
   223 	 * @param name
       
   224 	 *            name of the rule
       
   225 	 * @param listener
       
   226 	 *            TreeItem Listener
       
   227 	 * @return the newly created component item
       
   228 	 */
       
   229 	private TreeItem processComponentRule(Node node, TreeItem root,
       
   230 			String name, TreeItemListener listener) {
       
   231 		TreeItem newItem;
       
   232 		int componentId = BasePropertyDialog.WILDCARD_INTEGER;
       
   233 		int groupId = BasePropertyDialog.WILDCARD_INTEGER;
       
   234 		NodeList properties = node.getChildNodes();
       
   235 		for (int i = 0; i < properties.getLength(); i++) {
       
   236 			if (properties.item(i) instanceof Element) {
       
   237 				String nodeName = properties.item(i).getNodeName();
       
   238 				if (nodeName.equals(COMPONENTID_TAG)) {
       
   239 					// Get component ID. If parse fails, it means that component
       
   240 					// ID is a wildcard
       
   241 					try {
       
   242 						componentId = Integer.parseInt(properties.item(i)
       
   243 								.getTextContent());
       
   244 					} catch (NumberFormatException e) {
       
   245 					}
       
   246 				} else if (nodeName.equals(GROUPID_TAG)) {
       
   247 					// Get group ID. If parse fails, it means that group
       
   248 					// ID is a wildcard
       
   249 					try {
       
   250 						groupId = Integer.parseInt(properties.item(i)
       
   251 								.getTextContent());
       
   252 					} catch (NumberFormatException e) {
       
   253 					}
       
   254 				}
       
   255 			}
       
   256 		}
       
   257 
       
   258 		// Create new component item
       
   259 		newItem = new LineCountTreeComponentItem(listener, root, name,
       
   260 				LineCountTreeItem.Rule.COMPONENT_RULE, componentId, groupId);
       
   261 		return newItem;
       
   262 	}
       
   263 }