示例#1
0
//#region 获得html文件内容
//        public string DoParseURL(string url, out StrTreeNode tree)
//        {
//            string html = "";
//            try
//            {
//                //System.Net.WebClient aWebClient = new System.Net.WebClient();
//                //aWebClient.Encoding = System.Text.Encoding.UTF8;
//                //html = aWebClient.DownloadString(url);
//                //aWebClient.Dispose();
//                FileStream stream = File.Open(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//                byte[] buffer = new byte[stream.Length];
//                stream.Read(buffer, 0, buffer.Length);
//                html = PlatformTools.BS2String_UTF8(buffer);
//            }
//            catch (Exception ex)
//            {
//                PopupDialogView.Popup(弹框类型.错误提示, ex.Message);
//                Debug.LogException(ex);
//            }
//            DoParseString(html, out tree);
//            return html;
//        }
//#endregion

        #region 分析html节点
        public void DoParseString(string str, out StrTreeNode tree)
        {
            tree = new StrTreeNode("root");
            Lexer    lexer     = new Lexer(str);
            Parser   parser    = new Parser(lexer);
            NodeList htmlNodes = parser.Parse(null);

            for (int i = 0; i < htmlNodes.Count; i++)
            {
                RecursionHtmlNode(tree, htmlNodes[i], false);
            }
        }
示例#2
0
        void RecursionHtmlNode(StrTreeNode treeNode, INode htmlNode, bool siblingRequired)
        {
            if (htmlNode == null || treeNode == null)
            {
                return;
            }

            StrTreeNode current = treeNode;

            //current node
            if (htmlNode is ITag)
            {
                string        nodeString = "";
                ITag          tag        = (htmlNode as ITag);
                string[]      values     = null;
                HtmlTagType[] types      = GetFullTagInfo(tag, out values);
                if (!tag.IsEndTag())
                {
                    for (int i = 0; i < types.Length; ++i)
                    {
                        if (null != OnReadTagBegin)
                        {
                            OnReadTagBegin(types[i], values[i]);
                        }
                        nodeString += types[i].ToString() + "={" + values[i] + "} ";
                    }
                    //if (tag.Attributes != null && tag.Attributes.Count > 0)
                    //{
                    //    if (tag.Attributes["ID"] != null)
                    //    {
                    //        if (null != OnReadTagBegin)
                    //            OnReadTagBegin(tag.TagName, "ID", tag.Attributes["ID"].ToString());
                    //        nodeString = nodeString + " { id=\"" + tag.Attributes["ID"].ToString() + "\" }";
                    //    }
                    //    if (tag.Attributes["HREF"] != null)
                    //    {
                    //        if (null != OnReadTagBegin)
                    //            OnReadTagBegin(tag.TagName, "HREF", tag.Attributes["HREF"].ToString());
                    //        nodeString = nodeString + " { href=\"" + tag.Attributes["HREF"].ToString() + "\" }";
                    //    }
                    //    if (tag.Attributes["SIZE"] != null)
                    //    {
                    //        if (null != OnReadTagBegin)
                    //            OnReadTagBegin(tag.TagName, "SIZE", tag.Attributes["SIZE"].ToString());
                    //        nodeString = nodeString + " { size=\"" + tag.Attributes["SIZE"].ToString() + "\" }";
                    //    }
                    //    if (tag.Attributes["COLOR"] != null)
                    //    {
                    //        if (null != OnReadTagBegin)
                    //            OnReadTagBegin(tag.TagName, "COLOR", tag.Attributes["COLOR"].ToString());
                    //        nodeString = nodeString + " { color=\"" + tag.Attributes["COLOR"].ToString() + "\" }";
                    //    }
                    //}
                }
                else
                {
                    for (int i = 0; i < types.Length; ++i)
                    {
                        if (null != OnReadTagEnd)
                        {
                            OnReadTagEnd(types[i]);
                        }
                    }
                    nodeString = tag.TagName + " End";
                }
                current = treeNode.AddItem(nodeString);
            }
            //获取节点间的内容
            if (htmlNode.Children != null && htmlNode.Children.Count > 0)
            {
                RecursionHtmlNode(current, htmlNode.FirstChild, true);
                //content = htmlNode.FirstChild.GetText();
                //节点列表.Add(content);
            }
            else if (!(htmlNode is ITag))
            {
                if (htmlNode is IText)
                {
                    IText  tex        = htmlNode as IText;
                    string nodeString = tex.GetText();
                    nodeString = nodeString.Replace("\r\n", "");
                    nodeString = nodeString.Replace("&nbsp;", " ");
                    nodeString = nodeString.Replace("\t", "    ");
                    byte[] utf8Bom = new byte[] { 239, 187, 191 };
                    nodeString = nodeString.Replace(PlatformTools.BS2String_UTF8(utf8Bom), "");
                    bool allIsSpace = true;
                    for (int i = 0; i < nodeString.Length; ++i)
                    {
                        if (nodeString[i] != ' ')
                        {
                            allIsSpace = false;
                            break;
                        }
                    }
                    if (!string.IsNullOrEmpty(nodeString) && !allIsSpace)
                    {
                        if (null != OnReadText)
                        {
                            OnReadText(nodeString);
                        }
                        current = treeNode.AddItem(nodeString);
                    }
                }
                else
                {
                    string typestr = htmlNode.GetType().ToString();
                    if (null != OnReadText)
                    {
                        OnReadText(typestr);
                    }
                    current = treeNode.AddItem(typestr);
                }
            }

            //the sibling nodes
            if (siblingRequired)
            {
                INode sibling = htmlNode.NextSibling;
                while (sibling != null)
                {
                    RecursionHtmlNode(current, sibling, false);
                    sibling = sibling.NextSibling;
                }
            }
        }
示例#3
0
        void Parse(string text)
        {
            if (mParsing)
            {
                return;
            }
            if (!FirstRunning)
            {
                mCacheTrans.localPosition = mLastViewPos;
            }
            else
            {
                mLastViewPos = mCacheTrans.localPosition;
            }
            if (地址.Length > 0 || text.Length > 0)
            {
                mParsing = true;
                HtmlParse reader = new HtmlParse();
                reader.OnReadTagBegin += OnHandleTagBegin;
                reader.OnReadTagEnd   += OnHandleTagEnd;
                reader.OnReadText     += OnCreateText;
                if (地址.Length > 0)
                {
                    byte[] buffer = null;
#if UNITY_ANDROID && !UNITY_EDITOR
                    WWW file = StreamAssetHelper.LoadAsset(StreamAssetRoot.HTML_ROOT, 地址);
                    while (!file.isDone && (file.progress < 0.9f))
                    {
                        Thread.Sleep(100);
                    }
                    if (file.bytes != null)
                    {
                        buffer = file.bytes;
                        内容     = PlatformTools.BS2String_UTF8(buffer);
                    }
                    AssetBundle bdl = file.assetBundle;
                    if (null != bdl)
                    {
                        bdl.Unload(true);
                    }
                    file.Dispose();
                    file = null;
#else
                    Stream stream = StreamAssetHelper.LoadFile(StreamAssetRoot.HTML_ROOT, 地址);
                    buffer = new byte[stream.Length];
                    int len = stream.Read(buffer, 0, (int)stream.Length);
                    if (len != (int)stream.Length)
                    {
                        Debug.LogError("读取页面地址[" + 地址 + "]失败,读取的长度["
                                       + len.ToString() + "]与文件长度[" + stream.Length + "]不匹配!");
                        return;
                    }
                    内容 = PlatformTools.BS2String_UTF8(buffer);
#endif
                }
                else
                {
                    内容 = text;
                }
                StrTreeNode treeNode = null;
                if (内容.Length > 0)
                {
                    reader.DoParseString(内容, out treeNode);
                }
                重新计算边界 = true;
                ClearParam();
                if (null != OnContextChanged)
                {
                    OnContextChanged();
                }
                GetBounds();
                mParsing = false;
                ReposNowLine();
                mNowLineUnits.Clear();
            }
            OnParseOvered();
            if (null != OnContextParseOvered)
            {
                OnContextParseOvered(Bounds, 内容锚定方向);
            }
        }