M dist/mcp-test/src/test/java/cardemo/CarDemoTest.java

- remove Robot from this class.  Moved into MCP

M dom/classes/org/mozilla/dom/DocumentImpl.java
M dom/jni/org_mozilla_dom_DocumentImpl.cpp

- implement getDocumentURI().

M dom/classes/org/mozilla/dom/util/DOMTreeDumper.java

- added findElementWithName().

R webclient/classes_spec/org/mozilla/webclient/test/DOMTreeModel.java
A dom/classes/org/mozilla/dom/util/DOMTreeModel.java
R webclient/test/manual/src/classes/org/mozilla/webclient/test/DOMTreeNotifier.java
A dom/classes/org/mozilla/dom/util/DOMTreeNotifier.java
M webclient/classes_spec/org/mozilla/webclient/test/DOMAccessPanel.java
M webclient/classes_spec/org/mozilla/webclient/test/DOMViewerFrame.java
M webclient/test/manual/src/classes/org/mozilla/webclient/test/DOMViewerFrame.java


- Move these over from test browser package

M webclient/classes_spec/org/mozilla/mcp/MCP.java

- added useful new public methods

findElement
clickElement
blockingClickElement

- absorbed functionality of Robot.

R webclient/classes_spec/org/mozilla/webclient/impl/DOMTreeDumper.java
R webclient/test/manual/src/classes/org/mozilla/webclient/test/DOMTreeDumper.java

- For some reason, there were several copies of this file.

M webclient/classes_spec/org/mozilla/webclient/impl/wrapper_native/CurrentPageImpl.java

- Use newly implemented getDocumentURI() for logging.
This commit is contained in:
edburns%acm.org 2007-03-06 22:03:43 +00:00
Родитель 563938030b
Коммит 8d813e255b
17 изменённых файлов: 208 добавлений и 881 удалений

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

@ -7,8 +7,6 @@
package cardemo;
import java.awt.Robot;
import java.awt.event.InputEvent;
import org.mozilla.mcp.MCP;
import org.mozilla.webclient.WebclientTestCase;
import org.w3c.dom.Element;
@ -41,19 +39,9 @@ public class CarDemoTest extends WebclientTestCase {
public void testCardemo() throws Exception {
mcp.getRealizedVisibleBrowserWindow();
mcp.blockingLoad("http://webdev1.sun.com/jsf-ajax-cardemo/faces/chooseLocale.jsp");
Element germanButton = mcp.getElementInCurrentPageById("j_id_id73:Germany");
assertNotNull(germanButton);
String clientX = germanButton.getAttribute("clientX");
String clientY = germanButton.getAttribute("clientY");
assertNotNull(clientX);
assertNotNull(clientY);
int x = Integer.valueOf(clientX).intValue();
int y = Integer.valueOf(clientY).intValue();
Robot robot = new Robot();
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
mcp.blockingClickElement("j_id_id73:Germany");
mcp.blockingClickElement("j_id_id18:j_id_id43");
Thread.currentThread().sleep(30000);
}

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

@ -62,6 +62,7 @@ public class DocumentImpl extends NodeImpl implements Document, DocumentEvent {
public native Event createEvent(String type);
public native NodeList getElementsByTagNameNS(String namespaceURI, String localName);
public native Element getElementById(String elementId);
public native String getDocumentURI();
public Node importNode(Node importedNode, boolean deep) throws DOMException {
throw new UnsupportedOperationException();
@ -77,10 +78,6 @@ public class DocumentImpl extends NodeImpl implements Document, DocumentEvent {
throw new UnsupportedOperationException();
}
public String getDocumentURI() {
throw new UnsupportedOperationException();
}
public DOMConfiguration getDomConfig() {
throw new UnsupportedOperationException();
}

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

@ -20,12 +20,12 @@
package org.mozilla.dom.util;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.IOException;
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;
@ -222,6 +222,58 @@ public class DOMTreeDumper {
dumpDocument(doc);
dbg("finished dumping...");
}
public String dump(Document doc) {
ByteArrayOutputStream baos = null;
baos = new ByteArrayOutputStream(1024);
ps = new PrintStream(baos);
dbg("dumping to String");
dumpDocument(doc);
dbg("finished dumping...");
return baos.toString();
}
private Element findElementWithName(Element start, String name) {
Element result = null;
Node child = null;
String elementName = start.getAttribute("name");
if (null != elementName && elementName.equals(name)) {
return start;
}
else {
NodeList children = start.getChildNodes();
int length = 0;
boolean hasChildren = ((children != null) &&
((length = children.getLength()) > 0));
if (!hasChildren) {
return result;
}
for (int i=0; i < length; i++) {
child = children.item(i);
result = null;
if (child instanceof Element) {
result = findElementWithName((Element) child, name);
}
if (null != result) {
break;
}
}
}
return result;
}
public Element findFirstElementWithName(Document doc, String name) {
Element result = null;
result = doc.getDocumentElement();
if (null != result) {
result.normalize();
result = findElementWithName(result, name);
}
return result;
}
private void dbg(String str) {
if (debug) {

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

@ -19,7 +19,7 @@
* Louis-Philippe Gagnon <louisphilippe@macadamian.com>
*/
package org.mozilla.webclient.test;
package org.mozilla.dom.util;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeModel;
@ -30,7 +30,7 @@ import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.Vector;
class DOMTreeModel implements TreeModel, DOMTreeNotifier {
public class DOMTreeModel implements TreeModel, DOMTreeNotifier {
private Node rootNode;
private Vector treeModelListeners = new Vector();

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

@ -16,11 +16,11 @@
* Contributor(s): Igor Kushnirskiy <idk@eng.sun.com>
*/
package org.mozilla.webclient.test;
package org.mozilla.dom.util;
import javax.swing.event.TreeModelEvent;
interface DOMTreeNotifier {
public interface DOMTreeNotifier {
/*
* Invoked after a node (or a set of siblings) has changed in some way.
*/

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

@ -20,7 +20,9 @@
*/
#include "prlog.h"
#include "nsCOMPtr.h"
#include "nsIDOMDocument.h"
#include "nsIDOM3Document.h"
#include "nsIDOMAttr.h"
#include "nsIDOMComment.h"
#include "nsIDOMElement.h"
@ -757,3 +759,40 @@ JNIEXPORT jobject JNICALL Java_org_mozilla_dom_DocumentImpl_getElementById
return JavaDOMGlobals::CreateNodeSubtype(env, (nsIDOMNode*)element);
}
/*
* Class: org_mozilla_dom_DocumentImpl
* Method: getDocumentURI
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_org_mozilla_dom_DocumentImpl_getDocumentURI
(JNIEnv *env, jobject jthis)
{
nsIDOMDocument* doc = (nsIDOMDocument*)
env->GetLongField(jthis, JavaDOMGlobals::nodePtrFID);
jstring result = nsnull;
nsresult rv = NS_OK;
nsCOMPtr<nsIDOM3Document> dom3 = do_QueryInterface(doc, &rv);
if (NS_FAILED(rv) || !dom3) {
JavaDOMGlobals::ThrowException(env,
"Document.getDocumentURI: failed", rv);
return result;
}
nsString ret;
rv = dom3->GetDocumentURI(ret);
if (NS_FAILED(rv)) {
JavaDOMGlobals::ThrowException(env,
"Document.getDocumentURI: failed", rv);
return result;
}
result = env->NewString((jchar*) ret.get(), ret.Length());
if (!result) {
PR_LOG(JavaDOMGlobals::log, PR_LOG_ERROR,
("Document.getDocumentURI: NewString failed\n"));
}
return result;
}

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

@ -9,12 +9,16 @@
package org.mozilla.mcp;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.mozilla.dom.util.DOMTreeDumper;
import org.mozilla.webclient.BrowserControl;
import org.mozilla.webclient.BrowserControlCanvas;
import org.mozilla.webclient.BrowserControlFactory;
@ -50,13 +54,14 @@ public class MCP {
private Navigation2 navigation = null;
private EventRegistration2 eventRegistration = null;
private PageInfoListener pageInfoListener = null;
private CurrentPage2 currentPage = null;
private Frame frame = null;
private int x = 0;
private int y = 0;
private int width = 1280;
private int height = 960;
private boolean initialized = false;
private Robot robot;
private DOMTreeDumper treeDumper = null;
public void setAppData(String absolutePathToNativeBrowserBinDir)
throws FileNotFoundException,
@ -65,7 +70,14 @@ public class MCP {
initialized = true;
}
void setBounds(int x, int y, int width, int height) {
private DOMTreeDumper getDOMTreeDumper() {
if (null == treeDumper) {
treeDumper = new DOMTreeDumper("MCP", false);
}
return treeDumper;
}
public void setBounds(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
@ -108,20 +120,20 @@ public class MCP {
}
private CurrentPage2 getCurrentPage() {
if (null == currentPage) {
try {
currentPage = (CurrentPage2)
getBrowserControl().queryInterface(BrowserControl.CURRENT_PAGE_NAME);
}
catch (Throwable th) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.throwing(this.getClass().getName(), "getCurrentPage",
th);
LOGGER.severe("Unable to obtain CurrentPage2 reference from BrowserControl");
}
}
CurrentPage2 currentPage = null;
try {
currentPage = (CurrentPage2)
getBrowserControl().queryInterface(BrowserControl.CURRENT_PAGE_NAME);
}
catch (Throwable th) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.throwing(this.getClass().getName(), "getCurrentPage",
th);
LOGGER.severe("Unable to obtain CurrentPage2 reference from BrowserControl");
}
}
return currentPage;
}
@ -184,14 +196,68 @@ public class MCP {
return frame;
}
public Element getElementInCurrentPageById(String id) {
public Element findElement(String id) {
Element result = null;
Document dom = getCurrentPage().getDOM();
result = dom.getElementById(id);
try {
result = dom.getElementById(id);
}
catch (Exception e) {
}
if (null == result) {
result = getDOMTreeDumper().findFirstElementWithName(dom, id);
}
return result;
}
public void clickElement(String id) {
Element element = findElement(id);
String clientX = null, clientY = null;
if (null != element) {
clientX = element.getAttribute("clientX");
clientY = element.getAttribute("clientY");
int x,y;
if (null != clientX && null != clientY) {
try {
x = Integer.valueOf(clientX).intValue();
y = Integer.valueOf(clientY).intValue();
Robot robot = getRobot();
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (NumberFormatException ex) {
LOGGER.throwing(this.getClass().getName(), "clickElementGivenId",
ex);
ex.printStackTrace();
throw new IllegalStateException(ex);
}
}
}
if (null == element || null == clientX || null == clientY) {
throw new IllegalStateException("Unable to click element " + id);
}
}
public void blockingClickElement(String idOrName) {
synchronized (this) {
try {
clickElement(idOrName);
this.wait();
}
catch (IllegalStateException ise) {
LOGGER.throwing(this.getClass().getName(), "blockingClickElementGivenId",
ise);
}
catch (InterruptedException ie) {
LOGGER.throwing(this.getClass().getName(), "blockingClickElementGivenId",
ie);
}
}
}
public void blockingLoad(String url) {
Navigation2 nav = getNavigation();
synchronized (this) {
@ -206,6 +272,19 @@ public class MCP {
}
}
private Robot getRobot() {
if (null == robot) {
try {
robot = new Robot();
} catch (AWTException ex) {
LOGGER.throwing(this.getClass().getName(), "getRobot",
ex);
ex.printStackTrace();
}
}
return robot;
}
private class PageInfoListenerImpl implements PageInfoListener {
private MCP owner = null;
PageInfoListenerImpl(MCP owner) {

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

@ -1,231 +0,0 @@
/*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (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 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): Denis Sharypov <sdv@sparc.spb.su>
*
*/
package org.mozilla.webclient.impl;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.IOException;
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;
public class DOMTreeDumper {
private String prefix = "DOMTreeDumper";
private boolean debug;
private PrintStream ps;
private boolean inA;
private final String[] endTagForbiddenNames = {"AREA",
"BASE",
"BASEFONT",
"BR",
"COL",
"FRAME",
"HR",
"IMG",
"INPUT",
"ISINDEX",
"LINK",
"META",
"PARAM"};
public DOMTreeDumper() {
this(true);
}
public DOMTreeDumper(boolean debug) {
this.debug = debug;
}
private void dumpDocument(Document doc) {
if (doc == null) return;
Element element = doc.getDocumentElement();
if (element == null) return;
element.normalize();
// DocumentType dt = doc.getDoctype();
// dumpNode(dt);
dumpNode(element);
ps.println();
ps.flush();
element = null;
doc = null;
}
private void dumpNode(Node node) {
dumpNode(node, false);
}
private void dumpNode(Node node, boolean isMapNode) {
if (node == null) {
return;
}
int type = node.getNodeType();
String name = node.getNodeName();
String value = node.getNodeValue();
switch (type) {
case Node.ELEMENT_NODE:
if (name.equals("A")) inA = true;
if (!(inA || name.equals("BR"))) {
ps.println();
}
ps.print("<" + name);
dumpAttributes(node);
ps.print(">");
dumpChildren(node);
if (name.equals("A")) inA = false;
if (!endTagForbidden(name)) {
ps.print("</" + node.getNodeName() + ">");
}
break;
case Node.ATTRIBUTE_NODE:
ps.print(" " + name.toUpperCase() + "=\"" + value + "\"");
break;
case Node.TEXT_NODE:
if (!node.getParentNode().getNodeName().equals("PRE")) {
value = value.trim();
}
if (!value.equals("")) {
if (!inA) {
ps.println();
}
ps.print(canonicalize(value));
}
break;
case Node.COMMENT_NODE:
ps.print("\n<!--" + value + "-->");
break;
case Node.CDATA_SECTION_NODE:
case Node.ENTITY_REFERENCE_NODE:
case Node.ENTITY_NODE:
case Node.PROCESSING_INSTRUCTION_NODE:
case Node.DOCUMENT_NODE:
case Node.DOCUMENT_TYPE_NODE:
case Node.DOCUMENT_FRAGMENT_NODE:
case Node.NOTATION_NODE:
ps.println("\n<!-- NOT HANDLED: " + name +
" value=" + value + " -->");
break;
}
}
private void dumpAttributes(Node node) {
NamedNodeMap map = node.getAttributes();
if (map == null) return;
int length = map.getLength();
for (int i=0; i < length; i++) {
Node item = map.item(i);
dumpNode(item, true);
}
}
private void dumpChildren(Node node) {
NodeList children = node.getChildNodes();
int length = 0;
boolean hasChildren = ((children != null) && ((length = children.getLength()) > 0));
if (!hasChildren) {
return;
}
for (int i=0; i < length; i++) {
dumpNode(children.item(i), false);
}
if (!inA) {
ps.println();
}
}
private String canonicalize(String str) {
StringBuffer in = new StringBuffer(str);
int length = in.length();
StringBuffer out = new StringBuffer(length);
char c;
for (int i = 0; i < length; i++) {
switch (c = in.charAt(i)) {
case '&' :
out.append("&amp;");
break;
case '<':
out.append("&lt;");
break;
case '>':
out.append("&gt;");
break;
case '\u00A0':
out.append("&nbsp;");
break;
default:
out.append(c);
}
}
return out.toString();
}
private boolean endTagForbidden(String name) {
for (int i = 0; i < endTagForbiddenNames.length; i++) {
if (name.equals(endTagForbiddenNames[i])) {
return true;
}
}
return false;
}
public void dumpToFile(String fileName, Document doc) {
try {
FileOutputStream fos = new FileOutputStream(fileName);
ps = new PrintStream(new BufferedOutputStream(fos, 1024));
} catch (IOException ex) {
ex.printStackTrace();
return;
}
dbg("dumping to " + fileName);
dumpDocument(doc);
dbg("finished dumping...");
}
public String dump(Document doc) {
ByteArrayOutputStream baos = null;
baos = new ByteArrayOutputStream(1024);
ps = new PrintStream(baos);
dbg("dumping to String");
dumpDocument(doc);
dbg("finished dumping...");
return baos.toString();
}
private void dbg(String str) {
if (debug) {
System.out.println(prefix + ": " + str);
}
}
}

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

@ -34,7 +34,6 @@ import org.mozilla.webclient.BrowserControl;
import org.mozilla.webclient.CurrentPage2;
import org.mozilla.webclient.Selection;
import org.mozilla.webclient.impl.WrapperFactory;
import org.mozilla.webclient.impl.DOMTreeDumper;
import java.util.Properties;
import java.util.logging.Level;
@ -46,6 +45,7 @@ import org.w3c.dom.Node;
import org.mozilla.webclient.UnimplementedException;
import org.mozilla.dom.DOMAccessor;
import org.mozilla.dom.util.DOMTreeDumper;
import org.mozilla.util.Log;
public class CurrentPageImpl extends ImplObjectNative implements CurrentPage2,
@ -297,6 +297,10 @@ public Document getDOM()
{
// PENDING(edburns): run this on the event thread.
Document result = nativeGetDOM(getNativeBrowserControl());
if (LOGGER.isLoggable((Level.INFO))) {
LOGGER.info("CurrentPageImpl.getDOM(): getting DOM with URI: " +
result.getDocumentURI());
}
return result;
}

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

@ -21,6 +21,7 @@ package org.mozilla.webclient.test;
import java.awt.*;
import java.awt.event.*;
import org.mozilla.dom.util.DOMTreeNotifier;
import org.w3c.dom.*;
import org.mozilla.dom.*;
import javax.swing.*;

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

@ -1,215 +0,0 @@
/*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (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 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): Denis Sharypov <sdv@sparc.spb.su>
*
*/
package org.mozilla.webclient.test;
import java.io.BufferedOutputStream;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.IOException;
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;
public class DOMTreeDumper {
private String prefix = "DOMTreeDumper";
private boolean debug;
private PrintStream ps;
private boolean inA;
private final String[] endTagForbiddenNames = {"AREA",
"BASE",
"BASEFONT",
"BR",
"COL",
"FRAME",
"HR",
"IMG",
"INPUT",
"ISINDEX",
"LINK",
"META",
"PARAM"};
DOMTreeDumper() {
this(true);
}
DOMTreeDumper(boolean debug) {
this.debug = debug;
}
private void dumpDocument(Document doc) {
if (doc == null) return;
Element element = doc.getDocumentElement();
if (element == null) return;
element.normalize();
// DocumentType dt = doc.getDoctype();
// dumpNode(dt);
dumpNode(element);
ps.println();
ps.flush();
element = null;
doc = null;
}
private void dumpNode(Node node) {
dumpNode(node, false);
}
private void dumpNode(Node node, boolean isMapNode) {
if (node == null) {
return;
}
int type = node.getNodeType();
String name = node.getNodeName();
String value = node.getNodeValue();
switch (type) {
case Node.ELEMENT_NODE:
if (name.equals("A")) inA = true;
if (!(inA || name.equals("BR"))) {
ps.println();
}
ps.print("<" + name);
dumpAttributes(node);
ps.print(">");
dumpChildren(node);
if (name.equals("A")) inA = false;
if (!endTagForbidden(name)) {
ps.print("</" + node.getNodeName() + ">");
}
break;
case Node.ATTRIBUTE_NODE:
ps.print(" " + name.toUpperCase() + "=\"" + value + "\"");
break;
case Node.TEXT_NODE:
if (!node.getParentNode().getNodeName().equals("PRE")) {
value = value.trim();
}
if (!value.equals("")) {
if (!inA) {
ps.println();
}
ps.print(canonicalize(value));
}
break;
case Node.COMMENT_NODE:
ps.print("\n<!--" + value + "-->");
break;
case Node.CDATA_SECTION_NODE:
case Node.ENTITY_REFERENCE_NODE:
case Node.ENTITY_NODE:
case Node.PROCESSING_INSTRUCTION_NODE:
case Node.DOCUMENT_NODE:
case Node.DOCUMENT_TYPE_NODE:
case Node.DOCUMENT_FRAGMENT_NODE:
case Node.NOTATION_NODE:
ps.println("\n<!-- NOT HANDLED: " + name +
" value=" + value + " -->");
break;
}
}
private void dumpAttributes(Node node) {
NamedNodeMap map = node.getAttributes();
if (map == null) return;
int length = map.getLength();
for (int i=0; i < length; i++) {
Node item = map.item(i);
dumpNode(item, true);
}
}
private void dumpChildren(Node node) {
NodeList children = node.getChildNodes();
int length = 0;
boolean hasChildren = ((children != null) && ((length = children.getLength()) > 0));
if (!hasChildren) {
return;
}
for (int i=0; i < length; i++) {
dumpNode(children.item(i), false);
}
if (!inA) {
ps.println();
}
}
private String canonicalize(String str) {
StringBuffer in = new StringBuffer(str);
int length = in.length();
StringBuffer out = new StringBuffer(length);
char c;
for (int i = 0; i < length; i++) {
switch (c = in.charAt(i)) {
case '&' :
out.append("&amp;");
break;
case '<':
out.append("&lt;");
break;
case '>':
out.append("&gt;");
break;
case '\u00A0':
out.append("&nbsp;");
break;
default:
out.append(c);
}
}
return out.toString();
}
private boolean endTagForbidden(String name) {
for (int i = 0; i < endTagForbiddenNames.length; i++) {
if (name.equals(endTagForbiddenNames[i])) {
return true;
}
}
return false;
}
public void dumpToFile(String fileName, Document doc) {
try {
FileOutputStream fos = new FileOutputStream(fileName);
ps = new PrintStream(new BufferedOutputStream(fos, 1024));
} catch (IOException ex) {
ex.printStackTrace();
return;
}
dbg("dumping to " + fileName);
dumpDocument(doc);
dbg("finished dumping...");
}
private void dbg(String str) {
if (debug) {
System.out.println(prefix + ": " + str);
}
}
}

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

@ -1,116 +0,0 @@
/*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (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 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): Igor Kushnirskiy <idk@eng.sun.com>
* Ed Burns <edburns@acm.org>
* Jason Mawdsley <jason@macadamian.com>
* Louis-Philippe Gagnon <louisphilippe@macadamian.com>
*/
package org.mozilla.webclient.test;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeModel;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.Vector;
class DOMTreeModel implements TreeModel, DOMTreeNotifier {
private Node rootNode;
private Vector treeModelListeners = new Vector();
public DOMTreeModel(Node node) {
rootNode = node;
}
public void addTreeModelListener(TreeModelListener l) {
// use addElement instead of add for jdk1.1.x compatibility.
treeModelListeners.addElement(l);
}
public void removeTreeModelListener(TreeModelListener l) {
treeModelListeners.removeElement(l);
}
public Object getChild(Object parent, int index) {
return ((Node)parent).getChildNodes().item(index);
}
public int getChildCount(Object parent) {
return ((Node)parent).getChildNodes().getLength();
}
public int getIndexOfChild(Object parent, Object child) {
NodeList childNodes = ((Node)parent).getChildNodes();
int res = -1;
int length = childNodes.getLength();
for (int i = 0; i < length; i++) {
if (childNodes.item(i) == child) {
res = i;
break;
}
}
return res;
}
public Object getRoot() {
return rootNode;
}
public boolean isLeaf(Object node) {
return getChildCount(node) == 0;
}
public void valueForPathChanged(TreePath path, Object newValue) {
return;
}
/*
* Invoked after a node (or a set of siblings) has changed in some way.
*/
public void treeNodesChanged(TreeModelEvent e) {
for (int i = 0; i < treeModelListeners.size() ; i++) {
((TreeModelListener)treeModelListeners.elementAt(i)).
treeNodesChanged(e);
}
}
/*
* Invoked after nodes have been inserted into the tree.
*/
public void treeNodesInserted(TreeModelEvent e) {
for (int i = 0; i < treeModelListeners.size(); i++) {
((TreeModelListener)treeModelListeners.elementAt(i)).
treeNodesInserted(e);
}
}
/*
* Invoked after nodes have been removed from the tree.
*/
public void treeNodesRemoved(TreeModelEvent e) {
for (int i = 0; i < treeModelListeners.size(); i++) {
((TreeModelListener)treeModelListeners.elementAt(i)).
treeNodesRemoved(e);
}
}
/*
* Invoked after the tree has drastically changed structure from a given node down.
*/
public void treeStructureChanged(TreeModelEvent e) {
for (int i = 0; i < treeModelListeners.size(); i++) {
((TreeModelListener)treeModelListeners.elementAt(i)).
treeStructureChanged(e);
}
}
}

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

@ -1,43 +0,0 @@
/*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (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 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): Igor Kushnirskiy <idk@eng.sun.com>
*/
package org.mozilla.webclient.test;
import javax.swing.event.TreeModelEvent;
interface DOMTreeNotifier {
/*
* Invoked after a node (or a set of siblings) has changed in some way.
*/
public void treeNodesChanged(TreeModelEvent e);
/*
* Invoked after nodes have been inserted into the tree.
*/
public void treeNodesInserted(TreeModelEvent e);
/*
* Invoked after nodes have been removed from the tree.
*/
public void treeNodesRemoved(TreeModelEvent e);
/*
* Invoked after the tree has drastically changed structure from a given node down.
*/
public void treeStructureChanged(TreeModelEvent e);
};

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

@ -27,6 +27,7 @@
package org.mozilla.webclient.test;
import org.mozilla.dom.util.DOMTreeModel;
import org.mozilla.util.Assert;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@ -57,7 +58,7 @@ import java.util.Stack;
* A dom viewer Frame
*
* @version $Id: DOMViewerFrame.java,v 1.7 2001/04/03 01:37:28 edburns%acm.org Exp $
* @version $Id: DOMViewerFrame.java,v 1.8 2007/03/06 22:03:43 edburns%acm.org Exp $
*
* @see org.mozilla.webclient.BrowserControlFactory

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

@ -21,8 +21,9 @@ package org.mozilla.webclient.test;
import java.awt.*;
import java.awt.event.*;
import org.mozilla.dom.util.DOMTreeNotifier;
import org.mozilla.dom.util.DOMTreeDumper;
import org.w3c.dom.*;
import org.mozilla.dom.*;
import javax.swing.*;
import javax.swing.tree.*; //idk
import javax.swing.border.*;

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

@ -1,231 +0,0 @@
/*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (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 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): Denis Sharypov <sdv@sparc.spb.su>
*
*/
package org.mozilla.webclient.test;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.IOException;
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;
public class DOMTreeDumper {
private String prefix = "DOMTreeDumper";
private boolean debug;
private PrintStream ps;
private boolean inA;
private final String[] endTagForbiddenNames = {"AREA",
"BASE",
"BASEFONT",
"BR",
"COL",
"FRAME",
"HR",
"IMG",
"INPUT",
"ISINDEX",
"LINK",
"META",
"PARAM"};
DOMTreeDumper() {
this(true);
}
DOMTreeDumper(boolean debug) {
this.debug = debug;
}
private void dumpDocument(Document doc) {
if (doc == null) return;
Element element = doc.getDocumentElement();
if (element == null) return;
element.normalize();
// DocumentType dt = doc.getDoctype();
// dumpNode(dt);
dumpNode(element);
ps.println();
ps.flush();
element = null;
doc = null;
}
private void dumpNode(Node node) {
dumpNode(node, false);
}
private void dumpNode(Node node, boolean isMapNode) {
if (node == null) {
return;
}
int type = node.getNodeType();
String name = node.getNodeName();
String value = node.getNodeValue();
switch (type) {
case Node.ELEMENT_NODE:
if (name.equals("A")) inA = true;
if (!(inA || name.equals("BR"))) {
ps.println();
}
ps.print("<" + name);
dumpAttributes(node);
ps.print(">");
dumpChildren(node);
if (name.equals("A")) inA = false;
if (!endTagForbidden(name)) {
ps.print("</" + node.getNodeName() + ">");
}
break;
case Node.ATTRIBUTE_NODE:
ps.print(" " + name.toUpperCase() + "=\"" + value + "\"");
break;
case Node.TEXT_NODE:
if (!node.getParentNode().getNodeName().equals("PRE")) {
value = value.trim();
}
if (!value.equals("")) {
if (!inA) {
ps.println();
}
ps.print(canonicalize(value));
}
break;
case Node.COMMENT_NODE:
ps.print("\n<!--" + value + "-->");
break;
case Node.CDATA_SECTION_NODE:
case Node.ENTITY_REFERENCE_NODE:
case Node.ENTITY_NODE:
case Node.PROCESSING_INSTRUCTION_NODE:
case Node.DOCUMENT_NODE:
case Node.DOCUMENT_TYPE_NODE:
case Node.DOCUMENT_FRAGMENT_NODE:
case Node.NOTATION_NODE:
ps.println("\n<!-- NOT HANDLED: " + name +
" value=" + value + " -->");
break;
}
}
private void dumpAttributes(Node node) {
NamedNodeMap map = node.getAttributes();
if (map == null) return;
int length = map.getLength();
for (int i=0; i < length; i++) {
Node item = map.item(i);
dumpNode(item, true);
}
}
private void dumpChildren(Node node) {
NodeList children = node.getChildNodes();
int length = 0;
boolean hasChildren = ((children != null) && ((length = children.getLength()) > 0));
if (!hasChildren) {
return;
}
for (int i=0; i < length; i++) {
dumpNode(children.item(i), false);
}
if (!inA) {
ps.println();
}
}
private String canonicalize(String str) {
StringBuffer in = new StringBuffer(str);
int length = in.length();
StringBuffer out = new StringBuffer(length);
char c;
for (int i = 0; i < length; i++) {
switch (c = in.charAt(i)) {
case '&' :
out.append("&amp;");
break;
case '<':
out.append("&lt;");
break;
case '>':
out.append("&gt;");
break;
case '\u00A0':
out.append("&nbsp;");
break;
default:
out.append(c);
}
}
return out.toString();
}
private boolean endTagForbidden(String name) {
for (int i = 0; i < endTagForbiddenNames.length; i++) {
if (name.equals(endTagForbiddenNames[i])) {
return true;
}
}
return false;
}
public void dumpToFile(String fileName, Document doc) {
try {
FileOutputStream fos = new FileOutputStream(fileName);
ps = new PrintStream(new BufferedOutputStream(fos, 1024));
} catch (IOException ex) {
ex.printStackTrace();
return;
}
dbg("dumping to " + fileName);
dumpDocument(doc);
dbg("finished dumping...");
}
public String dump(Document doc) {
ByteArrayOutputStream baos = null;
baos = new ByteArrayOutputStream(1024);
ps = new PrintStream(baos);
dbg("dumping to String");
dumpDocument(doc);
dbg("finished dumping...");
return baos.toString();
}
private void dbg(String str) {
if (debug) {
System.out.println(prefix + ": " + str);
}
}
}

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

@ -27,6 +27,7 @@
package org.mozilla.webclient.test;
import org.mozilla.dom.util.DOMTreeModel;
import org.mozilla.util.Assert;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@ -57,7 +58,7 @@ import java.util.Stack;
* A dom viewer Frame
*
* @version $Id: DOMViewerFrame.java,v 1.1 2005/04/17 20:19:46 edburns%acm.org Exp $
* @version $Id: DOMViewerFrame.java,v 1.2 2007/03/06 22:03:43 edburns%acm.org Exp $
*
* @see org.mozilla.webclient.BrowserControlFactory