crashanalysis/crashanalyser/com.nokia.s60tools.crashanalyser/src/com/nokia/s60tools/crashanalyser/model/ErrorsXmlReader.java
changeset 0 5ad7ad99af01
child 4 615035072f7e
equal deleted inserted replaced
-1:000000000000 0:5ad7ad99af01
       
     1 /*
       
     2 * Copyright (c) 2008 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 */
       
    17 
       
    18 package com.nokia.s60tools.crashanalyser.model;
       
    19 
       
    20 import javax.xml.parsers.SAXParser;
       
    21 import javax.xml.parsers.SAXParserFactory;
       
    22 import javax.xml.validation.SchemaFactory;
       
    23 import javax.xml.XMLConstants;
       
    24 import org.xml.sax.Attributes;
       
    25 import org.xml.sax.SAXException;
       
    26 import org.xml.sax.helpers.DefaultHandler;
       
    27 import java.util.*;
       
    28 import java.io.File;
       
    29 import java.io.FilenameFilter;
       
    30 import java.util.ArrayList;
       
    31 import com.nokia.s60tools.crashanalyser.containers.ErrorLibraryError;
       
    32 import com.nokia.s60tools.crashanalyser.plugin.*;
       
    33 
       
    34 /**
       
    35  * This class reads all error xml files with SAX parser.  
       
    36  *
       
    37  */
       
    38 public class ErrorsXmlReader extends DefaultHandler {
       
    39 
       
    40 	Map<String, ErrorLibraryError> errors = null;
       
    41 	Map<String, ErrorLibraryError> panics = null;
       
    42 	Map<String, ErrorLibraryError> categories = null;
       
    43 	ErrorLibraryError error = null;
       
    44 	ErrorLibraryError error2 = null;
       
    45 	ErrorLibraryError panic = null;
       
    46 	ErrorLibraryError category = null;
       
    47 	String nodeText = "";
       
    48 	String categoryDescription = "";
       
    49 	String categoryKey = "";
       
    50 	String panicKey = "";
       
    51 	String errorKey1 = "";
       
    52 	String errorKey2 = "";
       
    53 	String errorComponent = "";
       
    54 	static final String TAG_CATEGORY_NAME = "category_name";
       
    55 	static final String TAG_CATEGORY_DESCRIPTION = "category_description";
       
    56 	static final String TAG_PANIC_ID = "panic_id";
       
    57 	static final String TAG_PANIC_DESCRIPTION = "panic_description";
       
    58 	static final String TAG_PANIC_CATEGORY = "panic_category";
       
    59 	static final String TAG_ERROR_NAME = "error_name";
       
    60 	static final String TAG_ERROR_VALUE = "error_value";
       
    61 	static final String TAG_ERROR_TEXT = "error_text";
       
    62 	static final String TAG_ERROR_COMPONENT = "error_component";
       
    63 
       
    64 	/**
       
    65 	 * Returns list of errors and gives up the ownership of the list.
       
    66 	 * @return list of errors
       
    67 	 */
       
    68 	public Map<String, ErrorLibraryError> getErrorsOwnership() {
       
    69 		if (errors == null || errors.isEmpty()) {
       
    70 			return null;
       
    71 		} else {
       
    72 			Map<String, ErrorLibraryError> err = errors; 
       
    73 			errors = null;
       
    74 			return err;
       
    75 		}
       
    76 	}
       
    77 	
       
    78 	/**
       
    79 	 * Returns list of panics and gives up the ownership of the list.
       
    80 	 * @return list of panics
       
    81 	 */
       
    82 	public Map<String, ErrorLibraryError> getPanicsOwnership() {
       
    83 		if (panics == null || panics.isEmpty()) {
       
    84 			return null;
       
    85 		} else {
       
    86 			Map<String, ErrorLibraryError> pan = panics;
       
    87 			panics = null;
       
    88 			return pan;
       
    89 		}
       
    90 	}
       
    91 	
       
    92 	/**
       
    93 	 * Returns list of panic categories and gives up the ownership of the list.
       
    94 	 * @return list of panic categories
       
    95 	 */
       
    96 	public Map<String, ErrorLibraryError> getCategoriesOwnership() {
       
    97 		if (categories == null || categories.isEmpty()) {
       
    98 			return null;
       
    99 		} else {
       
   100 			Map<String, ErrorLibraryError> cat = categories;
       
   101 			categories = null;
       
   102 			return cat;
       
   103 		}
       
   104 	}
       
   105 
       
   106 	/**
       
   107 	 * Reads all error xml files
       
   108 	 */
       
   109 	public void readAll() {
       
   110 		try {
       
   111 			errors = null;
       
   112 			panics = null;
       
   113 			categories = null;
       
   114 			
       
   115 			String dataPath = CrashAnalyserPlugin.getDataPath();
       
   116 			SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
       
   117 			SAXParserFactory spf = SAXParserFactory.newInstance();
       
   118 			spf.setSchema(sf.newSchema(new File(dataPath + "schema.xsd")));
       
   119 			spf.setValidating(true);
       
   120 			SAXParser sp = spf.newSAXParser();
       
   121 			
       
   122 			List<String> xmlFiles = getXmlFiles(dataPath);
       
   123 			
       
   124 			if (xmlFiles == null || xmlFiles.isEmpty())
       
   125 				return;
       
   126 			
       
   127 			for (int i = 0; i < xmlFiles.size(); i++) {
       
   128 				try {
       
   129 					// parse the file and also register this class for call backs
       
   130 					sp.parse(new File(xmlFiles.get(i)), this);
       
   131 				} catch (Exception ex) {
       
   132 					ex.printStackTrace();
       
   133 				}
       
   134 			}
       
   135 
       
   136 		}catch(Exception e) {
       
   137 			e.printStackTrace();
       
   138 		}
       
   139 	}
       
   140 	
       
   141 	@Override
       
   142 	public void startDocument() throws SAXException {
       
   143 		if (errors == null)
       
   144 			errors = new HashMap<String, ErrorLibraryError>();
       
   145 		if (panics == null)
       
   146 			panics = new HashMap<String, ErrorLibraryError>();
       
   147 		if (categories == null)
       
   148 			categories = new HashMap<String, ErrorLibraryError>();
       
   149 		super.startDocument();
       
   150 	}
       
   151 
       
   152 	@Override
       
   153 	public void startElement(String arg0, String arg1, String arg2,	Attributes arg3) 
       
   154 			throws SAXException {
       
   155 		nodeText = "";
       
   156 		super.startElement(arg0, arg1, arg2, arg3);
       
   157 	}
       
   158 	
       
   159 	@Override
       
   160 	public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
       
   161 		String s = String.copyValueOf(arg0, arg1, arg2);
       
   162 		nodeText += s;
       
   163 		super.characters(arg0, arg1, arg2);
       
   164 	}	
       
   165 
       
   166 	@Override
       
   167 	public void endElement(String arg0, String arg1, String arg2)
       
   168 			throws SAXException {
       
   169 		if (TAG_CATEGORY_NAME.equals(arg2)) {
       
   170 			category = new ErrorLibraryError();
       
   171 			category.SetName(nodeText);
       
   172 			categoryKey = nodeText;
       
   173 			
       
   174 		} else if (TAG_CATEGORY_DESCRIPTION.equals(arg2)) {
       
   175 			categoryDescription = HtmlFormatter.formatCategoryDescription(category.toString(), nodeText);
       
   176 			category.SetDescription(categoryDescription);
       
   177 			
       
   178 		} else if (TAG_PANIC_ID.equals(arg2)) {
       
   179 			category.AddToDescription(HtmlFormatter.formatPanicName(category.toString(), nodeText));
       
   180 			panic = new ErrorLibraryError();
       
   181 			panic.SetName(category.toString() + " " + nodeText);
       
   182 			panicKey = categoryKey + nodeText;
       
   183 			
       
   184 		} else if (TAG_PANIC_DESCRIPTION.equals(arg2)) {
       
   185 			category.AddToDescription(nodeText);
       
   186 			panic.SetDescription(HtmlFormatter.formatPanicDescription(panic.toString(), nodeText));
       
   187 			panic.AddToDescription(categoryDescription);
       
   188 			panics.put(panicKey, panic);
       
   189 			panic = null;
       
   190 			panicKey = "";
       
   191 			
       
   192 		} else if (TAG_PANIC_CATEGORY.equals(arg2)) {
       
   193 			categories.put(categoryKey, category);
       
   194 			category = null;
       
   195 			categoryKey = "";
       
   196 			
       
   197 		} else if (TAG_ERROR_NAME.equals(arg2)) {
       
   198 			error = new ErrorLibraryError();
       
   199 			error.SetName(nodeText);
       
   200 			errorKey1 = nodeText;
       
   201 			
       
   202 		} else if (TAG_ERROR_VALUE.equals(arg2)) {
       
   203 			error.SetDescription(HtmlFormatter.formatErrorDescription(error.toString(), nodeText));
       
   204 			error2 = new ErrorLibraryError();
       
   205 			error2.SetName(nodeText);
       
   206 			error2.SetDescription(HtmlFormatter.formatErrorDescription(nodeText, error.toString()));
       
   207 			errorKey2 = nodeText;
       
   208 			
       
   209 		} else if (TAG_ERROR_COMPONENT.equals(arg2)) {
       
   210 			errorComponent = nodeText;
       
   211 			
       
   212 		} else if (TAG_ERROR_TEXT.equals(arg2)) {
       
   213 			error.AddToDescription(nodeText);
       
   214 			error.AddToDescription(HtmlFormatter.formatErrorComponent(errorComponent));
       
   215 			error2.AddToDescription(nodeText);
       
   216 			error2.AddToDescription(HtmlFormatter.formatErrorComponent(errorComponent));
       
   217 			errors.put(errorKey1, error);
       
   218 			error = null;
       
   219 			errorKey1 = "";
       
   220 			errors.put(errorKey2, error2);
       
   221 			error2 = null;
       
   222 			errorKey2 = "";
       
   223 		}
       
   224 		
       
   225 		super.endElement(arg0, arg1, arg2);
       
   226 	}
       
   227 
       
   228 	/**
       
   229 	 * Returns a list of all error xml files 
       
   230 	 * @param path xml files path
       
   231 	 * @return a list of all error xml files
       
   232 	 */
       
   233 	List<String> getXmlFiles(String path) {
       
   234 		try {
       
   235 			List<String> xmlFiles = new ArrayList<String>();
       
   236 			
       
   237 			File file = new File(path);
       
   238 			
       
   239 			FilenameFilter filter = new FilenameFilter() {
       
   240 				public boolean accept(File dir, String name) {
       
   241 					return (name.endsWith("xml"));
       
   242 				}
       
   243 			};
       
   244 			File[] files = file.listFiles(filter);
       
   245 			
       
   246 			for (int i = 0; i < files.length; i++) {
       
   247 				xmlFiles.add(files[i].getAbsolutePath());
       
   248 			}
       
   249 			
       
   250 			if (xmlFiles.isEmpty())
       
   251 				return null;
       
   252 			
       
   253 			return xmlFiles;
       
   254 		
       
   255 		} catch (Exception e) {
       
   256 			e.printStackTrace();
       
   257 			return null;
       
   258 		}
       
   259 	}	
       
   260 }