Example #1
0
        /// <summary>
        /// Looks up the namespace corresponding to an XML namespace prefix
        /// </summary>
        /// <param name="elem">The DependencyObject containing the namespace mappings to be
        ///  used in the lookup</param>
        /// <param name="prefix">The XML namespace prefix to look up</param>
        /// <returns>The namespace corresponding to the given prefix if it exists,
        /// null otherwise</returns>
        public static string LookupNamespace(DependencyObject elem, string prefix)
        {
            if (elem == null)
            {
                throw new ArgumentNullException("elem");
            }
            if (prefix == null)
            {
                throw new ArgumentNullException("prefix");
            }

            // Walk up the parent chain until one of the parents has the passed prefix in
            // its xmlns dictionary, or until a null parent is reached.
            XmlnsDictionary d = GetXmlnsDictionary(elem);

            while ((null == d || null == d[prefix]) && null != elem)
            {
                elem = LogicalTreeHelper.GetParent(elem);
                if (elem != null)
                {
                    d = GetXmlnsDictionary(elem);
                }
            }

            if (null != d)
            {
                return(d[prefix]);
            }
            else
            {
                return(null);
            }
        }
        public object Read(string xaml)
        {
            if (string.IsNullOrEmpty(xaml))
            {
                return(null);
            }
            var node = XamlUtil.GetNode(xaml);

            if (node == null)
            {
                return(null);
            }
            XmlnsDictionary.Collect(node);

            //创建元素
            var obj  = ComponentFactory.Create(node.OriginalName);
            var type = obj as Type;

            if (type != null)
            {
                //是基础类型
                return(DataUtil.ToValue(node.InnerHtml, type));
            }
            else
            {
                Load(obj, node);
            }
            return(obj);
        }
Example #3
0
        /// <summary>
        /// Set value of XmlnsDictionary on the passed DependencyObject
        /// </summary>
        public static void SetXmlnsDictionary(DependencyObject dependencyObject, XmlnsDictionary value)
        {
            if (dependencyObject == null)
            {
                throw new ArgumentNullException("dependencyObject");
            }

            if (dependencyObject.IsSealed == false)
            {
                dependencyObject.SetValue(XmlnsDictionaryProperty, value);
            }
        }
        /// <summary>
        /// 加载xaml文件中的信息到obj中
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="xaml"></param>
        /// <param name="connector"></param>
        public void Load(object obj, string xaml)
        {
            if (obj == null)
            {
                return;
            }
            var node = XamlUtil.GetNode(xaml);

            if (node == null)
            {
                return;
            }
            XmlnsDictionary.Collect(node);
            Load(obj, node);
        }
        public void addNamespace <T>(string name)
        {
            const string pattern = @"^\w[\w\d]*$";

            if (!Regex.IsMatch(name, pattern))
            {
                throw new ArgumentException($"Your namespace name is invalid. must match {pattern}. But you did '{name}' wich does not match exactly!");
            }

            var viewType           = typeof(T);
            var namespaceReference = viewType.Namespace ?? String.Empty;
            var assemblyName       = viewType.Assembly.FullName;

            XamlTypeMapper.AddMappingProcessingInstruction(name, namespaceReference, assemblyName);
            XmlnsDictionary.Add(name, name);
        }
Example #6
0
        /// <summary>
        /// 定位节点对应的xaml组件的类型,注意该方法忽略了x:Class属性,是以xaml根节点的名称为判断依据
        /// </summary>
        /// <param name="componentNode"></param>
        /// <returns></returns>
        public static Type Locate(HtmlNode componentNode)
        {
            if (componentNode == null || componentNode.OriginalName.IndexOf(".") > -1)
            {
                return(null);
            }
            if (componentNode.NodeType == HtmlNodeType.Text)
            {
                return(Run.Type);
            }
            if (componentNode.NodeType == HtmlNodeType.Comment)
            {
                return(Comment.Type);
            }
            var xmlns         = XmlnsDictionary.Collect(componentNode);
            var xamlNamespace = xmlns.GetXamlNamespace(componentNode.OriginalName);

            return(Locate(xamlNamespace, componentNode.LocalOriginalName()));
        }
Example #7
0
        /// <summary>
        ///    Constructor that takes the ParserContext.
        ///    A parserContext object will be built based on this.
        /// </summary>
        /// <param name="parserContext">xmlParserContext to use</param>
        internal ParserContext(ParserContext parserContext)
        {
            _xmlLang        = parserContext.XmlLang;
            _xmlSpace       = parserContext.XmlSpace;
            _xamlTypeMapper = parserContext.XamlTypeMapper;
            _mapTable       = parserContext.MapTable;
            _baseUri        = parserContext.BaseUri;
            _masterBracketCharacterCache = parserContext.MasterBracketCharacterCache;

            _rootElement = parserContext._rootElement;
            if (parserContext._nameScopeStack != null)
            {
                _nameScopeStack = (Stack)parserContext._nameScopeStack.Clone();
            }
            else
            {
                _nameScopeStack = null;
            }

            // Don't want to force the lazy init so we just set privates
            _skipJournaledProperties = parserContext._skipJournaledProperties;

            _xmlnsDictionary = null;

            // when there are no namespace prefix mappings in incoming ParserContext,
            // we are not going to create an empty XmlnsDictionary.
            if (parserContext._xmlnsDictionary != null &&
                parserContext._xmlnsDictionary.Count > 0)
            {
                _xmlnsDictionary = new XmlnsDictionary();

                XmlnsDictionary xmlDictionaryFrom = parserContext.XmlnsDictionary;

                if (null != xmlDictionaryFrom)
                {
                    foreach (string key in xmlDictionaryFrom.Keys)
                    {
                        _xmlnsDictionary[key] = xmlDictionaryFrom[key];
                    }
                }
            }
        }
Example #8
0
        /// <summary>
        /// Construct an XmlnsDictionary based on an already existing one. The Sealed property is not
        /// propagated between dictionaries.
        /// </summary>
        /// <param name="xmlnsDictionary">The dictionary on which to base the new one</param>
        public XmlnsDictionary(XmlnsDictionary xmlnsDictionary)
        {
            if (null == xmlnsDictionary)
            {
                throw new ArgumentNullException("xmlnsDictionary");
            }

            // Copy the Declarations if they exists
            if (xmlnsDictionary != null && xmlnsDictionary.Count > 0)
            {
                _lastDecl = xmlnsDictionary._lastDecl;
                if (_nsDeclarations == null)
                {
                    _nsDeclarations = new NamespaceDeclaration[_lastDecl + 1];
                }

                // Initialize the count to Zero and start counting.
                _countDecl = 0;

                for (int i = 0; i <= _lastDecl; i++)
                {
                    // We copy the entire Dictionary, but update the count only for non-null uri/
                    // The reason is, Count, doesn't make sense when we are implementing the
                    // storage in our own way. We don't remove the namespace declarations when
                    // asked to remove, instead we set their uri values to null.
                    if (xmlnsDictionary._nsDeclarations[i].Uri != null)
                    {
                        _countDecl++;
                    }

                    _nsDeclarations[i].Prefix     = xmlnsDictionary._nsDeclarations[i].Prefix;
                    _nsDeclarations[i].Uri        = xmlnsDictionary._nsDeclarations[i].Uri;
                    _nsDeclarations[i].ScopeCount = xmlnsDictionary._nsDeclarations[i].ScopeCount;
                }
            }
            else // create an empty array of Declarations. Default Size is 8.
            {
                Initialize();
            }
        }
Example #9
0
        /// <summary>
        /// Looks up the XML prefix corresponding to a namespaceUri
        /// </summary>
        /// <param name="elem">The DependencyObject containing the namespace mappings to
        ///  be used in the lookup</param>
        /// <param name="uri">The namespaceUri to look up</param>
        /// <returns>
        /// string.Empty if the given namespace corresponds to the default namespace; otherwise,
        /// the XML prefix corresponding to the given namespace, or null if none exists
        /// </returns>
        public static string LookupPrefix(DependencyObject elem, string uri)
        {
            DependencyObject oldElem = elem;

            if (elem == null)
            {
                throw new ArgumentNullException("elem");
            }
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            // Following the letter of the specification, the default namespace should take
            // precedence in the case of redundancy, so we check that first.
            if (DefaultNamespace(elem) == uri)
            {
                return(string.Empty);
            }

            while (null != elem)
            {
                XmlnsDictionary d = GetXmlnsDictionary(elem);
                if (null != d)
                {
                    // Search through all key/value pairs in the dictionary
                    foreach (DictionaryEntry e in d)
                    {
                        if ((string)e.Value == uri && LookupNamespace(oldElem, (string)e.Key) == uri)
                        {
                            return((string)e.Key);
                        }
                    }
                }
                elem = LogicalTreeHelper.GetParent(elem);
            }
            return(null);
        }
Example #10
0
        /// <summary>
        ///    Constructor that takes the XmlParserContext.
        ///    A parserContext object will be built based on this.
        /// </summary>
        /// <param name="xmlParserContext">xmlParserContext to use</param>

        public ParserContext(XmlParserContext xmlParserContext)
        {
            if (xmlParserContext == null)
            {
                throw new ArgumentNullException("xmlParserContext");
            }

            _xmlLang = xmlParserContext.XmlLang;

            TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(XmlSpace));

            if (typeConverter != null)
            {
                _xmlSpace = (string)typeConverter.ConvertToString(null, TypeConverterHelper.InvariantEnglishUS, xmlParserContext.XmlSpace);
            }
            else
            {
                _xmlSpace = String.Empty;
            }

            _xmlnsDictionary = new XmlnsDictionary();

            if (xmlParserContext.BaseURI != null && xmlParserContext.BaseURI.Length > 0)
            {
                _baseUri = new Uri(xmlParserContext.BaseURI, UriKind.RelativeOrAbsolute);
            }

            XmlNamespaceManager xmlnsManager = xmlParserContext.NamespaceManager;

            if (null != xmlnsManager)
            {
                foreach (string key in xmlnsManager)
                {
                    _xmlnsDictionary.Add(key, xmlnsManager.LookupNamespace(key));
                }
            }
        }
Example #11
0
		XamlContext(ModuleDef module) {
			Module = module;
			NodeMap = new Dictionary<BamlRecord, BamlBlockNode>();
			XmlNs = new XmlnsDictionary();
		}
        /// <summary> 
        /// Set value of XmlnsDictionary on the passed DependencyObject
        /// </summary> 
        public static void SetXmlnsDictionary(DependencyObject dependencyObject, XmlnsDictionary value)
        {
            if (dependencyObject == null)
            { 
                throw new ArgumentNullException( "dependencyObject" );
            } 
 
            if (dependencyObject.IsSealed == false)
            { 
                dependencyObject.SetValue(XmlnsDictionaryProperty, value);
            }
        }
        /// <summary>
        /// Construct an XmlnsDictionary based on an already existing one. The Sealed property is not
        /// propagated between dictionaries.
        /// </summary>
        /// <param name="xmlnsDictionary">The dictionary on which to base the new one</param>
        public XmlnsDictionary(XmlnsDictionary xmlnsDictionary)
        {
            if(null == xmlnsDictionary)
            {
                throw new ArgumentNullException( "xmlnsDictionary" );
            }
            
            // Copy the Declarations if they exists 
            if (xmlnsDictionary != null && xmlnsDictionary.Count > 0)
            {
                _lastDecl = xmlnsDictionary._lastDecl;
                if (_nsDeclarations == null)
                {
                    _nsDeclarations = new NamespaceDeclaration[_lastDecl+1];
                }

                // Initialize the count to Zero and start counting.
                _countDecl = 0;
                
                for (int i = 0; i <= _lastDecl; i++)
                {
                    // We copy the entire Dictionary, but update the count only for non-null uri/
                    // The reason is, Count, doesn't make sense when we are implementing the 
                    // storage in our own way. We don't remove the namespace declarations when 
                    // asked to remove, instead we set their uri values to null.
                    if (xmlnsDictionary._nsDeclarations[i].Uri != null)
                    {    _countDecl++;  }

                    _nsDeclarations[i].Prefix     = xmlnsDictionary._nsDeclarations[i].Prefix;
                    _nsDeclarations[i].Uri        = xmlnsDictionary._nsDeclarations[i].Uri;
                    _nsDeclarations[i].ScopeCount = xmlnsDictionary._nsDeclarations[i].ScopeCount;
                }
            }
            else // create an empty array of Declarations. Default Size is 8. 
            {
                Initialize();
            }
        }
        /// <summary>
        ///    Constructor that takes the ParserContext.
        ///    A parserContext object will be built based on this.
        /// </summary>
        /// <param name="parserContext">xmlParserContext to use</param>
        internal ParserContext(ParserContext parserContext)
        {
            _xmlLang     = parserContext.XmlLang;
            _xmlSpace    = parserContext.XmlSpace;
            _xamlTypeMapper      = parserContext.XamlTypeMapper;
            _mapTable    = parserContext.MapTable;
            _baseUri     = parserContext.BaseUri;

            _rootElement = parserContext._rootElement;
            if (parserContext._nameScopeStack != null)
                _nameScopeStack = (Stack)parserContext._nameScopeStack.Clone();
            else
                _nameScopeStack = null;

            // Don't want to force the lazy init so we just set privates
            _skipJournaledProperties = parserContext._skipJournaledProperties;

            _xmlnsDictionary = null;

            // when there are no namespace prefix mappings in incoming ParserContext,
             // we are not going to create an empty XmlnsDictionary.
            if (parserContext._xmlnsDictionary != null &&
                parserContext._xmlnsDictionary.Count > 0)
            {
                _xmlnsDictionary = new XmlnsDictionary();

                XmlnsDictionary xmlDictionaryFrom = parserContext.XmlnsDictionary;

                if (null != xmlDictionaryFrom)
                {
                    foreach (string key in xmlDictionaryFrom.Keys)
                    {
                        _xmlnsDictionary[key] = xmlDictionaryFrom[key];
                    }
                }
            }
        }
        /// <summary>
        ///    Constructor that takes the XmlParserContext.
        ///    A parserContext object will be built based on this.
        /// </summary>
        /// <param name="xmlParserContext">xmlParserContext to use</param>

        public ParserContext(XmlParserContext xmlParserContext)
        {
            if (xmlParserContext == null)
            {
                throw new ArgumentNullException( "xmlParserContext" );
            }

            _xmlLang     = xmlParserContext.XmlLang;

            TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(XmlSpace));
            if (typeConverter != null)
                _xmlSpace = (string) typeConverter.ConvertToString(null, TypeConverterHelper.InvariantEnglishUS, xmlParserContext.XmlSpace);
            else
                _xmlSpace = String.Empty;

            _xmlnsDictionary = new XmlnsDictionary() ;

            if (xmlParserContext.BaseURI != null && xmlParserContext.BaseURI.Length > 0)
            {
                _baseUri = new Uri(xmlParserContext.BaseURI, UriKind.RelativeOrAbsolute);
            }

            XmlNamespaceManager xmlnsManager = xmlParserContext.NamespaceManager;

            if (null != xmlnsManager)
            {
                foreach (string key in xmlnsManager)
                {
                    _xmlnsDictionary.Add(key, xmlnsManager.LookupNamespace(key));
                }
            }
        }
 public XamlNamespace()
 {
     XamlTypeMapper = new XamlTypeMapper(new string[0]);
     XmlnsDictionary.Add(String.Empty, "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
     XmlnsDictionary.Add(X_DEFAILT_NAMESPACE, "http://schemas.microsoft.com/winfx/2006/xaml");
 }