trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/engine/preferences/XMLVariableTracingConfigurationExporter.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 Variable Tracing configuration to XML file
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.engine.preferences;
       
    20 
       
    21 import javax.xml.transform.stream.StreamResult;
       
    22 
       
    23 import org.w3c.dom.Attr;
       
    24 import org.w3c.dom.Document;
       
    25 import org.w3c.dom.NamedNodeMap;
       
    26 import org.w3c.dom.Node;
       
    27 import org.w3c.dom.NodeList;
       
    28 
       
    29 import com.nokia.traceviewer.dialog.treeitem.TreeItem;
       
    30 import com.nokia.traceviewer.dialog.treeitem.VariableTracingTreeItem;
       
    31 import com.nokia.traceviewer.dialog.treeitem.VariableTracingTreeTextItem;
       
    32 import com.nokia.traceviewer.engine.TraceViewerGlobals;
       
    33 
       
    34 /**
       
    35  * Exports Variable Tracing configuration to XML file
       
    36  */
       
    37 public final class XMLVariableTracingConfigurationExporter extends
       
    38 		XMLBaseConfigurationExporter {
       
    39 
       
    40 	/**
       
    41 	 * Constructor
       
    42 	 * 
       
    43 	 * @param root
       
    44 	 *            root of the LineCount elements
       
    45 	 * @param configurationFileName
       
    46 	 *            file name where to export data
       
    47 	 * @param pathRelative
       
    48 	 *            tells is the path relative
       
    49 	 */
       
    50 	public XMLVariableTracingConfigurationExporter(TreeItem root,
       
    51 			String configurationFileName, boolean pathRelative) {
       
    52 		super(root, configurationFileName, pathRelative);
       
    53 	}
       
    54 
       
    55 	/**
       
    56 	 * Exports data
       
    57 	 */
       
    58 	public void export() {
       
    59 		Document doc = getDocument();
       
    60 		Node parent;
       
    61 
       
    62 		// Get root node of line count rules
       
    63 		NodeList list = doc.getElementsByTagName(VARIABLETRACING_TAG);
       
    64 
       
    65 		// Tag not found
       
    66 		if (list.getLength() == 0) {
       
    67 			parent = doc.createElement(VARIABLETRACING_TAG);
       
    68 			// Get the main configurations tag
       
    69 			NodeList list2 = doc.getElementsByTagName(CONFIGURATIONS_TAG);
       
    70 			Node mainRoot = list2.item(0);
       
    71 			mainRoot.appendChild(parent);
       
    72 		} else {
       
    73 			parent = list.item(0);
       
    74 		}
       
    75 
       
    76 		// Delete old rule information
       
    77 		NodeList children = parent.getChildNodes();
       
    78 		int len = children.getLength();
       
    79 		for (int i = 0; i < len; i++) {
       
    80 			parent.removeChild(children.item(0));
       
    81 		}
       
    82 
       
    83 		// Process through the tree
       
    84 		for (int i = 0; i < root.getChildren().length; i++) {
       
    85 			processChild((VariableTracingTreeItem) root.getChildren()[i], doc,
       
    86 					parent);
       
    87 		}
       
    88 
       
    89 		// Get result from document
       
    90 		StreamResult result = getResultFromDocument(doc);
       
    91 
       
    92 		// Print out
       
    93 		String xmlString = result.getWriter().toString();
       
    94 		writeFile(xmlString);
       
    95 	}
       
    96 
       
    97 	/**
       
    98 	 * Process child
       
    99 	 * 
       
   100 	 * @param item
       
   101 	 *            child to process
       
   102 	 * @param doc
       
   103 	 *            document
       
   104 	 * @param root
       
   105 	 *            node to insert this child to
       
   106 	 */
       
   107 	private void processChild(VariableTracingTreeItem item, Document doc,
       
   108 			Node root) {
       
   109 
       
   110 		// Process through the tree
       
   111 		Node newItem = null;
       
   112 		if (item.getRule() == VariableTracingTreeItem.Rule.GROUP) {
       
   113 			newItem = doc.createElement(GROUP_TAG);
       
   114 		} else if (item.getRule() == VariableTracingTreeItem.Rule.TEXT_RULE) {
       
   115 			newItem = doc.createElement(CONFIGURATION_TAG);
       
   116 		}
       
   117 		if (newItem != null) {
       
   118 
       
   119 			// Get attributes
       
   120 			NamedNodeMap itemAttributes = newItem.getAttributes();
       
   121 
       
   122 			// Set name
       
   123 			Attr name = doc.createAttribute(NAME_TAG);
       
   124 			name.setValue(item.getName());
       
   125 			itemAttributes.setNamedItem(name);
       
   126 
       
   127 			// Set rule if not group item
       
   128 			if (item.getRule() != VariableTracingTreeItem.Rule.GROUP) {
       
   129 				Attr rule = doc.createAttribute(RULE_TAG);
       
   130 				if (item.getRule() == VariableTracingTreeItem.Rule.TEXT_RULE) {
       
   131 					rule.setValue(TEXTRULE_TAG);
       
   132 				} else {
       
   133 					String unknownText = Messages
       
   134 							.getString("XMLVariableTracingConfigurationExporter.Unkn"); //$NON-NLS-1$ 
       
   135 					rule.setValue(unknownText);
       
   136 				}
       
   137 				itemAttributes.setNamedItem(rule);
       
   138 
       
   139 				// Add enabled attribute
       
   140 				Attr enabled = doc.createAttribute(ENABLED_TAG);
       
   141 				boolean checked = isChecked(item);
       
   142 				if (checked) {
       
   143 					enabled.setValue(YES_TAG);
       
   144 				} else {
       
   145 					enabled.setValue(NO_TAG);
       
   146 				}
       
   147 				itemAttributes.setNamedItem(enabled);
       
   148 			}
       
   149 
       
   150 			// Set as child to parent
       
   151 			root.appendChild(newItem);
       
   152 
       
   153 			// Set nodes when text rule
       
   154 			if (item.getRule() == VariableTracingTreeItem.Rule.TEXT_RULE) {
       
   155 				VariableTracingTreeTextItem textItem = (VariableTracingTreeTextItem) item;
       
   156 
       
   157 				// Text
       
   158 				Node text = doc.createElement(TEXT_TAG);
       
   159 				text.setTextContent(textItem.getText());
       
   160 				newItem.appendChild(text);
       
   161 
       
   162 				// Match case
       
   163 				Node matchCase = doc.createElement(MATCHCASE_TAG);
       
   164 				if (textItem.isMatchCase()) {
       
   165 					matchCase.setTextContent(YES_TAG);
       
   166 				} else {
       
   167 					matchCase.setTextContent(NO_TAG);
       
   168 				}
       
   169 				newItem.appendChild(matchCase);
       
   170 
       
   171 				// Save History
       
   172 				Node saveHistory = doc.createElement(SAVEHISTORY_TAG);
       
   173 				saveHistory.setTextContent(String.valueOf(textItem
       
   174 						.getHistoryCount()));
       
   175 				newItem.appendChild(saveHistory);
       
   176 			}
       
   177 
       
   178 			// Loop through own children
       
   179 			for (int i = 0; i < item.getChildren().length; i++) {
       
   180 				processChild((VariableTracingTreeItem) item.getChildren()[i],
       
   181 						doc, newItem);
       
   182 			}
       
   183 		}
       
   184 	}
       
   185 
       
   186 	/**
       
   187 	 * Checks it the given item is checked in the dialog
       
   188 	 * 
       
   189 	 * @param item
       
   190 	 *            item to be checked
       
   191 	 * @return true if the given item is checked
       
   192 	 */
       
   193 	private boolean isChecked(VariableTracingTreeItem item) {
       
   194 		boolean checked = false;
       
   195 		if (TraceViewerGlobals.getTraceViewer().getDataProcessorAccess()
       
   196 				.getVariableTracingProcessor().getTextRules().contains(item)) {
       
   197 			checked = true;
       
   198 		}
       
   199 		return checked;
       
   200 	}
       
   201 }