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

📄 domecho05.java

📁 XML_JAVA指南 书籍语言: 简体中文 书籍类型: 程序设计 授权方式: 免费软件 书籍大小: 377 KB
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        "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;      }      return false;    }    // This class wraps a DOM node and returns the text we want to    // display in the tree. It also returns children, index values,    // and child counts.    public class AdapterNode     {       org.w3c.dom.Node domNode;      // Construct an Adapter node from a DOM node      public AdapterNode(org.w3c.dom.Node node) {        domNode = node;      }      // Return a string that identifies this node in the tree      // *** Refer to table at top of org.w3c.dom.Node ***      public String toString() {        String s = typeName[domNode.getNodeType()];        String nodeName = domNode.getNodeName();        if (! nodeName.startsWith("#")) {           s += ": " + nodeName;        }        if (compress) {           String t = content().trim();           int x = t.indexOf("\n");           if (x >= 0) t = t.substring(0, x);           s += " " + t;           return s;        }        if (domNode.getNodeValue() != null) {           if (s.startsWith("ProcInstr"))               s += ", ";            else               s += ": ";           // Trim the value to get rid of NL's at the front           String t = domNode.getNodeValue().trim();           int x = t.indexOf("\n");           if (x >= 0) t = t.substring(0, x);           s += t;        }        return s;      }      public String content() {        String s = "";        org.w3c.dom.NodeList nodeList = domNode.getChildNodes();        for (int i=0; i<nodeList.getLength(); i++) {          org.w3c.dom.Node node = nodeList.item(i);          int type = node.getNodeType();          AdapterNode adpNode = new AdapterNode(node); //inefficient, but works          if (type == ELEMENT_TYPE) {            // Skip subelements that are displayed in the tree.               if ( treeElement(node.getNodeName()) ) continue;            // EXTRA-CREDIT HOMEWORK:            //   Special case the SLIDE element to use the TITLE text            //   and ignore TITLE element when constructing the tree.            // EXTRA-CREDIT            //   Convert ITEM elements to html lists using            //   <ul>, <li>, </ul> tags            s += "<" + node.getNodeName() + ">";            s += adpNode.content();            s += "</" + node.getNodeName() + ">";          } else if (type == TEXT_TYPE) {            s += node.getNodeValue();          } else if (type == ENTITYREF_TYPE) {            // The content is in the TEXT node under it            s += adpNode.content();          } else if (type == CDATA_TYPE) {            // The "value" has the text, same as a text node.            //   while EntityRef has it in a text node underneath.            //   (because EntityRef can contain multiple subelements)            // Convert angle brackets and ampersands for display            StringBuffer sb = new StringBuffer( node.getNodeValue() );            for (int j=0; j<sb.length(); j++) {              if (sb.charAt(j) == '<') {                sb.setCharAt(j, '&');                sb.insert(j+1, "lt;");                j += 3;              } else if (sb.charAt(j) == '&') {                sb.setCharAt(j, '&');                sb.insert(j+1, "amp;");                j += 4;              }            }            s += "<pre>" + sb + "\n</pre>";          }           // Ignoring these:           //   ATTR_TYPE      -- not in the DOM tree           //   ENTITY_TYPE    -- does not appear in the DOM           //   PROCINSTR_TYPE -- not "data"           //   COMMENT_TYPE   -- not "data"           //   DOCUMENT_TYPE  -- Root node only. No data to display.           //   DOCTYPE_TYPE   -- Appears under the root only           //   DOCFRAG_TYPE   -- equiv. to "document" for fragments           //   NOTATION_TYPE  -- nothing but binary data in here        }        return s;      }      /*       * Return children, index, and count values       */      public int index(AdapterNode child) {        //System.err.println("Looking for index of " + child);        int count = childCount();        for (int i=0; i<count; i++) {          AdapterNode n = this.child(i);          if (child.domNode == n.domNode) return i;        }        return -1; // Should never get here.      }      public AdapterNode child(int searchIndex) {        //Note: JTree index is zero-based.         org.w3c.dom.Node node =              domNode.getChildNodes().item(searchIndex);        if (compress) {          // Return Nth displayable node          int elementNodeIndex = 0;          for (int i=0; i<domNode.getChildNodes().getLength(); i++) {            node = domNode.getChildNodes().item(i);            if (node.getNodeType() == ELEMENT_TYPE             && treeElement( node.getNodeName() )            && elementNodeIndex++ == searchIndex) {               break;             }          }        }        return new AdapterNode(node);       }      public int childCount() {        if (!compress) {          // Indent this          return domNode.getChildNodes().getLength();          }         int count = 0;        for (int i=0; i<domNode.getChildNodes().getLength(); i++) {           org.w3c.dom.Node node = domNode.getChildNodes().item(i);            if (node.getNodeType() == ELEMENT_TYPE           && treeElement( node.getNodeName() ))            {             // Note:              //   Have to check for proper type.              //   The DOCTYPE element also has the right name             ++count;           }        }        return count;      }    }    // This adapter converts the current Document (a DOM) into     // a JTree model.     public class DomToTreeModelAdapter       implements javax.swing.tree.TreeModel     {      // Basic TreeModel operations      public Object  getRoot() {        //System.err.println("Returning root: " +document);        return new AdapterNode(document);      }      public boolean isLeaf(Object aNode) {        // Determines whether the icon shows up to the left.        // Return true for any node with no children        AdapterNode node = (AdapterNode) aNode;        if (node.childCount() > 0) return false;        return true;      }      public int     getChildCount(Object parent) {        AdapterNode node = (AdapterNode) parent;        return node.childCount();      }      public Object getChild(Object parent, int index) {        AdapterNode node = (AdapterNode) parent;        return node.child(index);      }      public int getIndexOfChild(Object parent, Object child) {        AdapterNode node = (AdapterNode) parent;        return node.index((AdapterNode) child);      }      public void valueForPathChanged(TreePath path, Object newValue) {        // Null. We won't be making changes in the GUI        // If we did, we would ensure the new value was really new,        // adjust the model, and then fire a TreeNodesChanged event.      }      /*       * Use these methods to add and remove event listeners.       * (Needed to satisfy TreeModel interface, but not used.)       */      private Vector listenerList = new Vector();      public void addTreeModelListener(TreeModelListener listener) {        if ( listener != null         && ! listenerList.contains( listener ) ) {           listenerList.addElement( listener );        }      }      public void removeTreeModelListener(TreeModelListener listener) {        if ( listener != null ) {           listenerList.removeElement( listener );        }      }      // Note: Since XML works with 1.1, this example uses Vector.      // If coding for 1.2 or later, though, I'd use this instead:      //   private List listenerList = new LinkedList();      // The operations on the List are then add(), remove() and      // iteration, via:      //  Iterator it = listenerList.iterator();      //  while ( it.hasNext() ) {      //    TreeModelListener listener = (TreeModelListener)it.next();      //    ...      //  }      /*       * Invoke these methods to inform listeners of changes.       * (Not needed for this example.)       * Methods taken from TreeModelSupport class described at        *   http://java.sun.com/products/jfc/tsc/articles/jtree/index.html       * That architecture (produced by Tom Santos and Steve Wilson)       * is more elegant. I just hacked 'em in here so they are       * immediately at hand.       */      public void fireTreeNodesChanged( TreeModelEvent e ) {        Enumeration listeners = listenerList.elements();        while ( listeners.hasMoreElements() ) {          TreeModelListener listener =             (TreeModelListener)listeners.nextElement();          listener.treeNodesChanged( e );        }      }       public void fireTreeNodesInserted( TreeModelEvent e ) {        Enumeration listeners = listenerList.elements();        while ( listeners.hasMoreElements() ) {           TreeModelListener listener =             (TreeModelListener)listeners.nextElement();           listener.treeNodesInserted( e );        }      }         public void fireTreeNodesRemoved( TreeModelEvent e ) {        Enumeration listeners = listenerList.elements();        while ( listeners.hasMoreElements() ) {          TreeModelListener listener =             (TreeModelListener)listeners.nextElement();          listener.treeNodesRemoved( e );        }      }         public void fireTreeStructureChanged( TreeModelEvent e ) {        Enumeration listeners = listenerList.elements();        while ( listeners.hasMoreElements() ) {          TreeModelListener listener =            (TreeModelListener)listeners.nextElement();          listener.treeStructureChanged( e );        }      }    }}

⌨️ 快捷键说明

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