⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 domecho06.java

📁 XML_JAVA指南 书籍语言: 简体中文 书籍类型: 程序设计 授权方式: 免费软件 书籍大小: 377 KB
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#)DomEcho06.java	1.9 98/11/10 * * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved. * * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) Licensee does not utilize the software in a manner * which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. */import javax.xml.parsers.DocumentBuilderFactory;  import javax.xml.parsers.FactoryConfigurationError;  import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.DocumentBuilder;  import org.xml.sax.SAXException;  import org.xml.sax.SAXParseException;  import org.w3c.dom.Document;import org.w3c.dom.DOMException;import org.w3c.dom.Element;import java.io.File;import java.io.IOException;// Basic GUI componentsimport javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTree;// GUI components for right-hand sideimport javax.swing.JSplitPane;import javax.swing.JEditorPane;// GUI support classesimport java.awt.BorderLayout;import java.awt.Dimension;import java.awt.Toolkit;import java.awt.event.WindowEvent;import java.awt.event.WindowAdapter;// For creating bordersimport javax.swing.border.EmptyBorder;import javax.swing.border.BevelBorder;import javax.swing.border.CompoundBorder;// For creating a TreeModelimport javax.swing.tree.*;import javax.swing.event.*;import java.util.*;public class DomEcho06  extends JPanel{    // Global value so it can be ref'd by the tree-adapter    static Document document;     // Could set this with a command-line argument, but    // not much point -- the list of tree elements still    // has to be defined internally.    // Extra credit: Read the list from a file    // Super-extra credit: Process a DTD and build the list    // --no standard DTD parsers!    boolean compress = false;    static final int windowHeight = 460;    static final int leftWidth = 300;    static final int rightWidth = 340;    static final int windowWidth = leftWidth + rightWidth;    public DomEcho06()    {       // Make a nice border       EmptyBorder eb = new EmptyBorder(5,5,5,5);       BevelBorder bb = new BevelBorder(BevelBorder.LOWERED);       CompoundBorder cb = new CompoundBorder(eb,bb);       this.setBorder(new CompoundBorder(cb,eb));       // Set up the tree       JTree tree = new JTree(new DomToTreeModelAdapter());       // Iterate over the tree and make nodes visible       // (Otherwise, the tree shows up fully collapsed)       //TreePath nodePath = ???;       //  tree.expandPath(nodePath);        // Build left-side view       JScrollPane treeView = new JScrollPane(tree);       treeView.setPreferredSize(             new Dimension( leftWidth, windowHeight ));       // Build right-side view       // (must be final to be referenced in inner class)       final        JEditorPane htmlPane = new JEditorPane("text/html","");       htmlPane.setEditable(false);       JScrollPane htmlView = new JScrollPane(htmlPane);       htmlView.setPreferredSize(            new Dimension( rightWidth, windowHeight ));       // Wire the two views together. Use a selection listener        // created with an anonymous inner-class adapter.       tree.addTreeSelectionListener(         new TreeSelectionListener() {           public void valueChanged(TreeSelectionEvent e) {             TreePath p = e.getNewLeadSelectionPath();             if (p != null) {               AdapterNode adpNode =                   (AdapterNode) p.getLastPathComponent();               htmlPane.setText(adpNode.content());             }           }         }       );       // Build split-pane view       JSplitPane splitPane =           new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,                          treeView,                          htmlView );       splitPane.setContinuousLayout( true );       splitPane.setDividerLocation( leftWidth );       splitPane.setPreferredSize(             new Dimension( windowWidth + 10, windowHeight+10 ));       // Add GUI components       this.setLayout(new BorderLayout());       this.add("Center", splitPane );    } // constructor    public static void main (String argv [])    {        if (argv.length != 1) {            buildDom();            makeFrame();            return;        }        DocumentBuilderFactory factory =           DocumentBuilderFactory.newInstance();        try {           DocumentBuilder builder = factory.newDocumentBuilder();           document = builder.parse( new File(argv[0]) );            makeFrame();        } catch (SAXParseException spe) {           // Error generated by the parser           System.out.println ("\n** Parsing error"               + ", line " + spe.getLineNumber ()              + ", uri " + spe.getSystemId ());           System.out.println("   " + spe.getMessage() );           // Use the contained exception, if any           Exception  x = spe;           if (spe.getException() != null)               x = spe.getException();           x.printStackTrace();        } catch (SAXException sxe) {           // Error generated by this application           // (or a parser-initialization error)           Exception  x = sxe;           if (sxe.getException() != null)               x = sxe.getException();           x.printStackTrace();        } catch (ParserConfigurationException pce) {            // Parser with specified options can't be built            pce.printStackTrace();        } catch (IOException ioe) {           // I/O error           ioe.printStackTrace();        }    } // main    public static void makeFrame() {        // Set up a GUI framework        JFrame frame = new JFrame("DOM Echo");        frame.addWindowListener(          new WindowAdapter() {            public void windowClosing(WindowEvent e) {System.exit(0);}          }          );        // Set up the tree, the views, and display it all        final DomEcho06 echoPanel =            new DomEcho06();        frame.getContentPane().add("Center", echoPanel );        frame.pack();        Dimension screenSize =            Toolkit.getDefaultToolkit().getScreenSize();        int w = windowWidth + 10;        int h = windowHeight + 10;        frame.setLocation(screenSize.width/3 - w/2,                           screenSize.height/2 - h/2);        frame.setSize(w, h);        frame.setVisible(true);    } // makeFrame    public static void buildDom ()    {        DocumentBuilderFactory factory =           DocumentBuilderFactory.newInstance();        try {          DocumentBuilder builder = factory.newDocumentBuilder();          document = builder.newDocument();  // Create from whole cloth          Element root =                   (Element) document.createElement("rootElement");           document.appendChild (root);          root.appendChild( document.createTextNode("Some") );          root.appendChild( document.createTextNode(" ")    );          root.appendChild( document.createTextNode("text") );          // normalize text representation          // getDocumentElement() returns the document's root node          document.getDocumentElement().normalize();        } catch (ParserConfigurationException pce) {            // Parser with specified options can't be built            pce.printStackTrace();        }    } // buildDom    // An array of names for DOM node-types    static final String[] typeName = {        "none",        "Element",        "Attr",        "Text",        "CDATA",        "EntityRef",        "Entity",        "ProcInstr",        "Comment",        "Document",        "DocType",        "DocFragment",        "Notation",    };    static final int ELEMENT_TYPE =   1;    static final int ATTR_TYPE =      2;    static final int TEXT_TYPE =      3;    static final int CDATA_TYPE =     4;    static final int ENTITYREF_TYPE = 5;    static final int ENTITY_TYPE =    6;    static final int PROCINSTR_TYPE = 7;    static final int COMMENT_TYPE =   8;    static final int DOCUMENT_TYPE =  9;    static final int DOCTYPE_TYPE =  10;    static final int DOCFRAG_TYPE =  11;    static final int NOTATION_TYPE = 12;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -