KickJava   Java API By Example, From Geeks To Geeks.

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


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  * FieldNode.java
28  *
29  * Created on 22 giugno 2004, 15.09
30  *
31  */

32
33 package it.businesslogic.ireport.connection;
34
35 /**
36  *
37  * @author Administrator
38  */

39 public class FieldNode {
40     
41    /** Attributi del nodo */
42     private java.util.Properties JavaDoc attributes;
43     /** Nodi figli di questo nodo */
44     private java.util.Vector JavaDoc children;
45     
46     /** Nome del nodo di configurazione */
47     private String JavaDoc name = "";
48     
49     /** Valore del nodo */
50     private String JavaDoc value = null;
51     
52     private int childIndex = 0;
53     
54     private boolean consumed = false;
55         
56     /**
57      * Creates a new instance of FieldNode
58      * @param name Nome del nodo
59      */

60     public FieldNode(String JavaDoc name) {
61         attributes = new java.util.Properties JavaDoc();
62         children = new java.util.Vector JavaDoc();
63         this.name = name;
64     }
65     
66     /**
67      * Attributi del nodo.
68      * @return Value of property attributes.
69      */

70     public java.util.Properties JavaDoc getAttributes() {
71         return attributes;
72     }
73     
74     /**
75      * Setter for property attributes.
76      * @param attributes New value of property attributes.
77      */

78     public void setAttributes(java.util.Properties JavaDoc attributes) {
79         this.attributes = attributes;
80     }
81        
82     /**
83      * Getter for property name.
84      * @return Value of property name.
85      */

86     public java.lang.String JavaDoc getName() {
87         return name;
88     }
89     
90     /**
91      * Setter for property name.
92      * @param name New value of property name.
93      */

94     public void setName(java.lang.String JavaDoc name) {
95         this.name = name;
96     }
97     
98     /**
99      * Restituisce il valore di questo nodo come stringa
100      * @return Value of property value.
101      */

102     public java.lang.String JavaDoc getValue() {
103         return value;
104     }
105     
106         
107     /**
108      * Restituisce il valore di questo nodo come stringa
109      * @return Value of property value.
110      */

111     public java.lang.String JavaDoc getValue(String JavaDoc def) {
112         if (value == null || value.length() == 0) return def;
113         return value;
114     }
115     
116     /**
117      * Restituisce il valore di questo nodo come booleano
118      * @return Value of property value.
119      */

120     public boolean getValueAsBoolean() {
121         if (value !=null && value.trim().equalsIgnoreCase("true")) return true;
122         return false;
123     }
124     
125     
126     /**
127      * Restituisce il valore di questo nodo come intero
128      * @return Value of property value.
129      */

130     public int getValueAsInteger() {
131         try {
132             return Integer.parseInt( value + "" );
133         } catch (Exception JavaDoc ex) {
134             return 0;
135         }
136     }
137     
138     /**
139      * Setter for property value.
140      * @param value New value of property value.
141      */

142     public void setValue(java.lang.String JavaDoc value) {
143         this.value = value;
144     }
145     
146     /**
147      * Ritorna il child specificato.
148      * @param childName Nome del nodo figlio
149      * @param noNull Se true, verra' restituito un FieldNode invece di null. Il nuovo FieldNode verra' inserito nella lista dei childs.
150      * @return Ritorna il nodo richiesto
151      */

152     public FieldNode getChild(java.lang.String JavaDoc childName, boolean noNull) {
153         
154         java.util.Enumeration JavaDoc enum_children = children.elements();
155         while (enum_children.hasMoreElements())
156         {
157             FieldNode cn = (FieldNode)enum_children.nextElement();
158             if (cn.getName().equals(childName) ) return cn;
159         }
160         
161         if (noNull)
162         {
163             FieldNode cn = new FieldNode(childName);
164             this.children.add(cn);
165             return cn;
166         }
167         return null;
168     }
169     
170     
171     /**
172      * Ritorna un vettore di children di nome childName
173      * @param childName Nome del nodo figlio
174      * @return Ritorna il nodo richiesto
175      */

176     public java.util.Vector JavaDoc getChilddren(java.lang.String JavaDoc childName) {
177         
178         java.util.Vector JavaDoc result = new java.util.Vector JavaDoc();
179         java.util.Enumeration JavaDoc enum_children = children.elements();
180         while (enum_children.hasMoreElements())
181         {
182             FieldNode cn = (FieldNode)enum_children.nextElement();
183             if (cn.getName().equals(childName) )
184             {
185                 result.add(cn);
186             }
187         }
188         return result;
189     }
190     
191     /**
192      * Ritorna il child specificato. Se il child non esiste, verra' restituito un FieldNode nuovo invece di null.
193      * @param childName Nome del nodo figlio
194      * @return Ritorna il nodo richiesto (non ritorna mai null)
195      */

196     public FieldNode getChild(java.lang.String JavaDoc childName) {
197        return getChild(childName, true);
198     }
199     
200     
201         /**
202      * Ritorna il child specificato. Se il child non esiste, verra' restituito un FieldNode nuovo invece di null.
203      * @param childName Nome del nodo figlio
204      * @return Ritorna il nodo richiesto (non ritorna mai null)
205      */

206     public FieldNode getNextChild() {
207        
208        
209        if (this.getChildren().size() == 0)
210        {
211             return new FieldNode("Unnamed");
212        }
213        if (childIndex < 0)
214        {
215            childIndex = 0;
216        }
217        return (FieldNode)this.getChildren().elementAt( childIndex );
218     }
219     
220     
221     /**
222      * Restituisce il valore di questo attributo come stringa
223      * @return Ritorna il valore come stringa. (Null se l'attributo richiesto non esiste)
224      * @param attrName Nome dell'attributo da ritornare
225      */

226     public java.lang.String JavaDoc getAttribute(String JavaDoc attrName) {
227         return attributes.getProperty(attrName);
228     }
229     
230      /**
231      * Restituisce il valore di questo attributo come stringa
232      * @return Ritorna il valore come stringa. (il valore di default specificato se l'attributo richiesto non esiste)
233      * @param attrName Nome dell'attributo da ritornare
234      * @param def Valore di default dell'attributo
235      */

236     public java.lang.String JavaDoc getAttribute(String JavaDoc attrName, String JavaDoc def) {
237         return attributes.getProperty(attrName,def);
238     }
239     
240     /**
241      * Restituisce il valore di questo attributo come booleano
242      * @return Ritorna true se l'attributo e' uguale alla stringa "true" (case insensitive).
243      * @param attrName Nome dell'attributo da ritornare
244      */

245     public boolean getAttributeAsBoolean(String JavaDoc attrName) {
246         String JavaDoc val = attributes.getProperty(attrName);
247         if (val !=null && val.trim().equalsIgnoreCase("true")) return true;
248         return false;
249     }
250     
251     
252     /**
253      * Restituisce il valore di questo attributo come intero
254      * @return 0 se l'attributo non e' parsabile come intero, altrimenti il valore intero.
255      * @param attrName Nome dell'attributo da ritornare
256      */

257     public int getAttributeAsInteger(String JavaDoc attrName) {
258         try {
259             String JavaDoc val = attributes.getProperty(attrName);
260             return Integer.parseInt( val + "" );
261         } catch (Exception JavaDoc ex) {
262             return 0;
263         }
264     }
265     
266     /**
267      * Getter for property children.
268      * @return Value of property children.
269      */

270     public java.util.Vector JavaDoc getChildren() {
271         return children;
272     }
273     
274     /**
275      * Setter for property children.
276      * @param children New value of property children.
277      */

278     public void setChildren(java.util.Vector JavaDoc children) {
279         this.children = children;
280     }
281     
282     public boolean next(String JavaDoc xmlPath) {
283         // Se sono una foglia ritorno false
284

285         //System.out.println(">>>" + xmlPath + " " + childIndex + "/" +);
286
if (xmlPath.startsWith("/"))
287         {
288          xmlPath = xmlPath.substring(1);
289         }
290         
291         if (xmlPath.indexOf("/") < 0)
292         {
293             // I am a leaf!!!
294
if (!consumed) { return consumed = true; }
295             else return false;
296         }
297         
298         //xmlPath = xmlPath.substring(1);
299
xmlPath = xmlPath.substring( xmlPath.indexOf("/"));
300
301         // The first time return ever true.
302
if (childIndex == 0 && getChildren().size() == 0)
303         {
304             if (!consumed) { return consumed = true; }
305             else return false;
306         }
307                
308         // See if our current child has other paths to explore...
309
if (!getNextChild().next(xmlPath))
310         {
311             childIndex++;
312             // if they are other childs, return true
313
if (childIndex < getChildren().size()) return getNextChild().next(xmlPath);
314         }
315         else
316         {
317             return true;
318         }
319         //for (; childIndex<getChildren().size(); ++childIndex)
320

321         return false;
322     }
323         
324     public String JavaDoc getChildsPath(String JavaDoc xmlPath)
325     {
326         if (xmlPath.indexOf("/") == xmlPath.lastIndexOf("/"))
327         {
328             // I am a leaf!!!
329
return "";
330         }
331         
332         xmlPath = xmlPath.substring(xmlPath.indexOf("/")+1);
333         return this.getNextChild().getName() +"(" + childIndex +")::" + this.getNextChild().getChildsPath(xmlPath);
334         
335     }
336 }
337
Popular Tags