Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/rohanclan/ashpool/core/XMLtoResultSetFilter.java


1   /*
2    * Ashpool - XML Database
3    * Copyright (C) 2003 Rob Rohan
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the
6    * Free Software Foundation; either version 2 of the License, or (at your
7    * option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful, but
10   * WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License along
15   * with this program; if not, write to the Free Software Foundation, Inc.,
16   * 675 Mass Ave, Cambridge, MA 02139, USA.
17   *
18   * XMLtoResultSetFilter.java
19   *
20   * Created on February 2, 2003, 8:45 AM
21   */
22  
23  package com.rohanclan.ashpool.core;
24  
25  import org.xml.sax.*;
26  import org.xml.sax.helpers.*;
27  
28  /**
29   * Takes an XML Document in the format
30   * <name>
31   *      <name>
32   *          <fieldname></fieldname>
33   *          <fieldname></fieldname>
34   *      </name>
35   *      <name>
36   *          <fieldname></fieldname>
37   *          <fieldname></fieldname>
38   *      </name>
39   *</name>
40   * and turns it into a AResultSet object which can be used by most sql programs
41   * as results from a database query
42   * @author Rob Rohan
43   */
44  public class XMLtoResultSetFilter extends XMLFilterImpl {
45      
46      private AResultSet ars;
47      private String currentElement="";
48      
49      private String rowmarker="";
50      private String tablemarker="";
51      
52      private int currentrow=-1;
53      
54      private StringBuffer ResValue;
55      
56      /** Creates a new instance of XMLtoResultSetFilter */
57      public XMLtoResultSetFilter(XMLReader reader) {
58          super(reader);
59          ars = new AResultSet();
60          ResValue = new StringBuffer();
61      }
62      
63      /** create the object with the desired reader and a pointer to a recordset */
64      public XMLtoResultSetFilter(XMLReader reader, AResultSet rs){
65          super(reader);
66          ResValue = new StringBuffer();
67          ars = rs;
68      }
69      
70      /** get the results of the object parse */
71      public AResultSet getResultSet(){
72          return this.ars;
73      }
74      
75      public void startDocument() throws SAXException{
76          ars.reset();
77      }
78      
79      /** handle start elements */
80      public void startElement(String uri, String localName, String qName, 
81                      Attributes attr) throws SAXException{
82          //System.out.println(localName);
83          try{
84              //the start of the result set should be wrapped in something
85              if(tablemarker.length() == 0){
86                  tablemarker = qName;
87              //if there is no row marker this is the first pass, into the table
88              //so assume this first element is the rowmarker, or if the localName
89              //is the same as the rowmarker assume this should be a new row
90              }else if(rowmarker.length() == 0 || rowmarker.equals(qName)){
91                  //System.out.println("setting rowmarker " + localName);
92                  rowmarker = qName;
93                  currentrow++;
94              //if we are in a row and the current element is empty focus on this node
95              }else if(currentElement.length() == 0){
96                  //System.out.println("in a row focusing on " + localName);
97                  currentElement = qName;
98  
99                  //if this name is not in the result set column, add it
100                 //System.out.println("column " + localName + " exists? " + ars.columnExists(localName));
101                 if(!ars.columnExists(qName)){
102                     //System.out.println("building a column for " + localName);
103                     ResultColumn rc = new ResultColumn();
104                     rc.type = java.sql.Types.VARCHAR;
105                     rc.columnName = currentElement;
106                     ars.addResultColumn(rc);
107                 //if this column does exist in the result set column
108                 //add a new field
109                 }else{
110                     
111                 }
112             //if there is something in the current element and it is not this 
113             //element, then it must be a sub element, so add the fragment
114             }else if(!currentElement.equals(qName)){
115                 ResValue.append("<" + qName + ">");
116             }
117         }catch(Exception e){
118             System.err.println("XML->RS:Start Element: " + e.toString() + " pass# " + currentrow);
119             e.printStackTrace(System.err);
120         }
121     }
122     
123     /** handle element data */
124     public void characters(char ch[], int start, int length) throws SAXException {
125         for(int x=start; x<(start+length); x++){
126             ResValue.append(ch[x]);
127         }
128     }
129     
130     /** handle end of the element */
131     public void endElement(String uri, String localName, 
132                     String qName) throws SAXException{
133         try{
134             //this is the end of this "field" clean up and start over
135             if(qName.equals(currentElement) && !currentElement.equals("")){
136                 
137                 //System.out.println("add data to " + localName + " and starting process over");
138                 
139                 ResultColumn trc = (ResultColumn)ars.getResultTable().get(ars.findColumn(currentElement));
140                 ((java.util.Vector)trc.columnData).add(ResValue.toString().trim());
141 
142                 currentElement="";
143                 //ResValue="";
144                 ResValue.delete(0, ResValue.length());
145                 //ResValue = new StringBuffer();
146             //if this is the end of the row
147             }else if(qName.equals(rowmarker)){
148                 //System.out.println("changing rowmarker to null");
149                 //currentrow++;
150                 rowmarker="";
151             }else{
152                 ResValue.append("</" + qName + ">");
153             }
154         }catch(Exception e){
155             System.err.println("XML->RS:End Element: " + e.toString() + " pass# " + currentrow);
156             e.printStackTrace(System.err);
157         }
158     }
159     
160 }