/// <summary> /// Initializes a new instance of the <see cref="Element" /> class /// from the specified element. /// </summary> /// <param name="e">The element that should be used to create a new instance of the <see cref="Element" /> class.</param> protected Element(Element e) : this() { _location = e._location; _project = e._project; _xmlNode = e._xmlNode; _nsMgr = e._nsMgr; }
/// <summary> /// Copies all instance data of the <see cref="Element" /> to a given /// <see cref="Element" />. /// </summary> protected void CopyTo(Element clone) { clone._location = _location; clone._nsMgr = _nsMgr; clone._parent = _parent; clone._project = _project; if (_xmlNode != null) { clone._xmlNode = _xmlNode.Clone(); } }
/// <summary> /// Initializes a new instance of the <see cref="AttributeConfigurator" /> /// class for the given <see cref="Element" />. /// </summary> /// <param name="element">The <see cref="Element" /> for which an <see cref="AttributeConfigurator" /> should be created.</param> /// <param name="elementNode">The <see cref="XmlNode" /> to initialize the <see cref="Element" /> with.</param> /// <param name="properties">The <see cref="PropertyDictionary" /> to use for property expansion.</param> /// <param name="targetFramework">The framework that the <see cref="Element" /> should target.</param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="element" /> is <see langword="null" />.</para> /// <para>-or-</para> /// <para><paramref name="elementNode" /> is <see langword="null" />.</para> /// <para>-or-</para> /// <para><paramref name="properties" /> is <see langword="null" />.</para> /// </exception> public AttributeConfigurator(Element element, XmlNode elementNode, PropertyDictionary properties, FrameworkInfo targetFramework) { if (element == null) { throw new ArgumentNullException("element"); } if (elementNode == null) { throw new ArgumentNullException("elementNode"); } if (properties == null) { throw new ArgumentNullException("properties"); } _element = element; _elementXml = elementNode; _properties = properties; _targetFramework = targetFramework; // collect a list of attributes, we will check to see if we use them all. _unprocessedAttributes = new StringCollection(); foreach (XmlAttribute attribute in elementNode.Attributes) { // skip non-nant namespace attributes if (attribute.NamespaceURI.Length > 0 && !attribute.NamespaceURI.Equals(NamespaceManager.LookupNamespace("nant")) ) { continue; } _unprocessedAttributes.Add(attribute.Name); } // create collection of node names _unprocessedChildNodes = new StringCollection(); foreach (XmlNode childNode in elementNode) { // skip non-nant namespace elements and special elements like comments, pis, text, etc. if (!(childNode.NodeType == XmlNodeType.Element) || !childNode.NamespaceURI.Equals(NamespaceManager.LookupNamespace("nant"))) { continue; } // skip existing names as we only need unique names. if (_unprocessedChildNodes.Contains(childNode.Name)) { continue; } _unprocessedChildNodes.Add(childNode.Name); } }
public void Set(XmlNode attributeNode, Element parent, PropertyInfo property, string value) { string uri = StringUtils.ConvertEmptyToNull(value); if (uri != null) { Uri propertyValue; // if uri does not contain a scheme, we'll consider it // to be a normal path and as such we need to resolve // it to an absolute path (relative to project base // directory if (value.IndexOf(Uri.SchemeDelimiter) == -1) { uri = parent.Project.GetFullPath(value); } try { propertyValue = new Uri(uri); } catch (Exception ex) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1022"), value, attributeNode.Name, parent.Name), parent.Location, ex); } try { property.SetValue(parent, propertyValue, BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture); } catch (Exception ex) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1022"), value, attributeNode.Name, parent.Name), parent.Location, ex); } } else { throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1193"), attributeNode.Name, parent.Name), parent.Location); } }
public static Element InitializeBuildElement(Element parent, XmlNode childNode, Element buildElement, Type elementType) { // if subtype of DataTypeBase DataTypeBase dataType = buildElement as DataTypeBase; if (dataType != null && dataType.CanBeReferenced && childNode.Attributes["refid"] != null ) { dataType.RefID = childNode.Attributes["refid"].Value; if (!StringUtils.IsNullOrEmpty(dataType.ID)) { // throw exception because of id and ref throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1183")), parent.Project.LocationMap.GetLocation(childNode)); } if (parent.Project.DataTypeReferences.Contains(dataType.RefID)) { dataType = parent.Project.DataTypeReferences[dataType.RefID]; // clear any instance specific state dataType.Reset(); } else { // reference not found exception throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1184"), dataType.Name, dataType.RefID), parent.Project.LocationMap.GetLocation(childNode)); } if (!elementType.IsAssignableFrom(dataType.GetType())) { // see if we have a valid copy constructor ConstructorInfo constructor = elementType.GetConstructor(new Type[] {dataType.GetType()}); if (constructor != null) { dataType = (DataTypeBase) constructor.Invoke(new object[] {dataType}); } else { ElementNameAttribute dataTypeAttr = (ElementNameAttribute) Attribute.GetCustomAttribute(dataType.GetType(), typeof(ElementNameAttribute)); ElementNameAttribute elementTypeAttr = (ElementNameAttribute) Attribute.GetCustomAttribute(elementType, typeof(ElementNameAttribute)); // throw error wrong type definition throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1185"), dataTypeAttr.Name, elementTypeAttr.Name), parent.Project.LocationMap.GetLocation(childNode)); } } // re-initialize the object with current context dataType.Project = parent.Project; dataType.Parent = parent; dataType.NamespaceManager = parent.NamespaceManager; dataType.Location = parent.Project.LocationMap.GetLocation(childNode); // return initialized data type return dataType; } else { // initialize the object with context buildElement.Project = parent.Project; buildElement.Parent = parent; buildElement.NamespaceManager = parent.NamespaceManager; // initialize element from XML buildElement.Initialize(childNode); // return initialize build element return buildElement; } }
public void Set(XmlNode attributeNode, Element parent, PropertyInfo property, string value) { string path = StringUtils.ConvertEmptyToNull(value); if (path != null) { object propertyValue; try { propertyValue = new FileInfo(parent.Project.GetFullPath(value)); } catch (Exception ex) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1022"), value, attributeNode.Name, parent.Name), parent.Location, ex); } try { property.SetValue(parent, propertyValue, BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture); } catch (Exception ex) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1022"), value, attributeNode.Name, parent.Name), parent.Location, ex); } } else { throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1193"), attributeNode.Name, parent.Name), parent.Location); } }
public void Set(XmlNode attributeNode, Element parent, PropertyInfo property, string value) { try { PathSet propertyValue = new PathSet(parent.Project, value); property.SetValue(parent, propertyValue, BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture); } catch (Exception ex) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1022"), value, attributeNode.Name, parent.Name), parent.Location, ex); } }
public void Set(XmlNode attributeNode, Element parent, PropertyInfo property, string value) { try { object propertyValue; // check for more specific type converter TypeConverter tc = TypeDescriptor.GetConverter(property.PropertyType); if (!(tc.GetType() == typeof(EnumConverter))) { propertyValue = tc.ConvertFrom(value); } else { propertyValue = Enum.Parse(property.PropertyType, value); } property.SetValue(parent, propertyValue, BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture); } catch (FormatException) { throw CreateBuildException(attributeNode, parent, property, value); } catch (ArgumentException) { throw CreateBuildException(attributeNode, parent, property, value); } }
private BuildException CreateBuildException(XmlNode attributeNode, Element parent, PropertyInfo property, string value) { StringBuilder sb = new StringBuilder(); foreach (object field in Enum.GetValues(property.PropertyType)) { if (sb.Length > 0) { sb.Append(", "); } sb.Append(field.ToString()); } string message = string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1023"), value, attributeNode.Name, parent.Name, sb.ToString()); return new BuildException(message, parent.Location); }
public void Set(XmlNode attributeNode, Element parent, PropertyInfo property, string value) { string encodingName = StringUtils.ConvertEmptyToNull(value); if (encodingName == null) { return; } Encoding encoding = null; try { encoding = System.Text.Encoding.GetEncoding( encodingName); } catch (ArgumentException) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1191"), encodingName), parent.Location); } catch (NotSupportedException) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1192"), encodingName), parent.Location); } try { property.SetValue(parent, encoding, BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture); } catch (Exception ex) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1022"), value, attributeNode.Name, parent.Name), parent.Location, ex); } }
public static Filter CreateFilter(XmlNode elementNode, Element parent) { if (elementNode == null) { throw new ArgumentNullException("elementNode"); } if (parent == null) { throw new ArgumentNullException("parent"); } string filterName = elementNode.Name; FilterBuilder builder = FilterBuilders[filterName]; if (builder == null) { Location location = parent.Project.LocationMap.GetLocation(elementNode); throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1082"), filterName), location); } Filter filter = (Filter) builder.CreateFilter(); filter.Parent = parent; filter.Project = parent.Project; filter.NamespaceManager = parent.Project.NamespaceManager; filter.Initialize(elementNode); // check whether the type (or its base class) is deprecated ObsoleteAttribute obsoleteAttribute = (ObsoleteAttribute) Attribute.GetCustomAttribute(filter.GetType(), typeof(ObsoleteAttribute), true); if (obsoleteAttribute != null) { Location location = parent.Project.LocationMap.GetLocation(elementNode); string obsoleteMessage = string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1079"), filterName, obsoleteAttribute.Message); if (obsoleteAttribute.IsError) { throw new BuildException(obsoleteMessage, location); } else { parent.Project.Log(Level.Warning, "{0} {1}", location, obsoleteMessage); } } return filter; }
public ConditionalConfigurator(Element element, XmlNode elementNode, PropertyDictionary properties, FrameworkInfo targetFramework) : base(element, elementNode, properties, targetFramework) { IConditional conditional = element as IConditional; if (conditional == null) return; Type currentType = element.GetType(); BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance; PropertyInfo ifdefined = currentType.GetProperty("IfDefined", flags); InitializeAttribute(ifdefined); if (!conditional.IfDefined) { _enabled = false; } else { PropertyInfo unlessDefined = currentType.GetProperty("UnlessDefined", flags); InitializeAttribute(unlessDefined); _enabled = !conditional.UnlessDefined; } if (!_enabled) { // since we will not be processing other attributes or // child nodes, clear these collections to avoid // errors for unrecognized attributes/elements UnprocessedAttributes.Clear(); UnprocessedChildNodes.Clear(); } }
public LoopItemsConfigurator(Element element, XmlNode elementNode, PropertyDictionary properties, FrameworkInfo targetFramework) : base(element, elementNode, properties, targetFramework) { }
public void Set(XmlNode attributeNode, Element parent, PropertyInfo property, string value) { try { object propertyValue; // check for more specific type converter TypeConverter tc = TypeDescriptor.GetConverter(property.PropertyType); if (!(tc.GetType() == typeof(EnumConverter))) { propertyValue = tc.ConvertFrom(value); } else { propertyValue = Enum.Parse(property.PropertyType, value); } property.SetValue(parent, propertyValue, BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture); } catch (ArgumentException) { string message = string.Format(CultureInfo.InvariantCulture, "'{0}' is not a valid value for attribute" + " '{1}' of <{2} ... />. Valid values are: ", value, attributeNode.Name, parent.Name); foreach (object field in Enum.GetValues(property.PropertyType)) { message += field.ToString() + ", "; } // strip last , message = message.Substring(0, message.Length - 2); throw new BuildException(message, parent.Location); } }