Project

General

Profile

« Previous | Next » 

Revision 374

Added by bojilova over 23 years ago

change the resultset format for DataGuide

View differences:

DBUtil.java
19 19
import java.sql.SQLException;
20 20
import java.sql.PreparedStatement;
21 21
import java.sql.ResultSet;
22
import java.util.Hashtable;
23 22
import java.util.Enumeration;
24 23
import java.util.Vector;
24
import java.util.Stack;
25 25

  
26 26
/**
27 27
 * A suite of utility classes for quering DB
......
43 43
     if (args.length < 1)
44 44
     {
45 45
        System.err.println("Wrong number of arguments!!!");
46
        System.err.println("USAGE: java DBUtil <-dt | -dg doctype>");
46
        System.err.println("USAGE: java DBUtil <-dt | -dg [doctype]>");
47 47
        return;
48 48
     } else {
49 49
        try {
......
63 63
            String dataguide = dbutil.readDataGuide(doctype);
64 64
            System.out.println(dataguide);
65 65
          } else {
66
            System.err.println("USAGE: java DBUtil <-dt | -dg doctype>");
66
            System.err.println("USAGE: java DBUtil <-dt | -dg [doctype]>");
67 67
          }  
68 68

  
69 69
        } catch (Exception e) {
......
134 134
                                          "ORDER BY nodeid");
135 135
            pstmt.setString(1, doctype);
136 136
        } else {
137
            pstmt = conn.prepareStatement("SELECT distinct path, nodeid " + 
137
            pstmt = conn.prepareStatement("SELECT distinct path, doctype, nodeid " + 
138 138
                                          "FROM xml_index " +
139 139
                                          "WHERE path LIKE '/%' " + 
140
                                          "ORDER BY nodeid");
140
                                          "ORDER BY doctype, nodeid");
141 141
        }
142 142

  
143 143
        pstmt.execute();
......
154 154
        pstmt.close();
155 155

  
156 156
    } catch (SQLException e) {
157
      System.out.println("DBUtil.readDoctypes(): " + e.getMessage());
157
      System.out.println("DBUtil.readDataGuide(): " + e.getMessage());
158 158
      throw e;
159 159
    }
160 160

  
161
    return formatToXML(dataguide, "path");
161
    return formatToXML(dataguide);
162 162
  }
163 163

  
164 164
  /**
165
   * format the DataGuide ResultSet to XML
166
   */
167
  private String formatToXML(Vector resultset) {
168
  
169
    String currPath = null;
170
    String currElement = null;
171
    String prevElement = null;
172
    StringBuffer result = new StringBuffer();
173
    Enumeration rs = resultset.elements(); 
174
    Stack st = new Stack();
175
    int i = 0;
176

  
177
    result.append("<?xml version=\"1.0\"?>\n");
178
    result.append("<resultset>\n"); 
179
    
180
    while (rs.hasMoreElements()) {
181
        currPath = (String)rs.nextElement();
182
        while ( !In(prevElement, currPath) ) {
183
            currElement = (String)st.pop();
184
            result.append(pad(" ",i--) + "</" + currElement + ">\n");
185
            if ( st.empty() ) 
186
                prevElement = null;
187
            else    
188
                prevElement = (String)st.peek();
189
        }    
190
        currElement = getElementFromPath(currPath);
191
        st.push(currElement);
192
        result.append(pad(" ",++i) + "<" + currElement + ">\n");
193
        prevElement = currElement;
194
    }
195
    while ( !st.empty() ) {
196
        prevElement = (String)st.pop();
197
        result.append(pad(" ",i--) + "</" + prevElement + ">\n");
198
    }    
199
    result.append("</resultset>\n"); 
200

  
201
    return result.toString();
202
  }
203

  
204
  /**
205
   * check if element is in path like /elem1/elem2/elemn3
206
   */
207
  private boolean In(String element, String path) {
208
    
209
    if ( element == null ) return true;
210
    return ( path.indexOf(element) != -1 );
211
  }
212

  
213
  /**
214
   * get last element from path like /elem1/elem2/elemn3
215
   */
216
  private String getElementFromPath(String path) {
217
    
218
    return ( path.substring(path.lastIndexOf("/")+1) );
219
  }
220

  
221
  /**
222
   * repeates the str n-times
223
   */
224
  private String pad(String str, int n) {
225
    
226
    String result = "";
227
    for ( int i = 0; i < n; i++ )
228
        result = result.concat(str);
229
        
230
    return result;    
231
  }
232

  
233
  /**
165 234
   * format the ResultSet to XML
166 235
   */
167 236
  private String formatToXML(Vector resultset, String tag) {
168 237
  
169 238
    String val = null;
170
    String doctype = null;
171 239
    StringBuffer result = new StringBuffer();
172 240
    Enumeration rs = resultset.elements(); 
173 241

  

Also available in: Unified diff