trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/engine/preferences/XMLBaseConfigurationExporter.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  * Base class for all configuration exporters
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.engine.preferences;
       
    20 
       
    21 import java.io.BufferedOutputStream;
       
    22 import java.io.File;
       
    23 import java.io.FileOutputStream;
       
    24 import java.io.IOException;
       
    25 import java.io.OutputStream;
       
    26 import java.io.OutputStreamWriter;
       
    27 import java.io.StringWriter;
       
    28 
       
    29 import javax.xml.parsers.DocumentBuilder;
       
    30 import javax.xml.parsers.DocumentBuilderFactory;
       
    31 import javax.xml.parsers.ParserConfigurationException;
       
    32 import javax.xml.transform.OutputKeys;
       
    33 import javax.xml.transform.Transformer;
       
    34 import javax.xml.transform.TransformerConfigurationException;
       
    35 import javax.xml.transform.TransformerException;
       
    36 import javax.xml.transform.TransformerFactory;
       
    37 import javax.xml.transform.dom.DOMSource;
       
    38 import javax.xml.transform.stream.StreamResult;
       
    39 
       
    40 import org.eclipse.core.runtime.IPath;
       
    41 import org.w3c.dom.Document;
       
    42 import org.xml.sax.SAXException;
       
    43 
       
    44 import com.nokia.traceviewer.TraceViewerPlugin;
       
    45 import com.nokia.traceviewer.dialog.treeitem.TreeItem;
       
    46 
       
    47 /**
       
    48  * Base class for all configuration exporters
       
    49  * 
       
    50  */
       
    51 public abstract class XMLBaseConfigurationExporter implements
       
    52 		XMLConfigurationFileConstants {
       
    53 
       
    54 	/**
       
    55 	 * Root Treeitem
       
    56 	 */
       
    57 	protected TreeItem root;
       
    58 
       
    59 	/**
       
    60 	 * File path
       
    61 	 */
       
    62 	private String filePath;
       
    63 
       
    64 	/**
       
    65 	 * Output writer
       
    66 	 */
       
    67 	private OutputStreamWriter out;
       
    68 
       
    69 	/**
       
    70 	 * Constructor
       
    71 	 * 
       
    72 	 * @param root
       
    73 	 *            root of the elements
       
    74 	 * @param configurationFileName
       
    75 	 *            file name where to export data
       
    76 	 * @param pathRelative
       
    77 	 *            tells is the path relative
       
    78 	 */
       
    79 	public XMLBaseConfigurationExporter(TreeItem root,
       
    80 			String configurationFileName, boolean pathRelative) {
       
    81 		this.root = root;
       
    82 
       
    83 		// If path is relative, get plugins metadata folder
       
    84 		if (pathRelative) {
       
    85 			IPath path = TraceViewerPlugin.getDefault().getStateLocation()
       
    86 					.append(configurationFileName);
       
    87 			filePath = path.toOSString();
       
    88 		} else {
       
    89 			filePath = configurationFileName;
       
    90 		}
       
    91 		createFile();
       
    92 	}
       
    93 
       
    94 	/**
       
    95 	 * Creates the file
       
    96 	 */
       
    97 	public void createFile() {
       
    98 		File file = new File(filePath);
       
    99 
       
   100 		// File doesn't exist, create it
       
   101 		if (!file.exists()) {
       
   102 			createFileSkeleton();
       
   103 		} else {
       
   104 			// File exists
       
   105 		}
       
   106 	}
       
   107 
       
   108 	/**
       
   109 	 * Creates XML file skeleton
       
   110 	 */
       
   111 	private void createFileSkeleton() {
       
   112 		try {
       
   113 			// Open an Output Stream Writer and set encoding
       
   114 			OutputStream fout = new FileOutputStream(filePath);
       
   115 			OutputStream bout = new BufferedOutputStream(fout);
       
   116 			out = new OutputStreamWriter(bout, "UTF-8"); //$NON-NLS-1$
       
   117 
       
   118 			// Write dummy Dictionary XML file
       
   119 			out.write(XML_HEADER);
       
   120 			out.write(FILE_START);
       
   121 			out.write(MAINCONFIGURATIONS_START);
       
   122 			out.write(GENERAL_CONFIGURATION_START);
       
   123 			out.write(GENERAL_CONFIGURATION_END);
       
   124 			out.write(COLOR_CONFIGURATION_START);
       
   125 			out.write(COLOR_CONFIGURATION_END);
       
   126 			out.write(FILTER_CONFIGURATION_START);
       
   127 			out.write(FILTER_CONFIGURATION_END);
       
   128 			out.write(LINECOUNT_CONFIGURATION_START);
       
   129 			out.write(LINECOUNT_CONFIGURATION_END);
       
   130 			out.write(VARIABLETRACING_CONFIGURATION_START);
       
   131 			out.write(VARIABLETRACING_CONFIGURATION_END);
       
   132 			out.write(TRIGGER_CONFIGURATION_START);
       
   133 			out.write(TRIGGER_CONFIGURATION_END);
       
   134 			out.write(MAINCONFIGURATIONS_END);
       
   135 			out.write(FILE_END);
       
   136 
       
   137 			// Flush and close the stream
       
   138 			out.flush();
       
   139 			out.close();
       
   140 		} catch (IOException e) {
       
   141 			e.printStackTrace();
       
   142 		}
       
   143 
       
   144 	}
       
   145 
       
   146 	/**
       
   147 	 * Writes ready XML string to file
       
   148 	 * 
       
   149 	 * @param xmlString
       
   150 	 */
       
   151 	protected void writeFile(String xmlString) {
       
   152 
       
   153 		try {
       
   154 			// Open an Output Stream Writer to set encoding
       
   155 			OutputStream fout = new FileOutputStream(filePath);
       
   156 			OutputStream bout = new BufferedOutputStream(fout);
       
   157 			out = new OutputStreamWriter(bout, "UTF-8"); //$NON-NLS-1$
       
   158 
       
   159 		} catch (IOException e) {
       
   160 			e.printStackTrace();
       
   161 		}
       
   162 		try {
       
   163 			out.write(xmlString);
       
   164 			out.flush();
       
   165 			out.close();
       
   166 		} catch (IOException e) {
       
   167 			e.printStackTrace();
       
   168 		}
       
   169 	}
       
   170 
       
   171 	/**
       
   172 	 * Gets result from document
       
   173 	 * 
       
   174 	 * @param doc
       
   175 	 *            document
       
   176 	 * @return result
       
   177 	 */
       
   178 	protected StreamResult getResultFromDocument(Document doc) {
       
   179 		TransformerFactory tf = TransformerFactory.newInstance();
       
   180 		int indentSize = 2;
       
   181 		tf.setAttribute("indent-number", Integer.valueOf(indentSize)); //$NON-NLS-1$
       
   182 		Transformer transformer = null;
       
   183 		try {
       
   184 			transformer = tf.newTransformer();
       
   185 		} catch (TransformerConfigurationException e1) {
       
   186 			e1.printStackTrace();
       
   187 		}
       
   188 		if (transformer != null) {
       
   189 			transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
       
   190 		}
       
   191 
       
   192 		// initialize StreamResult with File object to save to file
       
   193 		StreamResult result = new StreamResult(new StringWriter());
       
   194 		DOMSource source = new DOMSource(doc);
       
   195 		try {
       
   196 			if (transformer != null) {
       
   197 				transformer.transform(source, result);
       
   198 			}
       
   199 		} catch (TransformerException e) {
       
   200 			e.printStackTrace();
       
   201 		}
       
   202 		return result;
       
   203 	}
       
   204 
       
   205 	/**
       
   206 	 * Gets DOM document for the file path
       
   207 	 * 
       
   208 	 * @return document
       
   209 	 */
       
   210 	protected Document getDocument() {
       
   211 		DocumentBuilderFactory docFactory = DocumentBuilderFactory
       
   212 				.newInstance();
       
   213 		DocumentBuilder docBuilder = null;
       
   214 		try {
       
   215 			docBuilder = docFactory.newDocumentBuilder();
       
   216 		} catch (ParserConfigurationException e) {
       
   217 			e.printStackTrace();
       
   218 		}
       
   219 
       
   220 		// Get the document
       
   221 		Document doc = null;
       
   222 		try {
       
   223 			if (docBuilder != null) {
       
   224 				File file = new File(filePath);
       
   225 				doc = docBuilder.parse(file);
       
   226 			}
       
   227 		} catch (SAXException e1) {
       
   228 			e1.printStackTrace();
       
   229 		} catch (IOException e1) {
       
   230 			e1.printStackTrace();
       
   231 		}
       
   232 		return doc;
       
   233 	}
       
   234 
       
   235 }