View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/datalist/dao/XMLDataResult.java,v 1.18 2005/12/02 12:35:47 hkollmann Exp $
3    * $Revision: 1.18 $
4    * $Date: 2005/12/02 12:35:47 $
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;
37  import org.w3c.dom.Node;
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         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 root,
65                          String  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 expression,
88                              int    objectType,
89                              Object 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 getItemValue(int    i,
104                               String expression,
105                               int    objectType) {
106       Object result = null;
107 
108       try {
109          Node 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(toString(n));
130 
131                         break;
132 
133                      case FieldTypes.INTEGER:
134                         result = new Integer(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 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 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 item(int index) {
189       Node 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 toString(Node element) {
212       String result = null;
213 
214       if (element != null) {
215          if (element.getNodeType() == Node.TEXT_NODE) {
216             result = element.getNodeValue();
217          } else {
218             for (Node 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 }