Esempio n. 1
0
        private void ExecConfig(ResourceNode resourceNode, XmlNode config, XmlNode data)
        {
            try
            {
                foreach (XmlNode node in config.ChildNodes)
                {
                    string elementFunc = node.Name.Substring(0, 1).ToUpper() + node.Name.Substring(1).ToLower();

                    string func = "Do" + elementFunc;

                    object[] parameters = new object[3] {resourceNode, node, data};
                    switch (func)
                    {
                            //ignore these ones
                        case "Do#text":
                        case "Do#comment":
                        case "DoPartition":
                        case "DoNamespaces":
                        case "DoSplit":
                        case "DoNamedmapping":
                            continue;
                        case "DoForeach":
                            func = "DoMapping";
                            break;
                        case "DoResource":
                            parameters = new object[2] {node, data};
                            break;
                        case "DoIdentifier":
                            //check to see if we have already found the identifier for the current resource - if not, then process
                            if (resourceNode.HasIdentifier)
                                continue;
                            break;
                    }

                    Type thisType = this.GetType();
                    MethodInfo theMethod = thisType.GetMethod(func, BindingFlags.NonPublic | BindingFlags.Instance);
                    if (theMethod != null)
                    {
                        theMethod.Invoke(this, parameters);
                    }
                    else
                        throw new Exception("Cannot process: " + node.OuterXml + ". Cannot identify tag: <" + elementFunc +">");
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error in ExecConfig(): " + ex.Message + (_includeStackTraceInOutput ? ex.StackTrace : ""));
                if (ex.InnerException != null)
                    Console.WriteLine(ex.InnerException.Message + (_includeStackTraceInOutput ? ex.InnerException.StackTrace : ""));
            }
        }
Esempio n. 2
0
        private void DoUniqueidentifier(ResourceNode resourceNode, XmlNode uniqueidentifierNode, XmlNode data)
        {
            string uniqueIdentifierName = string.Empty;
            bool generate = true;

            //there must be a name and generate must equal true.
            if (uniqueidentifierNode.Attributes["name"] == null)
                throw new Exception("Error in config: Missing attribut @name in <uniqueidentifier> element.  Please specify a name of the unique identifier variable.");
            uniqueIdentifierName = uniqueidentifierNode.Attributes["name"].Value;
            if (uniqueidentifierNode.Attributes["generate"] != null)
            {
                if(!bool.TryParse(uniqueidentifierNode.Attributes["generate"].Value,out generate))
                {
                    throw new Exception("Error in config: @generate attribute in <uniqueidentifier> element is not a true/false");
                }
            }

            if (generate)
            {
                //first check to see if the identifier already exists - if not, then create and add it!
                UniqueIdentifier uniqueIdentifier = null;
                if (_uniqueIdentifiers.ContainsKey(uniqueIdentifierName))
                    uniqueIdentifier = _uniqueIdentifiers[uniqueIdentifierName];
                else
                {
                    uniqueIdentifier = new UniqueIdentifier(uniqueIdentifierName);
                    _uniqueIdentifiers.Add(uniqueIdentifierName, uniqueIdentifier);
                }

                //Do the generation
                uniqueIdentifier.Generate();
            }
        }
Esempio n. 3
0
        private void DoUsenamedmapping(ResourceNode resourceNode, XmlNode mapping, XmlNode data)
        {
            //check if there is a specified name attribute
            if(mapping.Attributes["name"] == null)
                throw new Exception("Error in config: Missing attribut @name in <usenamedmapping> element.  Please specify a name of a namedmapping.");

            var namedMappingName = mapping.Attributes["name"];
            var namedMapping = _config.SelectSingleNode("//config/namedmapping[@name='" + namedMappingName.Value + "']");

            if(namedMapping == null)
                throw new Exception("Error in config: no named mapping was found for: " + namedMappingName + ".  Please ensure that a <namedmapping> element is used and defined directly under <config>");

            //recurse
            ExecConfig(resourceNode, namedMapping, data);
        }
Esempio n. 4
0
        private void DoTriple(ResourceNode resourceNode, XmlNode triple, XmlNode data)
        {
            try
            {
                //  if no predicate then abort!
                if (triple.Attributes["predicate"] == null)
                    throw new Exception("Error in config: Missing predicate=\"...\" attribute in <triple>");

                var pred = triple.Attributes["predicate"].Value;
                var predNs = pred.Split(':')[0];
                var predName = pred.Split(':')[1];

                ModifierDelegate mod_func = null;

                if (triple.Attributes["modifier"] != null)
                    mod_func = Modifier.GetModifierMethod(triple.Attributes["modifier"].Value);

                //  is it a literal or a resource?
                if (triple.Attributes["object"] != null)
                {
                    string[] objs = GetValues("object", triple, data, mod_func);
                    foreach (string obj in objs)
                    {
                        var obj2 = SanitiseUri(obj);
                        if (string.IsNullOrEmpty(obj2.Trim()))
                            continue;
                        string valuePrefix = string.Empty;
                        string valueUri = string.Empty;
                        ShortUri(obj2, out valuePrefix, out valueUri);

                        _output.AddPredicateAndObject(resourceNode.xmlResouceNode, predNs, predName, "rdf", "resource", valuePrefix,
                                                      valueUri);
                    }
                }
                else
                {
                    //literal
                    string[] literals = GetValues("value", triple, data, mod_func);
                    foreach (string lit in literals)
                    {
                        if (string.IsNullOrEmpty(lit))
                            continue;

                        var sanitisedLit = SanitiseString(lit.Trim());

                        //check to see if there is a type to assert
                        if (triple.Attributes["type"] != null)
                        {
                            var valueType = triple.Attributes["type"].Value;
                            _output.AddPredicateAndLiteralWithType(resourceNode.xmlResouceNode, predNs, predName, sanitisedLit,valueType);
                        }
                        else if(triple.Attributes["language"] != null)
                        {
                            var valueLang = triple.Attributes["language"].Value;
                            _output.AddPredicateAndLiteralWithLanguage(resourceNode.xmlResouceNode, predNs, predName, sanitisedLit, valueLang);
                        }
                        else
                        {
                            _output.AddPredicateAndLiteral(resourceNode.xmlResouceNode, predNs, predName, sanitisedLit);
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error in Triple tag: " + triple.OuterXml + " " + ex.Message + (_includeStackTraceInOutput ? ex.StackTrace : ""));
            }
        }
Esempio n. 5
0
        private void DoType(ResourceNode resourceNode, XmlNode type, XmlNode data)
        {
            try
            {
                var types = GetValues("value", type, data, null, 1); //min=1
                foreach (string t in types)
                {
                    var prefix = string.Empty;
                    var uri = string.Empty;
                    ShortUri(t, out prefix, out uri);

                    _output.AddPredicateAndObject(resourceNode.xmlResouceNode, "rdf", "type", "rdf", "resource", prefix, uri);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error in Type tag: " + type.OuterXml + " " + ex.Message + (_includeStackTraceInOutput ? ex.StackTrace : ""));
            }
        }
Esempio n. 6
0
        private void DoResource(XmlNode resourceMapping, XmlNode data)
        {
            try
            {

                //XmlNode resourceNode = null;
                ResourceNode resourceNode = new ResourceNode();

                //deal with the identifier (if present)
                XmlNode[] identifiers = GetChildren(resourceMapping, "identifier");

                //if (identifiers.Count<XmlNode>() > 1)
                //    throw new Exception("Error in config: A <resource> may only have one <identifier>");

                if (identifiers.Count<XmlNode>() == 1)
                {
                    //We have an identifier!
                    resourceNode.HasIdentifier = true;

                    //  we have a valid identifier specification
                    XmlNode identifier = identifiers[0];

                    ModifierDelegate modifierDelegate = null;
                    if (identifier.Attributes["modifier"] != null)
                        modifierDelegate = Modifier.GetModifierMethod(identifier.Attributes["modifier"].Value);

                    //  grab the value of the required field from the data
                    string[] ids = GetValues("value", identifier, data, modifierDelegate, 1, 1);
                    foreach (string idValue in ids)
                    {
                        //  apply modifier to the value?
                        //  finally output the identifier as per the spec

                        var id = SanitiseUri(idValue);
                        string prefix = string.Empty;
                        string shortUri = string.Empty;

                        //convert to short url
                        ShortUri(id, out prefix, out shortUri);

                        resourceNode.xmlResouceNode = _output.AddResourceWithIdentifier(prefix, shortUri);

                    }
                }
                else
                {
                    //  no identifier, outout a bnode. b-node
                    resourceNode.xmlResouceNode = _output.AddResource();
                }

                //recurse
                ExecConfig(resourceNode, resourceMapping, data);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in Resource tag: " + resourceMapping.OuterXml + " " + ex.Message + (_includeStackTraceInOutput ? ex.StackTrace : ""));
            }
        }
Esempio n. 7
0
        private void DoSwitch(ResourceNode resourceNode, XmlNode switchNode, XmlNode data)
        {
            try
            {
                XmlNodeList switchValues = null;

                //  does this switch have a match value?
                if (switchNode.Attributes["match"] != null)
                {
                    var match = RemoveClosingBrackets(switchNode.Attributes["match"].Value);
                    match = MatchNestedInternalFunctionCalls(match);
                    switchValues = data.SelectNodes(match, _namespaceManager);
                }

                //  process cases, apply those which satisfy their
                //  match conditions
                bool caseMatched = false;
                var cases = GetChildren(switchNode, "case");
                foreach (XmlNode caseNode in cases)
                {
                    bool breakBoth = false;

                    if (caseNode.Attributes["match"] != null)
                    {
                        var match = RemoveClosingBrackets(caseNode.Attributes["match"].Value);
                        match = MatchNestedInternalFunctionCalls(match);

                        //  if that match attribute is satisfied...
                        var nodeList = data.SelectNodes(match, _namespaceManager);

                        if (nodeList.Count > 0)
                        {
                            caseMatched = true;
                            ExecConfig(resourceNode, caseNode, data);

                            //  by default when we find a matched case, we break and ignore
                            //  other cases, unless attribute break="false" is present
                            if (caseNode.Attributes["break"] == null)
                                break;
                        }

                    }
                    else if (caseNode.Attributes["value"] != null)
                    {
                        var value = caseNode.Attributes["value"].Value;
                        foreach (XmlNode v in switchValues)
                        {
                            if (v.InnerText == value)
                            {
                                caseMatched = true;
                                ExecConfig(resourceNode, caseNode, data);

                                //  by default when we find a matched case, we break and ignore
                                //  other cases, unless attribute break="false" is present
                                if (caseNode.Attributes["break"] != null)
                                {
                                    breakBoth = true;
                                    break;
                                }
                            }
                        }
                        if (breakBoth)
                            break;
                    }
                    else
                    {
                        throw new Exception(
                            "Error in config: <case> tag must have attribute match=\"{xpath}\" or value=\"...\"\n\n");
                    }
                }

                //  if no case statements have been matched and there is
                //  a default case then include anything from that
                if (!caseMatched)
                {
                    var defaults = GetChildren(switchNode, "default");
                    if (defaults.Length > 1)
                    {
                        throw new Exception("Error in config: <switch> tag must not contain more than one <default> tag");
                    }
                    if (defaults.Length == 1)
                    {
                        ExecConfig(resourceNode, defaults[0], data);
                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error in Switch tag: " + switchNode.OuterXml + " " + ex.Message + (_includeStackTraceInOutput ? ex.StackTrace : ""));
            }
        }
Esempio n. 8
0
        private void DoIf(ResourceNode resourceNode, XmlNode ifNode, XmlNode data)
        {
            try
            {
                //validate
                if (ifNode.Attributes["match"] == null)
                    throw new Exception("Error in config: Missing match=\"...\" attribute in <if>\n\n");

                var match = RemoveClosingBrackets(ifNode.Attributes["match"].Value);
                match = MatchNestedInternalFunctionCalls(match);

                XPathNavigator nav = data.CreateNavigator();
                XPathExpression expr = nav.Compile(RemoveClosingBrackets(match));
                expr.SetContext(_namespaceManager);

                _ifStatementMatch = false;
                switch (expr.ReturnType)
                {
                    case XPathResultType.NodeSet:
                        XPathNodeIterator iterator = (XPathNodeIterator)nav.Select(expr);
                        if (iterator.Count > 0)
                        {
                            ExecConfig(resourceNode, ifNode, data);
                            _ifStatementMatch = true;
                        }
                        break;
                    case XPathResultType.Boolean:
                        if((bool)nav.Evaluate(expr))
                        {
                            ExecConfig(resourceNode, ifNode, data);
                            _ifStatementMatch = true;
                        }
                        break;
                    case XPathResultType.String:
                        string st = (string)nav.Evaluate(expr);
                        if (!string.IsNullOrEmpty(st) && st.Trim().Length > 0)
                        {
                            bool resultStr = false;
                            if (bool.TryParse(st, out resultStr))
                            {
                                if (resultStr)
                                {
                                    ExecConfig(resourceNode, ifNode, data);
                                    _ifStatementMatch = true;
                                }
                            }
                        }
                        break;
                    case XPathResultType.Number:
                        //Assume it's a double
                        double dNumber = (double)nav.Evaluate(expr);
                        int iNumber;
                        bool resultNum = false;
                        //if it can be an int, then make it so...
                        if (bool.TryParse(dNumber.ToString(), out resultNum))
                        {
                            if (resultNum)
                            {
                                ExecConfig(resourceNode, ifNode, data);
                                _ifStatementMatch = true;
                            }
                        }
                        break;
                    default:
                        throw new Exception("Could not evaluate If statement: " + match);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in If tag: " + ifNode.OuterXml + " " + ex.Message + (_includeStackTraceInOutput ? ex.StackTrace : ""));
            }
        }
Esempio n. 9
0
        private void DoMapping(ResourceNode resourceNode, XmlNode mapping, XmlNode data)
        {
            try
            {
                //TODO: set xpath globally for error output
                if (mapping.Attributes["match"] == null)
                    throw new Exception("Error in config: Missing match=XPATH attribute in <mapping>");

                //get xpath without the closing {}
                var match = RemoveClosingBrackets(mapping.Attributes["match"].Value);
                match = MatchNestedInternalFunctionCalls(match);

                //execute xpath on data
                var nodeList = data.SelectNodes(match, _namespaceManager);

                foreach (XmlNode node in nodeList)
                {
                    ExecConfig(resourceNode, mapping, node);

                    try
                    {
                        /*get the named graph URI*/
                        if (mapping.Attributes["namedgraph"] != null)
                        {
                            string[] namedGraphs = GetValues("namedgraph", mapping, node);
                            if (namedGraphs.Length == 1)
                            {
                                _output.AddNamedGraphToCurrentNodes(SanitiseUri(namedGraphs[0]));
                            }
                            else
                            {
                                Console.WriteLine(
                                    "Error in Mapping tag: too many values found in attribute: @namedgraph.  No named graph asserted.");
                            }
                        }
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine("Error in Mapping tag with namedgraph attribute: " + ex.Message);
                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error in Mapping tag: " + mapping.OuterXml + " " + ex.Message + (_includeStackTraceInOutput ? ex.StackTrace : ""));
            }
        }
Esempio n. 10
0
        private void DoIdentifier(ResourceNode resourceNode, XmlNode identifier, XmlNode data)
        {
            //We have an identifier!
            resourceNode.HasIdentifier = true;

            ModifierDelegate modifierDelegate = null;
            if (identifier.Attributes["modifier"] != null)
                modifierDelegate = Modifier.GetModifierMethod(identifier.Attributes["modifier"].Value);

            //  grab the value of the required field from the data
            string[] ids = GetValues("value", identifier, data, modifierDelegate, 1, 1);
            foreach (string idValue in ids)
            {
                //  apply modifier to the value?
                //  finally output the identifier as per the spec

                var id = SanitiseUri(idValue);
                string prefix = string.Empty;
                string shortUri = string.Empty;

                //convert to short url
                ShortUri(id, out prefix, out shortUri);

                _output.AddIdentifierForResource(resourceNode.xmlResouceNode, prefix, shortUri);
            }
        }
Esempio n. 11
0
        private void DoError(ResourceNode resourceNode, XmlNode errorNode, XmlNode data)
        {
            string message = string.Empty;
            bool exit = false;

            if (errorNode.Attributes["message"] == null)
                throw new Exception("Error in config: Missing attribute @message in <error> element.  Please specify the message for the error.");
            else
                message = errorNode.Attributes["message"].Value.ToString();

            if(errorNode.Attributes["exit"] != null)
                if(!bool.TryParse(errorNode.Attributes["exit"].Value.ToString(),out exit))
                    throw new Exception("Error in config: Unknown value for attribute @exit in <error> element.  Please specify either true or false.");

            //if user wishes to end the application then throw exception
            //else just log it to the console.
            if (exit)
                throw new Exception("User specified error has occured at: " + resourceNode.xmlResouceNode.InnerXml + ",\nDue to error statement in config: " + message);
            else
                Console.WriteLine("User specified error has occured at: " + resourceNode.xmlResouceNode.InnerXml + ",\nDue to error statement in config: " + message);
        }
Esempio n. 12
0
 private void DoElse(ResourceNode resourceNode, XmlNode elseNode, XmlNode data)
 {
     if(!_ifStatementMatch)
         ExecConfig(resourceNode, elseNode, data);
 }
Esempio n. 13
0
        private void DoCounter(ResourceNode resourceNode, XmlNode counterNode, XmlNode data)
        {
            //default initial value of counter & iteration
            int initialValue = 0;
            eCounterIteration iteration = eCounterIteration.Increment;
            string counterName = string.Empty;
            bool iterate = false;

            if (counterNode.Attributes["name"] == null)
                throw new Exception("Error in config: Missing attribut @name in <counter> element.  Please specify a name of the counter variable.");
            counterName = counterNode.Attributes["name"].Value;

            if (counterNode.Attributes["iteration"] != null)
            {
                if (counterNode.Attributes["iteration"].Value.Trim().ToLower() == "decrement")
                {
                    iteration = eCounterIteration.Decrement;
                }
            }

            if (counterNode.Attributes["initialValue"] != null)
            {
                if (!int.TryParse(counterNode.Attributes["initialValue"].Value, out initialValue))
                {
                    throw new Exception("Error in config: @initialValue attribute in <counter> element is not a number/integer.");
                }
            }

            if (counterNode.Attributes["iterate"] != null)
            {
                if (!bool.TryParse(counterNode.Attributes["iterate"].Value, out iterate))
                {
                    throw new Exception("Error in config: @iterate attribute in <counter> element is not a true/false");
                }
            }

            if (iterate)
            {
                var counter = _counters[counterName];
                counter.IterateCounter();
            }
            else
            {
                //check if it is in there first...
                Counter counter = null;
                if (_counters.ContainsKey(counterName))
                    counter = _counters[counterName];
                else
                    //if we get here we'll create a new counter
                    counter = new Counter(counterName);

                counter.CounterValue = initialValue;
                counter.Iteration = iteration;

                try
                {
                    if (!_counters.ContainsKey(counterName))
                        _counters.Add(counterName, counter);
                }
                catch (Exception ex)
                {
                    throw new Exception("An issue occured when storing the counter variable: " + ex.Message);
                }
            }
        }
Esempio n. 14
0
        private void DoBnode(ResourceNode resourceNode, XmlNode bNode, XmlNode data)
        {
            try
            {
                if (bNode.Attributes["predicate"] == null)
                    throw new Exception("Error in config: Missing predicate=\"...\" attribute in <bnode>");
                if (bNode.Attributes["type"] == null)
                    throw new Exception("Error in config: Missing type=\"...\" attribute in <bnode>");

                var pred = bNode.Attributes["predicate"].Value;
                var type = bNode.Attributes["type"].Value;

                XmlNode predNode = _output.AddBnode(pred);
                ResourceNode typeResource = new ResourceNode();
                typeResource.xmlResouceNode = _output.AddBnode(type);

                predNode.AppendChild(typeResource.xmlResouceNode);
                resourceNode.xmlResouceNode.AppendChild(predNode);
                ExecConfig(typeResource, bNode, data);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error in BNode tag: " + bNode.OuterXml + " " + ex.Message + (_includeStackTraceInOutput ? ex.StackTrace : ""));
            }
        }