internal static void ProcessDefFragment(IDocumentProcessor docProcessor, XmlNode node, ITemplate template) { XmlAttribute nameAttr = (XmlAttribute)node.Attributes.GetNamedItem("name"); XmlAttribute paramsAttr = (XmlAttribute)node.Attributes.GetNamedItem("params"); if (nameAttr == null) { throw ParserUtils.TemplateErrorException("The <def-fragment> element must have the name attribute specified."); } string name = nameAttr.Value; if (!ParserUtils.IsValidUnqualifiedName(name)) { throw ParserUtils.TemplateErrorException("The name " + name + " is not a valid unqualified identifier."); } if (template.HasMember(name)) { throw ParserUtils.TemplateErrorException("Duplicate definition of member " + name + "."); } RenderFunctionMember m = new RenderFunctionMember(nameAttr.Value, paramsAttr != null ? paramsAttr.Value : ""); Utils.DoForEachChild(node, delegate(XmlNode n) { docProcessor.ProcessRecursive(n, template, m); }); if (template.HasMember(name)) { throw ParserUtils.TemplateErrorException("Duplicate definition of member " + name + "."); // Just in case it has already been added during the recursive call. } template.AddMember(m); }
internal static void ProcessSwitchContent(IDocumentProcessor docProcessor, XmlNode switchNode, ITemplate template, IRenderFunction currentRenderFunction) { bool hasDefault = false; bool hasAny = false; Utils.DoForEachChild(switchNode, delegate(XmlNode child) { #if CLIENT if (((child.NodeType == XmlNodeType.Text || child.NodeType == XmlNodeType.CDATA) && child.Value.Trim() == "") || child.NodeType == XmlNodeType.Comment) { return; } #else if (child.NodeType == XmlNodeType.Whitespace || child.NodeType == XmlNodeType.SignificantWhitespace || child.NodeType == XmlNodeType.Comment) { return; } #endif if (child.NodeType == XmlNodeType.Element && child.Name == "case") { XmlAttribute valueAttr = (XmlAttribute)child.Attributes.GetNamedItem("value"); if (valueAttr == null) { throw ParserUtils.TemplateErrorException("The <case> element must have the value attribute specified."); } currentRenderFunction.AddFragment(new CodeFragment("case " + valueAttr.Value + ": {", 1)); } else if (child.NodeType == XmlNodeType.Element && child.Name == "default") { if (hasDefault) { throw ParserUtils.TemplateErrorException("There can only be one <default> element inside <switch>"); } hasDefault = true; currentRenderFunction.AddFragment(new CodeFragment("default: {", 1)); } else { throw ParserUtils.TemplateErrorException("The <switch> element can only have <case> and <default> elements as children."); } hasAny = true; Utils.DoForEachChild(child, delegate(XmlNode grandchild) { docProcessor.ProcessRecursive(grandchild, template, currentRenderFunction); }); currentRenderFunction.AddFragment(new CodeFragment("break;", -1)); currentRenderFunction.AddFragment(new CodeFragment("}", 0)); }); if (!hasAny) { throw ParserUtils.TemplateErrorException("The <switch> element must contain at least one <case> or <default>"); } }
public bool TryProcess(IDocumentProcessor docProcessor, XmlNode node, bool isRoot, ITemplate template, IRenderFunction currentRenderFunction) { if (node.NodeType != XmlNodeType.Element) { return(false); } GenericElementProcessorContext context = new GenericElementProcessorContext(); currentRenderFunction.AddFragment(new LiteralFragment("<" + node.Name)); AddAttributeFragments(docProcessor, node, isRoot, template, currentRenderFunction, context); if (context.Id != null) { string tagName = node.Name; if (tagName.ToLowerCase() == "input" && context.Type != null) { tagName += "/" + context.Type; } template.AddMember(new NamedElementMember(tagName, context.Id)); } if (noContentTags.Contains(node.Name)) { if (Utils.GetNumChildNodes(node) > 0) { throw ParserUtils.TemplateErrorException("The tag " + node.Name + " can not have children."); } currentRenderFunction.AddFragment(new LiteralFragment("/>")); } else { currentRenderFunction.AddFragment(new LiteralFragment(">")); Utils.DoForEachChild(node, delegate(XmlNode child) { docProcessor.ProcessRecursive(child, template, currentRenderFunction); }); currentRenderFunction.AddFragment(new LiteralFragment("</" + node.Name + ">")); } return(true); }
internal static void ProcessDefFragment(IDocumentProcessor docProcessor, XmlNode node, ITemplate template) { XmlAttribute nameAttr = (XmlAttribute)node.Attributes.GetNamedItem("name"); XmlAttribute paramsAttr = (XmlAttribute)node.Attributes.GetNamedItem("params"); if (nameAttr == null) throw ParserUtils.TemplateErrorException("The <def-fragment> element must have the name attribute specified."); string name = nameAttr.Value; if (!ParserUtils.IsValidUnqualifiedName(name)) throw ParserUtils.TemplateErrorException("The name " + name + " is not a valid unqualified identifier."); if (template.HasMember(name)) throw ParserUtils.TemplateErrorException("Duplicate definition of member " + name + "."); RenderFunctionMember m = new RenderFunctionMember(nameAttr.Value, paramsAttr != null ? paramsAttr.Value : ""); Utils.DoForEachChild(node, delegate(XmlNode n) { docProcessor.ProcessRecursive(n, template, m); }); if (template.HasMember(name)) throw ParserUtils.TemplateErrorException("Duplicate definition of member " + name + "."); // Just in case it has already been added during the recursive call. template.AddMember(m); }
public bool TryProcess(IDocumentProcessor docProcessor, XmlNode node, bool isRoot, ITemplate template, IRenderFunction currentRenderFunction) { if (node.NodeType != XmlNodeType.Element || node.Name != "control") return false; if (isRoot) throw ParserUtils.TemplateErrorException("The root element can not be a control."); string id = null; string type = null; bool customInstantiate = false; Dictionary<string, TypedMarkupData> additionalProperties = new Dictionary<string, TypedMarkupData>(); Utils.DoForEachAttribute(node, delegate(XmlAttribute attr) { if (attr.Name == "id") { if (!ParserUtils.IsValidUnqualifiedName(attr.Value)) throw ParserUtils.TemplateErrorException("The id '" + attr.Value + "' is not a valid identifier."); id = attr.Value; } else if (attr.Name == "type") { if (string.IsNullOrEmpty(attr.Value)) throw ParserUtils.TemplateErrorException("The control type '" + attr.Value + "' is invalid."); type = attr.Value; } else if (attr.Name == "customInstantiate") { string v = attr.Value.ToLowerCase(); customInstantiate = Utils.ParseBool(v); } else { additionalProperties[attr.Name] = docProcessor.ParseTypedMarkup(attr.Value); } }); if (customInstantiate && additionalProperties.Count > 0) throw ParserUtils.TemplateErrorException("There can not be any property assignments when customInstantiate is true."); if (type == null) throw ParserUtils.TemplateErrorException("The control '" + id + "' does not have a type specified."); if (id == null) id = template.GetUniqueId(); if (template.HasMember(id)) throw ParserUtils.TemplateErrorException("Duplicate definition of member " + id); var dependencies = new List<IMember>(); int numInnerFragments = 0; if (Utils.GetNumChildNodes(node) > 0) { Utils.DoForEachChild(node, delegate(XmlNode n) { if (n.OuterXml.Trim() != "") { numInnerFragments++; string innerName = id + "_inner" + Utils.ToStringInvariantInt(numInnerFragments); if (template.HasMember(innerName)) throw ParserUtils.TemplateErrorException("The internal name " + innerName + " is already in use."); IRenderFunction innerFunction = new RenderFunctionMember(innerName, ""); template.AddMember((IMember)innerFunction); docProcessor.ProcessRecursive(n, template, innerFunction); dependencies.Add(innerFunction); } }); } if (!template.HasMember("Container")) template.AddMember(new PropertyMember("Container", "IContainer", "IContainer", AccessModifier._Public, "_container", "IContainer", "IContainer", true, true, null, true)); IMember controlMember = new InstantiatedControlMember(id, type, customInstantiate, additionalProperties, dependencies); template.AddMember(controlMember); currentRenderFunction.AddFragment(new InstantiatedControlFragment(id, customInstantiate, numInnerFragments)); currentRenderFunction.AddDependency(controlMember); return true; }
public bool TryProcess(IDocumentProcessor docProcessor, XmlNode node, bool isRoot, ITemplate template, IRenderFunction currentRenderFunction) { if (node.NodeType != XmlNodeType.Element || node.Name != "control") { return(false); } if (isRoot) { throw ParserUtils.TemplateErrorException("The root element can not be a control."); } string id = null; string type = null; bool customInstantiate = false; Dictionary <string, TypedMarkupData> additionalProperties = new Dictionary <string, TypedMarkupData>(); Utils.DoForEachAttribute(node, delegate(XmlAttribute attr) { if (attr.Name == "id") { if (!ParserUtils.IsValidUnqualifiedName(attr.Value)) { throw ParserUtils.TemplateErrorException("The id '" + attr.Value + "' is not a valid identifier."); } id = attr.Value; } else if (attr.Name == "type") { if (string.IsNullOrEmpty(attr.Value)) { throw ParserUtils.TemplateErrorException("The control type '" + attr.Value + "' is invalid."); } type = attr.Value; } else if (attr.Name == "customInstantiate") { string v = attr.Value.ToLowerCase(); customInstantiate = Utils.ParseBool(v); } else { additionalProperties[attr.Name] = docProcessor.ParseTypedMarkup(attr.Value); } }); if (customInstantiate && additionalProperties.Count > 0) { throw ParserUtils.TemplateErrorException("There can not be any property assignments when customInstantiate is true."); } if (type == null) { throw ParserUtils.TemplateErrorException("The control '" + id + "' does not have a type specified."); } if (id == null) { id = template.GetUniqueId(); } if (template.HasMember(id)) { throw ParserUtils.TemplateErrorException("Duplicate definition of member " + id); } var dependencies = new List <IMember>(); int numInnerFragments = 0; if (Utils.GetNumChildNodes(node) > 0) { Utils.DoForEachChild(node, delegate(XmlNode n) { if (n.OuterXml.Trim() != "") { numInnerFragments++; string innerName = id + "_inner" + Utils.ToStringInvariantInt(numInnerFragments); if (template.HasMember(innerName)) { throw ParserUtils.TemplateErrorException("The internal name " + innerName + " is already in use."); } IRenderFunction innerFunction = new RenderFunctionMember(innerName, ""); template.AddMember((IMember)innerFunction); docProcessor.ProcessRecursive(n, template, innerFunction); dependencies.Add(innerFunction); } }); } if (!template.HasMember("Container")) { template.AddMember(new PropertyMember("Container", "IContainer", "IContainer", AccessModifier._Public, "_container", "IContainer", "IContainer", true, true, null, true)); } IMember controlMember = new InstantiatedControlMember(id, type, customInstantiate, additionalProperties, dependencies); template.AddMember(controlMember); currentRenderFunction.AddFragment(new InstantiatedControlFragment(id, customInstantiate, numInnerFragments)); currentRenderFunction.AddDependency(controlMember); return(true); }
public bool TryProcess(IDocumentProcessor docProcessor, XmlNode node, bool isRoot, ITemplate template, IRenderFunction currentRenderFunction) { if (node.NodeType != XmlNodeType.Element) return false; GenericElementProcessorContext context = new GenericElementProcessorContext(); currentRenderFunction.AddFragment(new LiteralFragment("<" + node.Name)); AddAttributeFragments(docProcessor, node, isRoot, template, currentRenderFunction, context); if (context.Id != null) { string tagName = node.Name; if (tagName.ToLowerCase() == "input" && context.Type != null) tagName += "/" + context.Type; template.AddMember(new NamedElementMember(tagName, context.Id)); } if (noContentTags.Contains(node.Name)) { if (Utils.GetNumChildNodes(node) > 0) throw ParserUtils.TemplateErrorException("The tag " + node.Name + " can not have children."); currentRenderFunction.AddFragment(new LiteralFragment("/>")); } else { currentRenderFunction.AddFragment(new LiteralFragment(">")); Utils.DoForEachChild(node, delegate(XmlNode child) { docProcessor.ProcessRecursive(child, template, currentRenderFunction); }); currentRenderFunction.AddFragment(new LiteralFragment("</" + node.Name + ">")); } return true; }
public bool TryProcess(IDocumentProcessor docProcessor, XmlNode node, bool isRoot, ITemplate template, IRenderFunction currentRenderFunction) { if (node.NodeType != XmlNodeType.Element) { return(false); } if (node.Name == "case" || node.Name == "default") { throw ParserUtils.TemplateErrorException("<case> and <default> can only occur inside <switch>"); } if (node.Name == "else-if" || node.Name == "else") { throw ParserUtils.TemplateErrorException("<else-if> and <else> can only occur inside <if>"); } string statement = GetStatement(node); if (statement == null) { return(false); } if (isRoot) { throw ParserUtils.TemplateErrorException("Control flow nodes cannot be root elements."); } currentRenderFunction.AddFragment(new CodeFragment(statement + " {", 1)); if (node.Name == "switch") { ProcessSwitchContent(docProcessor, node, template, currentRenderFunction); } else { bool hasElse = false; Utils.DoForEachChild(node, delegate(XmlNode child) { if (node.Name == "if" && (child.NodeType == XmlNodeType.Element && (child.Name == "else-if" || child.Name == "else"))) { if (hasElse) { throw ParserUtils.TemplateErrorException("There cannot be other <else-if> or <else> elements after <else>."); } if (Utils.GetNumChildNodes(child) > 0) { throw ParserUtils.TemplateErrorException("<" + child.Name + "> elements should not have children."); } string possibleTest; if (child.Name == "else-if") { XmlAttribute testAttr = (XmlAttribute)child.Attributes.GetNamedItem("test"); if (testAttr == null) { throw ParserUtils.TemplateErrorException("The <else-if> elements must have the test attribute specified."); } possibleTest = "if (" + testAttr.Value + ") "; } else { hasElse = true; possibleTest = ""; } currentRenderFunction.AddFragment(new CodeFragment(null, -1)); currentRenderFunction.AddFragment(new CodeFragment("}", 0)); currentRenderFunction.AddFragment(new CodeFragment("else " + possibleTest + "{", 1)); } else { docProcessor.ProcessRecursive(child, template, currentRenderFunction); } }); } currentRenderFunction.AddFragment(new CodeFragment(null, -1)); currentRenderFunction.AddFragment(new CodeFragment("}", 0)); return(true); }