001: /*
002: * Copyright (c) 2001, Jacob Smullyan.
003: *
004: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
005: * for the latest version.
006: *
007: * SkunkDAV is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License as published
009: * by the Free Software Foundation; either version 2, or (at your option)
010: * any later version.
011: *
012: * SkunkDAV is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with SkunkDAV; see the file COPYING. If not, write to the Free
019: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021: */
022:
023: package org.skunk.minixml;
024:
025: import java.awt.BorderLayout;
026: import java.awt.Component;
027: import java.util.Enumeration;
028: import java.util.ListIterator;
029: import java.util.Vector;
030: import javax.swing.JComponent;
031: import javax.swing.JOptionPane;
032: import javax.swing.JPanel;
033: import javax.swing.JScrollPane;
034: import javax.swing.JTree;
035: import javax.swing.tree.DefaultMutableTreeNode;
036: import javax.swing.tree.DefaultTreeCellRenderer;
037: import javax.swing.tree.DefaultTreeModel;
038: import javax.swing.tree.TreeNode;
039:
040: /**
041: * displays an XMLDocument in a JTree
042: */
043: public class XMLViewer extends JPanel {
044: private JTree xmlTree;
045: private static final String[] tooltips = { "XML element", "PI",
046: "CDATA", "PCDATA", "XML comment", "DOCTYPE" };
047:
048: public XMLViewer(XMLDocument doc) {
049: super ();
050: TreeNode rootNode = new XMLTreeNode(
051: (doc.hasRoot()) ? (Object) doc.getRootElement()
052: : (Object) doc);
053: xmlTree = new JTree(new DefaultTreeModel(rootNode));
054: initComponents();
055: }
056:
057: private void initComponents() {
058: xmlTree.setCellRenderer(new DefaultTreeCellRenderer() {
059: public Component getTreeCellRendererComponent(JTree tree,
060: Object value, boolean sel, boolean expanded,
061: boolean leaf, int row, boolean hasFocus) {
062: Component comp = super
063: .getTreeCellRendererComponent(tree, value, sel,
064: expanded, leaf, row, hasFocus);
065: if (value instanceof XMLTreeNode
066: && comp instanceof JComponent) {
067: int type = ((XMLTreeNode) value).getType();
068: if (type < XMLViewer.tooltips.length)
069: ((JComponent) comp)
070: .setToolTipText(XMLViewer.tooltips[type]);
071: }
072: return comp;
073: }
074: });
075: setLayout(new BorderLayout(2, 2));
076: add(new JScrollPane(xmlTree), BorderLayout.CENTER);
077: }
078:
079: public static void viewDocumentDialog(XMLDocument doc) {
080: XMLViewer viewer = new XMLViewer(doc);
081: JOptionPane.showMessageDialog(null, viewer, "XML Document",
082: JOptionPane.INFORMATION_MESSAGE, null);
083: }
084: }
085:
086: class XMLTreeNode extends DefaultMutableTreeNode {
087: public static final int ELEMENT_TYPE = 0;
088: public static final int PI_TYPE = 1;
089: public static final int CDATA_TYPE = 2;
090: public static final int PCDATA_TYPE = 3;
091: public static final int COMMENT_TYPE = 4;
092: public static final int DOCTYPE_TYPE = 5;
093: public static final int DOCUMENT_TYPE = 6;
094: public static final int UNKNOWN_TYPE = 7;
095:
096: private int type;
097: private Vector children = new Vector();
098: private Object xmlObject;
099:
100: public XMLTreeNode(Object xmlObject) {
101: this (xmlObject, true);
102: }
103:
104: public XMLTreeNode(Object xmlObject, boolean allowsChildren) {
105: super (getUserObject(xmlObject), allowsChildren);
106: this .xmlObject = xmlObject;
107: setType();
108: init();
109: }
110:
111: protected Object getXMLObject() {
112: return xmlObject;
113: }
114:
115: private static Object getUserObject(Object xmlObject) {
116: if (xmlObject instanceof XMLElement) {
117: XMLElement elem = (XMLElement) xmlObject;
118: StringBuffer sb = new StringBuffer();
119: String ns = elem.getElementNamespaceCode();
120: if (ns != null && ns.length() > 0) {
121: sb.append(ns.trim());
122: sb.append(':');
123: }
124: sb.append(elem.getElementName());
125: for (ListIterator lit = elem.attributes(); lit.hasNext();) {
126: sb.append(" ");
127: XMLAttribute attr = (XMLAttribute) lit.next();
128: sb.append(attr.getName().trim());
129: sb.append("=");
130: sb.append(attr.getValue().trim());
131: }
132: return sb.toString();
133: } else if (xmlObject instanceof XMLDocument)
134: return "XML Document";
135: else
136: return xmlObject;
137: }
138:
139: private void init() {
140: switch (type) {
141: case ELEMENT_TYPE:
142: XMLElement elem = (XMLElement) xmlObject;
143: for (ListIterator lit = elem.children(); lit.hasNext();)
144: children.addElement(new XMLTreeNode(lit.next()));
145: break;
146: case DOCUMENT_TYPE:
147: XMLDocument doc = (XMLDocument) xmlObject;
148: for (ListIterator lit = doc.contents(); lit.hasNext();)
149: children.addElement(new XMLTreeNode(lit.next()));
150: break;
151: default:
152: //do nothing
153: }
154: }
155:
156: public TreeNode getChildAt(int index) {
157: if (index >= 0 && index < children.size())
158: return (TreeNode) children.elementAt(index);
159: else
160: return null;
161: }
162:
163: public Enumeration children() {
164: return children.elements();
165: }
166:
167: public int getChildCount() {
168: return children.size();
169: }
170:
171: private void setType() {
172: if (xmlObject instanceof XMLElement)
173: type = ELEMENT_TYPE;
174: else if (xmlObject instanceof XMLPInstruction)
175: type = PI_TYPE;
176: else if (xmlObject instanceof XMLComment)
177: type = COMMENT_TYPE;
178: else if (xmlObject instanceof String) {
179: if (((String) xmlObject).startsWith("<!DOCTYPE"))
180: type = DOCTYPE_TYPE;
181: else if (((String) xmlObject).startsWith("<![CDATA"))
182: type = CDATA_TYPE;
183: else
184: type = PCDATA_TYPE;
185: } else if (xmlObject instanceof XMLDocument)
186: type = DOCUMENT_TYPE;
187: else
188: type = UNKNOWN_TYPE;
189: }
190:
191: public int getType() {
192: return type;
193: }
194:
195: }
196:
197: /* $Log: XMLViewer.java,v $
198: /* Revision 1.3 2001/07/17 03:00:04 smulloni
199: /* added XMLCData class to represent CDATA, and fixed CDATA parsing, which was
200: /* wrong in any case; added license preambles where I had forgotten to put them.
201: /*
202: /* Revision 1.2 2000/11/09 23:35:12 smullyan
203: /* log added to every Java file, with the help of python. Lock stealing
204: /* implemented, and treatment of locks made more robust.
205: /* */
|