crashanalysercmd/Libraries/Engine/ErrorLibrary/ErrorLibrary.cs
changeset 2 0c91f0baec58
equal deleted inserted replaced
1:7a31f7298d8f 2:0c91f0baec58
       
     1 /*
       
     2 * Copyright (c) 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 * The class XmlErrorLibrary provides methods to read panic description and error description.
       
    16 * If the information is not yet parsed from the XML files, it will be read when the error/panic
       
    17 * description is asked first time.
       
    18 * 
       
    19 */
       
    20 using System;
       
    21 using System.Collections.Generic;
       
    22 using System.Text;
       
    23 using System.IO;
       
    24 using System.Xml;
       
    25 using System.Collections.Specialized;
       
    26 using System.Reflection;
       
    27 using System.Resources;
       
    28 using System.Collections;
       
    29 using System.Globalization;
       
    30 
       
    31 namespace ErrorLibrary
       
    32 {
       
    33     public static class XmlErrorLibrary
       
    34     {
       
    35 
       
    36         /**
       
    37          * Returns panic description based on panic category and panic id.
       
    38          * @param category Panic category.
       
    39          * @param id Panic id.
       
    40          * @return Panic description.
       
    41          */
       
    42         public static string GetPanicDescription(string category, string id)
       
    43         {
       
    44             lock (createLock)
       
    45             {
       
    46                 if (iPanics == null)
       
    47                 {
       
    48                     // Data not loaded, load it:
       
    49                     LoadData();
       
    50                 }
       
    51             }
       
    52             
       
    53             if (iPanics != null)
       
    54             {
       
    55                 string searchKey = category + id;
       
    56                 foreach (ErrorLibraryError error in iPanics)
       
    57                 {
       
    58                     if (error.GetSearchKey().Equals(searchKey))
       
    59                     {
       
    60                         return error.GetDescription();
       
    61                     }
       
    62                 }
       
    63             }
       
    64             return string.Empty;
       
    65         }
       
    66 
       
    67         /**
       
    68          * Loads panic description data from resources (xml-files).
       
    69          */
       
    70         private static void LoadData()
       
    71         {
       
    72 
       
    73             ResourceManager resMgr = ErrorLibrary.Properties.Resources.ResourceManager;
       
    74 
       
    75 
       
    76             if (resMgr == null)
       
    77             {
       
    78                 // Resource manager not available, not possible to read resources.
       
    79                 return;
       
    80             }
       
    81 
       
    82             ResourceSet resSet = resMgr.GetResourceSet(CultureInfo.CurrentCulture, true, true);
       
    83 
       
    84             if (resSet == null)
       
    85             {
       
    86                 // Resources not available, can not read them.
       
    87                 return;
       
    88             }
       
    89 
       
    90             IDictionaryEnumerator enumerator = resSet.GetEnumerator();
       
    91 
       
    92             createDataStructures();
       
    93 
       
    94             while (enumerator.MoveNext())
       
    95             {
       
    96                 object obj = enumerator.Value;
       
    97                 if (obj is string)
       
    98                 {
       
    99                     try
       
   100                     {
       
   101                         loadResources((string)obj);
       
   102                     }
       
   103                     catch (Exception)
       
   104                     {
       
   105                         // Exception reading one file. Try still to read others.
       
   106                     }
       
   107                 }
       
   108             }
       
   109         }
       
   110 
       
   111         /**
       
   112          * Adds the data from one XML file (i.e. resourceString) to 
       
   113          * data structures.
       
   114          * 
       
   115          * @param resourceString One XML-file as string.
       
   116          */
       
   117         private static void loadResources(string resourceString)
       
   118         {
       
   119             ErrorLibraryError error = null;
       
   120             ErrorLibraryError error2 = null;
       
   121             ErrorLibraryError panic = null;
       
   122 
       
   123             string nodeText = "";
       
   124             string categoryDescription = "";
       
   125             string categoryName = "";
       
   126             string panicKey = "";
       
   127             string errorKey1 = "";
       
   128             string errorKey2 = "";
       
   129             string errorComponent = "";
       
   130 
       
   131             byte[] xmlByteArray = Encoding.ASCII.GetBytes(resourceString);
       
   132             MemoryStream memoryStream = new MemoryStream(xmlByteArray);
       
   133             XmlTextReader textReader = new XmlTextReader(memoryStream);
       
   134             while (textReader.Read())
       
   135             {
       
   136                 XmlNodeType nType = textReader.NodeType;
       
   137                 if (nType == XmlNodeType.Text)
       
   138                 {
       
   139                     nodeText = textReader.Value;
       
   140                     switch (tagType)
       
   141                     {
       
   142                         case TagType.TagTypeCategoryName:
       
   143                             categoryName = nodeText;
       
   144                             break;
       
   145                         case TagType.TagTypeCategoryDescription:
       
   146                             categoryDescription = HtmlFormatter.formatCategoryDescription(categoryName, nodeText);
       
   147                             break;
       
   148                         case TagType.TagTypePanicId:
       
   149                             panic = new ErrorLibraryError();
       
   150                             panic.SetName(categoryName + " " + nodeText);
       
   151                             panicKey = categoryName + nodeText;
       
   152                             break;
       
   153                         case TagType.TagTypePanicDescription:
       
   154                             panic.SetDescription(HtmlFormatter.formatPanicDescription(panic.ToString(), nodeText));
       
   155                             panic.AddToDescription(categoryDescription);
       
   156                             // iPanics.put(panicKey, panic);
       
   157                             panic.SetSearchKey(panicKey);
       
   158                             iPanics.Add(panic);
       
   159                             panic = null;
       
   160                             panicKey = "";
       
   161                             break;
       
   162                         case TagType.TagTypePanicCategory:
       
   163                             // Do nothing
       
   164                             break;
       
   165                         case TagType.TagTypeErrorName:
       
   166                             error = new ErrorLibraryError();
       
   167                             error.SetName(nodeText);
       
   168                             errorKey1 = nodeText;
       
   169                             break;
       
   170                         case TagType.TagTypeErrorValue:
       
   171                             error.SetDescription(HtmlFormatter.formatErrorDescription(error.ToString(), nodeText));
       
   172                             error2 = new ErrorLibraryError();
       
   173                             error2.SetName(nodeText);
       
   174                             error2.SetDescription(HtmlFormatter.formatErrorDescription(nodeText, error.ToString()));
       
   175                             errorKey2 = nodeText;
       
   176                             break;
       
   177                         case TagType.TagTypeErrorComponent:
       
   178                             errorComponent = nodeText;
       
   179                             break;
       
   180                         case TagType.TagTypeErrorText:
       
   181                             error.AddToDescription(nodeText);
       
   182                             error.AddToDescription(HtmlFormatter.formatErrorComponent(errorComponent));
       
   183                             error2.AddToDescription(nodeText);
       
   184                             error2.AddToDescription(HtmlFormatter.formatErrorComponent(errorComponent));
       
   185                             error.SetSearchKey(errorKey1);
       
   186                             iErrors.Add(error);
       
   187                             error = null;
       
   188                             errorKey1 = "";
       
   189                             error2.SetSearchKey(errorKey2);
       
   190                             iErrors.Add(error2);
       
   191                             error2 = null;
       
   192                             errorKey2 = "";
       
   193                             break;
       
   194                     }
       
   195                 }
       
   196                 // if node type is an element
       
   197                 else if (nType == XmlNodeType.Element)
       
   198                 {
       
   199                     string name = textReader.Name.ToString();
       
   200                     nodeText = textReader.Value;
       
   201 
       
   202                     if (TAG_CATEGORY_NAME.Equals(name))
       
   203                     {
       
   204                         tagType = TagType.TagTypeCategoryName;
       
   205                     }
       
   206                     else if (TAG_CATEGORY_DESCRIPTION.Equals(name))
       
   207                     {
       
   208                         tagType = TagType.TagTypeCategoryDescription;
       
   209                     }
       
   210                     else if (TAG_PANIC_ID.Equals(name))
       
   211                     {
       
   212                         tagType = TagType.TagTypePanicId;
       
   213                     }
       
   214                     else if (TAG_PANIC_DESCRIPTION.Equals(name))
       
   215                     {
       
   216                         tagType = TagType.TagTypePanicDescription;
       
   217                     }
       
   218                     else if (TAG_PANIC_CATEGORY.Equals(name))
       
   219                     {
       
   220                         tagType = TagType.TagTypePanicCategory;
       
   221                     }
       
   222                     else if (TAG_ERROR_NAME.Equals(name))
       
   223                     {
       
   224                         tagType = TagType.TagTypeErrorName;
       
   225                     }
       
   226                     else if (TAG_ERROR_VALUE.Equals(name))
       
   227                     {
       
   228                         tagType = TagType.TagTypeErrorValue;
       
   229                     }
       
   230                     else if (TAG_ERROR_COMPONENT.Equals(name))
       
   231                     {
       
   232                         tagType = TagType.TagTypeErrorComponent;
       
   233                     }
       
   234                     else if (TAG_ERROR_TEXT.Equals(name))
       
   235                     {
       
   236                         tagType = TagType.TagTypeErrorText;
       
   237                     }
       
   238 
       
   239                 }
       
   240             }
       
   241 
       
   242         }
       
   243 
       
   244         /**
       
   245          * Create data structures for panics/errors.
       
   246          */
       
   247         private static void createDataStructures()
       
   248         {
       
   249             iErrors = new ArrayList();
       
   250             iPanics = new ArrayList();
       
   251         }
       
   252 
       
   253 
       
   254         #region Data members
       
   255 
       
   256         private enum TagType
       
   257         {
       
   258             TagTypeCategoryName,
       
   259             TagTypeCategoryDescription,
       
   260             TagTypePanicId,
       
   261             TagTypePanicDescription,
       
   262             TagTypePanicCategory,
       
   263             TagTypeErrorName,
       
   264             TagTypeErrorValue,
       
   265             TagTypeErrorText,
       
   266             TagTypeErrorComponent
       
   267         }
       
   268 
       
   269         private static string TAG_CATEGORY_NAME = "category_name";
       
   270         private static string TAG_CATEGORY_DESCRIPTION = "category_description";
       
   271         private static string TAG_PANIC_ID = "panic_id";
       
   272         private static string TAG_PANIC_DESCRIPTION = "panic_description";
       
   273         private static string TAG_PANIC_CATEGORY = "panic_category";
       
   274         private static string TAG_ERROR_NAME = "error_name";
       
   275         private static string TAG_ERROR_VALUE = "error_value";
       
   276         private static string TAG_ERROR_TEXT = "error_text";
       
   277         private static string TAG_ERROR_COMPONENT = "error_component";
       
   278         
       
   279         private static TagType tagType;
       
   280         private static ArrayList iErrors = null;
       
   281         private static ArrayList iPanics = null;
       
   282 
       
   283         private static readonly object createLock = new object();
       
   284 
       
   285         #endregion
       
   286 
       
   287     }
       
   288 }