📄 swingparseradaptor.java
字号:
try
{
result.write(txt.toString());
} catch (Exception e)
{
System.err.println("Error parsing:" + e);
}
return this;
}
/*
* Used to write all character content to the output stream.
* Returns a reference to itself so that these calls can be chained.
*
* @param txt Any character text to be written out directly to stream.
* @return A handle to the this, the callback, for chaining results.
*
*/
private Callback addToResult(char[] txt)
{
//if (ignoreLevel > 0) return this;
try
{
result.write(txt);
} catch (Exception e)
{ /* ignore */
}
return this;
}
/*
* Accessor to the Callback's content-String
*
* @return Cleaned and rewritten HTML-Content
*/
public String getResult()
{
try
{
result.flush();
} catch (Exception e)
{ /* ignore */
}
// WARNING: doesn't work, if you remove " " + ... but don't know why
String res = " " + result.toString();
return res;
}
/*
* Flushes the output stream. NOT IMPLEMENTED
*
*/
public void flush() throws javax.swing.text.BadLocationException
{
// nothing to do here ...
}
/*
* Writes output to the final stream for all attributes of a given tag.
*
* @param tag The HTML tag being output.
* @param attrs The mutable HTML attribute set for the current HTML tag.
*
*/
private void appendTagToResult(HTML.Tag tag, MutableAttributeSet attrs)
{
convertURLS(tag,attrs);
Enumeration e = attrs.getAttributeNames();
addToResult("<").addToResult(tag);
while (e.hasMoreElements())
{
Object attr = e.nextElement();
String value = attrs.getAttribute(attr).toString();
addToResult(" ").addToResult(attr).addToResult("=\"").
addToResult(value).addToResult("\"");
}
if (simpleTag)
addToResult("/>");
else
addToResult(">");
}
/*
* Determines which HTML Tag/Element is being inspected, and calls the
* appropriate converter for that context. This method contains all the
* logic for determining how tags are rewritten.
*
* TODO: it would be better to drive this logic off a state table that is not
* tied to the Hot Java parser.
*
* @param tag TAG from the Callback-Interface.
* @param attrs The mutable HTML attribute set for the current HTML element.
*/
private void convertURLS( HTML.Tag tag, MutableAttributeSet attrs )
{
rewriter.convertTagEvent(tag, attrs);
if ((tag == HTML.Tag.A) &&
(attrs.getAttribute(HTML.Attribute.HREF) != null))
{
// ---- CHECKING <A HREF
addProxiedConvertedAttribute( tag, HTML.Attribute.HREF, attrs);
}
else if (((tag == HTML.Tag.IMG ||
tag == HTML.Tag.INPUT
) &&
(attrs.getAttribute(HTML.Attribute.SRC) != null)
))
{
// ---- CHECKING <IMG SRC & <INPUT SRC
addConvertedAttribute( tag,
HTML.Attribute.SRC,
attrs,
rewriter.proxyAllTags());
} else if (((tag == HTML.Tag.OPTION) ) &&
(attrs.getAttribute(HTML.Attribute.VALUE) != null))
{
// ---- CHECKING <OPTION
addProxiedConvertedAttribute( tag, HTML.Attribute.VALUE, attrs );
} else if (((tag == HTML.Tag.LINK) ) &&
(attrs.getAttribute(HTML.Attribute.HREF) != null))
{
// ---- CHECKING <LINK
addConvertedAttribute( tag,
HTML.Attribute.HREF,
attrs,
rewriter.proxyAllTags());
} else if ( tag == HTML.Tag.APPLET )
{
// ---- CHECKING <APPLET CODEBASE=
addConvertedAttribute( tag,
HTML.Attribute.CODEBASE,
attrs,
rewriter.proxyAllTags());
} else if ( tag == HTML.Tag.FRAME )
{
// ---- CHECKING <FRAME SRC=
addProxiedConvertedAttribute( tag, HTML.Attribute.SRC, attrs);
} else if ( tag == HTML.Tag.SCRIPT )
{
// ---- CHECKING <SCRIPT SRC=
if (attrs.getAttribute(HTML.Attribute.SRC) != null)
{
// script is external
String s = attrs.getAttribute(HTML.Attribute.SRC).toString();
if (s.indexOf("%3E") == -1)
{
addConvertedAttribute( tag,
HTML.Attribute.SRC,
attrs,
rewriter.proxyAllTags());
}
} else
{
// script is inline
//parserOff = true;
}
} else if (tag == HTML.Tag.FORM)
{
// ---- CHECKING <FORM ACTION=
inForm = true; // buggy <form> handling in jdk 1.3
if (attrs.getAttribute(HTML.Attribute.ACTION) == null)
{
// always post
attrs.addAttribute(HTML.Attribute.METHOD, "POST");
//self referencing <FORM>
// attrs.addAttribute(HTML.Attribute.ACTION,
// baseURL);
} else
{
// always post
attrs.addAttribute(HTML.Attribute.METHOD, "POST");
addProxiedConvertedAttribute( tag, HTML.Attribute.ACTION, attrs);
}
} else if (((tag == HTML.Tag.AREA) ) &&
(attrs.getAttribute(HTML.Attribute.HREF) != null))
{
// ---- CHECKING <AREA
addProxiedConvertedAttribute( tag, HTML.Attribute.HREF,
attrs );
} else if (((tag == HTML.Tag.BODY) ) &&
(attrs.getAttribute(HTML.Attribute.BACKGROUND) != null))
{
// ---- CHECKING <BODY
addConvertedAttribute( tag,
HTML.Attribute.BACKGROUND,
attrs,
rewriter.proxyAllTags());
} else if (tag == HTML.Tag.TD)
{
// ---- CHECKING <TD BACKGROUND=
if (! (attrs.getAttribute(HTML.Attribute.BACKGROUND) == null))
{
addConvertedAttribute( tag,
HTML.Attribute.BACKGROUND,
attrs,
rewriter.proxyAllTags());
}
}
/*
if ( removeScript && (tag == HTML.Tag.SCRIPT)) {
ignoreLevel ++;
*/
}
/*
* Converts the given attribute's URL compatible element to a proxied URL.
* Uses the proxy parameter to determine if the URL should be written back as a
* proxied URL, or as a fullpath to the original host.
*
* @param attr The HTML attribute to be proxied.
* @param attrs The mutable HTML attribute set for the current HTML element.
* @param proxy If set true, the URL is written back as a proxied URL, otherwise
* it is written back as a fullpath back to the original host.
*
*/
private void addConvertedAttribute( HTML.Tag tag,
HTML.Attribute attr,
MutableAttributeSet attrs,
boolean proxy )
{
if (proxy)
{
addProxiedConvertedAttribute(tag, attr,attrs);
} else
{
if ( attrs.getAttribute( attr ) != null )
{
attrs.addAttribute( attr,
generateNewUrl( tag, attrs, attr, false ) );
}
}
}
/**
*
* Converts the given attribute's URL compatible element to a proxied URL.
* This method will always add the proxy host prefix to the rewritten URL.
*
* @param attr The HTML attribute to be proxied.
* @param attrs The mutable HTML attribute set for the current HTML element.
*
*/
private void addProxiedConvertedAttribute( HTML.Tag tag,
HTML.Attribute attr,
MutableAttributeSet attrs ) {
if ( attrs.getAttribute( attr ) != null )
{
String attrSource = attrs.getAttribute( attr ).toString();
// special case: mailto should not be sent to the proxy server
if (attrSource.startsWith("mailto:"))
{
attrs.addAttribute( attr,
generateNewUrl( tag, attrs, attr, true ) );
} else if (attrSource.startsWith("javascript:"))
{
attrs.addAttribute( attr,
attrSource);
} else
{
attrs.addAttribute( attr,
generateNewUrl( tag, attrs, attr, true ) );
}
}
}
/*
* Calls the rewriter's URL generator callback, which will translate the old url
* into a new fullpath URL, either relative to the proxy server, or a fullpath
* to the original web server, depending on the 'proxied' parameter.
*
* @param oldURL The original URL, before it is tranlated.
* @param proxied Boolean indicator denotes if the URL should be written back
* as a proxied URL (true), or as a fully addressable address to the
* original web server.
* @return The translated new URL.
*
*/
private String generateNewUrl(HTML.Tag tag,
MutableAttributeSet attrs,
HTML.Attribute attr,
boolean proxied)
{
String oldURL = attrs.getAttribute( attr ).toString();
// System.out.println("Generating new url: " + oldURL);
return rewriter.generateNewUrl(oldURL, tag, attr);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -