Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml.Linq;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace RecursiveParser
- {
- class Parser
- {
- //! \brief Constructor
- //! \brief The path to the file which gets parsed
- public Parser(String fileToParse)
- {
- // Check if the file is supported by the parser
- if (!setFileToParse(fileToParse))
- {
- System.Console.WriteLine("File to parse wasn't set correctly.");
- return;
- }
- // Initialize the root XML element
- m_rootNode = new XElement(@"IconConfigurations");
- }
- public void parse()
- {
- XElement infoNode = getInfoNode();
- m_rootNode.Add(infoNode);
- IEnumerable<String> files = Directory.EnumerateFiles(m_directoryToParse, "*.*", SearchOption.AllDirectories);
- foreach (String file in files)
- {
- if (Path.GetExtension(file) != ".png")
- {
- continue;
- }
- String fileName = Path.GetFileNameWithoutExtension(file);
- fileName = replaceStringCharacters(fileName);
- XElement iconNode = new XElement(TAG_ICON);
- String iconNodeNameAttribute = "RSST_BITMAP_" + fileName;
- iconNode.SetAttributeValue(ATTRIBUTE_NAME, iconNodeNameAttribute);
- XElement filePathNode = new XElement(ATTRIBUTE_FILEPATH);
- System.Uri uriSrc = new Uri(Path.GetFullPath(m_outputFile), UriKind.Absolute);
- System.Uri uriDest = new Uri(Path.GetFullPath(file), UriKind.Absolute);
- System.Uri relativeUri = uriSrc.MakeRelativeUri(uriDest);
- String filePathString = relativeUri.ToString() + @"/" + Path.GetFileName(file);
- byte[] bytes = Encoding.Default.GetBytes(filePathString);
- filePathString = Encoding.UTF8.GetString(bytes);
- filePathNode.SetValue(filePathString);
- iconNode.Add(filePathNode);
- m_rootNode.Add(iconNode);
- }
- }
- public String replaceStringCharacters(String str)
- {
- return Regex.Replace(str, @"[^a-zA-Z0-9_]", "_");
- }
- //! \brief Sets the path to the file which gets parsed
- //! \brief \param The path to the file to parse
- //! \return true if the file path was successfully set, false otherwise
- public bool setFileToParse(String directoryToParse)
- {
- FileAttributes attribute = File.GetAttributes(directoryToParse);
- // Detect whether path points to a directory
- if ((attribute & FileAttributes.Directory) != FileAttributes.Directory)
- {
- System.Console.WriteLine(@"Path doesn't point to a directory.");
- return false;
- }
- // Initialize the path of the directory to parse
- m_directoryToParse = directoryToParse;
- return true;
- }
- private XElement getInfoNode()
- {
- const String TAG_INFO = @"Info";
- const String TAG_COMPANY = @"Company";
- const String TAG_COMPANY_VALUE = @"Continental AG";
- const String TAG_CUSTOMER = @"Customer";
- const String TAG_CUSTOMER_VALUE = @"Daimler AG";
- const String TAG_MODEL_LINE = @"ModelLine";
- const String TAG_MODEL_LINE_VALUE = @"IC222";
- const String TAG_SAMPLE_PHASE = @"SamplePhase";
- const String TAG_RELEASE_NOTES = @"ReleaseNotes";
- const String TAG_RELEASE_NOTE = @"ReleaseNote";
- const String ATTRIBUTE_AUTHOR = @"author";
- const String ATTRIBUTE_AUTHOR_VALUE = @"CrJi";
- const String ATTRIBUTE_DATE = @"author";
- XElement infoNode = new XElement(TAG_INFO);
- XElement companyNode = new XElement(TAG_COMPANY);
- companyNode.SetValue(TAG_COMPANY_VALUE);
- XElement customerNode = new XElement(TAG_CUSTOMER);
- customerNode.SetValue(TAG_CUSTOMER_VALUE);
- XElement modelLineNode = new XElement(TAG_MODEL_LINE);
- modelLineNode.SetValue(TAG_MODEL_LINE_VALUE);
- XElement samplePhaseNode = new XElement(TAG_SAMPLE_PHASE);
- XElement releaseNotesNode = new XElement(TAG_RELEASE_NOTES);
- XElement releaseNoteNode = new XElement(TAG_RELEASE_NOTE);
- DateTime date = new DateTime(2010, 6, 17);
- releaseNoteNode.SetAttributeValue(ATTRIBUTE_AUTHOR, ATTRIBUTE_AUTHOR_VALUE);
- releaseNoteNode.SetAttributeValue(ATTRIBUTE_DATE, date.ToString("yyyy-MM-dd"));
- releaseNotesNode.Add(releaseNoteNode);
- infoNode.Add(companyNode);
- infoNode.Add(customerNode);
- infoNode.Add(modelLineNode);
- infoNode.Add(samplePhaseNode);
- infoNode.Add(releaseNotesNode);
- return infoNode;
- }
- //! \brief Saves the XML structure to file
- //! \param filePath The path of the file to which the data will be written
- public void saveToFile()
- {
- m_rootNode.Save(m_outputFile);
- }
- public void setOutputFile(String filePath)
- {
- m_outputFile = filePath;
- }
- private const String TAG_ICON = @"Icon";
- private const String ATTRIBUTE_NAME = @"name";
- private const String ATTRIBUTE_FILEPATH = @"FilePath";
- private const String INVALID_FILENAME_CHARS = @"";
- private XElement m_rootNode;
- private String m_directoryToParse;
- private String m_outputFile;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement