void DoParse() { #region 获得网页的html try { 富文本内容 = ""; 节点列表.Clear(); string url = mFileFullPath.Trim(); 富文本内容 = PlatformTools.DownloadWebString(url); } catch (Exception ex) { Debug.LogException(ex); } #endregion #region 分析网页html节点 Lexer lexer = new Lexer(富文本内容); Parser parser = new Parser(lexer); NodeList htmlNodes = parser.Parse(null); 节点列表.Clear(); 节点列表.Add("root"); string treeRoot = 节点列表[0]; for (int i = 0; i < htmlNodes.Count; i++) { this.RecursionHtmlNode(treeRoot, htmlNodes[i], false); } #endregion }
/// <summary> Look up a reference by kernel. /// Use a binary search on the ordered list of known references. /// Since the binary search returns the position at which a new item should /// be inserted, we check the references earlier in the list if there is /// a failure. /// </summary> /// <param name="key">A character reference with the kernel set to the string /// to be found. It need not be truncated at the exact end of the reference. /// </param> protected internal static CharacterReference LookUp(CharacterReference key) { System.String string_Renamed; int index; System.String kernel; char character; CharacterReference test; CharacterReference ret; // Care should be taken here because some entity references are // prefixes of others, i.e.: // \u2209[notin] \u00ac[not] // \u00ba[ordm] \u2228[or] // \u03d6[piv] \u03c0[pi] // \u00b3[sup3] \u2283[sup] ret = null; index = SortImpl.Bsearch(mCharacterReferences, key); string_Renamed = key.Kernel; if (index < mCharacterReferences.Length) { ret = mCharacterReferences[index]; kernel = ret.Kernel; //if (!(String.Compare(string_Renamed, 0, kernel, 0, kernel.Length) == 0)) if (!(PlatformTools.CompareStr(string_Renamed, 0, kernel, 0, kernel.Length) == 0)) { // not exact, check references starting with same character // to see if a subset matches ret = null; } } if (null == ret) { character = string_Renamed[0]; while (--index >= 0) { test = mCharacterReferences[index]; kernel = test.Kernel; if (character == kernel[0]) { //if (String.Compare(string_Renamed, 0, kernel, 0, kernel.Length) == 0) if (PlatformTools.CompareStr(string_Renamed, 0, kernel, 0, kernel.Length) == 0) { ret = test; break; } } else { break; } } } return(ret); }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <returns></returns> public int CompareTo(object obj) { MetaTagData obOther = obj as MetaTagData; if (null != obOther) { //return String.Compare(obOther.Name, this.Name, true); return(PlatformTools.CompareStr(obOther.Name, this.Name, true)); } throw new ArgumentException("Object is not of " + this.GetType().Name); }
public string ReadString() { var n = ReadSizeT(); if (n == 0) { return(null); } var bytes = ReadBytes(n); // n=1: removing trailing '\0' #if !UNITY_3 && !UNITY_4 && !UNITY_5 string ret = System.Text.Encoding.UTF8.GetString(bytes, 0, n - 1); #else string ret = PlatformTools.BS2String_UTF8(bytes, 0, n - 1); #endif #if DEBUG_BINARY_READER ULDebug.Log("ReadString n:" + n + " ret:" + ret); #endif return(ret); }
private static int ENC_Decode(ILuaState lua) { var s = lua.ToString(1); var e = lua.ToString(2); if (e != ENC_UTF8) { throw new Exception("unsupported encoding:" + e); } var bytes = new Byte[s.Length]; for (int i = 0; i < s.Length; ++i) { bytes[i] = (byte)s[i]; } #if !UNITY_3 && !UNITY_4 && !UNITY_5 lua.PushString(Encoding.UTF8.GetString(bytes)); #else lua.PushString(PlatformTools.BS2String_UTF8(bytes)); #endif return(1); }
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(" ", " "); 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; } } }
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, 内容锚定方向); } }
private void RecursionHtmlNode(string treeNode, INode htmlNode, bool siblingRequired) { if (htmlNode == null || treeNode == null) { return; } string current = treeNode; //string content; //current node if (htmlNode is ITag) { string nodeString = ""; ITag tag = (htmlNode as ITag); if (!tag.IsEndTag()) { nodeString = tag.TagName; if (tag.Attributes != null && tag.Attributes.Count > 0) { if (tag.Attributes["ID"] != null) { nodeString = nodeString + " { id=\"" + tag.Attributes["ID"].ToString() + "\" }"; } if (tag.Attributes["HREF"] != null) { nodeString = nodeString + " { href=\"" + tag.Attributes["HREF"].ToString() + "\" }"; } if (tag.Attributes["SIZE"] != null) { nodeString = nodeString + " { size=\"" + tag.Attributes["SIZE"].ToString() + "\" }"; } if (tag.Attributes["COLOR"] != null) { nodeString = nodeString + " { color=\"" + tag.Attributes["COLOR"].ToString() + "\" }"; } } } else { nodeString = tag.TagName + " End"; } current = nodeString; 节点列表.Add(current); } //获取节点间的内容 if (htmlNode.Children != null && htmlNode.Children.Count > 0) { this.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; current = tex.GetText(); current = current.Replace("\r\n", ""); current = current.Replace(" ", " "); byte[] utf8Bom = new byte[] { 239, 187, 191 }; current = current.Replace(PlatformTools.BS2String_UTF8(utf8Bom), ""); bool allIsSpace = true; for (int i = 0; i < current.Length; ++i) { if (current[i] != ' ') { allIsSpace = false; break; } } if (!string.IsNullOrEmpty(current) && current != "\t" && !allIsSpace) { 节点列表.Add(current); } } else { string typestr = htmlNode.GetType().ToString(); 节点列表.Add(typestr); } } //the sibling nodes if (siblingRequired) { INode sibling = htmlNode.NextSibling; while (sibling != null) { this.RecursionHtmlNode(treeNode, sibling, false); sibling = sibling.NextSibling; } } }
private static void ClickJavaButton() { PlatformTools.NativeGetAppVersion(); }