CharacterSetArray.cs :  » Database » SQL-Power-Injector » SQLPowerInjector » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Database » SQL Power Injector 
SQL Power Injector » SQLPowerInjector » CharacterSetArray.cs
//*********************************************************************
//                                   //
//  SQL Power Injector 1.2 Copyright (c) 2006-2007 Francois Larouche //
//                                   //
//  Author  : francois.larouche@sqlpowerinjector.com         //
//  Web Site: www.sqlpowerinjector.com                 //
//                                   //
//*******************************************************************//
using System;
using System.Collections;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace SQLPowerInjector{
  /// <summary>
  /// Summary description for CharacterSetArray.
  /// </summary>
  [XmlRootAttribute("SPInj_Character_Saved_Sets", Namespace="", IsNullable=false)]
  public class CharacterSetArray
  {
    #region Members
    #region Private
    private ArrayList _characterSetArray = new ArrayList();
    private ArrayList _initialCharacterSetArray = new ArrayList();
    private string _pathAndFileName = "";
    #endregion
    #endregion

    #region Public Enums
    public enum Add_Result
    {
      FailedNameAlreadyExists  = -1,
      FailedUnknownReasons  = 0,
      Success          = 1
    }

    public enum Modify_Result
    {
      FailedCharacterSetReadOnly  = -3,
      FailedCharacterSetNotFound  = -2,
      FailedNameAlreadyExists    = -1,
      FailedUnknownReasons    = 0,
      Success            = 1
    }

    public enum Remove_Result
    {
      FailedCharacterSetReadOnly  = -2,
      FailedCharacterSetNotFound  = -1,
      FailedUnknownReasons    = 0,
      Success            = 1
    }
    #endregion

    #region Constants
    private const string CHARACTERSET_FILE = "characterSavedSets.xml";
    #endregion

    #region Constructors
    public CharacterSetArray()
    {
      _pathAndFileName = "";

      _pathAndFileName = "\\Settings\\" + CHARACTERSET_FILE;

      #if DEBUG
        _pathAndFileName = "\\..\\..\\" + _pathAndFileName;
      #endif

      _pathAndFileName = Application.StartupPath + _pathAndFileName;

      _characterSetArray = LoadCharacterSetArray(_pathAndFileName);
    }

    public CharacterSetArray(string characterSetFile)
    {
      _pathAndFileName = "";

      _pathAndFileName = "\\Settings\\" + characterSetFile;

      #if DEBUG
        _pathAndFileName = "\\..\\..\\" + _pathAndFileName;
      #endif

      _pathAndFileName = Application.StartupPath + _pathAndFileName;

      _characterSetArray = LoadCharacterSetArray(_pathAndFileName);
    }
    #endregion

    #region Public Attributes
    [XmlIgnoreAttribute()]
    public ArrayList GetCharacterSetArray
    {
      get 
      {
        _characterSetArray.Sort(new CharacterSetArrayComparer());
        return _characterSetArray; 
      }
    }

    [XmlArray ("CharacterSets"), XmlArrayItem("CharacterSet", typeof(CharacterSet))]
    public ArrayList GetInitialCharacterSetArray
    {
      get 
      {
        _initialCharacterSetArray.Sort(new CharacterSetArrayComparer());
        return _initialCharacterSetArray; 
      }
    }
    #endregion

    public Add_Result AddCharacterSet(CharacterSet characterSetToAdd)
    {
      Add_Result addedResult = Add_Result.FailedUnknownReasons;
      bool isCharacterSetNameUnique = true;
      _initialCharacterSetArray = LoadCharacterSetArray(_pathAndFileName);
      ArrayList tmpCharacterSet = _initialCharacterSetArray;

      try
      {
        // Check first if name exists
        foreach(CharacterSet currentCharSet in _initialCharacterSetArray)
        {
          if(currentCharSet != null)
            if(currentCharSet.CharacterSetName.ToUpper().Trim() == characterSetToAdd.CharacterSetName.ToUpper().Trim())
            {
              addedResult = Add_Result.FailedNameAlreadyExists;
              isCharacterSetNameUnique = false;
              break;
            }
        }

        if(isCharacterSetNameUnique)
        {
          _initialCharacterSetArray.Add(characterSetToAdd);

          if(SaveCharacterSetArray())
          {
            _characterSetArray.Add(characterSetToAdd);
            addedResult = Add_Result.Success;
          }
          else
            _initialCharacterSetArray = tmpCharacterSet;
        }
      }
      catch(Exception ex)
      {
        throw(ex);
      }

      return addedResult;
    }

    public Remove_Result RemoveCharacterSet(string characterSetID)
    {
      Remove_Result removedCharacterSetResult = Remove_Result.FailedUnknownReasons;
      int foundCharacterSetArrayIndex = -1;
      int foundInitialCharacterSetArrayIndex = -1;
      _initialCharacterSetArray = LoadCharacterSetArray(_pathAndFileName);
      ArrayList tmpCharacterSet = _initialCharacterSetArray;

      try
      {
        for(int curCharSetIndex = 0; curCharSetIndex< _characterSetArray.Count; curCharSetIndex++)
        {
          if(_characterSetArray[curCharSetIndex] != null)
            if(((CharacterSet)(_characterSetArray[curCharSetIndex])).CharacterSetID == characterSetID)
            {
              foundCharacterSetArrayIndex = curCharSetIndex;
              break;
            }
        }
        if(foundCharacterSetArrayIndex > -1)
        {
          // First check if that array exists, we never know it might be different
          if(_initialCharacterSetArray[foundCharacterSetArrayIndex] != null)
          {
            // Now we check if the character set in _characterSetArray has the same index than
            // the one in the initial one, it should be the case since it's always reflecting
            // the _initialCharacterSetArray, but we double check it anyways in case some weird thing happened
            if(((CharacterSet)(_characterSetArray[foundCharacterSetArrayIndex])).CharacterSetID == ((CharacterSet)(_initialCharacterSetArray[foundCharacterSetArrayIndex])).CharacterSetID)
              foundInitialCharacterSetArrayIndex = foundCharacterSetArrayIndex;
            else // it's not the same so we need to find the right index
            {
              for(int curInitialCharSetIndex = 0; curInitialCharSetIndex< _initialCharacterSetArray.Count; curInitialCharSetIndex++)
              {
                if(_initialCharacterSetArray[curInitialCharSetIndex] != null)
                  if(((CharacterSet)(_initialCharacterSetArray[curInitialCharSetIndex])).CharacterSetID == characterSetID)
                  {
                    foundInitialCharacterSetArrayIndex = curInitialCharSetIndex;
                    break;
                  }
              }
            }
            if(foundInitialCharacterSetArrayIndex > -1) // We found both characterSet in each array
            {
              // we check if it's readonly, if so we can't modify it. Presets are read only
              if(((CharacterSet)(_characterSetArray[foundCharacterSetArrayIndex])).ReadOnly || ((CharacterSet)(_initialCharacterSetArray[foundInitialCharacterSetArrayIndex])).ReadOnly)
                removedCharacterSetResult = Remove_Result.FailedCharacterSetReadOnly;
              else
              {
                _initialCharacterSetArray.RemoveAt(foundInitialCharacterSetArrayIndex);

                // We now can remove without problem the current the displayed character set
                if(SaveCharacterSetArray())
                {
                  _characterSetArray.RemoveAt(foundCharacterSetArrayIndex);
                  removedCharacterSetResult = Remove_Result.Success;
                }
                else // We have to reinitialize with the initial values since it didn't work
                  _initialCharacterSetArray = tmpCharacterSet;
              }
            }
            else
              removedCharacterSetResult = Remove_Result.FailedCharacterSetNotFound;
          }
        }
        else
          removedCharacterSetResult = Remove_Result.FailedCharacterSetNotFound;
      }
      catch(Exception ex)
      {
        throw(ex);
      }

      return removedCharacterSetResult;
    }

    public Modify_Result ModifyCharacterSet(CharacterSet characterSetToModify)
    {
      Modify_Result modifiedCharacterSet = Modify_Result.FailedUnknownReasons;
      bool isCharacterSetNameUnique = true;
      int foundCharacterSetArrayIndex = -1;
      int foundInitialCharacterSetArrayIndex = -1;
      _initialCharacterSetArray = LoadCharacterSetArray(_pathAndFileName);
      ArrayList tmpCharacterSet = _initialCharacterSetArray;

      try
      {
        // First we find it in the character set
        for(int curCharSetIndex = 0; curCharSetIndex< _characterSetArray.Count; curCharSetIndex++)
        {
          if(_characterSetArray[curCharSetIndex] != null)
          {
            if(((CharacterSet)(_characterSetArray[curCharSetIndex])).CharacterSetID == characterSetToModify.CharacterSetID)
            {
              foundCharacterSetArrayIndex = curCharSetIndex;
              break;
            }
          }
        }
        
        if(foundCharacterSetArrayIndex > -1)
        {
          // Secondly, we find the index of the initialCharacterSet
          if(_initialCharacterSetArray[foundCharacterSetArrayIndex] != null)
          {
            if(((CharacterSet)(_characterSetArray[foundCharacterSetArrayIndex])).CharacterSetID == ((CharacterSet)(_initialCharacterSetArray[foundCharacterSetArrayIndex])).CharacterSetID)
              foundInitialCharacterSetArrayIndex = foundCharacterSetArrayIndex;
            else // it's not the same so we need to find the right index
            {
              for(int curInitialCharSetIndex = 0; curInitialCharSetIndex< _initialCharacterSetArray.Count; curInitialCharSetIndex++)
              {
                if(_initialCharacterSetArray[curInitialCharSetIndex] != null)
                  if(((CharacterSet)(_initialCharacterSetArray[curInitialCharSetIndex])).CharacterSetID == characterSetToModify.CharacterSetID)
                  {
                    foundInitialCharacterSetArrayIndex = curInitialCharSetIndex;
                    break;
                  }
              }
            }
          
            if(foundInitialCharacterSetArrayIndex > -1)
            {
              // we check if it's readonly, if so we can't modify it. Presets are read only
              if(((CharacterSet)(_characterSetArray[foundCharacterSetArrayIndex])).ReadOnly || ((CharacterSet)(_initialCharacterSetArray[foundInitialCharacterSetArrayIndex])).ReadOnly)
                modifiedCharacterSet = Modify_Result.FailedCharacterSetReadOnly;
              else
              {
                // Now we check if the potentially modified name already exists that its not itself of course
                foreach(CharacterSet currentCharSet in _characterSetArray)
                {
                  if(currentCharSet != null)
                    if(currentCharSet.CharacterSetName.ToUpper().Trim() == characterSetToModify.CharacterSetName.ToUpper().Trim() &&
                      currentCharSet.CharacterSetID != ((CharacterSet)(_characterSetArray[foundCharacterSetArrayIndex])).CharacterSetID)
                    {
                      modifiedCharacterSet = Modify_Result.FailedNameAlreadyExists;
                      isCharacterSetNameUnique = false;
                      break;
                    }
                }
              
                if(isCharacterSetNameUnique)
                {
                  foreach(CharacterSet currentCharSet in _initialCharacterSetArray)
                  {
                    if(currentCharSet != null)
                      if(currentCharSet.CharacterSetName.ToUpper().Trim() == characterSetToModify.CharacterSetName.ToUpper().Trim() &&
                        currentCharSet.CharacterSetID != ((CharacterSet)(_initialCharacterSetArray[foundInitialCharacterSetArrayIndex])).CharacterSetID)
                      {
                        modifiedCharacterSet = Modify_Result.FailedNameAlreadyExists;
                        isCharacterSetNameUnique = false;
                        break;
                      }
                  }

                  // It could have changed on the second pass, so we need to check it again
                  if(isCharacterSetNameUnique)
                  {
                    _initialCharacterSetArray.RemoveAt(foundInitialCharacterSetArrayIndex);
                    _initialCharacterSetArray.Add(characterSetToModify);

                    if(SaveCharacterSetArray())
                      modifiedCharacterSet = Modify_Result.Success;
                    else // We have to reinitialize with the initial values since it didn't work
                      _initialCharacterSetArray = tmpCharacterSet;
                  }
                }
              }
            }
            else
              modifiedCharacterSet = Modify_Result.FailedCharacterSetNotFound;
          }
        }
        else
          modifiedCharacterSet = Modify_Result.FailedCharacterSetNotFound;
      }
      catch(Exception ex)
      {
        throw(ex);
      }

      return modifiedCharacterSet;
    }

    public int GetCharacterSetIndexByName(string characterSetName)
    {
      int foundCharacterSetArrayIndex = 0;

      for(int curCharSetIndex = 0; curCharSetIndex< _characterSetArray.Count; curCharSetIndex++)
      {
        if(_characterSetArray[curCharSetIndex] != null)
        {
          if(((CharacterSet)(_characterSetArray[curCharSetIndex])).CharacterSetName.ToUpper().Trim() == characterSetName.ToUpper().Trim())
          {
            foundCharacterSetArrayIndex = curCharSetIndex;
            break;
          }
        }
      }

      return foundCharacterSetArrayIndex;
    }

    private ArrayList LoadCharacterSetArray(string pathAndFileName)
    {
      XmlDocument xmlDocToParse = Utilities.LoadXMLFile(pathAndFileName);
      XmlNodeList xmlCharacterSetNodeList = null;
      XmlNodeList xmlCharacterNodeList = null;
      XmlElement curXMLEle = null;
      CharacterSet currentCharSet = null;
      ArrayList currentCharacterSetArray = new ArrayList();
      string currentCharSetName = "";
      string currentGuid = "";
      bool currentReadOnly = true;
      int charAsciiCodeToAdd = -1;

      // We first need to get the CharacterSet groups in the XML files
      xmlCharacterSetNodeList = xmlDocToParse.SelectNodes("descendant::CharacterSet");

      foreach(XmlNode curXmlNode in xmlCharacterSetNodeList)
      {
        if(curXmlNode.Attributes["CharacterSetName"] != null) 
          currentCharSetName = curXmlNode.Attributes["CharacterSetName"].Value;
        else
          currentCharSetName = "";

        if(curXmlNode.Attributes["CharacterSetID"] != null) 
          currentGuid = curXmlNode.Attributes["CharacterSetID"].Value;
        else
          currentGuid = "";

        if(curXmlNode.Attributes["ReadOnly"] != null) 
          currentReadOnly = Convert.ToBoolean(curXmlNode.Attributes["ReadOnly"].Value);
        else
          currentReadOnly = true;

        currentCharSet = new CharacterSet(currentCharSetName, currentGuid, currentReadOnly);

        xmlCharacterNodeList = curXmlNode.SelectNodes("descendant::Character");

        IEnumerator enumCharacterNodeList = xmlCharacterNodeList.GetEnumerator();
        while(enumCharacterNodeList.MoveNext())
        {
          curXMLEle = (XmlElement)enumCharacterNodeList.Current;
          if(Utilities.IsNumeric(curXMLEle.InnerText))
            charAsciiCodeToAdd = Convert.ToInt32(curXMLEle.InnerText);
          else
            charAsciiCodeToAdd = 255; // We sure we need it so we add it

          currentCharSet.AddCharacter(charAsciiCodeToAdd);
        }

        // We need it if not we will end up to have problem if the SQL injection value is
        // beyond the highest number in the custom ascii array
        currentCharSet.AddCharacter(255);

        currentCharacterSetArray.Add(currentCharSet);
      }

      return currentCharacterSetArray;
    }

    private bool SaveCharacterSetArray()
    {
      bool savedCharacterSetArray = false;
      string pathAndFileName = "";

      pathAndFileName = "Settings\\" + CHARACTERSET_FILE;

      #if DEBUG
        pathAndFileName = "\\..\\..\\" + pathAndFileName;
      #endif

      pathAndFileName = Application.StartupPath + pathAndFileName;
      savedCharacterSetArray = Utilities.SerializeXML(this, pathAndFileName);

      return savedCharacterSetArray;
    }

    // internal class for sorting purpose, it will sort according to the name in that case
    internal class CharacterSetArrayComparer : IComparer
    {
      public CharacterSetArrayComparer() {}

      int IComparer.Compare(object obj1, object obj2)
      {
        return string.Compare(((CharacterSet)obj1).CharacterSetName, ((CharacterSet)obj2).CharacterSetName, false);
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.