1 7 8 package org.dom4j.swing; 9 10 import java.io.Serializable ; 11 12 import org.dom4j.DocumentHelper; 13 import org.dom4j.Node; 14 import org.dom4j.XPath; 15 16 24 public class XMLTableColumnDefinition implements Serializable { 25 public static final int OBJECT_TYPE = 0; 26 27 public static final int STRING_TYPE = 1; 28 29 public static final int NUMBER_TYPE = 2; 30 31 public static final int NODE_TYPE = 3; 32 33 34 private int type; 35 36 37 private String name; 38 39 40 private XPath xpath; 41 42 43 private XPath columnNameXPath; 44 45 public XMLTableColumnDefinition() { 46 } 47 48 public XMLTableColumnDefinition(String name, String expression, int type) { 49 this.name = name; 50 this.type = type; 51 this.xpath = createXPath(expression); 52 } 53 54 public XMLTableColumnDefinition(String name, XPath xpath, int type) { 55 this.name = name; 56 this.xpath = xpath; 57 this.type = type; 58 } 59 60 public XMLTableColumnDefinition(XPath columnXPath, XPath xpath, int type) { 61 this.xpath = xpath; 62 this.columnNameXPath = columnXPath; 63 this.type = type; 64 } 65 66 public static int parseType(String typeName) { 67 if ((typeName != null) && (typeName.length() > 0)) { 68 if (typeName.equals("string")) { 69 return STRING_TYPE; 70 } else if (typeName.equals("number")) { 71 return NUMBER_TYPE; 72 } else if (typeName.equals("node")) { 73 return NODE_TYPE; 74 } 75 } 76 77 return OBJECT_TYPE; 78 } 79 80 public Class getColumnClass() { 81 switch (type) { 82 case STRING_TYPE: 83 return String .class; 84 85 case NUMBER_TYPE: 86 return Number .class; 87 88 case NODE_TYPE: 89 return Node.class; 90 91 default: 92 return Object .class; 93 } 94 } 95 96 public Object getValue(Object row) { 97 switch (type) { 98 case STRING_TYPE: 99 return xpath.valueOf(row); 100 101 case NUMBER_TYPE: 102 return xpath.numberValueOf(row); 103 104 case NODE_TYPE: 105 return xpath.selectSingleNode(row); 106 107 default: 108 return xpath.evaluate(row); 109 } 110 } 111 112 115 120 public int getType() { 121 return type; 122 } 123 124 130 public void setType(int type) { 131 this.type = type; 132 } 133 134 139 public String getName() { 140 return name; 141 } 142 143 149 public void setName(String name) { 150 this.name = name; 151 } 152 153 158 public XPath getXPath() { 159 return xpath; 160 } 161 162 168 public void setXPath(XPath xPath) { 169 this.xpath = xPath; 170 } 171 172 177 public XPath getColumnNameXPath() { 178 return columnNameXPath; 179 } 180 181 187 public void setColumnNameXPath(XPath columnNameXPath) { 188 this.columnNameXPath = columnNameXPath; 189 } 190 191 protected XPath createXPath(String expression) { 194 return DocumentHelper.createXPath(expression); 195 } 196 197 protected void handleException(Exception e) { 198 System.out.println("Caught: " + e); 200 } 201 } 202 203 239 | Popular Tags |