httpparser.java
来自「JavaExplorer是一个独立于平台的浏览器」· Java 代码 · 共 142 行
JAVA
142 行
/*
* Created on 12 nov. 03
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package javaexplorer.manager.http;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import javaexplorer.util.XFileBuffer;
/**
* @author veeb7280
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class HttpParser {
HttpXF root = null;
XFileBuffer buffer = new XFileBuffer();
HTMLEditorKit.ParserCallback callback = null;
private static HttpParser hp = new HttpParser();
public static HttpParser getParser(){
return hp;
}
public HttpParser(){
callback = new HTMLEditorKit.ParserCallback () {
boolean start = false;
String adress = null;
String name = null;
public void handleText(char[] data, int pos) {
if( start && name == null ) name = new String(data);
if( name == null || name.length() == 0 ) name = null;
}
public void handleStartTag(HTML.Tag t,
MutableAttributeSet a, int pos){
if( t.equals(HTML.Tag.IMG) ){
adress = (String) a.getAttribute(HTML.Attribute.SRC);
name = (String) a.getAttribute(HTML.Attribute.ALT);
}
else if(t.equals(HTML.Tag.A)){
adress = (String)a.getAttribute(HTML.Attribute.HREF);
name = (String)a.getAttribute(HTML.Attribute.ID);
start = true;
}
else if( t.equals(HTML.Tag.MAP) ){
adress = (String)a.getAttribute(HTML.Attribute.NAME);
name = (String)a.getAttribute(HTML.Attribute.ALT);
}
else if(t.equals(HTML.Tag.AREA)){
adress = (String)a.getAttribute(HTML.Attribute.HREF);
name = (String)a.getAttribute(HTML.Attribute.ALT);
}
else if(t.equals(HTML.Tag.FRAME)){
adress = (String)a.getAttribute(HTML.Attribute.SRC);
name = (String)a.getAttribute(HTML.Attribute.NAME);
}
}
public void handleEndTag(HTML.Tag t, int pos){
if( adress != null ){
buffer.add( makeXFile(adress, name) );
}
if( start && t.equals(HTML.Tag.A)){
start = false;
name = null;
}
}
public void handleSimpleTag(HTML.Tag t,
MutableAttributeSet a, int pos){
handleStartTag(t, a, pos);
handleEndTag(t, pos);
}
};
}
public XFileBuffer parseFile( HttpXF xfile ){
buffer.clear();
root = xfile;
read(xfile);
return buffer;
}
private HttpXF makeXFile(String address, String name){
HttpXF xfile = null;
try{
URL newURL = new URL(root.getURL(), address);
//On ne conserve pas les renvois dans la m阭e page s'ils existent
String test = newURL.getRef();
if( test != null ){
//On supprime cette r閒閞ence qui n'a pas d'int閞阾
//pour le type de construction XFile
newURL = new URL(newURL.getProtocol(), newURL.getHost(), newURL.getPort(), newURL.getFile());
}
if( root.getURL().sameFile(newURL) ) return null;
xfile = new HttpXF();
xfile.setLauncher(root.getLauncher());
xfile.setParent(root);
xfile.setDirectory(false);
xfile.setUseLocalFile(false);
xfile.setURL(newURL);
if( name != null ){
xfile.setName(name);
}
String contentType = URLConnection.guessContentTypeFromName(newURL.getFile());
if( contentType != null && contentType.toUpperCase().indexOf("HTML") != -1){
xfile.setDirectory(true);
}
}catch(Exception e){
xfile = null;
javaexplorer.util.Log.addError(e);
}
return xfile;
}
private void read( HttpXF xfile ){
try{
Reader reader = new InputStreamReader(xfile.getInputStream(0));
new ParserDelegator().parse(reader, callback, true);
}catch(Exception e){
buffer = null;
javaexplorer.util.Log.addError(e);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?