Source Code Cross Referenced for XMLViewer.java in  » Net » SkunkDAV » org » skunk » minixml » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » SkunkDAV » org.skunk.minixml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         /* */
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.