예제 #1
0
 /// <summary>
 /// Resolves the given value taken from an object definition according to its type
 /// </summary>
 /// <param name="value">the value to resolve</param>
 /// <returns>the resolved value</returns>
 protected virtual object ResolveValue(object value)
 {
     if (value is IObjectDefinition)
     {
         VisitObjectDefinition((IObjectDefinition)value);
     }
     else if (value is ObjectDefinitionHolder)
     {
         VisitObjectDefinition(((ObjectDefinitionHolder)value).ObjectDefinition);
     }
     else if (value is RuntimeObjectReference)
     {
         RuntimeObjectReference ror = (RuntimeObjectReference)value;
         //name has to be of string type.
         string newObjectName = ResolveStringValue(ror.ObjectName);
         if (!newObjectName.Equals(ror.ObjectName))
         {
             return(new RuntimeObjectReference(newObjectName));
         }
     }
     else if (value is ManagedList)
     {
         VisitManagedList((ManagedList)value);
     }
     else if (value is ManagedSet)
     {
         VisitManagedSet((ManagedSet)value);
     }
     else if (value is ManagedDictionary)
     {
         VisitManagedDictionary((ManagedDictionary)value);
     }
     else if (value is NameValueCollection)
     {
         VisitNameValueCollection((NameValueCollection)value);
     }
     else if (value is TypedStringValue)
     {
         TypedStringValue typedStringValue = (TypedStringValue)value;
         String           stringValue      = typedStringValue.Value;
         if (stringValue != null)
         {
             String visitedString = ResolveStringValue(stringValue);
             typedStringValue.Value = visitedString;
         }
     }
     else if (value is string)
     {
         return(ResolveStringValue((string)value));
     }
     else if (value is ExpressionHolder)
     {
         ExpressionHolder holder = (ExpressionHolder)value;
         string           newExpressionString = ResolveStringValue(holder.ExpressionString);
         return(new ExpressionHolder(newExpressionString));
     }
     return(value);
 }
        /// <summary>
        /// Gets a dictionary definition.
        /// </summary>
        /// <param name="mapEle">The element describing the dictionary definition.</param>
        /// <param name="name">The name of the object (definition) associated with the dictionary definition.</param>
        /// <param name="parserContext">The namespace-aware parser.</param>
        /// <returns>The dictionary definition.</returns>
        protected IDictionary ParseDictionaryElement(XmlElement mapEle, string name, ParserContext parserContext)
        {
            ManagedDictionary dictionary = new ManagedDictionary();
            string keyTypeName = GetAttributeValue(mapEle, "key-type");
            string valueTypeName = GetAttributeValue(mapEle, "value-type");
            if (StringUtils.HasText(keyTypeName))
            {
                dictionary.KeyTypeName = keyTypeName;
            }
            if (StringUtils.HasText(valueTypeName))
            {
                dictionary.ValueTypeName = valueTypeName;
            }
            dictionary.MergeEnabled = ParseMergeAttribute(mapEle, parserContext.ParserHelper);

            XmlNodeList entryElements = SelectNodes(mapEle, ObjectDefinitionConstants.EntryElement);
            foreach (XmlElement entryEle in entryElements)
            {
                #region Key

                object key = null;

                XmlAttribute keyAtt = entryEle.Attributes[ObjectDefinitionConstants.KeyAttribute];
                if (keyAtt != null)
                {
                    key = keyAtt.Value;
                }
                else
                {
                    // ok, we're not using the 'key' attribute; lets check for the ref shortcut...
                    XmlAttribute keyRefAtt = entryEle.Attributes[ObjectDefinitionConstants.DictionaryKeyRefShortcutAttribute];
                    if (keyRefAtt != null)
                    {
                        key = new RuntimeObjectReference(keyRefAtt.Value);
                    }
                    else
                    {
                        // so check for the 'key' element...
                        XmlNode keyNode = SelectSingleNode(entryEle, ObjectDefinitionConstants.KeyElement);
                        if (keyNode == null)
                        {
                            throw new ObjectDefinitionStoreException(
                                parserContext.ReaderContext.Resource, name,
                                string.Format("One of either the '{0}' element, or the the '{1}' or '{2}' attributes " +
                                              "is required for the <{3}/> element.",
                                              ObjectDefinitionConstants.KeyElement,
                                              ObjectDefinitionConstants.KeyAttribute,
                                              ObjectDefinitionConstants.DictionaryKeyRefShortcutAttribute,
                                              ObjectDefinitionConstants.EntryElement));
                        }
                        XmlElement keyElement = (XmlElement)keyNode;
                        XmlNodeList keyNodes = keyElement.GetElementsByTagName("*");
                        if (keyNodes == null || keyNodes.Count == 0)
                        {
                            throw new ObjectDefinitionStoreException(
                                parserContext.ReaderContext.Resource, name,
                                string.Format("Malformed <{0}/> element... the value of the key must be " +
                                    "specified as a child value-style element.",
                                              ObjectDefinitionConstants.KeyElement));
                        }
                        key = ParsePropertySubElement((XmlElement)keyNodes.Item(0), name, parserContext);
                    }
                }

                #endregion

                #region Value

                XmlAttribute inlineValueAtt = entryEle.Attributes[ObjectDefinitionConstants.ValueAttribute];
                if (inlineValueAtt != null)
                {
                    // ok, we're using the value attribute shortcut...
                    dictionary[key] = inlineValueAtt.Value;
                }
                else if (entryEle.Attributes[ObjectDefinitionConstants.DictionaryValueRefShortcutAttribute] != null)
                {
                    // ok, we're using the value-ref attribute shortcut...
                    XmlAttribute inlineValueRefAtt = entryEle.Attributes[ObjectDefinitionConstants.DictionaryValueRefShortcutAttribute];
                    RuntimeObjectReference ror = new RuntimeObjectReference(inlineValueRefAtt.Value);
                    dictionary[key] = ror;
                }
                else if (entryEle.Attributes[ObjectDefinitionConstants.ExpressionAttribute] != null)
                {
                    // ok, we're using the expression attribute shortcut...
                    XmlAttribute inlineExpressionAtt = entryEle.Attributes[ObjectDefinitionConstants.ExpressionAttribute];
                    ExpressionHolder expHolder = new ExpressionHolder(inlineExpressionAtt.Value);
                    dictionary[key] = expHolder;
                }
                else
                {
                    XmlNode keyNode = SelectSingleNode(entryEle, ObjectDefinitionConstants.KeyElement);
                    if (keyNode != null)
                    {
                        entryEle.RemoveChild(keyNode);
                    }
                    // ok, we're using the original full-on value element...
                    XmlNodeList valueElements = entryEle.GetElementsByTagName("*");
                    if (valueElements == null || valueElements.Count == 0)
                    {
                        throw new ObjectDefinitionStoreException(
                            parserContext.ReaderContext.Resource, name,
                            string.Format("One of either the '{0}' or '{1}' attributes, or a value-style element " +
                                "is required for the <{2}/> element.",
                                          ObjectDefinitionConstants.ValueAttribute, ObjectDefinitionConstants.DictionaryValueRefShortcutAttribute, ObjectDefinitionConstants.EntryElement));
                    }
                    dictionary[key] = ParsePropertySubElement((XmlElement)valueElements.Item(0), name, parserContext);
                }

                #endregion
            }
            return dictionary;
        }
 private object ParseExpressionElement(XmlElement element, string name, ParserContext parserContext)
 {
     string expression = GetAttributeValue(element, ObjectDefinitionConstants.ValueAttribute);
     ExpressionHolder holder = new ExpressionHolder(expression);
     holder.Properties = ParsePropertyElements(name, element, parserContext);
     return holder;
 }