KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > event > datalist > dao > XMLDataResult


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/datalist/dao/XMLDataResult.java,v 1.16 2005/02/19 21:26:30 hkollmann Exp $
3  * $Revision: 1.16 $
4  * $Date: 2005/02/19 21:26:30 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.event.datalist.dao;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.dbforms.config.FieldTypes;
30
31 import org.dbforms.dom.DOMFactory;
32
33 import org.dbforms.util.TimeUtil;
34 import org.dbforms.util.Util;
35
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.xpath.XPathEvaluator;
39 import org.w3c.dom.xpath.XPathNSResolver;
40 import org.w3c.dom.xpath.XPathResult;
41
42
43
44 /**
45  * Delegates the whole xpath stuff to this class. Holds the result of an xpath
46  * query. Do the mapping between java objects and fields.
47  *
48  * @author hkk
49  */

50 public class XMLDataResult {
51    private static Log logCat = LogFactory.getLog(XMLDataResult.class);
52    private Element JavaDoc root;
53    private XPathEvaluator evaluator;
54    private XPathNSResolver resolver;
55    private XPathResult data;
56    private boolean changed = false;
57
58    /**
59     * Creates a new XMLDataResult object.
60     *
61     * @param root xml dom object
62     * @param qry xpath string to query
63     */

64    public XMLDataResult(Element JavaDoc root,
65                         String JavaDoc qry) {
66       this.root = root;
67       this.evaluator = DOMFactory.instance()
68                                  .newXPathEvaluator();
69       resolver = evaluator.createNSResolver(root);
70
71       // Evaluate the xpath expression.
72
data = (XPathResult) evaluator.evaluate(qry, this.root, resolver,
73                                               XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
74                                               null);
75    }
76
77    /**
78     * sets the field value of a special node as string. node is decribed by an
79     * xpath string
80     *
81     * @param i node of result to return
82     * @param expression xpath string which discribes the field to return
83     * @param objectType field type to return
84     * @param value value to set
85     */

86    public void setItemValue(int i,
87                             String JavaDoc expression,
88                             int objectType,
89                             Object JavaDoc value) {
90    }
91
92
93    /**
94     * returns the field value of a special node as Object. Node is decribed by
95     * an xpath string
96     *
97     * @param i node of result to return
98     * @param expression xpath string which discribes the field to return
99     * @param objectType field type to return
100     *
101     * @return value as Object of selected type
102     */

103    public Object JavaDoc getItemValue(int i,
104                               String JavaDoc expression,
105                               int objectType) {
106       Object JavaDoc result = null;
107
108       try {
109          Node JavaDoc n = item(i);
110
111          if (n != null) {
112             XPathResult pdata = (XPathResult) evaluator.evaluate(expression, n,
113                                                                 resolver,
114                                                                 XPathResult.FIRST_ORDERED_NODE_TYPE,
115                                                                 null);
116
117             if (pdata != null) {
118                n = pdata.getSingleNodeValue();
119
120                if (n != null) {
121                   switch (objectType) {
122                      case FieldTypes.CHAR:
123                         result = toString(n);
124
125                         break;
126
127                      case FieldTypes.FLOAT:
128                      case FieldTypes.NUMERIC:
129                         result = new Double JavaDoc(toString(n));
130
131                         break;
132
133                      case FieldTypes.INTEGER:
134                         result = new Integer JavaDoc(toString(n));
135
136                         break;
137
138                      case FieldTypes.DATE:
139                      case FieldTypes.TIMESTAMP:
140                      case FieldTypes.TIME:
141                         result = TimeUtil.parseISO8601Date(toString(n));
142
143                         break;
144
145                      default:
146                         result = toString(n);
147
148                         break;
149                   }
150                }
151             }
152          }
153       } catch (Exception JavaDoc e) {
154          logCat.error("getItemValue", e);
155       }
156
157       return result;
158    }
159
160
161    /**
162     * DOCUMENT ME!
163     *
164     * @return DOCUMENT ME!
165     */

166    public Element JavaDoc getRoot() {
167       return root;
168    }
169
170
171    /**
172     * DOCUMENT ME!
173     *
174     * @return DOCUMENT ME!
175     */

176    public boolean hasChanged() {
177       return changed;
178    }
179
180
181    /**
182     * returns the result at index as dom node
183     *
184     * @param index node of result to return
185     *
186     * @return the node at index
187     */

188    public Node JavaDoc item(int index) {
189       Node JavaDoc res = null;
190
191       if (index < data.getSnapshotLength()) {
192          res = data.snapshotItem(index);
193       }
194
195       return res;
196    }
197
198
199    /**
200     * size of resultset
201     *
202     * @return size of result set
203     */

204    public int size() {
205       int res = data.getSnapshotLength();
206
207       return res;
208    }
209
210
211    private String JavaDoc toString(Node JavaDoc element) {
212       String JavaDoc result = null;
213
214       if (element != null) {
215          if (element.getNodeType() == Node.TEXT_NODE) {
216             result = element.getNodeValue();
217          } else {
218             for (Node JavaDoc tx = element.getFirstChild(); tx != null;
219                        tx = tx.getNextSibling()) {
220                result = toString(tx);
221
222                if (!Util.isNull(result)) {
223                   break;
224                }
225             }
226          }
227       }
228
229       return result;
230    }
231 }
232
Popular Tags