1 32 33 package it.businesslogic.ireport.connection; 34 35 36 import javax.xml.parsers.*; 37 import org.w3c.dom.*; 38 39 40 45 public class JRXMLDataSource implements net.sf.jasperreports.engine.JRDataSource { 46 47 private FieldNode rootFieldNode = null; 48 private String rowPath = "/"; 49 private FieldNode actualPath = null; 50 51 public JRXMLDataSource(String uri, String rowPath) 52 { 53 this.rowPath = rowPath; 54 try { 55 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 56 org.w3c.dom.Document doc = db.parse( uri ); 57 build(doc); 58 } catch (Exception ex) 59 { 60 ex.printStackTrace(); 61 } 62 } 63 64 public JRXMLDataSource(java.io.File file, String rowPath) 65 { 66 this.rowPath = rowPath; 67 try { 68 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 69 org.w3c.dom.Document doc = db.parse( file ); 70 build(doc); 71 } catch (Exception ex) 72 { 73 ex.printStackTrace(); 74 } 75 } 76 77 public JRXMLDataSource(java.io.InputStream is, String rowPath) 78 { 79 this.rowPath = rowPath; 80 try { 81 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 82 org.w3c.dom.Document doc = db.parse( is ); 83 build(doc); 84 } catch (Exception ex) 85 { 86 ex.printStackTrace(); 87 } 88 } 89 90 public JRXMLDataSource(FieldNode rootFieldNode, String rowPath) 91 { 92 this.rowPath = rowPath; 93 this.rootFieldNode = rootFieldNode; 94 } 95 96 private void build(org.w3c.dom.Document doc) throws Exception 97 { 98 Element element = doc.getDocumentElement(); 99 rootFieldNode = createNode(element, null); 100 } 101 102 private FieldNode createNode(Node element, FieldNode parent) throws Exception 103 { 104 105 FieldNode cn = new FieldNode( element.getNodeName() ); 106 107 NamedNodeMap attributes = element.getAttributes(); 109 for (int i=0; i< attributes.getLength(); ++i) 110 { 111 Node node = attributes.item(i); 112 cn.getAttributes().setProperty( node.getNodeName(), node.getNodeValue() ); 113 } 114 NodeList nl = element.getChildNodes(); 115 for (int i=0; i< nl.getLength(); ++i) 116 { 117 Node node = nl.item(i); 118 if (node.getNodeType() == Node.ELEMENT_NODE) 119 { 120 createNode( node, cn); 121 } 122 else if (node.getNodeType() == Node.CDATA_SECTION_NODE || 123 node.getNodeType() == Node.TEXT_NODE) 124 { 125 cn.setValue( node.getNodeValue() ); 126 } 127 } 128 129 if (parent != null) 130 { 131 parent.getChildren().add( cn); 132 } 133 else 134 { 135 parent = cn; 136 } 137 138 return parent; 139 } 140 141 public Object getFieldValue(net.sf.jasperreports.engine.JRField jRField) throws net.sf.jasperreports.engine.JRException { 142 143 String path = jRField.getDescription(); 144 145 Object val = getPathValue(rootFieldNode, path); 146 { 148 return val; 149 } 150 } 152 153 public boolean next() throws net.sf.jasperreports.engine.JRException { 154 return rootFieldNode.next( this.rowPath ); 156 } 157 158 159 public String getActualPath(){ 160 161 String childPath = this.rowPath; 162 childPath = rowPath.substring( rootFieldNode.getName().length() + 1); 163 return rootFieldNode.getName() + "::" + rootFieldNode.getChildsPath( rowPath ); 164 } 165 166 public static void main(String [] argv) 167 { 168 JRXMLDataSource ds = new JRXMLDataSource("C:\\test_ireport.xml","/addressbook/address/ciccio"); 169 try { 170 System.out.println("starting"); 171 while (ds.next()) 172 { 173 System.out.println(ds.getActualPath()); 174 } 175 System.out.println("finishing"); 176 } catch (Exception ex) 177 { 178 ex.printStackTrace(); 179 } 180 } 181 182 private Object getPathValue(FieldNode startingNode, String path) 183 { 184 String tag = ""; 185 if (path == null) return startingNode.getValue(); 186 187 if (path.startsWith("/")) path = path.substring(1); 188 189 String sub_path = ""; 190 if (path.indexOf("+") >= 0 ) 191 { 192 sub_path = path.substring(path.indexOf("+")+1); 193 path = path.substring(0,path.indexOf("+")+1); 194 } 195 196 if (path.indexOf("/") <0) 197 { 198 if (path.indexOf("@") >=0) 199 { 200 tag = path.substring(path.indexOf("@") + 1); 201 return startingNode.getAttribute(tag); 202 } 204 else if (path.indexOf("*") >=0) 205 { 206 String childName = path.substring(path.indexOf("*") + 1); 207 FieldNode fn = new FieldNode( startingNode.getName() ); 209 fn.setAttributes( startingNode.getAttributes() ); 210 fn.setChildren( startingNode.getChilddren( childName )); 211 return new JRXMLDataSource(fn, "/" + startingNode.getName() + "/" + childName ); 212 } 214 else if (path.indexOf("+") >=0) 215 { 216 String childToTake = sub_path; 217 childToTake = getNextNodeName( sub_path); 218 return getSubPathValue(startingNode.getChild(childToTake) , sub_path); 219 } 220 else 221 { 222 return startingNode.getValue(); 223 } 224 } 225 226 path = path.substring(path.indexOf("/") + 1); 227 path += sub_path; 228 237 238 return getPathValue(startingNode.getNextChild(), path ); 239 240 } 241 242 243 private Object getSubPathValue(FieldNode startingNode, String path) 244 { 245 String tag = ""; 246 if (path == null) return startingNode.getValue()+"[" + path + "]"; 247 248 249 252 if (path.startsWith("/")) path = path.substring(1); 253 254 if (path.indexOf("/") <0) 255 { 256 if (path.indexOf("@") >=0) 257 { 258 tag = path.substring(path.indexOf("@") + 1); 259 return startingNode.getAttribute(tag); } 262 else if (path.indexOf("*") >=0) 263 { 264 String childName = path.substring(path.indexOf("*") + 1); 265 FieldNode fn = new FieldNode( startingNode.getName() ); 267 fn.setAttributes( startingNode.getAttributes() ); 268 fn.setChildren( startingNode.getChilddren( childName )); 269 return new JRXMLDataSource(fn, "/" + startingNode.getName() + "/" + childName ); 270 } 272 else 273 { 274 return startingNode.getValue(); } 276 } 277 else 278 { 279 path = path.substring(path.indexOf("/") + 1); 280 281 String childToTake = path; 282 childToTake = getNextNodeName( path); 283 return getSubPathValue(startingNode.getChild(childToTake), path); 285 } 286 287 288 290 } 291 292 private static String getNextNodeName( String path) 293 { 294 295 if (path == null || path.length() ==0) return ""; 296 if (path.startsWith("/")) path = path.substring(1); 297 298 String childToTake = path; 299 if (path.indexOf("/") >= 0) 300 { 301 childToTake = path.substring(0,path.indexOf("/")); 302 } 303 304 if (childToTake.indexOf("@") >= 0) 305 { 306 childToTake = childToTake.substring(0,path.indexOf("@")); 307 } 308 309 if (childToTake.indexOf("*") >= 0) 310 { 311 childToTake = childToTake.substring(0,path.indexOf("*")); 312 } 313 314 return childToTake; 315 } 316 } 317 | Popular Tags |