added a new example pluglet:
DOMViewer a=idk@eng.sun.com, sdv@sparc.spb.su r=idk@eng.sun.com, sdv@sparc.spb.su git-svn-id: svn://10.0.0.236/trunk@61229 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
parent
7ee2f2126f
commit
edaf56e8c7
390
mozilla/java/plugins/examples/dom/DOMAccessPanel.java
Normal file
390
mozilla/java/plugins/examples/dom/DOMAccessPanel.java
Normal file
@ -0,0 +1,390 @@
|
||||
/*
|
||||
* 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>
|
||||
* Igor Kushnirskiy <idk@eng.sun.com>
|
||||
*/
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import org.w3c.dom.*;
|
||||
import org.mozilla.dom.*;
|
||||
import javax.swing.*;
|
||||
import javax.swing.tree.*; //idk
|
||||
import javax.swing.border.*;
|
||||
import javax.swing.event.*;
|
||||
import javax.swing.JFileChooser;
|
||||
|
||||
import DOMTreeDumper;
|
||||
|
||||
public class DOMAccessPanel extends JPanel implements ActionListener, ItemListener, TreeSelectionListener {
|
||||
|
||||
private JTextField name, aValue;
|
||||
private JComboBox type, aName;
|
||||
private JTextArea value;
|
||||
private JButton newNode, insert, append, remove, set, removeAttr, save;
|
||||
private Node node, prv;
|
||||
private NamedNodeMap attrMap;
|
||||
private boolean updating;
|
||||
private String[] types = {
|
||||
"ELEMENT",
|
||||
"ATTRIBUTE",
|
||||
"TEXT",
|
||||
"CDATA_SECTION",
|
||||
"ENTITY_REF",
|
||||
"ENTITY",
|
||||
"PROC_INSTR",
|
||||
"COMMENT",
|
||||
"DOCUMENT",
|
||||
"DOCUMENT_TYPE",
|
||||
"DOC_FRAGM",
|
||||
"NOTATION"
|
||||
};
|
||||
|
||||
// private boolean debug = true;
|
||||
private boolean debug = false;
|
||||
|
||||
private Component tree;
|
||||
private DOMTreeNotifier treeNotifier;
|
||||
private TreePath nodePath;
|
||||
private DOMTreeDumper treeDumper;
|
||||
private JFileChooser chooser;
|
||||
|
||||
|
||||
public DOMAccessPanel(DOMTreeNotifier notifier) {
|
||||
|
||||
treeNotifier = notifier;
|
||||
GridBagLayout layout = new GridBagLayout();
|
||||
GridBagConstraints c = new GridBagConstraints();
|
||||
JPanel nodeInfo = new JPanel();
|
||||
nodeInfo.setLayout(layout);
|
||||
|
||||
|
||||
JLabel l = new JLabel("Name:");
|
||||
c.anchor = GridBagConstraints.WEST;
|
||||
c.fill = GridBagConstraints.NONE;
|
||||
layout.setConstraints(l, c);
|
||||
nodeInfo.add(l);
|
||||
|
||||
l = new JLabel("Type:");
|
||||
c.gridwidth = GridBagConstraints.REMAINDER; //end row
|
||||
layout.setConstraints(l, c);
|
||||
nodeInfo.add(l);
|
||||
|
||||
c.gridwidth = GridBagConstraints.RELATIVE; //new row
|
||||
name = new JTextField(10);
|
||||
layout.setConstraints(name, c);
|
||||
nodeInfo.add(name);
|
||||
|
||||
c.gridwidth = GridBagConstraints.REMAINDER; //end row
|
||||
type = new JComboBox(types);
|
||||
layout.setConstraints(type, c);
|
||||
nodeInfo.add(type);
|
||||
|
||||
// Attribute panel
|
||||
|
||||
JPanel attrPanel = new JPanel();
|
||||
GridBagLayout attrLayout = new GridBagLayout();
|
||||
attrPanel.setLayout(attrLayout);
|
||||
|
||||
c.gridwidth = GridBagConstraints.RELATIVE; //new row
|
||||
aName = new JComboBox();
|
||||
aName.setEditable(true);
|
||||
aName.addItemListener(this);
|
||||
attrLayout.setConstraints(aName, c);
|
||||
attrPanel.add(aName);
|
||||
|
||||
JPanel p = new JPanel();
|
||||
l = new JLabel("=");
|
||||
p.add(l);
|
||||
aValue = new JTextField(9);
|
||||
p.add(aValue);
|
||||
c.gridwidth = GridBagConstraints.REMAINDER; //end row
|
||||
attrLayout.setConstraints(p, c);
|
||||
attrPanel.add(p);
|
||||
|
||||
p = new JPanel();
|
||||
set = new JButton("Set");
|
||||
set.addActionListener(this);
|
||||
p.add(set);
|
||||
removeAttr = new JButton("Remove");
|
||||
removeAttr.setActionCommand("remove_attr");
|
||||
removeAttr.addActionListener(this);
|
||||
p.add(removeAttr);
|
||||
attrLayout.setConstraints(p, c);
|
||||
attrPanel.add(p);
|
||||
attrPanel.setBorder(new TitledBorder(new BevelBorder(BevelBorder.LOWERED), " Attributes "));
|
||||
layout.setConstraints(attrPanel, c);
|
||||
|
||||
// End Attribute panel
|
||||
|
||||
nodeInfo.add(attrPanel);
|
||||
|
||||
l = new JLabel("Value:");
|
||||
layout.setConstraints(l, c);
|
||||
nodeInfo.add(l);
|
||||
|
||||
value = new JTextArea(5, 23);
|
||||
value.addCaretListener (new CaretListener() {
|
||||
public void caretUpdate(CaretEvent e) {
|
||||
dbg("caret:");
|
||||
if (node == null || updating) return;
|
||||
String valueStr = value.getText();
|
||||
if (!valueStr.equals("")) {
|
||||
node.setNodeValue(valueStr);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
JScrollPane valueScrollPane = new JScrollPane(value);
|
||||
layout.setConstraints(valueScrollPane, c);
|
||||
nodeInfo.add(valueScrollPane);
|
||||
|
||||
JPanel nodeButtonPanel = new JPanel();
|
||||
GridLayout gl = new GridLayout(2, 2);
|
||||
nodeButtonPanel.setLayout(gl);
|
||||
newNode = new JButton("New");
|
||||
newNode.setToolTipText("Create a new node");
|
||||
newNode.addActionListener(this);
|
||||
nodeButtonPanel.add(newNode);
|
||||
insert = new JButton("Insert");
|
||||
insert.setToolTipText("Inserts a node before the selected node ");
|
||||
insert.addActionListener(this);
|
||||
nodeButtonPanel.add(insert);
|
||||
append = new JButton("Append");
|
||||
append.setToolTipText("Adds a node to the end of the list of children of the selected node");
|
||||
append.addActionListener(this);
|
||||
nodeButtonPanel.add(append);
|
||||
remove = new JButton("Remove");
|
||||
remove.setToolTipText("Removes the selected node from the tree");
|
||||
remove.addActionListener(this);
|
||||
nodeButtonPanel.add(remove);
|
||||
nodeButtonPanel.setBorder(new TitledBorder(new BevelBorder(BevelBorder.LOWERED), " Node Manipulation "));
|
||||
|
||||
c.fill = GridBagConstraints.HORIZONTAL;
|
||||
layout.setConstraints(nodeButtonPanel, c);
|
||||
|
||||
nodeInfo.add(nodeButtonPanel);
|
||||
nodeInfo.setBorder(new TitledBorder(new EtchedBorder(), " Node info "));
|
||||
|
||||
|
||||
layout = new GridBagLayout();
|
||||
setLayout(layout);
|
||||
layout.setConstraints(nodeInfo, c);
|
||||
add(nodeInfo);
|
||||
|
||||
save = new JButton("Dump Tree To File");
|
||||
save.setActionCommand("save");
|
||||
save.addActionListener(this);
|
||||
c.anchor = GridBagConstraints.CENTER;
|
||||
c.fill = GridBagConstraints.BOTH;
|
||||
layout.setConstraints(save, c);
|
||||
add(save);
|
||||
|
||||
setButtonState();
|
||||
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String command = e.getActionCommand();
|
||||
dbg("AttrInfoPanel.actionPerformed: " + command);
|
||||
if (command.equalsIgnoreCase("new")) {
|
||||
prv = node;
|
||||
updateInfo(null);
|
||||
type.setEnabled(true);
|
||||
insert.setEnabled(true);
|
||||
append.setEnabled(true);
|
||||
} else if (command.equalsIgnoreCase("insert")) {
|
||||
insertNode();
|
||||
treeNotifier.treeStructureChanged(
|
||||
new TreeModelEvent(this, nodePath.getParentPath()));
|
||||
} else if (command.equalsIgnoreCase("append")) {
|
||||
appendNode();
|
||||
treeNotifier.treeStructureChanged(
|
||||
new TreeModelEvent(this, nodePath.getParentPath()));
|
||||
} else if (command.equalsIgnoreCase("remove")) {
|
||||
if (node == null) return;
|
||||
Node parent = node.getParentNode();
|
||||
parent.removeChild(node);
|
||||
node = parent;
|
||||
treeNotifier.treeStructureChanged(new TreeModelEvent(this, nodePath.getParentPath()));
|
||||
} else if (command.equalsIgnoreCase("save")) {
|
||||
saveDoc();
|
||||
} else if (command.equalsIgnoreCase("set")) {
|
||||
((Element)node).setAttribute((String)aName.getSelectedItem(), aValue.getText());
|
||||
updateInfo(node);
|
||||
} else if (command.equalsIgnoreCase("remove_attr")) {
|
||||
removeAttribute();
|
||||
}
|
||||
}
|
||||
|
||||
public void itemStateChanged(ItemEvent e) {
|
||||
if ((node != null) && (aName.getSelectedItem() != null)) {
|
||||
set.setEnabled(true);
|
||||
if (((attrMap = node.getAttributes()) != null) &&
|
||||
(aName.getSelectedIndex() >=0)) {
|
||||
dbg("setting attr...");
|
||||
aValue.setText(attrMap.item(aName.getSelectedIndex()).getNodeValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void updateInfo(Node node) {
|
||||
updating = true;
|
||||
dbg("updateInfo" + node);
|
||||
this.node = node;
|
||||
setButtonState();
|
||||
name.setText("");
|
||||
value.setText("");
|
||||
aValue.setText("");
|
||||
aName.removeAllItems();
|
||||
if (node == null) {
|
||||
updating = false;
|
||||
return;
|
||||
}
|
||||
prv = node;
|
||||
name.setText(node.getNodeName());
|
||||
type.setSelectedIndex(node.getNodeType() - 1);
|
||||
dbg("1 update node name: " + node.getNodeName());
|
||||
dbg("1 update node value:" + node.getNodeValue());
|
||||
value.setText(node.getNodeValue());
|
||||
dbg("2 update node name: " + node.getNodeName());
|
||||
dbg("2 update node value:" + node.getNodeValue());
|
||||
attrMap = node.getAttributes();
|
||||
if (attrMap == null) {
|
||||
updating = false;
|
||||
return;
|
||||
}
|
||||
int length = attrMap.getLength();
|
||||
for (int i=0; i < length; i++) {
|
||||
Attr a = (Attr)attrMap.item(i);
|
||||
aName.addItem(a.getName());
|
||||
}
|
||||
if (length > 0) {
|
||||
aName.setSelectedIndex(0);
|
||||
aValue.setText(attrMap.item(0).getNodeValue());
|
||||
}
|
||||
updating = false;
|
||||
}
|
||||
|
||||
public void valueChanged(TreeSelectionEvent e) {
|
||||
dbg("DOMAccessPanel.valueChanged");
|
||||
if (e.isAddedPath()) {
|
||||
updateInfo((Node)e.getPath().getLastPathComponent());
|
||||
}
|
||||
nodePath = e.getPath();
|
||||
tree = (Component)e.getSource();
|
||||
}
|
||||
|
||||
private Node createNode() {
|
||||
int nodeType = type.getSelectedIndex();
|
||||
Document doc = prv.getOwnerDocument();
|
||||
Node newNode = null;
|
||||
switch (nodeType + 1) {
|
||||
case Node.ELEMENT_NODE:
|
||||
dbg("ELEMENT");
|
||||
newNode = doc.createElement(name.getText());
|
||||
break;
|
||||
case Node.ATTRIBUTE_NODE:
|
||||
dbg("ATTRIBUTE");
|
||||
return null;
|
||||
case Node.TEXT_NODE:
|
||||
dbg("TEXT");
|
||||
newNode = doc.createTextNode(value.getText());
|
||||
break;
|
||||
case Node.CDATA_SECTION_NODE:
|
||||
dbg("CDATA_SECTION");
|
||||
return null;
|
||||
case Node.ENTITY_REFERENCE_NODE:
|
||||
dbg("ENTITY_REFERENCE");
|
||||
return null;
|
||||
case Node.ENTITY_NODE:
|
||||
dbg("ENTITY");
|
||||
return null;
|
||||
case Node.PROCESSING_INSTRUCTION_NODE:
|
||||
dbg("PROCESSING_INSTRUCTION");
|
||||
return null;
|
||||
case Node.COMMENT_NODE:
|
||||
dbg("COMMENT");
|
||||
newNode = doc.createComment(value.getText());
|
||||
break;
|
||||
case Node.DOCUMENT_NODE:
|
||||
dbg("DOCUMENT");
|
||||
return null;
|
||||
case Node.DOCUMENT_TYPE_NODE:
|
||||
dbg("DOCUMENT_TYPE");
|
||||
return null;
|
||||
case Node.DOCUMENT_FRAGMENT_NODE:
|
||||
dbg("DOCUMENT_FRAGMENT");
|
||||
return null;
|
||||
case Node.NOTATION_NODE:
|
||||
dbg("NOTATION");
|
||||
return null;
|
||||
}
|
||||
return newNode;
|
||||
}
|
||||
|
||||
private void insertNode() {
|
||||
Node newNode = createNode();
|
||||
if (newNode == null) return;
|
||||
dbg("inserting...");
|
||||
prv.getParentNode().insertBefore(newNode, prv);
|
||||
}
|
||||
|
||||
private void appendNode() {
|
||||
Node newNode = createNode();
|
||||
if (newNode == null) return;
|
||||
dbg("appending...");
|
||||
prv.appendChild(newNode);
|
||||
}
|
||||
|
||||
private void removeAttribute() {
|
||||
((Element)node).removeAttribute((String)aName.getSelectedItem());
|
||||
updateInfo(node);
|
||||
}
|
||||
|
||||
private void saveDoc() {
|
||||
if (chooser == null) {
|
||||
chooser = new JFileChooser();
|
||||
}
|
||||
int returnVal = chooser.showSaveDialog(this);
|
||||
if(returnVal == JFileChooser.APPROVE_OPTION) {
|
||||
String fileName = chooser.getSelectedFile().getPath();
|
||||
if (treeDumper == null) {
|
||||
treeDumper = new DOMTreeDumper(debug);
|
||||
}
|
||||
treeDumper.dumpToFile(fileName, node.getOwnerDocument());
|
||||
}
|
||||
}
|
||||
|
||||
private void setButtonState() {
|
||||
boolean nodeExists = (node != null);
|
||||
boolean attrExists = nodeExists && !(node.getAttributes() == null || node.getAttributes().getLength() == 0);
|
||||
type.setEnabled(nodeExists);
|
||||
newNode.setEnabled(nodeExists);
|
||||
insert.setEnabled(nodeExists);
|
||||
append.setEnabled(nodeExists);
|
||||
save.setEnabled(nodeExists);
|
||||
remove.setEnabled(nodeExists);
|
||||
set.setEnabled(attrExists);
|
||||
removeAttr.setEnabled(attrExists);
|
||||
}
|
||||
|
||||
private void dbg(String str) {
|
||||
if (debug) {
|
||||
System.out.println(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
213
mozilla/java/plugins/examples/dom/DOMTreeDumper.java
Normal file
213
mozilla/java/plugins/examples/dom/DOMTreeDumper.java
Normal file
@ -0,0 +1,213 @@
|
||||
/*
|
||||
* 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>
|
||||
*
|
||||
*/
|
||||
|
||||
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("&");
|
||||
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...");
|
||||
}
|
||||
|
||||
private void dbg(String str) {
|
||||
if (debug) {
|
||||
System.out.println(prefix + ": " + str);
|
||||
}
|
||||
}
|
||||
}
|
||||
248
mozilla/java/plugins/examples/dom/DOMViewerFactory.java
Normal file
248
mozilla/java/plugins/examples/dom/DOMViewerFactory.java
Normal file
@ -0,0 +1,248 @@
|
||||
/*
|
||||
* 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>
|
||||
*/
|
||||
|
||||
import org.mozilla.pluglet.*;
|
||||
import org.mozilla.pluglet.mozilla.*;
|
||||
|
||||
import org.mozilla.dom.*;
|
||||
|
||||
import java.awt.print.*;
|
||||
import java.awt.*;
|
||||
|
||||
import javax.swing.tree.*;
|
||||
import javax.swing.event.*;
|
||||
import javax.swing.*;
|
||||
import java.util.*;
|
||||
|
||||
import org.w3c.dom.*;
|
||||
|
||||
public class DOMViewerFactory implements PlugletFactory {
|
||||
public DOMViewerFactory() {
|
||||
}
|
||||
|
||||
public Pluglet createPluglet(String mimeType) {
|
||||
return new DOMViewer();
|
||||
}
|
||||
|
||||
public void initialize(PlugletManager manager) {
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
class DOMTreeModel implements TreeModel, DOMTreeNotifier {
|
||||
private Node rootNode;
|
||||
private Vector treeModelListeners = new Vector();
|
||||
public DOMTreeModel(Node node) {
|
||||
rootNode = node;
|
||||
}
|
||||
public void addTreeModelListener(TreeModelListener l) {
|
||||
treeModelListeners.add(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);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class DOMCellRenderer implements TreeCellRenderer {
|
||||
private TreeCellRenderer cellRenderer;
|
||||
public DOMCellRenderer(TreeCellRenderer cellRenderer) {
|
||||
this.cellRenderer = cellRenderer;
|
||||
}
|
||||
|
||||
public Component getTreeCellRendererComponent(JTree tree, Object value,
|
||||
boolean selected,
|
||||
boolean expanded,
|
||||
boolean leaf, int row,
|
||||
boolean hasFocus) {
|
||||
return cellRenderer.getTreeCellRendererComponent(tree,
|
||||
((Node)value).getNodeName(),
|
||||
selected,
|
||||
expanded,
|
||||
leaf, row,
|
||||
hasFocus);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class DOMViewer implements Pluglet {
|
||||
private PlugletPeer peer;
|
||||
private Node rootNode;
|
||||
private JFrame frame;
|
||||
private JScrollPane treePane;
|
||||
private JPanel panel;
|
||||
private JTree tree;
|
||||
private DOMAccessPanel elementPanel;
|
||||
|
||||
public DOMViewer() {
|
||||
|
||||
}
|
||||
|
||||
public void initialize(PlugletPeer peer) {
|
||||
try {
|
||||
this.peer = peer;
|
||||
PlugletTagInfo info = peer.getTagInfo();
|
||||
if (info instanceof PlugletTagInfo2) {
|
||||
PlugletTagInfo2 info2 = (PlugletTagInfo2)info;
|
||||
Element e = (Element) info2.getDOMElement();
|
||||
Document doc = e.getOwnerDocument();
|
||||
e = doc.getDocumentElement();
|
||||
e.normalize();
|
||||
rootNode = e;
|
||||
}
|
||||
DOMTreeModel treeModel = new DOMTreeModel(rootNode);
|
||||
elementPanel = new DOMAccessPanel(treeModel);
|
||||
tree = new JTree(treeModel );
|
||||
tree.addTreeSelectionListener(elementPanel);
|
||||
tree.setCellRenderer(new DOMCellRenderer(tree.getCellRenderer()));
|
||||
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
|
||||
treePane = new JScrollPane(tree);
|
||||
panel = new JPanel();
|
||||
panel.setLayout(new BorderLayout());
|
||||
panel.add("Center", treePane);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void start() {
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
public PlugletStreamListener newStream() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setWindow(Frame fr) {
|
||||
if(fr == null) {
|
||||
if (frame != null) {
|
||||
frame.hide();
|
||||
frame = null;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (frame == null) {
|
||||
frame = new JFrame("DOM Viewer");
|
||||
frame.getContentPane().setLayout(new BorderLayout());
|
||||
frame.getContentPane().add(new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel, elementPanel));
|
||||
frame.pack();
|
||||
frame.show();
|
||||
}
|
||||
}
|
||||
|
||||
public void print(PrinterJob printerJob) {
|
||||
}
|
||||
|
||||
}
|
||||
31
mozilla/java/plugins/examples/dom/Makefile
Normal file
31
mozilla/java/plugins/examples/dom/Makefile
Normal file
@ -0,0 +1,31 @@
|
||||
#
|
||||
# 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):
|
||||
|
||||
|
||||
DOMViewer.jar: DOMViewerFactory.class DOMAccessPanel.class DOMTreeDumper.class manifest
|
||||
$(JDKHOME)/bin/jar cvfm DOMViewer.jar manifest *.class
|
||||
|
||||
.SUFFIXES: .java .class
|
||||
.java.class:
|
||||
$(JDKHOME)/bin/javac -classpath .:../../classes:$(CLASSPATH) $<
|
||||
clobber:
|
||||
rm *.class *.jar
|
||||
install:
|
||||
cp *.jar $(PLUGLET)
|
||||
35
mozilla/java/plugins/examples/dom/README
Normal file
35
mozilla/java/plugins/examples/dom/README
Normal file
@ -0,0 +1,35 @@
|
||||
DOM Viewer Pluglet Example.
|
||||
================================
|
||||
|
||||
This directory contains the source code for zip compressed files
|
||||
viewer pluglet example.
|
||||
|
||||
========================================================================
|
||||
Win32 Build Directions:
|
||||
========================================================================
|
||||
Requirements:
|
||||
|
||||
* built latest mozilla tree for Windows
|
||||
|
||||
* JDK1.3
|
||||
|
||||
* built Java-Implemented Plugins.
|
||||
|
||||
* built Java-DOM (mozilla\java\dom)
|
||||
|
||||
How To Build:
|
||||
|
||||
* type "nmake /f makefile.win"
|
||||
|
||||
|
||||
How to Run:
|
||||
|
||||
* Follow the directions in ..\..\README
|
||||
|
||||
* Set PLUGLET environment to the directory you have DOMViewer.jar
|
||||
|
||||
* Run mozilla and load page test.html from this directory and you will see
|
||||
the DOM tree of test.html document
|
||||
|
||||
|
||||
|
||||
31
mozilla/java/plugins/examples/dom/makefile.win
Normal file
31
mozilla/java/plugins/examples/dom/makefile.win
Normal file
@ -0,0 +1,31 @@
|
||||
#
|
||||
# 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):
|
||||
|
||||
.SUFFIXES: .java .class
|
||||
.java.class:
|
||||
$(JDKHOME)\bin\javac -classpath ".;../../classes;%classpath%" $<
|
||||
default: DOMViewer.jar
|
||||
DOMViewer.jar: DOMViewerFactory.class DOMAccessPanel.class DOMTreeDumper.class manifest
|
||||
$(JDKHOME)\bin\jar cvfm DOMViewer.jar manifest *.class
|
||||
|
||||
clobber:
|
||||
-del *.class *.jar
|
||||
export: DOMViewver.jar
|
||||
copy DOMViewver.jar $(PLUGLET)
|
||||
2
mozilla/java/plugins/examples/dom/manifest
Normal file
2
mozilla/java/plugins/examples/dom/manifest
Normal file
@ -0,0 +1,2 @@
|
||||
MIMEDescription: application/dom-viewer::Pluglet DOM Viewer
|
||||
Pluglet-Class: DOMViewerFactory
|
||||
33
mozilla/java/plugins/examples/dom/test.html
Normal file
33
mozilla/java/plugins/examples/dom/test.html
Normal file
@ -0,0 +1,33 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>
|
||||
DOM demo document
|
||||
</title>
|
||||
</head>
|
||||
<body some_Attr="some_value" bgcolor="#FFFFFF" text="#000000" link="#FFFFFF">
|
||||
<EMBED type="application/dom-viewer" width=0 height=0>
|
||||
Hello
|
||||
<a href="t.html"> t.html </a>
|
||||
After
|
||||
|
||||
<!-- it's a comment -->
|
||||
|
||||
<FORM>
|
||||
<table width=300>
|
||||
<tr>
|
||||
<td width=75>
|
||||
cell 1
|
||||
<td width=100>
|
||||
cell 2
|
||||
<td>
|
||||
cell 3
|
||||
<tr>
|
||||
<td width=75>
|
||||
cell a1
|
||||
<td width=100>
|
||||
<INPUT TYPE=HIDDEN NAME="input_str" VALUE="cell a2">
|
||||
<td>
|
||||
</table>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@ -27,6 +27,6 @@ IGNORE_MANIFEST=1
|
||||
#//------------------------------------------------------------------------
|
||||
DEPTH = ..\..\..
|
||||
|
||||
DIRS = MediaPlayer pdf rtf zip
|
||||
DIRS = MediaPlayer pdf rtf zip dom
|
||||
|
||||
include <$(DEPTH)\config\rules.mak>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user