Example #1
0
        protected bool LoadChildren()
        {
            bool success = true;

            children.Clear();
            foreach (XmlNode currXmlNode in xmlNode.ChildNodes)
            {
                ScraperNode newScraperNode = ScraperNode.Load(currXmlNode, Context);
                if (newScraperNode != null && newScraperNode.LoadSuccess)
                {
                    children.Add(newScraperNode);
                }

                if (newScraperNode != null && !newScraperNode.LoadSuccess)
                {
                    success = false;
                }
            }

            return(success);
        }
Example #2
0
        public static ScraperNode Load(XmlNode xmlNode, ScriptableScraper context)
        {
            if (xmlNode == null || xmlNode.NodeType == XmlNodeType.Comment || xmlNode.NodeType == XmlNodeType.CDATA)
            {
                return(null);
            }

            Type   nodeType     = null;
            string nodeTypeName = xmlNode.Name;
            string nodeTypeKey  = "node:" + nodeTypeName;

            // try to grab the type from our dictionary
            if (TypeCache.ContainsKey(nodeTypeKey))
            {
                nodeType = TypeCache[nodeTypeKey];
            }

            // if it's not there, search the assembly for the type
            else
            {
                Type[] typeList = Assembly.GetExecutingAssembly().GetTypes();
                foreach (Type currType in typeList)
                {
                    foreach (Attribute currAttr in currType.GetCustomAttributes(true))
                    {
                        if (currAttr.GetType() == typeof(ScraperNodeAttribute) &&
                            nodeTypeName.Equals(((ScraperNodeAttribute)currAttr).NodeName))
                        {
                            // store our type and put it in our dictionary so we don't have to
                            // look it up again
                            TypeCache[nodeTypeKey] = currType;
                            nodeType = currType;
                            break;
                        }
                    }
                }
            }

            // if we couldn't find anything log the unhandled node and exit
            if (nodeType == null)
            {
                Logger.Error("ScriptableScraperProvider: Unsupported node type: {0}", xmlNode.OuterXml);
                return(null);
            }


            // try to create a new scraper node object
            try
            {
                ConstructorInfo constructor = nodeType.GetConstructor(new Type[] { typeof(XmlNode), typeof(ScriptableScraper) });
                ScraperNode     newNode     = (ScraperNode)constructor.Invoke(new object[] { xmlNode, context });
                return(newNode);
            }
            catch (Exception e)
            {
                if (e.GetType() == typeof(ThreadAbortException))
                {
                    throw e;
                }

                Logger.Error("ScriptableScraperProvider: Error instantiating ScraperNode based on: {0}", xmlNode.OuterXml, e);
                return(null);
            }
        }