protected bool ProcessStatement(XmlElement element, IXmlProcessorEngine engine) { if (!element.HasAttribute(DefinedAttrName) && !element.HasAttribute(NotDefinedAttrName)) { throw new XmlProcessorException("'if' elements expects a non empty defined or not-defined attribute"); } if (element.HasAttribute(DefinedAttrName) && element.HasAttribute(NotDefinedAttrName)) { throw new XmlProcessorException("'if' elements expects a non empty defined or not-defined attribute"); } bool processContents = false; if (element.HasAttribute(DefinedAttrName)) { processContents = engine.HasFlag(element.GetAttribute(DefinedAttrName)); } else { processContents = !engine.HasFlag(element.GetAttribute(NotDefinedAttrName)); } return processContents; }
/// <summary> /// Processes the string. /// </summary> /// <param name = "node">The node.</param> /// <param name = "value">The value.</param> /// <param name = "engine">The context.</param> public void ProcessString(XmlNode node, string value, IXmlProcessorEngine engine) { var fragment = CreateFragment(node); Match match; var pos = 0; while ((match = PropertyValidationRegExp.Match(value, pos)).Success) { if (pos < match.Index) { AppendChild(fragment, value.Substring(pos, match.Index - pos)); } var propRef = match.Groups[1].Value; // #!{ propKey } var propKey = match.Groups[2].Value; // propKey XmlNode prop = engine.GetProperty(propKey); if (prop != null) { // When node has a parentNode (not an attribute) // we copy any attributes for the property into the parentNode if (node.ParentNode != null) { MoveAttributes(node.ParentNode as XmlElement, prop as XmlElement); } AppendChild(fragment, prop.ChildNodes); } else if (IsRequiredProperty(propRef)) { throw new XmlProcessorException(String.Format("Required configuration property {0} not found", propKey)); } pos = match.Index + match.Length; } // Appending anything left if (pos > 0 && pos < value.Length) { AppendChild(fragment, value.Substring(pos, value.Length - pos)); } // we only process when there was at least one match // even when the fragment contents is empty since // that could mean that there was a match but the property // reference was a silent property if (pos > 0) { if (node.NodeType == XmlNodeType.Attribute) { node.Value = fragment.InnerText.Trim(); } else { ReplaceNode(node.ParentNode, fragment, node); } } }
/// <summary> /// /// </summary> /// <param name="nodeList"></param> /// <param name="engine"></param> /// <example> /// <code> /// <properties> /// <attributes> /// <myAttribute>attributeValue</myAttribute> /// </attributes> /// <myProperty>propertyValue</myProperty> /// </properties> /// </code> /// </example> public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine) { XmlElement element = nodeList.Current as XmlElement; IXmlProcessorNodeList childNodes = new DefaultXmlProcessorNodeList(element.ChildNodes); while(childNodes.MoveNext()) { // Properties processing its a little more complicated than usual // since we need to support all special tags (if,else,define...) // plus we need to register any regular element as a property asap // since we should support properties that reference other properties // i.e. <myprop2>#{prop1}</myprop2> if (engine.HasSpecialProcessor(childNodes.Current)) { // Current node its a special element so we bookmark it before processing it... XmlNode current = childNodes.Current; int pos = childNodes.CurrentPosition; engine.DispatchProcessCurrent(childNodes); // ...after processing we need to refresh childNodes // to account for any special element that affects the node tree (if,choose...) childNodes = new DefaultXmlProcessorNodeList(element.ChildNodes); // we only care about changes in the tree from the current node and forward // so if the new list is empty or smaller we just exit the loop if (pos < childNodes.Count) { childNodes.CurrentPosition = pos; // if the current node gets replaced in the new list we need to restart processing // otherwise we just continue as usual if (childNodes.Current != current) { childNodes.CurrentPosition -= 1; continue; } } else { break; } } else { engine.DispatchProcessCurrent(childNodes); } if (IgnoreNode(childNodes.Current)) continue; XmlElement elem = GetNodeAsElement(element, childNodes.Current); engine.AddProperty(elem); } RemoveItSelf(element); }
public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine) { XmlProcessingInstruction node = nodeList.Current as XmlProcessingInstruction; engine.RemoveFlag(node.Data); RemoveItSelf(node); }
public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine) { XmlElement element = nodeList.Current as XmlElement; XmlNode result = ProcessInclude(element, element.GetAttribute("uri"), engine); ReplaceItself(result, element); }
public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine) { XmlElement element = nodeList.Current as XmlElement; ProcessAttributes(element, engine); engine.DispatchProcessAll(new DefaultXmlProcessorNodeList(element.ChildNodes)); }
public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine) { XmlElement element = nodeList.Current as XmlElement; String flag = GetRequiredAttribute(element, FlagAttrName); Process(flag, engine); RemoveItSelf(element); }
public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine) { var assemblyName = GetRequiredAttribute((XmlElement)nodeList.Current, "assembly"); // that's it, at this point we just load the assembly into the appdomain so that it becomes // available to type matching system. var assembly = ReflectionUtil.GetAssemblyNamed(assemblyName); RemoveItSelf(nodeList.Current); }
/// <summary> /// Processes element attributes. /// if the attribute is include will append to the element /// all contents from the file. /// if the attribute has a property reference the reference will be /// expanded /// </summary> /// <param name="element">The element.</param> /// <param name="engine"></param> private void ProcessAttributes(XmlElement element, IXmlProcessorEngine engine) { ProcessIncludeAttribute(element, engine); foreach(XmlAttribute att in element.Attributes) { textProcessor.ProcessString(att, att.Value, engine); } }
public XmlNode ProcessInclude(XmlElement element, String includeUri, IXmlProcessorEngine engine) { XmlDocumentFragment frag = null; if (includeUri == null) { throw new ConfigurationProcessingException( String.Format("Found an include node without an 'uri' attribute: {0}", element.BaseURI)); } String[] uriList = includeUri.Split(','); frag = CreateFragment(element); foreach(String uri in uriList) { using(IResource resource = engine.GetResource(uri)) { XmlDocument doc = new XmlDocument(); try { using (var stream = resource.GetStreamReader()) { doc.Load(stream); } } catch(Exception ex) { throw new ConfigurationProcessingException( String.Format("Error processing include node: {0}", includeUri), ex); } engine.PushResource(resource); engine.DispatchProcessAll(new DefaultXmlProcessorNodeList(doc.DocumentElement)); engine.PopResource(); if (element.GetAttribute("preserve-wrapper") == "yes") { AppendChild(frag, doc.DocumentElement); } else { AppendChild(frag, doc.DocumentElement.ChildNodes); } } } return frag; }
private static void ProcessIncludeAttribute(XmlElement element, IXmlProcessorEngine engine) { XmlAttribute include = element.Attributes[IncludeAttrName]; if (include == null) return; // removing the include attribute from the element element.Attributes.RemoveNamedItem(IncludeAttrName); XmlNode includeContent = includeProcessor.ProcessInclude(element, include.Value, engine); if (includeContent != null) { element.PrependChild(includeContent); } }
public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine) { XmlElement element = nodeList.Current as XmlElement; bool processContents = ProcessStatement(element, engine); if (processContents) { XmlDocumentFragment fragment = CreateFragment(element); MoveChildNodes(fragment, element); engine.DispatchProcessAll(new DefaultXmlProcessorNodeList(fragment.ChildNodes)); ReplaceItself(fragment, element); } else { RemoveItSelf(element); } }
public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine) { var element = nodeList.Current as XmlElement; var fragment = CreateFragment(element); foreach (XmlNode child in element.ChildNodes) { if (IgnoreNode(child)) { continue; } var elem = GetNodeAsElement(element, child); var found = false; if (elem.Name == WhenElemName) { found = ProcessStatement(elem, engine); } else if (elem.Name == OtherwiseElemName) { found = true; } else { throw new XmlProcessorException("'{0} can not contain only 'when' and 'otherwise' elements found '{1}'", element.Name, elem.Name); } if (found) { if (elem.ChildNodes.Count > 0) { MoveChildNodes(fragment, elem); engine.DispatchProcessAll(new DefaultXmlProcessorNodeList(fragment.ChildNodes)); } break; } } ReplaceItself(fragment, element); }
/// <summary> /// /// </summary> /// <param name="nodeList"></param> /// <param name="engine"></param> /// <example> /// <code> /// <properties> /// <attributes> /// <myAttribute>attributeValue</myAttribute> /// </attributes> /// <myProperty>propertyValue</myProperty> /// </properties> /// </code> /// </example> public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine) { XmlElement element = nodeList.Current as XmlElement; DefaultXmlProcessorNodeList childNodes = new DefaultXmlProcessorNodeList(element.ChildNodes); while(childNodes.MoveNext()) { engine.DispatchProcessCurrent(childNodes); if (IgnoreNode(childNodes.Current)) continue; XmlElement elem = GetNodeAsElement(element, childNodes.Current); AppendElementAsAttribute(element.ParentNode, childNodes.Current as XmlElement); } RemoveItSelf(element); }
public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine) { XmlProcessingInstruction node = nodeList.Current as XmlProcessingInstruction; XmlDocumentFragment fragment = CreateFragment(node); string expression = node.Data; // We don't have an expression evaluator right now, so expression will // be just pre-defined literals that we know how to evaluate object evaluated = ""; if (string.Compare(expression, "$basedirectory", true) == 0) { evaluated = AppDomain.CurrentDomain.BaseDirectory; } fragment.AppendChild(node.OwnerDocument.CreateTextNode(evaluated.ToString())); ReplaceNode(node.ParentNode, fragment, node); }
public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine) { var node = nodeList.Current as XmlProcessingInstruction; AssertData(node, true); var state = engine.HasFlag(node.Data) ? StatementState.Collect : StatementState.Init; var nodesToProcess = new List <XmlNode>(); var nestedLevels = 0; RemoveItSelf(nodeList.Current); while (nodeList.MoveNext()) { if (nodeList.Current.NodeType == XmlNodeType.ProcessingInstruction) { var pi = nodeList.Current as XmlProcessingInstruction; if (pi.Name == EndPiName) { nestedLevels--; if (nestedLevels < 0) { RemoveItSelf(nodeList.Current); break; } } else if (pi.Name == IfPiName) { nestedLevels++; } else if (nestedLevels == 0) { if (pi.Name == ElsePiName || pi.Name == ElsifPiName) { ProcessElseElement(pi, engine, ref state); continue; } } } if (state == StatementState.Collect) { nodesToProcess.Add(nodeList.Current); } else { RemoveItSelf(nodeList.Current); } } if (nestedLevels != -1) { throw new XmlProcessorException("Unbalanced pi if element"); } if (nodesToProcess.Count > 0) { engine.DispatchProcessAll(new DefaultXmlProcessorNodeList(nodesToProcess)); } }
protected virtual void Process(string flag, IXmlProcessorEngine engine) { engine.AddFlag(flag); }
public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine) { XmlProcessingInstruction node = nodeList.Current as XmlProcessingInstruction; AssertData(node, true); StatementState state = engine.HasFlag(node.Data) ? StatementState.Collect : StatementState.Init; List<XmlNode> nodesToProcess = new List<XmlNode>(); int nestedLevels = 0; RemoveItSelf(nodeList.Current); while(nodeList.MoveNext()) { if (nodeList.Current.NodeType == XmlNodeType.ProcessingInstruction) { XmlProcessingInstruction pi = nodeList.Current as XmlProcessingInstruction; if (pi.Name == EndPiName) { nestedLevels--; if (nestedLevels < 0) { RemoveItSelf(nodeList.Current); break; } } else if (pi.Name == IfPiName) { nestedLevels++; } else if (nestedLevels == 0) { if (pi.Name == ElsePiName || pi.Name == ElsifPiName) { ProcessElseElement(pi, engine, ref state); continue; } } } if (state == StatementState.Collect) { nodesToProcess.Add(nodeList.Current); } else { RemoveItSelf(nodeList.Current); } } if (nestedLevels != -1) { throw new XmlProcessorException("Unbalanced pi if element"); } if (nodesToProcess.Count > 0) { engine.DispatchProcessAll(new DefaultXmlProcessorNodeList(nodesToProcess)); } }
public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine) { var node = nodeList.Current as XmlCharacterData; ProcessString(node, node.Value, engine); }
public abstract void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine);
private void ProcessElseElement(XmlProcessingInstruction pi, IXmlProcessorEngine engine, ref StatementState state) { AssertData(pi, pi.Name == ElsifPiName); if (state == StatementState.Collect) { state = StatementState.Finished; } else if (pi.Name == ElsePiName || engine.HasFlag(pi.Data)) { if (state == StatementState.Init) { state = StatementState.Collect; } } RemoveItSelf(pi); return; }
///<summary> ///</summary> ///<param name = "nodeList"></param> ///<param name = "engine"></param> ///<example> /// <code> /// <properties> /// <attributes> /// <myAttribute>attributeValue</myAttribute> /// </attributes> /// <myProperty>propertyValue</myProperty> /// </properties> /// </code> ///</example> public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine) { var element = nodeList.Current as XmlElement; IXmlProcessorNodeList childNodes = new DefaultXmlProcessorNodeList(element.ChildNodes); while (childNodes.MoveNext()) { // Properties processing its a little more complicated than usual // since we need to support all special tags (if,else,define...) // plus we need to register any regular element as a property asap // since we should support properties that reference other properties // i.e. <myprop2>#{prop1}</myprop2> if (engine.HasSpecialProcessor(childNodes.Current)) { // Current node its a special element so we bookmark it before processing it... var current = childNodes.Current; var pos = childNodes.CurrentPosition; engine.DispatchProcessCurrent(childNodes); // ...after processing we need to refresh childNodes // to account for any special element that affects the node tree (if,choose...) childNodes = new DefaultXmlProcessorNodeList(element.ChildNodes); // we only care about changes in the tree from the current node and forward // so if the new list is empty or smaller we just exit the loop if (pos < childNodes.Count) { childNodes.CurrentPosition = pos; // if the current node gets replaced in the new list we need to restart processing // otherwise we just continue as usual if (childNodes.Current != current) { childNodes.CurrentPosition -= 1; continue; } } else { break; } } else { engine.DispatchProcessCurrent(childNodes); } if (IgnoreNode(childNodes.Current)) { continue; } var elem = GetNodeAsElement(element, childNodes.Current); engine.AddProperty(elem); } RemoveItSelf(element); }
/// <summary> /// Initializes a new instance of the <see cref="XmlProcessor"/> class. /// </summary> /// <param name="environmentName">Name of the environment.</param> /// <param name="resourceSubSystem">The resource sub system.</param> public XmlProcessor(string environmentName, IResourceSubSystem resourceSubSystem) { engine = new XmlProcessorEngine(environmentName, resourceSubSystem); RegisterProcessors(); }
/// <summary> /// Initializes a new instance of the <see cref="XmlProcessor"/> class. /// </summary> public XmlProcessor(string environmentName) { engine = new DefaultXmlProcessorEngine(environmentName); RegisterProcessors(); }
/// <summary> /// Initializes a new instance of the <see cref="XmlProcessor"/> class. /// </summary> /// <param name="environmentName">Name of the environment.</param> /// <param name="resourceSubSystem">The resource sub system.</param> public XmlProcessor(string environmentName, IResourceSubSystem resourceSubSystem) { engine = new DefaultXmlProcessorEngine(environmentName, resourceSubSystem); RegisterProcessors(); }
protected override void Process(String flag, IXmlProcessorEngine engine) { engine.RemoveFlag(flag); }
/// <summary> /// Processes the string. /// </summary> /// <param name = "node">The node.</param> /// <param name = "value">The value.</param> /// <param name = "engine">The context.</param> public void ProcessString(XmlNode node, string value, IXmlProcessorEngine engine) { var fragment = CreateFragment(node); Match match; var pos = 0; while ((match = PropertyValidationRegExp.Match(value, pos)).Success) { if (pos < match.Index) { AppendChild(fragment, value.Substring(pos, match.Index - pos)); } var propRef = match.Groups[1].Value; // #!{ propKey } var propKey = match.Groups[2].Value; // propKey { } var prop = engine.GetProperty(propKey); if (prop != null) { // When node has a parentNode (not an attribute) // we copy any attributes for the property into the parentNode if (node.ParentNode != null) { MoveAttributes(node.ParentNode as XmlElement, prop); } AppendChild(fragment, prop.ChildNodes); } else if (IsRequiredProperty(propRef)) { // fallback to reading from appSettings var appSetting = ConfigurationManager.AppSettings[propKey]; if (appSetting != null) { AppendChild(fragment, appSetting); } else { throw new XmlProcessorException(String.Format("Required configuration property {0} not found", propKey)); } } pos = match.Index + match.Length; } // Appending anything left if (pos > 0 && pos < value.Length) { AppendChild(fragment, value.Substring(pos, value.Length - pos)); } // we only process when there was at least one match // even when the fragment contents is empty since // that could mean that there was a match but the property // reference was a silent property if (pos > 0) { if (node.NodeType == XmlNodeType.Attribute) { node.Value = fragment.InnerText.Trim(); } else { ReplaceNode(node.ParentNode, fragment, node); } } }
/// <summary> /// Initializes a new instance of the <see cref = "XmlProcessor" /> class. /// </summary> public XmlProcessor(string environmentName) { engine = new DefaultXmlProcessorEngine(environmentName); RegisterProcessors(); }