Esempio n. 1
0
 private void loadActionNodes()
 {
     actionNodes = new Dictionary <string, ScraperNode>();
     foreach (XmlNode currAction in xml.DocumentElement.SelectNodes("child::action"))
     {
         ActionNode newNode = (ActionNode)ScraperNode.Load(currAction, this);
         if (newNode != null && newNode.LoadSuccess)
         {
             actionNodes[newNode.Name] = newNode;
         }
         else
         {
             logger.Error("Error loading action node: " + currAction.OuterXml);
             loadSuccessful = false;
         }
     }
 }
Esempio n. 2
0
        protected bool loadChildren()
        {
            bool success = true;

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

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

            return(success);
        }
Esempio n. 3
0
        public static ScraperNode Load(XmlNode xmlNode, InternalScriptSettings settings)
        {
            if (xmlNode == null || xmlNode.NodeType == XmlNodeType.Comment || xmlNode.NodeType == XmlNodeType.CDATA)
            {
                return(null);
            }

            Type   nodeType     = null;
            string nodeTypeName = xmlNode.Name;


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

            // 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 dont have to
                            // look it up again
                            nodeTypes[nodeTypeName] = currType;
                            nodeType = currType;
                            break;
                        }
                    }
                }
            }

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


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

                logger.Error("Error instantiating ScraperNode based on: " + xmlNode.OuterXml, e);
                return(null);
            }
        }