Example #1
0
 /// <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;
 }
Example #2
0
 /// <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();
     }
 }
Example #3
0
            /// <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)
            {
                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;

                // collect a list of attributes, we will check to see if we use them all.
                _unprocessedAttributes = new StringCollection();
                foreach (XmlAttribute attribute in elementNode.Attributes) {
                    _unprocessedAttributes.Add(attribute.Name);
                }

                // create collection of node names
                _unprocessedChildNodes = new StringCollection();
                foreach (XmlNode childNode in elementNode) {
                    // skip non-ciscript namespace elements and special elements like comments, pis, text, etc.
                    if (!(childNode.NodeType == XmlNodeType.Element) || !childNode.NamespaceURI.Equals(NamespaceManager.LookupNamespace("ciscript"))) {
                        continue;
                    }

                    // skip existing names as we only need unique names.
                    if (_unprocessedChildNodes.Contains(childNode.Name)) {
                        continue;
                    }

                    _unprocessedChildNodes.Add(childNode.Name);
                }
            }
Example #4
0
        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 = parent.Properties.ExpandProperties(childNode.Attributes["refid"].Value, buildElement.Location);

                if (!StringUtils.IsNullOrEmpty(dataType.ID)) {
                    // throw exception because of id and ref
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                        "Datatype references cannot contain an id attribute."),
                        dataType.Location);
                }

                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,
                        "{0} reference '{1}' not defined.", dataType.Name, dataType.RefID),
                        dataType.Location);
                }
                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,
                            "Attempting to use a <{0}> reference where a <{1}> is required.",
                            dataTypeAttr.Name, elementTypeAttr.Name), Location.UnknownLocation);
                    }
                }
                // 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;
            }
        }
Example #5
0
 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);
     }
 }
Example #6
0
                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,
                            "An empty string is not a valid value for attribute"
                            + " '{0}' of <{1} ... />.",
                            attributeNode.Name, parent.Name), parent.Location);
                    }
                }
Example #7
0
                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);
                    }
                }
Example #8
0
                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,
                            "\"{0}\" is not a valid encoding.",
                            encodingName), parent.Location);
                    } catch (NotSupportedException) {
                        throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                            "\"{0}\" encoding is not supported on the current platform.",
                            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);
                    }
                }