📄 jabberstreamreader.cs
字号:
#define Client
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Xml.Serialization;
namespace gowk.core
{
public class JabberStreamReader
{
JabberStream js;
XmlDocument doc;
StringBuilder sb;
public JabberStreamReader()
{
this.sb=new StringBuilder();
this.doc=new XmlDocument();
}
public JabberStream Stream
{
get{return this.js;}
set
{
if(this.js!=value)
{
this.js=value;
}
}
}
public XmlNode ReadStartStream()
{
string xxx=sb.ToString();
System.Diagnostics.Trace.WriteLine(xxx);
int s=0;
this.CrearBuffer();
string e=null;
do
{
try
{
e=this.ReadElementInternal(ref s);
this.sb.Remove(0,s);
s=0;
}
catch(System.IO.EndOfStreamException eos)
{
System.Diagnostics.Trace.WriteLine("end of stream in read starat stream");
return null;
}
if(this.GetElementName(e)=="stream:stream")
{
e+="</stream:stream>";
XmlDocument doc=new XmlDocument();
try
{
doc.LoadXml(e);
this.Stream.ID=doc.DocumentElement.Attributes["id"].Value;
return doc.DocumentElement;
}
catch(System.Xml.XmlException ex)
{
System.Diagnostics.Trace.WriteLine("StreamException/XmlException in read starat stream");
throw new StreamException(ex.Message,ex);
}
}
}
while(true);
// return null;
}
private void CrearBuffer()
{
this.sb.Remove(0,sb.Length);
}
public XmlElement ReadStanza()
{
string e;
try
{
e=this.ReadElement();
e=e.Replace("xmlns='jabber:client'","");
e=e.Replace("xmlns=\"jabber:client\"","");
}
catch(System.IO.EndOfStreamException eos)
{
// this.CrearBuffer();
// throw;
System.Diagnostics.Trace.WriteLine("rcv: null");
return null;
}
System.Diagnostics.Trace.WriteLine("rcv:"+e);
XmlDocument doc=new XmlDocument();
try
{
doc.LoadXml(e); //.................
}
catch(System.Xml.XmlException ex)
{
// this.CrearBuffer();
throw new StreamException(ex.Message,ex);
}
return doc.DocumentElement;
}
public string ReadElement()
{
int s=0;
string e=this.ReadElementInternal(ref s);
if(this.IsEmptyElement(e))
{
sb.Remove(0,s);
return e;
}
string en=this.GetElementName(e);
do
{
e=this.ReadElementInternal(ref s);
if(this.GetElementName(e)==en)
{
string ret=this.sb.ToString(0,s);
sb.Remove(0,s);
return ret;
}
}while(true);
}
#region
private string GetElementName(string txt)
{
int s=txt.IndexOf("<")+1;
int e=txt.IndexOf(">",s);
string name=txt.Substring(s,e-s);
name=name.Trim().Trim('/');
name=name.Split(' ')[0];
// System.Diagnostics.Trace.WriteLine("get name:"+name +" form:"+txt);
return name.Trim();
// return this.IsEndElement(e)?this.GetEndElementName(e):this.GetStartElementName(e);
}
private string ReadElementInternal(ref int start)
{
int end=this.SearchEnd(start)+1;
string e=this.sb.ToString().Substring(start,end-start);
start=end;
return e;
}
private bool IsEndElement(string e)
{
string e2=(string)e.Clone();
int index=e2.LastIndexOf("/");
if(index==-1)return false;
e2=e2.Substring(0,index+1);
e2.Replace(" ","");
return e2=="</";
}
private bool IsEmptyElement(string e)
{
string e2=(string)e.Clone();
int index=e2.LastIndexOf("/");
if(index==-1)return false;
e2=e2.Substring(index);
e2.Replace(" ","");
return e2=="/>";
}
private int SearchEnd(int start)
{
string txt=this.sb.ToString();
int pos=txt.IndexOf(">",start);
System.Diagnostics.Trace.WriteLine("SearchEnd("+start.ToString()+"):pos="+pos.ToString()+" text:"+txt);
if(pos==-1)
{
this.Read();
return this.SearchEnd(txt.Length);
}
return pos;
}
private int SearchStart(int start)
{
string txt=this.sb.ToString();
int pos=txt.IndexOf("<",start);
if(pos==-1)
{
this.Read();
return this.SearchStart(txt.Length);
}
return pos;
}
#endregion
public bool Read()
{
byte[] rb=this.js.Read();
if(rb==null)
{
// return false;
throw new System.IO.EndOfStreamException();
}
else
sb.Append(System.Text.Encoding.UTF8.GetChars(rb));
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -