Added some new tests.
This commit is contained in:
akhil.arora%sun.com 1999-11-22 22:10:44 +00:00
Родитель e71f346f0f
Коммит 988ff3795a
3 изменённых файлов: 231 добавлений и 1 удалений

Просмотреть файл

@ -0,0 +1,208 @@
/*
The contents of this file are subject to the Mozilla Public
License Version 1.1 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
implied. See the License for the specific language governing
rights and limitations under the License.
The Original Code is mozilla.org code.
The Initial Developer of the Original Code is Sun Microsystems,
Inc. Portions created by Sun are
Copyright (C) 1999 Sun Microsystems, Inc. All
Rights Reserved.
Contributor(s):
*/
package org.mozilla.dom.tests;
import java.applet.Applet;
import java.io.BufferedOutputStream;
import java.io.PrintStream;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.mozilla.dom.DOMAccessor;
import org.mozilla.dom.DOMAccessorImpl;
import org.mozilla.dom.DocumentLoadListener;
import org.mozilla.dom.tests.TestDocLoadListener;
public class TestDOMAccessorApplet1 extends Applet implements DocumentLoadListener
{
private DOMAccessor accessor;
private static final boolean debug = true;
public void init()
{
accessor = DOMAccessorImpl.getInstance();
accessor.addDocumentLoadListener(this);
System.out.println("inited......");
System.out.println("accsessor: " + accessor);
}
public void startURLLoad(String url, String contentType, Document doc) {
if (debug)
System.err.println("DOM: start URL load - " +
url.toString() + " " +
contentType);
}
public void endURLLoad(String url, int status, Document doc) {
if (debug)
System.err.println("DOM: end URL load - " +
url.toString() + " " +
Integer.toHexString(status));
if (!(url.endsWith(".xml") || url.endsWith(".html"))) return;
if (doc == null) return;
Element element = doc.getDocumentElement();
if (element == null) return;
element.normalize();
PrintStream ps = null;
ps = new PrintStream(new BufferedOutputStream(System.out, 1024));
ps.println("\n+++ " + url.toString() + " +++");
dump(ps, element, 0, false);
ps.println();
ps.flush();
element = null;
doc = null;
}
public void progressURLLoad(String url, int progress, int progressMax,
Document doc) {
if (debug)
System.err.println("DOM: progress URL load - " +
url.toString() + " " +
Integer.toString(progress) + "/" +
Integer.toString(progressMax));
}
public void statusURLLoad(String url, String message, Document doc) {
if (debug)
System.err.println("DOM: status URL load - " +
url.toString() + " (" +
message + ")");
}
public void startDocumentLoad(String url) {
if (debug)
System.err.println("DOM: start load - " +
url.toString());
}
public void endDocumentLoad(String url, int status, Document doc) {
if (debug) {
System.err.println("DOM: end load - " +
url.toString() + " " +
Integer.toHexString(status));
}
}
private void dump(PrintStream ps, Node node, int indent, boolean isMapNode) {
if (node == null) return;
ps.println();
for (int i=0; i<indent; i++)
ps.print(' ');
ps.print("name=\"" + node.getNodeName() + "\"" +
" type=" + dumpType(node.getNodeType()) +
" value=\"" + stripWhiteSpace(node.getNodeValue()) + "\"");
if (isMapNode) return;
NamedNodeMap map = node.getAttributes();
dumpMap(ps, map, indent);
NodeList children = node.getChildNodes();
if (children == null) return;
/*
Node fc = node.getFirstChild();
if (fc != null)
System.out.println("firstchild=" + fc.toString());
Node lc = node.getLastChild();
if (lc != null)
System.out.println("lastchild=" + lc.toString());
*/
int length = children.getLength();
for (int i=0; i < length; i++) {
dump(ps, children.item(i), indent+2, false);
}
}
private void dumpMap(PrintStream ps, NamedNodeMap map, int indent) {
if (map == null) return;
ps.println();
int length = map.getLength();
if (length > 0) {
for (int i=0; i<indent; i++)
ps.print(' ');
ps.print("------- start attributes -------");
}
for (int i=0; i < length; i++) {
Node item = map.item(i);
dump(ps, item, indent, true);
}
if (length > 0) {
ps.println();
for (int i=0; i<indent; i++)
ps.print(' ');
ps.print("------- end attributes -------");
}
}
private static String dumpType(int type) {
switch (type) {
case Node.ELEMENT_NODE: return "ELEMENT";
case Node.ATTRIBUTE_NODE: return "ATTRIBUTE";
case Node.TEXT_NODE: return "TEXT";
case Node.CDATA_SECTION_NODE: return "CDATA_SECTION";
case Node.ENTITY_REFERENCE_NODE: return "ENTITY_REFERENCE";
case Node.ENTITY_NODE: return "ENTITY";
case Node.PROCESSING_INSTRUCTION_NODE: return "PROCESSING_INSTRUCTION";
case Node.COMMENT_NODE: return "COMMENT";
case Node.DOCUMENT_NODE: return "DOCUMENT";
case Node.DOCUMENT_TYPE_NODE: return "DOCUMENT_TYPE";
case Node.DOCUMENT_FRAGMENT_NODE: return "DOCUMENT_FRAGMENT";
case Node.NOTATION_NODE: return "NOTATION";
}
return "ERROR";
}
private static String stripWhiteSpace(String s) {
int len = s.length();
StringBuffer sb = new StringBuffer(len);
char c = ' ';
char pc = ' ';
for (int i=0; i < len; i++) {
c = s.charAt(i);
if ((pc == ' ' || pc == '\n' || pc == '\t') &&
(c == ' ' || c == '\n' || c == '\t'))
continue;
sb.append(c);
pc = c;
}
return sb.toString();
}
}

Просмотреть файл

@ -1,7 +1,12 @@
<html>
<body name="test_dom_accessor">
<pre>
This should run DOMAccessor test.
<applet code="TestDOMAccessorApplet.class"
Look into the java plugin console for
document load notifications while surfing
around
<pre>
<applet code="org.mozilla.dom.tests.TestDOMAccessorApplet"
width=0
height=0
alt="Your browser understands the &lt;APPLET&gt; tag but isn't running the applet, for some reason.">

Просмотреть файл

@ -0,0 +1,17 @@
<html>
<body name="test_dom_accessor">
<pre>
This should run DOMAccessor test.
TestDOMAccessorApplet1 registers itself
as a DocumentLoadListener via DOMAccessor
and dumps load notifications and document
source into the java plugin console.
<pre>
<applet code="org.mozilla.dom.tests.TestDOMAccessorApplet1"
width=0
height=0
alt="Your browser understands the &lt;APPLET&gt; tag but isn't running the applet, for some reason.">
Your browser is completely ignoring the &lt;APPLET&gt; tag!
</applet>
</body>
</html>