Adding first code drop
[demorecorder] / src / XmlHelpers.vala
diff --git a/src/XmlHelpers.vala b/src/XmlHelpers.vala
new file mode 100644 (file)
index 0000000..8c7cb19
--- /dev/null
@@ -0,0 +1,93 @@
+/*  Demo Recorder for MAEMO 5
+*   Copyright (C) 2010 Dru Moore <usr@dru-id.co.uk>
+*   This program is free software; you can redistribute it and/or modify
+*   it under the terms of the GNU General Public License version 2,
+*   or (at your option) any later version, as published by the Free
+*   Software Foundation
+*
+*   This program is distributed in the hope that it will be useful,
+*   but WITHOUT ANY WARRANTY; without even the implied warranty of
+*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*   GNU General Public License for more details
+*
+*   You should have received a copy of the GNU General Public
+*   License along with this program; if not, write to the
+*   Free Software Foundation, Inc.,
+*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+
+namespace IdWorks {
+
+namespace XmlHelpers {
+  
+  public static bool parse_project_settings_file(string location, string version, ref SettingsStructures.ProjectSettings project, ref string errors) {
+    bool ret = false;
+    project = SettingsStructures.ProjectSettings();
+    errors = "";
+    // prepare the Xml Parser
+    Xml.Parser.init();
+    // load the document
+    Xml.Doc* doc = Xml.Parser.parse_file(location);
+    if (null != doc) {
+      // load the root node
+      Xml.Node* root = doc->get_root_element();
+      if (null != root) {
+        // check element is ProjectSettings
+        if ("ProjectSettings" == root->name) {
+          // check version
+          string? doc_version = get_node_attribute(root, "version");
+          if (null != doc_version && version == doc_version) {
+            // looking good let's get parsing.
+            try {
+              project.from_xml_node(root);
+              ret = true;
+            }
+            catch (GLib.Error ex) {
+              errors = "ProjectSettings parser threw an error.";
+              ret = false;
+            }
+            finally {
+              // no op
+            }
+          }
+          else {
+            errors = "Incorrect version or unversioned document.";
+          }
+        }
+        else {
+          errors = "Not a valid settings file.";
+        }
+      }
+      else {
+        errors = "No root node found.";
+      }
+      // clean up
+      delete doc;
+    }
+    else {
+      errors = "Couldn't parse file as valid XML.";
+    }
+    // clean up the parser
+    Xml.Parser.cleanup();
+    
+    return ret;
+  }
+  
+  private static string? get_node_attribute(Xml.Node* node, string key) {
+    string? ret = null;
+    // Loop over the passed node's attributes
+    for (Xml.Attr* prop = node->properties; prop != null; prop = prop->next) {
+      string attr_name = prop->name;
+      // Notice the ->children which points to a Node*
+      // (Attr doesn't feature content)
+      string attr_content = prop->children->content;
+      if (key == attr_name) {
+        ret = attr_content;
+        break;
+      }
+    }
+    return ret;
+  }
+}
+
+}
\ No newline at end of file