DOMAdapter.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:10k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: DOMAdapter.java,v 1.2 2005/10/10 17:01:11 rbair Exp $
  3.  *
  4.  * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.binding.swingx.adapter;
  22. import org.jdesktop.binding.metadata.Converter;
  23. import org.jdesktop.swingx.treetable.DefaultTreeTableModel;
  24. import org.w3c.dom.Document;
  25. import org.w3c.dom.Element;
  26. import org.w3c.dom.Node;
  27. import org.w3c.dom.NodeList;
  28. // CLEAN: temporarily moved from old jdnc/swingx tree to allow
  29. // markup project to compile
  30. public class DOMAdapter extends DefaultTreeTableModel {
  31. protected Document dom = null;
  32. //    private Element columns = null;
  33.     private HierarchicalDataMetaData metaData;
  34. /*
  35.     private int userDataSupportType = 0; // 1 == getUserData; 2 == getUserObject
  36.     private static final int GET_USER_DATA = 1;
  37.     private static final int GET_USER_OBJECT = 2;
  38. */
  39. public DOMAdapter() {
  40. }
  41.     public DOMAdapter(Document dom) {
  42.         bind(dom);
  43.     }
  44.     public final void bind(Document dom) {
  45.         if (dom == null) {
  46.             throw new IllegalArgumentException("null document object model");
  47.         }
  48.         if (this.dom == null) {
  49.             this.dom = dom;
  50.             /** @todo Handle case where no metaData is present! */
  51.             Element metaDataElem = (Element) dom.getDocumentElement().getElementsByTagNameNS(
  52.                 "http://www.jdesktop.org/2004/05/jdnc", "metaData").item(0);
  53.             setMetaData(new HierarchicalDataMetaData(metaDataElem));
  54. /*
  55.             Object[] cells = null;
  56.             try {
  57.                 cells = (Object[]) (new Expression(columns, "getUserData",
  58.                                                   new Object[0])).getValue();
  59.                 userDataSupportType = GET_USER_DATA;
  60.             }
  61.             catch (Exception ex) {
  62.                 try {
  63.                     cells = (Object[]) (new Expression(columns,
  64.                         "getUserObject", new Object[0])).getValue();
  65.                     userDataSupportType = GET_USER_OBJECT;
  66.                 }
  67.                 catch (Exception nex) {
  68.                 }
  69.             }
  70. */
  71.         }
  72.         else {
  73.             throw new IllegalArgumentException("dom already bound");
  74.         }
  75.     }
  76.     public String convertValueToText(Object value) {
  77.         // This method requires support from JXTree. Will NOT work with JTree!
  78.         if(value != null) {
  79.             Object realValue = getValueAt(value, 0);
  80.             if (realValue != null) {
  81.                 return realValue.toString();
  82.             }
  83.         }
  84.         return "";
  85.     }
  86. public Object getRoot() {
  87.         return dom.getDocumentElement().getElementsByTagNameNS(
  88.                 "http://www.jdesktop.org/2004/05/jdnc", "rows").item(0);
  89.     }
  90.     public Class getColumnClass(int column) {
  91.         return column == 0 ? super.getColumnClass(0) :
  92.             getMetaData().getColumnClass(column+1);
  93.     }
  94. public Object getChild(Object parent, int index) {
  95.         Element parentElement = (Element) parent;
  96.         NodeList list = ((Element) parentElement).getChildNodes();
  97.         int i = 0, k = index, max = list.getLength();
  98.         Node node;
  99.         Element elem;
  100.         while (i < max) {
  101.             node = list.item(i++);
  102.             if (node instanceof Element) {
  103.                 elem = (Element) node;
  104.                 if (elem.getLocalName().equals("row")) {
  105.                     if (k-- == 0) {
  106.                         /*
  107.                         System.out.print("got child:");
  108.                         NamedNodeMap attributes = elem.getAttributes();
  109.                         for (int a = 0; a < attributes.getLength(); a++) {
  110.                             System.out.print(" " +
  111.                                              attributes.item(a).getNamespaceURI() + ":" +
  112.                                              attributes.item(a).getLocalName() + "=" +
  113.                                              attributes.item(a).getNodeValue());
  114.                         }
  115.                         System.out.println(";");
  116. */
  117.                         return elem;
  118.                     }
  119.                 }
  120.             }
  121.         }
  122. /*
  123.         if (true) {
  124.             // We should never get here!
  125.             System.out.print(parentElement.getLocalName());
  126.             NamedNodeMap attributes = parentElement.getAttributes();
  127.             for (int a = 0; a < attributes.getLength(); a++) {
  128.                 System.out.print(" " + attributes.item(a).getNamespaceURI() + ":" +
  129.                                  attributes.item(a).getLocalName() + "=" +
  130.                                  attributes.item(a).getNodeValue());
  131.             }
  132.             System.out.println(" has no child at index " + k);
  133.         }
  134.  */
  135.         return null;
  136. }
  137. public int getChildCount(Object parent) {
  138.         Element parentElement = (Element) parent;
  139.         NodeList list = ((Element) parentElement).getChildNodes();
  140.         int i = 0, k = 0, max = list.getLength();
  141.         Node node;
  142.         Element elem = null;
  143.         while (i < max) {
  144.             node = list.item(i++);
  145.             if (node instanceof Element) {
  146.                 elem = (Element) node;
  147.                 if (elem.getLocalName().equals("row")) {
  148.                     k++;
  149.                 }
  150.             }
  151.         }
  152. /*
  153. if (true) {
  154.             System.out.print(parentElement.getLocalName());
  155.             NamedNodeMap attributes = parentElement.getAttributes();
  156.             for (int a = 0; a < attributes.getLength(); a++) {
  157.                 System.out.print(" " + attributes.item(a).getNamespaceURI() + ":" +
  158.                                  attributes.item(a).getLocalName() + "=" +
  159.                                  attributes.item(a).getNodeValue());
  160.             }
  161.             System.out.println(" has " + k + " children");
  162.         }
  163.  */
  164.         return k;
  165. }
  166.     public void setMetaData(HierarchicalDataMetaData metaData) {
  167.         this.metaData = metaData;
  168.     }
  169.     public HierarchicalDataMetaData getMetaData() {
  170.         return metaData;
  171.     }
  172. public int getColumnCount() {
  173.         return getMetaData().getColumnCount();
  174. }
  175.     /**
  176.      * @throws IllegalArgumentException if the column name does not exist in
  177.      *         this tabular data model
  178.      * @param columnName String containing the name of the column
  179.      * @return integer index of column in the data model which corresponds
  180.      *         to the specified column name
  181.      */
  182.     public int getColumnIndex(String columnName) {
  183.         return getMetaData().getColumnIndex(columnName);
  184.     }
  185. public String getColumnName(int column) {
  186.         return getMetaData().getColumnName(column+1);
  187. }
  188. public Object getValueAt(Object node, int column) {
  189.         if (node == null)
  190.             throw new IllegalArgumentException("Node is null; " + column);
  191.         Element parentElement = (Element) node;
  192.         NodeList list = ((Element) parentElement).getChildNodes();
  193.         int i = 0, k = column, max = list.getLength();
  194.         Node n;
  195.         Element elem;
  196.         while (i < max) {
  197.             n = list.item(i++);
  198.             if (n instanceof Element) {
  199.                 elem = (Element) n;
  200.                 if (elem.getLocalName().equals("cell")) {
  201.                     if (k-- == 0) {
  202.                         //    System.out.println("Value of " + node + " at column " +
  203.                         //                       column + "=" + elem + ";" + elem.getFirstChild());
  204.                         Node cellData = elem.getFirstChild();
  205.                         String rawValue = cellData == null ?
  206.                              "" : cellData.getNodeValue();
  207.                         Converter converter =
  208.                             getMetaData().getColumnConverter(column + 1);
  209.                         if (converter == null) {
  210.                             return rawValue;
  211.                         }
  212.                         else {
  213.                             try {
  214.                                 /** @todo cache converted value */
  215.                                 return converter.decode(rawValue, null);
  216.                             }
  217.                             catch (Exception ex) {
  218.                                 return rawValue;
  219.                             }
  220.                         }
  221.                     }
  222.                 }
  223.             }
  224.         }
  225.         return null;
  226. }
  227. /*
  228. private Object[] getCells(Element element) {
  229.         Object[] cells = null;
  230.         switch (userDataSupportType) {
  231.             case GET_USER_DATA: {
  232.                 try {
  233.                     cells = (Object[]) (new Expression(element, "getUserData",
  234.                         new Object[0])).getValue();
  235.                 }
  236.                 catch (Exception ex) {
  237.                 }
  238.                 break;
  239.             }
  240.             case GET_USER_OBJECT: {
  241.                 try {
  242.                     cells = (Object[]) (new Expression(element, "getUserObject",
  243.                         new Object[0])).getValue();
  244.                 }
  245.                 catch (Exception ex) {
  246.                 }
  247.                 break;
  248.             }
  249.             default: {
  250.                 break;
  251.             }
  252.         }
  253.         return cells;
  254.     }
  255. */
  256. }