1 7 8 package org.jdesktop.swing.data; 9 10 import java.io.File ; 11 12 import org.w3c.dom.Document ; 13 import org.w3c.dom.Element ; 14 import org.w3c.dom.*; 15 import org.w3c.dom.NodeList ; 16 import org.jdesktop.swing.treetable.DefaultTreeTableModel; 17 import java.beans.Expression ; 18 19 import org.jdesktop.swing.data.Converter; 20 21 public class DOMAdapter extends DefaultTreeTableModel { 22 protected Document dom = null; 23 private HierarchicalDataMetaData metaData; 25 30 public DOMAdapter() { 31 } 32 33 public DOMAdapter(Document dom) { 34 bind(dom); 35 } 36 37 public final void bind(Document dom) { 38 if (dom == null) { 39 throw new IllegalArgumentException ("null document object model"); 40 } 41 42 if (this.dom == null) { 43 this.dom = dom; 44 45 Element metaDataElem = (Element ) dom.getDocumentElement().getElementsByTagNameNS( 46 "http://www.jdesktop.org/2004/05/jdnc", "metaData").item(0); 47 setMetaData(new HierarchicalDataMetaData(metaDataElem)); 48 66 } 67 else { 68 throw new IllegalArgumentException ("dom already bound"); 69 } 70 } 71 72 public String convertValueToText(Object value) { 73 if(value != null) { 75 Object realValue = getValueAt(value, 0); 76 if (realValue != null) { 77 return realValue.toString(); 78 } 79 } 80 return ""; 81 } 82 83 public Object getRoot() { 84 return dom.getDocumentElement().getElementsByTagNameNS( 85 "http://www.jdesktop.org/2004/05/jdnc", "rows").item(0); 86 } 87 88 public Class getColumnClass(int column) { 89 return column == 0 ? super.getColumnClass(0) : 90 getMetaData().getColumnClass(column+1); 91 } 92 93 public Object getChild(Object parent, int index) { 94 Element parentElement = (Element ) parent; 95 NodeList list = ((Element ) parentElement).getChildNodes(); 96 int i = 0, k = index, max = list.getLength(); 97 Node node; 98 Element elem; 99 while (i < max) { 100 node = list.item(i++); 101 if (node instanceof Element ) { 102 elem = (Element ) node; 103 if (elem.getLocalName().equals("row")) { 104 if (k-- == 0) { 105 116 return elem; 117 } 118 } 119 } 120 } 121 134 return null; 135 } 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 164 return k; 165 } 166 167 public void setMetaData(HierarchicalDataMetaData metaData) { 168 this.metaData = metaData; 169 } 170 171 public HierarchicalDataMetaData getMetaData() { 172 return metaData; 173 } 174 175 public int getColumnCount() { 176 return getMetaData().getColumnCount(); 177 } 178 179 186 public int getColumnIndex(String columnName) { 187 return getMetaData().getColumnIndex(columnName); 188 } 189 190 public String getColumnName(int column) { 191 return getMetaData().getColumnName(column+1); 192 } 193 194 public Object getValueAt(Object node, int column) { 195 if (node == null) 196 throw new IllegalArgumentException ("Node is null; " + column); 197 Element parentElement = (Element ) node; 198 NodeList list = ((Element ) parentElement).getChildNodes(); 199 int i = 0, k = column, max = list.getLength(); 200 Node n; 201 Element elem; 202 while (i < max) { 203 n = list.item(i++); 204 if (n instanceof Element ) { 205 elem = (Element ) n; 206 if (elem.getLocalName().equals("cell")) { 207 if (k-- == 0) { 208 Node cellData = elem.getFirstChild(); 211 String rawValue = cellData == null ? 212 "" : cellData.getNodeValue(); 213 Converter converter = 214 getMetaData().getColumnConverter(column + 1); 215 if (converter == null) { 216 return rawValue; 217 } 218 else { 219 try { 220 221 return converter.decode(rawValue, null); 222 } 223 catch (Exception ex) { 224 return rawValue; 225 } 226 } 227 } 228 } 229 } 230 } 231 return null; 232 } 233 262 } 263 | Popular Tags |