private static void ReadRegisterTransform(XmlNode node, RewriterConfiguration config) { // Type attribute. XmlNode typeNode = node.Attributes[Constants.AttrTransform]; if (typeNode == null) { throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.AttributeRequired, Constants.AttrTransform), node); } if (node.ChildNodes.Count > 0) { throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNoElements, Constants.ElementRegister), node); } // Transform type specified. Create an instance and add it // as the mapper handler for this map. IRewriteTransform handler = TypeHelper.Activate(typeNode.Value, null) as IRewriteTransform; if (handler != null) { config.TransformFactory.AddTransform(handler); } else { // TODO: Error due to type. } }
/// <summary> /// Adds a transform. /// </summary> /// <param name="transform">The transform object.</param> public void AddTransform(IRewriteTransform transform) { if (transform == null) { throw new ArgumentNullException("transform"); } _transforms.Add(transform.Name, transform); }
/* * /// <summary> * /// Adds a transform. * /// </summary> * /// <param name="transformType">The type of the transform.</param> * public void AddTransform(string transformType) * { * AddTransform((IRewriteTransform)TypeHelper.Activate(transformType, null)); * } */ /// <summary> /// Adds a transform. /// </summary> /// <param name="transform">The transform object.</param> public void Add(IRewriteTransform transform) { if (transform == null) { throw new ArgumentNullException("transform"); } _transforms.Add(transform.Name, transform); }
private static void ReadRegisterTransform(XmlNode node, RewriterConfiguration config) { if (node.ChildNodes.Count > 0) { throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNoElements, Constants.ElementRegister), node); } string type = node.GetRequiredAttribute(Constants.AttrTransform); // Transform type specified. // Create an instance and add it as the mapper handler for this map. IRewriteTransform transform = TypeHelper.Activate(type, null) as IRewriteTransform; if (transform == null) { throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.InvalidTypeSpecified, type, typeof(IRewriteTransform)), node); } config.TransformFactory.AddTransform(transform); }
private string Reduce(RewriteContext context, StringReader reader) { string result; char ch = (char)reader.Read(); if (char.IsDigit(ch)) { string num = ch.ToString(); if (char.IsDigit((char)reader.Peek())) { ch = (char)reader.Read(); num += ch.ToString(); } if (context.LastMatch != null) { Group group = context.LastMatch.Groups[Convert.ToInt32(num)]; result = @group != null ? @group.Value : string.Empty; } else { result = string.Empty; } } else { switch (ch) { case '<': { string expr; using (StringWriter writer = new StringWriter()) { ch = (char)reader.Read(); while (ch != '>' && ch != (char)65535) { if (ch == '$') { writer.Write(this.Reduce(context, reader)); } else { writer.Write(ch); } ch = (char)reader.Read(); } expr = writer.GetStringBuilder().ToString(); } if (context.LastMatch != null) { Group group = context.LastMatch.Groups[expr]; if (@group != null) { result = @group.Value; } else { result = string.Empty; } } else { result = string.Empty; } } break; case '{': { string expr; bool isMap = false; bool isFunction = false; using (StringWriter writer = new StringWriter()) { ch = (char)reader.Read(); while (ch != '}' && ch != (char)65535) { if (ch == '$') { writer.Write(this.Reduce(context, reader)); } else { if (ch == ':') { isMap = true; } else if (ch == '(') { isFunction = true; } writer.Write(ch); } ch = (char)reader.Read(); } expr = writer.GetStringBuilder().ToString(); } if (isMap) { Match match = Regex.Match(expr, @"^([^\:]+)\:([^\|]+)(\|(.+))?$"); string mapName = match.Groups[1].Value; string mapArgument = match.Groups[2].Value; string mapDefault = match.Groups[4].Value; result = this._configuration.TransformFactory.GetTransform(mapName).ApplyTransform( mapArgument) ?? mapDefault; } else if (isFunction) { Match match = Regex.Match(expr, @"^([^\(]+)\((.+)\)$"); string functionName = match.Groups[1].Value; string functionArgument = match.Groups[2].Value; IRewriteTransform tx = this._configuration.TransformFactory.GetTransform(functionName); result = tx != null?tx.ApplyTransform(functionArgument) : expr; } else { result = context.Properties[expr]; } } break; default: result = ch.ToString(); break; } } return(result); }
private string Reduce(IRewriteContext context, StringReader reader) { string result; char ch = (char)reader.Read(); if (Char.IsDigit(ch)) { string num = ch.ToString(); if (Char.IsDigit((char)reader.Peek())) { ch = (char)reader.Read(); num += ch.ToString(); } if (context.LastMatch != null) { Group group = context.LastMatch.Groups[Convert.ToInt32(num)]; result = (group == null) ? String.Empty : group.Value; } else { result = String.Empty; } } else if (ch == '<') { string expr; using (StringWriter writer = new StringWriter()) { ch = (char)reader.Read(); while (ch != '>' && ch != EndChar) { if (ch == '$') { writer.Write(Reduce(context, reader)); } else { writer.Write(ch); } ch = (char)reader.Read(); } expr = writer.GetStringBuilder().ToString(); } if (context.LastMatch != null) { Group group = context.LastMatch.Groups[expr]; result = (group == null) ? String.Empty : group.Value; } else { result = String.Empty; } } else if (ch == '{') { string expr; bool isMap = false; bool isFunction = false; using (StringWriter writer = new StringWriter()) { ch = (char)reader.Read(); while (ch != '}' && ch != EndChar) { if (ch == '$') { writer.Write(Reduce(context, reader)); } else { if (ch == ':') { isMap = true; } else if (ch == '(') { isFunction = true; } writer.Write(ch); } ch = (char)reader.Read(); } expr = writer.GetStringBuilder().ToString(); } if (isMap) { Match match = Regex.Match(expr, @"^([^\:]+)\:([^\|]+)(\|(.+))?$"); string mapName = match.Groups[1].Value; string mapArgument = match.Groups[2].Value; string mapDefault = match.Groups[4].Value; IRewriteTransform tx = _configuration.TransformFactory.GetTransform(mapName); if (tx == null) { throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.MappingNotFound, mapName)); } result = tx.ApplyTransform(mapArgument) ?? mapDefault; } else if (isFunction) { Match match = Regex.Match(expr, @"^([^\(]+)\((.+)\)$"); string functionName = match.Groups[1].Value; string functionArgument = match.Groups[2].Value; IRewriteTransform tx = _configuration.TransformFactory.GetTransform(functionName); if (tx == null) { throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.TransformFunctionNotFound, functionName)); } result = tx.ApplyTransform(functionArgument); } else { result = context.Properties[expr]; } } else { result = ch.ToString(); } return(result); }