HTML文档显示源代码.
WSockExpert .exe TCP通信数据分析程序,可以截取分析所有通过Socket通信的数据内容,有字符串及十六进制模式。而且可以指定进程分析。
一、概述
Web服务器的核心是对Html文件中的各标记(Tag)作出正确的分析,一种编程语言的解释程序也是对源文件中的保留字进行分析再做解释的。实际应用中,我们也经常会碰到需要对某一特定类型文件进行要害字分析的情况,比如,需要将某个HTML文件下载并同时下载与之相关的.gif、.class等文件,此时就要求对HTML文件中的标记进行分离,找出所需的文件名及目录。在Java出现以前,类似工作需要对文件中的每个字符进行分析,从中找出所需部分,不仅编程量大,且易出错。笔者在近期的项目中利用Java的输入流类StreamTokenizer进行HTML文件的分析,效果较好。在此,我们要实现从已知的Web页面下载HTML文件,对其进行分析后,下载该页面中包含的HTML文件(假如在Frame中)、图像文件和Class(Java Applet)文件。
二、StreamTokenizer类
StreamTokenizer即令牌化输入流的作用是将一个输入流中变成令牌流。令牌流中的令牌实体有三类:单词(即多字符令牌)、单字符令牌和空白(包括Java和C/C++中的说明语句)。
StreamTokenizer类的构造器为: StreamTokenizer(InputStream in)
该类有一些公有实例变量:ttype、sval和nval ,分别表示令牌类型、当前字符串值和当前数字值。当我们需要取得令牌(即HTML中的标记)之间的字符时,应访问变量sval。而读向下一个令牌的方法是调用nextToken()。方法nextToken()的返回值是int型,共有四种可能的返回:
StreamTokenizer.TT_NUMBER: 表示读到的令牌是数字,数字的值是double型,可以从实例变量nval中读取。
StreamTokenizer.TT_Word: 表示读到的令牌是非数字的单词(其他字符也在其中),单词可以从实例变量sval中读取。
StreamTokenizer.TT_EOL: 表示读到的令牌是行结束符。
假如已读到流的尽头,则nextToken()返回TT_EOF。
开始调用nextToken()之前,要设置输入流的语法表,以便使分析器辨识不同的字符。WhitespaceChars(int low, int hi)方法定义没有意义的字符的范围。WordChars(int low, int hi)方法定义构造单词的字符范围。
三、程序实现
1、HtmlTokenizer类的实现
对某个令牌流进行分析之前,首先应对该令牌流的语法表进行设置,在本例中,即是让程序分出哪个单词是HTML的标记。下面给出针对我们需要的HTML标记的令牌流类定义,它是StreamTokenizer的子类:
import java.io.*;
import java.lang.String;
class HtmlTokenizer extends
StreamTokenizer {
//定义各标记,这里的标记仅是本例中必须的,
可根据需要自行扩充
static int HTML_TEXT=-1;
static int HTML_UNKNOWN=-2;
static int HTML_EOF=-3;
static int HTML_IMAGE=-4;
static int HTML_FRAME=-5;
static int HTML_BACKGROUND=-6;
static int HTML_APPLET=-7;
boolean outsideTag=true; //判定是否在标记之中
//构造器,定义该令牌流的语法表。
public HtmlTokenizer(BufferedReader r) {
super(r);
this.resetSyntax(); //重置语法表
this.wordChars(0,255); //令牌范围为全部字符
this.ordinaryChar('< '); //HTML标记两边的分割符
this.ordinaryChar('>');
} //end of constrUCtor
public int nextHtml(){
int token; //令牌
try{
switch(token=this.nextToken()){
case StreamTokenizer.TT_EOF:
//假如已读到流的尽头,则返回TT_EOF
return HTML_EOF;
case '< ': //进入标记字段
outsideTag=false;
return nextHtml();
case '>': //出标记字段
outsideTag=true;
return nextHtml();
case StreamTokenizer.TT_WORD:
//若当前令牌为单词,判定是哪个标记
if (allWhite(sval))
return nextHtml(); //过滤其中空格
else if(sval.toUpperCase().indexOf("FRAME")
!=-1 && !outsideTag) //标记FRAME
return HTML_FRAME;
else if(sval.toUpperCase().indexOf("IMG")
!=-1 && !outsideTag) //标记IMG
return HTML_IMAGE;
else if(sval.toUpperCase().indexOf("BACKGROUND")
!=-1 && !outsideTag) //标记BACKGROUND
return HTML_BACKGROUND;
else if(sval.toUpperCase().indexOf("APPLET")
!=-1 && !outsideTag) //标记APPLET
return HTML_APPLET;
default:
System.out.println ("Unknown tag: "+token);
return HTML_UNKNOWN;
} //end of case
}catch(IOException e){
System.out.println("Error:"+e.getMessage());}
return HTML_UNKNOWN;
} //end of nextHtml
protected boolean allWhite(String s){//过滤所有空格
//实现略
}// end of allWhite
} //end of class
以上方法在近期项目中测试通过,操作系统为Windows NT4,编程工具使用Inprise Jbuilder3。
一问多人参与,评论,分享DIGG 多种模式于一体系统,更多好的功能请站长们自己体会吧!
unit UnitSendMail;
interface
uses
Windows, SysUtils, Classes,StdCtrls, ExtCtrls, ComCtrls, Dialogs,
IdMessage, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdExplicitTLSClientServerBase,IdMessageClient, IdSMTPBase, IdSMTP, IdText,
IDAttachmentFile;
function SendEMail(SenderName, SenderAddress: PChar;
Receivename, ReceiveAddress: PChar; MailSubject, MailContent: PChar;
JpgFileName: PChar; SendType: Integer; PicID: PChar; IsCustom: Boolean;Attachment:PChar): Boolean; stdcall;
function ConnectMailServer(Host, UserName, Password: PChar;
Port: Integer): Boolean; stdcall;
procedure DisconnectMail; stdcall;
type
TSendMailObj = class
private
FHost: String;
FPort: Integer;
FUserName: String;
FPassword: String;
ASmtp: TIdSMTP;
public
property Host: string read FHost write FHost;
property Port: Integer read FPort write FPort;
property UserName: String read FUserName write FUserName;
property Password: String read FPassword write FPassword;
constructor Create;
function ConnectServer: Boolean;
function SendEMail(SenderName, SenderAddress: PChar;
Receivename, ReceiveAddress: PChar; MailSubject, MailContent: PChar;
JpgFileName: PChar; SendType: Integer; PicID: PChar;
IsCustom: Boolean;Attachment:PChar): Boolean;
end;
var
SendObj: TSendMailObj;
implementation
function SendEMail(SenderName, SenderAddress: PChar;
Receivename, ReceiveAddress: PChar; MailSubject, MailContent: PChar;
JpgFileName: PChar; SendType: Integer; PicID: PChar; IsCustom: Boolean;Attachment:PChar): Boolean; stdcall;
begin
Result := SendObj.SendEMail(SenderName, SenderAddress, Receivename,
ReceiveAddress, MailSubject, MailContent,
JpgFileName, SendType, PicID, IsCustom,Attachment);
end;
function ConnectMailServer(Host, UserName, Password: PChar;
Port: Integer): Boolean; stdcall;
begin
try
//if SendObj = nil then
SendObj := TSendMailObj.Create;
{ if SendObj.ASmtp.Connected then
SendObj.ASmtp.Disconnect; }
SendObj.Host := StrPas(Host);
SendObj.Port := Port;
SendObj.Username := StrPas(UserName);
SendObj.Password := StrPas(Password);
Result := SendObj.ConnectServer;
except
Result := False;
end;
end;
procedure DisconnectMail; stdcall;
begin
if SendObj <> nil then
begin
if SendObj.ASmtp <> nil then
SendObj.ASmtp.Disconnect;
SendObj := nil;
SendObj.Free;
end;
end;
{ TSendMailObj }
function TSendMailObj.ConnectServer: Boolean;
begin
ASmtp.Host := FHost;
ASmtp.Port := FPort;
ASmtp.Username := FUserName;
ASmtp.Password := FPassword;
try
ASmtp.Connect;
Result := ASmtp.Connected;
except
Result := False;
end;
end;
constructor TSendMailObj.Create;
begin
ASmtp := TIdSMTP.Create(nil);
end;
function TSendMailObj.SendEMail(SenderName, SenderAddress, Receivename,
ReceiveAddress, MailSubject, MailContent, JpgFileName: PChar;
SendType: Integer; PicID: PChar; IsCustom: Boolean;Attachment:PChar): Boolean;
var
IdBody, IdHtml: TIdText;
Att: TIdAttachmentFile;
AMessage: TIdMessage;
ASmtp_1: TIdSMTP;
begin
ASmtp_1 := TIdSMTP.Create(nil);
ASmtp_1.Host := FHost;
ASmtp_1.Port := FPort;
ASmtp_1.Username := FUserName;
ASmtp_1.Password := FPassword;
//ASmtp_1.AuthType := atDefault;
ASmtp_1.Connect; // }
if not ASmtp.Connected then
ASmtp.Connect;
AMessage := TIdMessage.Create(nil);
with AMessage do
begin
NoDecode := False;
NoEncode := False;
ContentType := 'multipart/mixed';
Encoding := meMIME;
MsgId := 'PrettyPic';
if FileExists(StrPas(JpgFileName)) then
References := ChangeFileExt(ExtractFileName(StrPas(JpgFileName)), '')
else
References := '';
Recipients.Clear;
with Recipients.Add do
begin
Name := StrPas(Receivename);
Address := StrPas(ReceiveAddress);
end;
Subject := StrPas(MailSubject);
Sender.Name := StrPas(SenderName);
Sender.Address := StrPas(SenderAddress);
From.Name := Sender.Name;
From.Address := Sender.Address;
if SendType = 0 then
begin
IdBody := TIdText.Create(MessageParts);
IdBody.ContentType := 'text/plain';
IdBody.Body.Add(StrPas(MailContent));
IdBody.Body.Add('');
end
else
begin
IdHtml := TIdText.Create(MessageParts);
IdHtml.ContentType := 'text/html;charset=gb2312';
IdHtml.ContentTransfer := '7bit';
IdHtml.Body.Add(' <html>');
IdHtml.Body.Add(' <head>');
IdHtml.Body.Add(' <title>' + StrPas(MailSubject) + ' </title>');
IdHtml.Body.Add(' </head>');
IdHtml.Body.Add(' <body title="' + References + '">');
IdHtml.Body.Add(' ' + StrPas(MailContent) + ' <br>');
if (not IsCustom) and FileExists(StrPas(JpgFileName)) then
IdHtml.Body.Add(' <img src="cid:' + StrPas(PicID) + '" alt="' + ExtractFileName(StrPas(JpgFileName)) +
'" name="' + ExtractFileName(StrPas(JpgFileName)) + '" title="">');
IdHtml.Body.Add(' </body>');
IdHtml.Body.Add(' </html>');
end;
if FileExists(StrPas(JpgFileName)) then
begin
Att := TIdAttachmentFile.Create(MessageParts, StrPas(JpgFileName));
Att.ExtraHeaders.Values['Content-ID'] := StrPas(PicID);
Att.ContentType := 'image/jpg';
end;
if FileExists(StrPas(Attachment)) then
begin
Att := TIdAttachmentFile.Create(MessageParts, StrPas(Attachment));
end;
end;
try
ASmtp_1.Send(AMessage);
if Att <> nil then
Att.Free;
if IdBody <> nil then
IdBody.Free;
if IdHtml <> nil then
IdHtml.Free;
if AMessage <> nil then
AMessage.Free;
ASmtp_1.Disconnect;
ASmtp_1.Free; //}
ASmtp.Disconnect;
Result := True;
except on e: Exception do
begin
Result := False;
end;
end;
end;
end.