public static XmlReader GetAsXmlReader(ITagAttribute attribute, TagModel model, string fallBack) { XmlReader reader = null; object raw = Get(attribute, model) ?? fallBack; if (raw == null) { return(reader); } if (raw is string && !String.IsNullOrEmpty((string)raw)) { var textReader = new StringReader((string)raw); reader = XmlReader.Create(textReader); } else if (raw is Stream) { XmlReader.Create((Stream)raw); } else if (raw is TextReader) { reader = XmlReader.Create((TextReader)raw); } else if (raw is XmlReader) { reader = (XmlReader)raw; } else { throw TagException.UnsupportedInput(raw.GetType(), typeof(string), typeof(Stream), typeof(TextReader), typeof(XmlReader)).Decorate(attribute.Context); } return(reader); }
public override object InternalEvaluate(TagModel model) { string dateStr = GetAutoValueAsString("Value", model); CultureInfo culture = ParseLocale != null ? new CultureInfo(GetAsString(ParseLocale, model)) : (CultureInfo)model[FormatConstants.LOCALE]; var format = (DateTimeFormatInfo)DateTimeFormatInfo.GetInstance(culture.DateTimeFormat).Clone(); DateTime?result = null; if (string.IsNullOrEmpty(dateStr)) { dateStr = GetAutoValueAsString(nameof(DefaultValue), model); } if (!String.IsNullOrEmpty(dateStr)) { if (GetAutoValueAsBool("Exact", model)) { string pattern = GetAsString(Pattern, model) ?? GetPattern(model, format); try { result = DateTime.ParseExact(dateStr, pattern, format); } catch (FormatException) { throw TagException.ParseException(dateStr, "Date").Decorate(Context); } } else { result = DateTime.Parse(dateStr, format); } } return(result); }
public ITag Parse(bool expectTag = false) { if (!_helper.At(TagLibConstants.START_TAG)) { _helper.Read(TagLibConstants.START_TAG); } if (_helper.IsAhead(TagLibConstants.CLOSE_SLASH)) { return(ParseCloseTag()); } var context = _helper.Current.Context; if (expectTag && !_helper.IsAhead(TokenType.Regular)) { throw TagException.ExpectedTagOrGroupName(_helper.Lookahead.Contents).Decorate(_helper.Lookahead.Context); } var tag = ParseOpenTag(); if (tag != null) { tag.Context = context; } ValidateTag(tag); return(tag); }
public void InsertTemplateWithIllegalAttribute() { var tag = new InsertTemplate() { Template = new MockAttribute(new Constant("insertEMaster.htm")) }; tag.Factory = _factory; var setAttribute = new Set { Var = new MockAttribute(new Constant("body")), Body = new MockAttribute(new Constant("Dit is de Test Body")) }; try { tag.AddNestedTag(setAttribute); string expected = (new StreamReader("insertEExpected.htm")).ReadToEnd(); tag.Evaluate(_model); Assert.Fail("Expect exception"); } catch (TagException Te) { Assert.That(Te.Message, Is.EqualTo( TagException.OnlyNestedTagsOfTypeAllowed(setAttribute.GetType(), typeof(PutAttribute)). Message)); } }
public void BugSequenceContainsNo() { var model = new Hashtable(); model.Add("small", 3); model.Add("large", 11); model.Add("text", "some text"); model.Add("greet", "Hello"); model.Add("to", "world"); var list = new ArrayList(new[] { "H1", "H2", "H3" }); model.Add("list", list); try { Formatter formatter = Formatter.FileBasedFormatter("bugsequencenoelement.htm", TagLibMode.StrictResolve, new TagLib(TagLibMode.StrictResolve, new Core(), new Format())); } catch (ExceptionWithContext Pe) { Assert.That(Pe.MessageWithOutContext, Text.StartsWith(TagException.UnkownTag("h1").Message)); } }
public void ErrorFileShouldSaveParseContext() { var lib = new TagLib(); lib.Register(new Tags.Tiles()); lib.Register(new Sharp()); var factory = new FileLocatorFactory().CloneForTagLib(lib) as FileLocatorFactory; try { new TilesSet(); var tile = new TemplateTile( "test", factory.Handle("errorinfile.htm", true), null ); } catch (TemplateExceptionWithContext TEWC) { Assert.That(TEWC.Context, Is.Not.Null); Assert.That(TEWC.Context.LineNumber, Is.EqualTo(2)); string fullPath = Path.GetFullPath("errorinfile.htm"); TagException tagException = TagException.UnbalancedCloseingTag(new ForEach() { Group = new Core() }, new If() { Group = new Core() }).Decorate(TEWC.Context); Assert.That(TEWC.Message, Is.EqualTo(TemplateExceptionWithContext.ErrorInTemplate(fullPath, tagException).Message)); } }
private static XPathNavigator GuardSingleNode(TagModel model, XPathNodeIterator nodes, ITagAttribute select) { if (nodes.Count > 1) { throw TagException.SingleNodeExpected(GetAsString(select, model)).Decorate(select.Context); } nodes.MoveNext(); return(nodes.Current); }
public ITag Get(string name, ParseContext context) { var tag = _tags.FirstOrDefault(t => t.TagName.Equals(name)); if (tag != null) { return(tag); } throw TagException.UnkownTag(name).Decorate(context); }
protected override ITag ParseTagType() { var tag = base.ParseTagType(); if (tag == null) { throw TagException.UnkownTag(_helper.Current.Contents, TagNames).Decorate(_helper.Current); } return(tag); }
public void AddNestedTag(ITag tag) { if (tag is Param) { _nestedTags.Add((Param)tag); } else { throw TagException.OnlyNestedTagsOfTypeAllowed(tag.GetType(), typeof(Param)).Decorate(tag.Context); } }
public void TestParseAndEvaluationOfNotNestedTagWhenNoneExpected() { try { Base().Parse("<c:remove var=\"text\"><c:remove var=\"text\"/></c:remove>"); Assert.Fail("Expected exception"); } catch (TagException Te) { Assert.That(Te.MessageWithOutContext, Is.EqualTo(TagException.ExpectedCloseTag(typeof(Remove)).Message)); } }
public void AddNestedTag(ITag tag) { if (tag is When || tag is Otherwise) { _nestedTags.Add(tag); } else { throw TagException.OnlyNestedTagsOfTypeAllowed(tag.GetType(), typeof(When), typeof(Otherwise)). Decorate(tag.Context); } }
public void TestParseOfDoubleAttribute() { try { Base().Parse( "<c:if test=\"true\" test=\"true\">Y</c:if>"); Assert.Fail("Expected exception"); } catch (TagException Te) { Assert.That(Te.MessageWithOutContext, Is.EqualTo(TagException.PropertyAlReadySet("Test").Message)); } }
public static XPathDocument GetAsXmlDocument(ITagAttribute attribute, TagModel model, string fallBack) { XPathDocument xDoc = null; object raw = Get(attribute, model) ?? fallBack; if (raw == null) { return(xDoc); } if (raw is XPathDocument) { xDoc = (XPathDocument)raw; } else if (raw is string) { if (!String.IsNullOrEmpty((string)raw)) { var rawAsString = (string)raw; using (var textReader = new StringReader(rawAsString.Trim())) { xDoc = new XPathDocument(textReader); } } else { xDoc = null; } } else if (raw is Stream) { xDoc = new XPathDocument((Stream)raw); } else if (raw is TextReader) { xDoc = new XPathDocument((TextReader)raw); } else if (raw is XmlReader) { xDoc = new XPathDocument((XmlReader)raw); } else { throw TagException.UnsupportedInput(raw.GetType(), typeof(XPathDocument), typeof(string), typeof(Stream), typeof(TextReader), typeof(XmlReader)).Decorate(attribute.Context); } return(xDoc); }
public void TestRedirectNoRepsonseSet() { var url = new Redirect(); url.Url = new MockAttribute(new Constant("www.sharptiles.org")); try { url.Evaluate(new TagModel(this)); } catch (TagException Te) { Assert.AreEqual(Te.Message, TagException.HttpResponseNotAvailable().Message); } }
public string Evaluate(TagModel model) { try { XPathNodeIterator result = XmlHelper.GetAndEvaluate(Source, Select, model); var escapeXml = GetAutoValueAsBool("EscapeXml", model); string resultStr = result != null?CollectionUtils.ToString(result, info) : String.Empty; return(escapeXml ? StringUtils.EscapeXml(resultStr) : resultStr); } catch (XPathException XPe) { throw TagException.IllegalXPath(XPe).Decorate(Context); } }
public void TestParseOfMissingRequiredttribute() { try { Base().Parse( "<c:if>Y</c:if>"); Assert.Fail("Expected exception"); } catch (TagException Te) { Assert.That(Te.MessageWithOutContext, Is.EqualTo(TagException.MissingRequiredAttribute(typeof(If), "Test").Message)); } }
public override object InternalEvaluate(TagModel model) { IResourceBundle bundle = null; string key = GetAsString(Key, model); bundle = GetBundle(model); object[] i18nparams = GetParams(model); try { return(bundle.Get(key, model, i18nparams)); } catch (Exception e) { throw TagException.EvaluationMessageError(key, e).Decorate(Context); } }
public void Should_Throw_Correct_Error_Message_On_Invalid_Char_After_Opening() { const string TEMPLATE = "<choose>< otherwise></otherwise></choose>"; var model = new TestModel(); try { var formatter = new Formatter(TEMPLATE).SwitchToMode(TagLibMode.StrictResolve).Parse(); Assert.Fail("Expected exception"); } catch (TagException Te) { Assert.That(Te.MessageWithOutContext, Text.StartsWith(TagException.ExpectedTagOrGroupName(" ").Message)); } }
public void Should_Throw_Correct_Error_Message_On_Unknown_Tag() { const string TEMPLATE = "<choose><unknown></unknown><otherwise></otherwise></choose>"; var model = new TestModel(); try { var formatter = new Formatter(TEMPLATE).SwitchToMode(TagLibMode.RelaxedResolve).Parse(); Assert.Fail("Expected exception"); } catch (TagException Te) { Assert.That(Te.MessageWithOutContext, Text.StartsWith(TagException.UnkownTag("unknown").Message)); } }
public void BadTag() { var tag = new Choose(); var outTag = new Out(); try { tag.AddNestedTag(outTag); } catch (TagException Te) { Assert.AreEqual(Te.Message, TagException.OnlyNestedTagsOfTypeAllowed(typeof(Out), typeof(When), typeof(When))); } }
public void TestDoubleAttribute() { var model = new TagModel(new Hashtable()); model.Page[Html.PAGE_MODEL_HTMLHELPER_INSTANCE] = GetHtmlHelper(); model.Model["name"] = "name"; try { CreateFactory().Parse("<html:textBox name=\"${Model.name}\" name=\"${Model.name}\"/>"); Assert.Fail("Expected exception"); } catch (TagException Te) { Assert.That(Te.MessageWithOutContext, Is.EqualTo(TagException.PropertyAlReadySet("name").Message)); } }
public void CheckUrlRequired() { var tag = new Url(); try { RequiredAttribute.Check(tag); Assert.Fail("Expected exception"); } catch (TagException Te) { Assert.That(Te.Message, Is.EqualTo(TagException.MissingRequiredAttribute(typeof(Url), "Value").Message)); } tag.Value = new MockAttribute(new Constant("www.sharptiles.org")); RequiredAttribute.Check(tag); }
public void CheckWhenRequired() { var tag = new When(); try { RequiredAttribute.Check(tag); Assert.Fail("Expected exception"); } catch (TagException Te) { Assert.That(Te.Message, Is.EqualTo(TagException.MissingRequiredAttribute(typeof(When), "Test").Message)); } tag.Test = new MockAttribute(new Property("True")); RequiredAttribute.Check(tag); }
public void TestSimpleParse_ExplicitExact_WrongFormat() { var date = new ParseDate(); date.Value = new MockAttribute(new Constant("1/6/2008 12:00:00")); date.Exact = new MockAttribute(new Constant("true")); try { date.Evaluate(_model); Assert.Fail("Should fail"); } catch (TagException Te) { Assert.That(Te.Message, Is.EqualTo(TagException.ParseException("1/6/2008 12:00:00", "Date").Message)); } }
public void CheckRequired() { var tag = new Parse(); try { RequiredAttribute.Check(tag); Assert.Fail("Expected Exception"); } catch (TagException Te) { Assert.That(Te.Message, Is.EqualTo(TagException.MissingRequiredAttribute(typeof(Parse), "Var").Message)); } tag.Var = new MockAttribute(new Constant("a")); RequiredAttribute.Check(tag); }
public void TestGetOfMessageNoBundle() { var model = new TagModel(new object(), new MockSessionState()); var tag = new Message(); tag.Key = new MockAttribute(new Constant("c")); try { tag.Evaluate(model); Assert.Fail("Expected exception"); } catch (TagException Te) { Assert.That(TagException.NoResourceBundleFoundInTagScope().Message, Is.EqualTo(Te.MessageWithOutContext)); } }
public void CheckSelectWrongXpathSyntax() { var tag = new Out(); tag.Select = new MockAttribute(new Constant("//note[@id=\"3\"/body")); tag.Source = new MockAttribute(new Constant("xml")); try { Assert.That(tag.Evaluate(_model), Is.EqualTo(String.Empty)); Assert.Fail("Expected exception"); } catch (TagException XPe) { Assert.That(XPe.Message.StartsWith(TagException.IllegalXPath(new Exception("")).Message), Is.True); } }
public void TestParseAndEvaluationOfNotAllowedNestedTag() { try { Base().Parse( "<c:choose><c:when test=\"true\">a</c:when><c:when test=\"true\">b</c:when><c:out>c</c:out></c:choose>"); Assert.Fail("Expected exception"); } catch (TagException Te) { Assert.That(Te.MessageWithOutContext, Is.EqualTo( TagException.OnlyNestedTagsOfTypeAllowed(typeof(Out), typeof(When), typeof(Otherwise)) .Message)); } }
public void CheckRequired() { var tag = new ForEach(); try { RequiredAttribute.Check(tag); Assert.Fail("Expected exception"); } catch (TagException Te) { Assert.That(Te.Message, Is.EqualTo(TagException.MissingRequiredAttribute(typeof(ForEach), "Items").Message)); } tag.Items = new MockAttribute(new Property("AList")); RequiredAttribute.Check(tag); }