Advertisement
Guest User

Untitled

a guest
May 30th, 2014
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using System.IO;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace RecursiveParser
  10. {
  11.     class Parser
  12.     {
  13.         //! \brief Constructor
  14.         //! \brief The path to the file which gets parsed
  15.         public Parser(String fileToParse)
  16.         {
  17.             // Check if the file is supported by the parser
  18.             if (!setFileToParse(fileToParse))
  19.             {
  20.                 System.Console.WriteLine("File to parse wasn't set correctly.");
  21.  
  22.                 return;
  23.             }
  24.  
  25.             // Initialize the root XML element
  26.             m_rootNode = new XElement(@"IconConfigurations");
  27.         }
  28.  
  29.         public void parse()
  30.         {
  31.             XElement infoNode = getInfoNode();
  32.             m_rootNode.Add(infoNode);
  33.  
  34.             IEnumerable<String> files = Directory.EnumerateFiles(m_directoryToParse, "*.*", SearchOption.AllDirectories);
  35.             foreach (String file in files)
  36.             {
  37.                 if (Path.GetExtension(file) != ".png")
  38.                 {
  39.                     continue;
  40.                 }
  41.  
  42.                 String fileName = Path.GetFileNameWithoutExtension(file);
  43.  
  44.                 fileName = replaceStringCharacters(fileName);
  45.  
  46.                 XElement iconNode = new XElement(TAG_ICON);
  47.                 String iconNodeNameAttribute = "RSST_BITMAP_" + fileName;
  48.                 iconNode.SetAttributeValue(ATTRIBUTE_NAME, iconNodeNameAttribute);
  49.  
  50.                 XElement filePathNode = new XElement(ATTRIBUTE_FILEPATH);
  51.  
  52.                 System.Uri uriSrc = new Uri(Path.GetFullPath(m_outputFile), UriKind.Absolute);
  53.                 System.Uri uriDest = new Uri(Path.GetFullPath(file), UriKind.Absolute);
  54.                 System.Uri relativeUri = uriSrc.MakeRelativeUri(uriDest);
  55.  
  56.                 String filePathString = relativeUri.ToString() + @"/" + Path.GetFileName(file);
  57.  
  58.                 byte[] bytes = Encoding.Default.GetBytes(filePathString);
  59.                 filePathString = Encoding.UTF8.GetString(bytes);
  60.                 filePathNode.SetValue(filePathString);
  61.  
  62.                 iconNode.Add(filePathNode);
  63.                 m_rootNode.Add(iconNode);
  64.             }
  65.  
  66.         }
  67.  
  68.         public String replaceStringCharacters(String str)
  69.         {
  70.             return Regex.Replace(str, @"[^a-zA-Z0-9_]", "_");
  71.         }
  72.  
  73.         //! \brief Sets the path to the file which gets parsed
  74.         //! \brief \param The path to the file to parse
  75.         //! \return true if the file path was successfully set, false otherwise
  76.         public bool setFileToParse(String directoryToParse)
  77.         {
  78.             FileAttributes attribute = File.GetAttributes(directoryToParse);
  79.  
  80.             // Detect whether path points to a directory
  81.             if ((attribute & FileAttributes.Directory) != FileAttributes.Directory)
  82.             {
  83.                 System.Console.WriteLine(@"Path doesn't point to a directory.");
  84.  
  85.                 return false;
  86.             }
  87.  
  88.             // Initialize the path of the directory to parse
  89.             m_directoryToParse = directoryToParse;
  90.  
  91.             return true;
  92.         }
  93.  
  94.         private XElement getInfoNode()
  95.         {
  96.             const String TAG_INFO = @"Info";
  97.             const String TAG_COMPANY = @"Company";
  98.             const String TAG_COMPANY_VALUE = @"Continental AG";
  99.             const String TAG_CUSTOMER = @"Customer";
  100.             const String TAG_CUSTOMER_VALUE = @"Daimler AG";
  101.             const String TAG_MODEL_LINE = @"ModelLine";
  102.             const String TAG_MODEL_LINE_VALUE = @"IC222";
  103.             const String TAG_SAMPLE_PHASE = @"SamplePhase";
  104.             const String TAG_RELEASE_NOTES = @"ReleaseNotes";
  105.             const String TAG_RELEASE_NOTE = @"ReleaseNote";
  106.             const String ATTRIBUTE_AUTHOR = @"author";
  107.             const String ATTRIBUTE_AUTHOR_VALUE = @"CrJi";
  108.             const String ATTRIBUTE_DATE = @"author";
  109.  
  110.             XElement infoNode = new XElement(TAG_INFO);
  111.  
  112.             XElement companyNode = new XElement(TAG_COMPANY);
  113.             companyNode.SetValue(TAG_COMPANY_VALUE);
  114.  
  115.             XElement customerNode = new XElement(TAG_CUSTOMER);
  116.             customerNode.SetValue(TAG_CUSTOMER_VALUE);
  117.  
  118.             XElement modelLineNode = new XElement(TAG_MODEL_LINE);
  119.             modelLineNode.SetValue(TAG_MODEL_LINE_VALUE);
  120.  
  121.             XElement samplePhaseNode = new XElement(TAG_SAMPLE_PHASE);
  122.  
  123.             XElement releaseNotesNode = new XElement(TAG_RELEASE_NOTES);
  124.  
  125.             XElement releaseNoteNode = new XElement(TAG_RELEASE_NOTE);
  126.             DateTime date = new DateTime(2010, 6, 17);
  127.             releaseNoteNode.SetAttributeValue(ATTRIBUTE_AUTHOR, ATTRIBUTE_AUTHOR_VALUE);
  128.             releaseNoteNode.SetAttributeValue(ATTRIBUTE_DATE, date.ToString("yyyy-MM-dd"));
  129.  
  130.             releaseNotesNode.Add(releaseNoteNode);
  131.  
  132.             infoNode.Add(companyNode);
  133.             infoNode.Add(customerNode);
  134.             infoNode.Add(modelLineNode);
  135.             infoNode.Add(samplePhaseNode);
  136.             infoNode.Add(releaseNotesNode);
  137.  
  138.             return infoNode;
  139.         }
  140.  
  141.         //! \brief Saves the XML structure to file
  142.         //! \param filePath The path of the file to which the data will be written
  143.         public void saveToFile()
  144.         {
  145.             m_rootNode.Save(m_outputFile);
  146.         }
  147.  
  148.         public void setOutputFile(String filePath)
  149.         {
  150.             m_outputFile = filePath;
  151.         }
  152.  
  153.         private const String TAG_ICON = @"Icon";
  154.         private const String ATTRIBUTE_NAME = @"name";
  155.         private const String ATTRIBUTE_FILEPATH = @"FilePath";
  156.         private const String INVALID_FILENAME_CHARS = @"";
  157.  
  158.         private XElement m_rootNode;
  159.         private String m_directoryToParse;
  160.         private String m_outputFile;
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement