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

📄 domecho04.java

📁 j2ee API 开发重要工具 免费下载 欢迎使用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#)DomEcho04.java	1.9 98/11/10 * * Copyright (c) 2003 Sun Microsystems, Inc.  All rights reserved.  U.S. * Government Rights - Commercial software.  Government users are subject * to the Sun Microsystems, Inc. standard license agreement and * applicable provisions of the FAR and its supplements.  Use is subject * to license terms.  * * This distribution may include materials developed by third parties. * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks * or registered trademarks of Sun Microsystems, Inc. in the U.S. and * other countries.  * * Copyright (c) 2003 Sun Microsystems, Inc. Tous droits reserves. * * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions * en vigueur de la FAR (Federal Acquisition Regulations) et des * supplements a celles-ci.  Distribue par des licences qui en * restreignent l'utilisation. * * Cette distribution peut comprendre des composants developpes par des * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE * sont des marques de fabrique ou des marques deposees de Sun * Microsystems, Inc. aux Etats-Unis et dans d'autres pays. */import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory;  import javax.xml.parsers.FactoryConfigurationError;  import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException;  import org.xml.sax.SAXParseException;  import java.io.File;import java.io.IOException;import org.w3c.dom.Document;import org.w3c.dom.DOMException;import org.w3c.dom.Node;// 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 DomEcho04  extends JPanel{    // Global value so it can be ref'd by the tree-adapter    static Document document;       boolean compress = true;      static final int windowHeight = 460;    static final int leftWidth = 300;    static final int rightWidth = 340;    static final int windowWidth = leftWidth + rightWidth;    public DomEcho04()    {       // 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) {          System.err.println("Usage: java DomEcho filename");          System.exit(1);                }        DocumentBuilderFactory factory =            DocumentBuilderFactory.newInstance();        //factory.setValidating(true);           //factory.setNamespaceAware(true);        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 during parsing)           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 DomEcho04 echoPanel =            new DomEcho04();        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      // An array of names for DOM node-types    // (Array indexes = nodeType() values.)    static final String[] typeName = {        "none",        "Element",        "Attr",        "Text",        "CDATA",        "EntityRef",        "Entity",        "ProcInstr",        "Comment",        "Document",        "DocType",        "DocFragment",        "Notation",    };    static final int ELEMENT_TYPE =   Node.ELEMENT_NODE;      static final int ATTR_TYPE =      Node.ATTRIBUTE_NODE;    static final int TEXT_TYPE =      Node.TEXT_NODE;    static final int CDATA_TYPE =     Node.CDATA_SECTION_NODE;    static final int ENTITYREF_TYPE = Node.ENTITY_REFERENCE_NODE;    static final int ENTITY_TYPE =    Node.ENTITY_NODE;    static final int PROCINSTR_TYPE = Node.PROCESSING_INSTRUCTION_NODE;    static final int COMMENT_TYPE =   Node.COMMENT_NODE;    static final int DOCUMENT_TYPE =  Node.DOCUMENT_NODE;    static final int DOCTYPE_TYPE =   Node.DOCUMENT_TYPE_NODE;    static final int DOCFRAG_TYPE =   Node.DOCUMENT_FRAGMENT_NODE;    static final int NOTATION_TYPE =  Node.NOTATION_NODE;        // The list of elements to display in the tree    // 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.   static String[] treeElementNames = {        "slideshow",        "slide",        "title",         // For slideshow #1        "slide-title",   // For slideshow #10        "item",    };    boolean treeElement(String elementName) {      for (int i=0; i<treeElementNames.length; i++) {        if ( elementName.equals(treeElementNames[i]) ) return true;

⌨️ 快捷键说明

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