下面列出了怎么用org.w3c.dom.html.HTMLDocument的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Returns the document type public identifier
* specified for this document, or null.
*/
public static String whichDoctypePublic( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getPublicId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLPublicId;
return null;
}
/**
* Returns the document type system identifier
* specified for this document, or null.
*/
public static String whichDoctypeSystem( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getSystemId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLSystemId;
return null;
}
/**
* Returns the document type public identifier
* specified for this document, or null.
*/
public static String whichDoctypePublic( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getPublicId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLPublicId;
return null;
}
/**
* Returns the document type system identifier
* specified for this document, or null.
*/
public static String whichDoctypeSystem( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getSystemId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLSystemId;
return null;
}
private void initEvent() {
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Document doc = webPage.getDocument(webPage.getMainFrame());
if (doc instanceof HTMLDocument) {
HTMLDocument htmlDocument = (HTMLDocument) doc;
Element e = htmlDocument.createElement("div");
e.setTextContent("hhhhhhh");
HTMLElement htmlDocumentElement = (HTMLElement)htmlDocument.getDocumentElement();
HTMLElement htmlBodyElement = (HTMLElement)htmlDocumentElement.getElementsByTagName("body").item(0);
//e.setAttribute(name, value);
//org.w3c.dom.Node n=htmlBodyElement.getLastChild();;
htmlBodyElement.appendChild(e);
webPage.getClientInsertPositionOffset();
// htmlBodyElement.ge
//hd.appendChild(e);
System.out.println(e.getClass());
}
}
});
}
/**
* Returns the document type public identifier
* specified for this document, or null.
*/
public static String whichDoctypePublic( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getPublicId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLPublicId;
return null;
}
/**
* Returns the document type system identifier
* specified for this document, or null.
*/
public static String whichDoctypeSystem( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getSystemId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLSystemId;
return null;
}
/**
* Returns the document type public identifier
* specified for this document, or null.
*/
public static String whichDoctypePublic( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getPublicId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLPublicId;
return null;
}
/**
* Returns the document type system identifier
* specified for this document, or null.
*/
public static String whichDoctypeSystem( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getSystemId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLSystemId;
return null;
}
/**
* Returns the document type public identifier
* specified for this document, or null.
*/
public static String whichDoctypePublic( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getPublicId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLPublicId;
return null;
}
/**
* Returns the document type system identifier
* specified for this document, or null.
*/
public static String whichDoctypeSystem( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getSystemId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLSystemId;
return null;
}
/**
* load
* @param response
*/
public void load( WebResponse response ) {
Function onLoadEvent=null;
try {
Context context = Context.enter();
context.initStandardObjects( null );
HTMLDocument htmlDocument = ((DomWindow) response.getScriptingHandler()).getDocument();
if (!(htmlDocument instanceof HTMLDocumentImpl)) return;
HTMLBodyElementImpl body = (HTMLBodyElementImpl) htmlDocument.getBody();
if (body == null) return;
onLoadEvent = body.getOnloadEvent();
if (onLoadEvent == null) return;
onLoadEvent.call( context, body, body, new Object[0] );
} catch (JavaScriptException e) {
ScriptingEngineImpl.handleScriptException(e, onLoadEvent.toString());
// HttpUnitUtils.handleException(e);
} catch (EcmaError ee) {
//throw ee;
ScriptingEngineImpl.handleScriptException(ee, onLoadEvent.toString());
} finally {
Context.exit();
}
}
/**
* Returns the document type public identifier
* specified for this document, or null.
*/
public static String whichDoctypePublic( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getPublicId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLPublicId;
return null;
}
/**
* Returns the document type system identifier
* specified for this document, or null.
*/
public static String whichDoctypeSystem( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getSystemId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLSystemId;
return null;
}
/**
* Returns the document type public identifier
* specified for this document, or null.
*/
public static String whichDoctypePublic( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getPublicId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLPublicId;
return null;
}
/**
* Returns the document type system identifier
* specified for this document, or null.
*/
public static String whichDoctypeSystem( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getSystemId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLSystemId;
return null;
}
/**
* Returns the document type public identifier
* specified for this document, or null.
*/
public static String whichDoctypePublic( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getPublicId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLPublicId;
return null;
}
/**
* Returns the document type system identifier
* specified for this document, or null.
*/
public static String whichDoctypeSystem( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getSystemId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLSystemId;
return null;
}
/**
* Returns the document type public identifier
* specified for this document, or null.
*/
public static String whichDoctypePublic( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getPublicId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLPublicId;
return null;
}
/**
* Returns the document type system identifier
* specified for this document, or null.
*/
public static String whichDoctypeSystem( Document doc )
{
DocumentType doctype;
/* DOM Level 2 was introduced into the code base*/
doctype = doc.getDoctype();
if ( doctype != null ) {
// Note on catch: DOM Level 1 does not specify this method
// and the code will throw a NoSuchMethodError
try {
return doctype.getSystemId();
} catch ( Error except ) { }
}
if ( doc instanceof HTMLDocument )
return DTD.XHTMLSystemId;
return null;
}
private void updateStyle() {
Document doc = webPage.getDocument(webPage.getMainFrame());
if (doc instanceof HTMLDocument) {
HTMLDocument htmlDocument = (HTMLDocument) doc;
HTMLElement htmlDocumentElement = (HTMLElement) htmlDocument.getDocumentElement();
HTMLElement htmlBodyElement = (HTMLElement) htmlDocumentElement.getElementsByTagName("body").item(0);
htmlBodyElement.setAttribute("style", createStyleValue().toString());
}
}
public void insertImage(String path, String id, String name, String value) {
Document doc = webPage.getDocument(webPage.getMainFrame());
if (doc instanceof HTMLDocument) {
HTMLDocument htmlDocument = (HTMLDocument) doc;
HTMLElement htmlDocumentElement = (HTMLElement) htmlDocument.getDocumentElement();
HTMLElement htmlBodyElement = (HTMLElement) htmlDocumentElement.getElementsByTagName("body").item(0);
if (htmlBodyElement instanceof HTMLBodyElementImpl) {
// HTMLBodyElementImpl htmlBodyElementImpl =
// (HTMLBodyElementImpl) htmlBodyElement;
// Element e = htmlBodyElementImpl.getOffsetParent();
// htmlBodyElementImpl.insertBefore(newChild, refChild)
// htmlDocument.
}
Element e = htmlDocument.createElement("img");
e.setAttribute("id", id);
e.setAttribute("name", name);
e.setAttribute("value", value);
e.setAttribute("src", path);
// webEngine.get
// webView.get
int positionOffset = webPage.getClientInsertPositionOffset();
int index = (positionOffset + 1);
NodeList nodeList = htmlBodyElement.getChildNodes();
int length = nodeList.getLength();
if (index < 0) {
htmlBodyElement.appendChild(e);
} else if (index < length) {
org.w3c.dom.Node node = nodeList.item(index);
htmlBodyElement.insertBefore(e, node);
} else {
htmlBodyElement.appendChild(e);
}
webPage.getClientInsertPositionOffset();
}
}
public void insertLast(String text, String fontName, int fontSize, String color, boolean bold, boolean underline, boolean italic) {
Document doc = webPage.getDocument(webPage.getMainFrame());
if (doc instanceof HTMLDocument) {
HTMLDocument htmlDocument = (HTMLDocument) doc;
HTMLElement htmlDocumentElement = (HTMLElement) htmlDocument.getDocumentElement();
HTMLElement htmlBodyElement = (HTMLElement) htmlDocumentElement.getElementsByTagName("body").item(0);
Element element = htmlDocument.createElement("div");
element.setTextContent(text);
element.setAttribute("style", createStyleValue(fontName, fontSize, color, bold, underline, italic).toString());
htmlBodyElement.appendChild(element);
}
}
private void updateStyle() {
Document doc = webPage.getDocument(webPage.getMainFrame());
if (doc instanceof HTMLDocument) {
HTMLDocument htmlDocument = (HTMLDocument) doc;
HTMLElement htmlDocumentElement = (HTMLElement) htmlDocument.getDocumentElement();
HTMLElement htmlBodyElement = (HTMLElement) htmlDocumentElement.getElementsByTagName("body").item(0);
htmlBodyElement.setAttribute("style", createStyleValue().toString());
}
}
public void insert(String text) {
Document doc = webPage.getDocument(webPage.getMainFrame());
if (doc instanceof HTMLDocument) {
HTMLDocument htmlDocument = (HTMLDocument) doc;
HTMLElement htmlDocumentElement = (HTMLElement) htmlDocument.getDocumentElement();
HTMLElement htmlBodyElement = (HTMLElement) htmlDocumentElement.getElementsByTagName("body").item(0);
if (htmlBodyElement instanceof HTMLBodyElementImpl) {
}
Element e = htmlDocument.createElement("div");
e.setTextContent(text);
int positionOffset = webPage.getClientInsertPositionOffset();
int index = (positionOffset + 1);
NodeList nodeList = htmlBodyElement.getChildNodes();
int length = nodeList.getLength();
if (index < 0) {
htmlBodyElement.appendChild(e);
} else if (index < length) {
org.w3c.dom.Node node = nodeList.item(index);
// htmlBodyElement.
htmlBodyElement.insertBefore(e, node);
} else {
htmlBodyElement.appendChild(e);
}
webPage.getClientInsertPositionOffset();
}
}
public void insertImage(String path, String id, String name, String value) {
Document doc = webPage.getDocument(webPage.getMainFrame());
if (doc instanceof HTMLDocument) {
HTMLDocument htmlDocument = (HTMLDocument) doc;
HTMLElement htmlDocumentElement = (HTMLElement) htmlDocument.getDocumentElement();
HTMLElement htmlBodyElement = (HTMLElement) htmlDocumentElement.getElementsByTagName("body").item(0);
if (htmlBodyElement instanceof HTMLBodyElementImpl) {
}
Element e = htmlDocument.createElement("img");
e.setAttribute("id", id);
e.setAttribute("name", name);
e.setAttribute("value", value);
e.setAttribute("src", path);
// webEngine.get
// webView.get
int positionOffset = webPage.getClientInsertPositionOffset();
int index = (positionOffset + 1);
NodeList nodeList = htmlBodyElement.getChildNodes();
int length = nodeList.getLength();
if (index < 0) {
htmlBodyElement.appendChild(e);
} else if (index < length) {
org.w3c.dom.Node node = nodeList.item(index);
htmlBodyElement.insertBefore(e, node);
} else {
htmlBodyElement.appendChild(e);
}
webPage.getClientInsertPositionOffset();
}
}
private void updateStyle() {
Document doc = webPage.getDocument(webPage.getMainFrame());
if (doc instanceof HTMLDocument) {
HTMLDocument htmlDocument = (HTMLDocument) doc;
HTMLElement htmlDocumentElement = (HTMLElement) htmlDocument.getDocumentElement();
HTMLElement htmlBodyElement = (HTMLElement) htmlDocumentElement.getElementsByTagName("body").item(0);
htmlBodyElement.setAttribute("style", createStyleValue().toString());
}
}
/**
* parse the given test with the given URL
* @param text
* @param pageURL
* @throws SAXException
* @throws IOException
*/
public void parse( String text, URL pageURL ) throws SAXException, IOException {
HTMLParserFactory.getHTMLParser().parse( pageURL, text, new DocumentAdapter() {
public void setDocument(HTMLDocument document ) { HTMLPage.this.setRootNode( document ); }
public String getIncludedScript( String srcAttribute ) throws IOException { return HTMLPage.this.getIncludedScript( srcAttribute ); }
public ScriptingHandler getScriptingHandler() { return getResponse().getScriptingHandler(); }
});
}
/**
* parse the given URL with the given pageText using the given document adapter
* @param pageURL
* @param pageText
* @param adapter
*/
public void parse( URL pageURL, String pageText, DocumentAdapter adapter ) throws IOException, SAXException {
try {
NekoDOMParser parser = NekoDOMParser.newParser( adapter, pageURL );
parser.parse( new InputSource( new StringReader( pageText ) ) );
Document doc=parser.getDocument();
adapter.setDocument( (HTMLDocument)doc );
} catch (NekoDOMParser.ScriptException e) {
throw e.getException();
}
}
public void parse( URL pageURL, String pageText, DocumentAdapter adapter ) throws IOException, SAXException {
try {
Document jtidyDocument = getParser( pageURL ).parseDOM( new ByteArrayInputStream( pageText.getBytes( UTF_ENCODING ) ), null );
HTMLDocument htmlDocument = new HTMLDocumentImpl();
NodeList nl = jtidyDocument.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node importedNode = nl.item(i);
if (importedNode.getNodeType() != Node.DOCUMENT_TYPE_NODE) htmlDocument.appendChild( htmlDocument.importNode( importedNode, true ) );
}
adapter.setDocument( htmlDocument );
} catch (UnsupportedEncodingException e) {
throw new RuntimeException( "UTF-8 encoding failed" );
}
}