KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > connection > JRXMLDataSource


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * JRXMLDataSource.java
28  *
29  * Created on 22 giugno 2004, 14.55
30  *
31  */

32
33 package it.businesslogic.ireport.connection;
34
35
36 import javax.xml.parsers.*;
37 import org.w3c.dom.*;
38
39
40 /**
41  * The XML datasource define a row as a path.
42  * A path can be truncated.
43  * @author Administrator
44  */

45 public class JRXMLDataSource implements net.sf.jasperreports.engine.JRDataSource {
46
47     private FieldNode rootFieldNode = null;
48     private String JavaDoc rowPath = "/";
49     private FieldNode actualPath = null;
50     
51     public JRXMLDataSource(String JavaDoc uri, String JavaDoc rowPath)
52     {
53         this.rowPath = rowPath;
54         try {
55             DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
56             org.w3c.dom.Document JavaDoc doc = db.parse( uri );
57             build(doc);
58         } catch (Exception JavaDoc ex)
59         {
60             ex.printStackTrace();
61         }
62     }
63     
64     public JRXMLDataSource(java.io.File JavaDoc file, String JavaDoc rowPath)
65     {
66         this.rowPath = rowPath;
67         try {
68             DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
69             org.w3c.dom.Document JavaDoc doc = db.parse( file );
70             build(doc);
71         } catch (Exception JavaDoc ex)
72         {
73             ex.printStackTrace();
74         }
75     }
76         
77     public JRXMLDataSource(java.io.InputStream JavaDoc is, String JavaDoc rowPath)
78     {
79         this.rowPath = rowPath;
80         try {
81             DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
82             org.w3c.dom.Document JavaDoc doc = db.parse( is );
83             build(doc);
84         } catch (Exception JavaDoc ex)
85         {
86             ex.printStackTrace();
87         }
88     }
89
90     public JRXMLDataSource(FieldNode rootFieldNode, String JavaDoc rowPath)
91     {
92         this.rowPath = rowPath;
93         this.rootFieldNode = rootFieldNode;
94     }
95         
96     private void build(org.w3c.dom.Document JavaDoc doc) throws Exception JavaDoc
97     {
98             Element element = doc.getDocumentElement();
99             rootFieldNode = createNode(element, null);
100     }
101     
102     private FieldNode createNode(Node element, FieldNode parent) throws Exception JavaDoc
103     {
104         
105         FieldNode cn = new FieldNode( element.getNodeName() );
106         
107         // Aggiungiamo gli attributi...
108
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 JavaDoc getFieldValue(net.sf.jasperreports.engine.JRField jRField) throws net.sf.jasperreports.engine.JRException {
142         
143         String JavaDoc path = jRField.getDescription();
144         
145         Object JavaDoc val = getPathValue(rootFieldNode, path);
146         //if (jRField.getClass() == String.class)
147
{
148             return val;
149         }
150         //return null;
151
}
152     
153     public boolean next() throws net.sf.jasperreports.engine.JRException {
154         // Create the next path.
155
return rootFieldNode.next( this.rowPath );
156     }
157     
158    
159     public String JavaDoc getActualPath(){
160         
161         String JavaDoc 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 JavaDoc[] 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 JavaDoc ex)
177         {
178             ex.printStackTrace();
179         }
180     }
181     
182     private Object JavaDoc getPathValue(FieldNode startingNode, String JavaDoc path)
183     {
184         String JavaDoc tag = "";
185         if (path == null) return startingNode.getValue();
186         
187         if (path.startsWith("/")) path = path.substring(1);
188         
189         String JavaDoc 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                 //path = path.substring(0, path.indexOf("@"));
203
}
204             else if (path.indexOf("*") >=0)
205             {
206                 String JavaDoc childName = path.substring(path.indexOf("*") + 1);
207                 // Create a datasource based on this type of chils...
208
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                 //path = path.substring(0, path.indexOf("@"));
213
}
214             else if (path.indexOf("+") >=0)
215             {
216                 String JavaDoc 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         /*
229         if (path.indexOf("/") > 0)
230         {
231             // go to the next path child...
232             String childToTake = path.substring(0,path.indexOf("/"));
233             System.out.println("taking child " + childToTake);
234             return getPathValue(startingNode.getChild(childToTake), path );
235         }
236         */

237         
238         return getPathValue(startingNode.getNextChild(), path );
239         
240     }
241     
242
243     private Object JavaDoc getSubPathValue(FieldNode startingNode, String JavaDoc path)
244     {
245         String JavaDoc tag = "";
246         if (path == null) return startingNode.getValue()+"[" + path + "]";
247         
248         
249         //System.out.println("Resolving path " + path + " now in " + startingNode.getName());
250
//System.out.flush();
251

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); //+ "(tag of " + startingNode.getName() + ")";
260
//path = path.substring(0, path.indexOf("@"));
261
}
262             else if (path.indexOf("*") >=0)
263             {
264                 String JavaDoc childName = path.substring(path.indexOf("*") + 1);
265                 // Create a datasource based on this type of chils...
266
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                 //path = path.substring(0, path.indexOf("@"));
271
}
272             else
273             {
274                 return startingNode.getValue(); //+" " + startingNode.getAttributes().size() +" ("+startingNode.getName() + ")";
275
}
276         }
277         else
278         {
279             path = path.substring(path.indexOf("/") + 1);
280             
281             String JavaDoc childToTake = path;
282             childToTake = getNextNodeName( path);
283             return getSubPathValue(startingNode.getChild(childToTake), path); //"(child of "+startingNode.getName() + " " + startingNode.getAttributes().size() + ")";
284

285         }
286         
287         
288         //return getSubPathValue(startingNode.getNextChild(), path );
289

290     }
291     
292     private static String JavaDoc getNextNodeName( String JavaDoc path)
293     {
294         
295         if (path == null || path.length() ==0) return "";
296         if (path.startsWith("/")) path = path.substring(1);
297         
298         String JavaDoc 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