mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-09 19:35:51 +00:00
This checkin re-implements CurrentPage.getSource(). It doesn't preserve
the whitespace formatting of the current page, which is a big problem. Next step is to get the DOMViewer working with the TestBrowser. M build.xml - exclude test from the default build A classes_spec/org/mozilla/webclient/impl/DOMTreeDumper.java - Copy this over from test. M classes_spec/org/mozilla/webclient/impl/wrapper_native/CurrentPageImpl.java - leverage DOMTreeDumper to print the current source. M test/automated/src/classes/org/mozilla/webclient/CurrentPageTest.java A test/automated/src/test/ViewSourceTest.html - exercise new methods.
This commit is contained in:
parent
1724b1b1e3
commit
0535edc189
@ -101,7 +101,7 @@
|
||||
<include name="org/mozilla/webclient/*"/>
|
||||
<include name="org/mozilla/webclient/impl/wrapper_native/*"/>
|
||||
<include name="org/mozilla/webclient/impl/*"/>
|
||||
<include name="org/mozilla/webclient/test/*"/>
|
||||
<exclude name="org/mozilla/webclient/test/**/*.*"/>
|
||||
</javac>
|
||||
|
||||
<mkdir dir="${build.home}/META-INF/services"/>
|
||||
|
@ -0,0 +1,231 @@
|
||||
/*
|
||||
* 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("&");
|
||||
break;
|
||||
case '<':
|
||||
out.append("<");
|
||||
break;
|
||||
case '>':
|
||||
out.append(">");
|
||||
break;
|
||||
case '\u00A0':
|
||||
out.append(" ");
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -30,6 +30,7 @@ import org.mozilla.webclient.CurrentPage2;
|
||||
import org.mozilla.webclient.Selection;
|
||||
import org.mozilla.webclient.WindowControl;
|
||||
import org.mozilla.webclient.impl.WrapperFactory;
|
||||
import org.mozilla.webclient.impl.DOMTreeDumper;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.io.*;
|
||||
@ -63,6 +64,8 @@ private static boolean domInitialized = false;
|
||||
|
||||
// Relationship Instance Variables
|
||||
|
||||
private DOMTreeDumper domDumper = null;
|
||||
|
||||
//
|
||||
// Constructors and Initializers
|
||||
//
|
||||
@ -75,6 +78,7 @@ public CurrentPageImpl(WrapperFactory yourFactory,
|
||||
if (!domInitialized) {
|
||||
DOMAccessor.initialize();
|
||||
}
|
||||
domDumper = new DOMTreeDumper();
|
||||
}
|
||||
|
||||
//
|
||||
@ -221,63 +225,25 @@ public Properties getPageInfo()
|
||||
public String getSource()
|
||||
{
|
||||
getWrapperFactory().verifyInitialized();
|
||||
String HTMLContent = new String();
|
||||
String currURL = getCurrentURL();
|
||||
System.out.println("\nThe Current URL is -- " + currURL);
|
||||
try {
|
||||
URL aURL = new URL(currURL);
|
||||
URLConnection connection = aURL.openConnection();
|
||||
connection.connect();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
boolean more = true;
|
||||
while (more)
|
||||
{
|
||||
String line = in.readLine();
|
||||
if (line == null) more = false;
|
||||
else
|
||||
{
|
||||
HTMLContent = HTMLContent + line;
|
||||
}
|
||||
}
|
||||
Document doc = getDOM();
|
||||
String HTMLContent = null;
|
||||
|
||||
if (null != doc) {
|
||||
HTMLContent = domDumper.dump(doc);
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
System.out.println("Error occurred while establishing connection -- \n ERROR - " + e);
|
||||
}
|
||||
|
||||
return HTMLContent;
|
||||
}
|
||||
|
||||
public byte [] getSourceBytes()
|
||||
{
|
||||
byte [] result = null;
|
||||
getWrapperFactory().verifyInitialized();
|
||||
byte [] result = null;
|
||||
|
||||
|
||||
String HTMLContent = new String();
|
||||
String currURL = getCurrentURL();
|
||||
System.out.println("\nThe Current URL is -- " + currURL);
|
||||
try {
|
||||
URL aURL = new URL(currURL);
|
||||
URLConnection connection = aURL.openConnection();
|
||||
connection.connect();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
boolean more = true;
|
||||
while (more)
|
||||
{
|
||||
String line = in.readLine();
|
||||
if (line == null) more = false;
|
||||
else
|
||||
{
|
||||
HTMLContent = HTMLContent + line;
|
||||
}
|
||||
}
|
||||
String HTMLContent = getSource();
|
||||
if (null != HTMLContent) {
|
||||
result = HTMLContent.getBytes();
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
System.out.println("Error occurred while establishing connection -- \n ERROR - " + e);
|
||||
}
|
||||
result = HTMLContent.getBytes();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: CurrentPageTest.java,v 1.9 2005/02/14 02:37:51 edburns%acm.org Exp $
|
||||
* $Id: CurrentPageTest.java,v 1.10 2005/03/22 15:58:51 edburns%acm.org Exp $
|
||||
*/
|
||||
|
||||
/*
|
||||
@ -497,4 +497,57 @@ public class CurrentPageTest extends WebclientTestCase implements ClipboardOwner
|
||||
BrowserControlFactory.deleteBrowserControl(firstBrowserControl);
|
||||
}
|
||||
|
||||
public void testGetSource() throws Exception {
|
||||
BrowserControl firstBrowserControl = null;
|
||||
DocumentLoadListenerImpl listener = null;
|
||||
Selection selection = null;
|
||||
firstBrowserControl = BrowserControlFactory.newBrowserControl();
|
||||
assertNotNull(firstBrowserControl);
|
||||
BrowserControlCanvas canvas = (BrowserControlCanvas)
|
||||
firstBrowserControl.queryInterface(BrowserControl.BROWSER_CONTROL_CANVAS_NAME);
|
||||
eventRegistration = (EventRegistration2)
|
||||
firstBrowserControl.queryInterface(BrowserControl.EVENT_REGISTRATION_NAME);
|
||||
|
||||
assertNotNull(canvas);
|
||||
Frame frame = new Frame();
|
||||
frame.setUndecorated(true);
|
||||
frame.setBounds(0, 0, 640, 480);
|
||||
frame.add(canvas, BorderLayout.CENTER);
|
||||
frame.setVisible(true);
|
||||
canvas.setVisible(true);
|
||||
|
||||
Navigation2 nav = (Navigation2)
|
||||
firstBrowserControl.queryInterface(BrowserControl.NAVIGATION_NAME);
|
||||
assertNotNull(nav);
|
||||
currentPage = (CurrentPage2)
|
||||
firstBrowserControl.queryInterface(BrowserControl.CURRENT_PAGE_NAME);
|
||||
|
||||
assertNotNull(currentPage);
|
||||
|
||||
eventRegistration.addDocumentLoadListener(listener = new DocumentLoadListenerImpl() {
|
||||
public void doEndCheck() {
|
||||
CurrentPageTest.keepWaiting = false;
|
||||
}
|
||||
});
|
||||
|
||||
Thread.currentThread().sleep(3000);
|
||||
|
||||
|
||||
CurrentPageTest.keepWaiting = true;
|
||||
|
||||
nav.loadURL("http://localhost:5243/ViewSourceTest.html");
|
||||
|
||||
// keep waiting until the previous load completes
|
||||
while (CurrentPageTest.keepWaiting) {
|
||||
Thread.currentThread().sleep(1000);
|
||||
}
|
||||
|
||||
String source = currentPage.getSource();
|
||||
assertTrue(-1 != source.indexOf("<HTML>"));
|
||||
assertTrue(-1 != source.indexOf("</HTML>"));
|
||||
|
||||
frame.setVisible(false);
|
||||
BrowserControlFactory.deleteBrowserControl(firstBrowserControl);
|
||||
}
|
||||
|
||||
}
|
||||
|
15
java/webclient/test/automated/src/test/ViewSourceTest.html
Normal file
15
java/webclient/test/automated/src/test/ViewSourceTest.html
Normal file
@ -0,0 +1,15 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>View Source Test</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>View Source Test</h1>
|
||||
|
||||
|
||||
|
||||
<hr>
|
||||
<address><a href="mailto:b_edward@bellsouth.net"></a></address>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user