Project

General

Profile

« Previous | Next » 

Revision 103

updated servlet UI and added new intgrated document validity feature to servlet. Working on transformation feature.

View differences:

src/edu/ucsb/nceas/metacat/DBReader.java
144 144
   */
145 145
  public String readXMLDocument(long docid) {
146 146
    StringBuffer doc = new StringBuffer();
147
    DoctypeInfo dti = getDoctypeInfo(docid);
148
    String docname = dti.getDocname();
149
    String doctype = dti.getDoctype();
150
    String sysid = dti.getSystemID();
147 151

  
148 152
    ReaderElement element = new ReaderElement(conn, getRootNode(docid));
149 153
    doc.append("<?xml version=\"1.0\"?>\n");
154
    
155
    if (docname != null) {
156
      if ((doctype != null) && (sysid != null)) {
157
        doc.append("<!DOCTYPE " + docname + " PUBLIC \"" + doctype + 
158
                   "\" \"" + sysid + "\">\n");
159
      } else {
160
        doc.append("<!DOCTYPE " + docname + ">\n");
161
      }
162
    }
150 163
    doc.append(element.toString());
151 164

  
152 165
    return (doc.toString());
153 166
  }
167

  
168
  /**
169
   * Look up the document type information from the database
170
   *
171
   * @param docid the id of the document to look up
172
   */
173
  public DoctypeInfo getDoctypeInfo(long docid) {
174
    PreparedStatement pstmt;
175
    String doctype = null;
176
    String docname = null;
177
    String sysid   = null;
178
    DoctypeInfo dti = null;
179

  
180
    try {
181
      pstmt =
182
        conn.prepareStatement("SELECT docname,doctype " +
183
                                "FROM xml_documents " +
184
                               "WHERE docid = ?");
185
      // Bind the values to the query
186
      pstmt.setLong(1, new Long(docid).longValue());
187

  
188
      pstmt.execute();
189
      ResultSet rs = pstmt.getResultSet();
190
      boolean tableHasRows = rs.next();
191
      if (tableHasRows) {
192
        docname  = rs.getString(1);
193
        doctype  = rs.getString(2);
194
      } 
195
      pstmt.close();
196

  
197
      pstmt =
198
        conn.prepareStatement("SELECT system_id " +
199
                                "FROM xml_catalog " +
200
                               "WHERE public_id = ?");
201
      // Bind the values to the query
202
      pstmt.setString(1, doctype);
203

  
204
      pstmt.execute();
205
      rs = pstmt.getResultSet();
206
      tableHasRows = rs.next();
207
      if (tableHasRows) {
208
        sysid  = rs.getString(1);
209
      } 
210
      pstmt.close();
211
    } catch (SQLException e) {
212
      System.out.println("Error getting id: " + e.getMessage());
213
    }
214

  
215
    dti = new DoctypeInfo(docname, doctype, sysid);
216
    return dti;
217
  }
218

  
219
  /**
220
   * A utility class that encapsulates document type information
221
   */
222
  public class DoctypeInfo {
223
    private String docname = null;
224
    private String doctype = null;
225
    private String system_id = null;
226

  
227
    /**
228
     * Constructor
229
     */
230
    public DoctypeInfo(String docname, String doctype, String system_id) {
231
      this.docname = docname;
232
      this.doctype = doctype;
233
      this.system_id = system_id;
234
    }
235

  
236
    /**
237
     * get the document name
238
     */
239
    public String getDocname() {
240
      return docname;
241
    }
242

  
243
    /**
244
     * get the document type
245
     */
246
    public String getDoctype() {
247
      return doctype;
248
    }
249

  
250
    /**
251
     * get the system identifier
252
     */
253
    public String getSystemID() {
254
      return system_id;
255
    }
256
  }
257

  
154 258
}
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
163 163
  
164 164
    String name = null;
165 165
    String[] value = null;
166
    String[] docid = new String[3];
166 167
    Hashtable params = new Hashtable();
167 168
    Enumeration paramlist = request.getParameterNames();
168 169
    while (paramlist.hasMoreElements()) {
169 170
      name = (String)paramlist.nextElement();
170 171
      value = request.getParameterValues(name);
172

  
173
      // Decode the docid and mouse click information
174
      if (name.endsWith(".y")) {
175
        docid[0] = name.substring(0,name.length()-2);
176
        //out.println("docid => " + docid[0]);
177
        params.put("docid", docid);
178
        name = "ypos";
179
      }
180
      if (name.endsWith(".x")) {
181
        name = "xpos";
182
      }
183

  
171 184
      //out.println(name + " => " + value[0]);
172 185
      params.put(name,value);
173 186
    }
174 187

  
188
    // Determine what type of request the user made
189
    // if the action parameter is set, use it as a default
190
    // but if the ypos param is set, calculate the action needed
175 191
    String action = ((String[])params.get("action"))[0];
192
    long ypos = 0;
193
    try {
194
      ypos = (new Long(((String[])params.get("ypos"))[0]).longValue());
195
      //out.println("<P>YPOS IS " + ypos);
196
      if (ypos <= 13) {
197
        action = "getdocument";
198
      } else if (ypos > 13 && ypos <= 27) {
199
        action = "validate";
200
      } else if (ypos > 27) {
201
        action = "transform";
202
      } else {
203
        action = "";
204
      }
205
    } catch (Exception npe) {
206
      //out.println("<P>Caught exception looking for Y value.");
207
    }
176 208

  
177 209
    if (action.equals("query")) {
178 210
      handleQueryAction(out, params, response);
......
180 212
      try {
181 213
        handleGetDocumentAction(out, params, response);
182 214
      } catch (ClassNotFoundException e) {
183
        System.out.println(e.getMessage());
215
        out.println(e.getMessage());
184 216
      } catch (SQLException se) {
185
        System.out.println(se.getMessage());
217
        out.println(se.getMessage());
186 218
      }
187 219
    } else if (action.equals("putdocument")) {
188 220
      handlePutDocumentAction(out, params, response);
......
292 324
        response.setContentType("text/html");
293 325

  
294 326
        // Look up the document type
295
        String sourcetype = getDoctype(docid);
327
        String sourcetype = docreader.getDoctypeInfo(docid).getDoctype();
296 328

  
297 329
        // Transform the document to the new doctype
298 330
        dbt.transformXMLDocument(doc, sourcetype, "-//W3C//HTML//EN", out);
......
333 365
   */
334 366
  private void handleValidateAction(PrintWriter out, Hashtable params, HttpServletResponse response) {
335 367

  
336
      // Get the document indicated
337
      String[] valtext = (String[])params.get("valtext");
368
    // Get the document indicated
369
    String valtext = null;
370
    try {
371
      valtext = ((String[])params.get("valtext"))[0];
372
    } catch (Exception nullpe) {
338 373

  
374
      String docidstr = null;
375
      long docid = 0;
376
      try {
377
        // Find the document id number
378
        docidstr = ((String[])params.get("docid"))[0]; 
379
        docid = (new Long(docidstr)).longValue();
380
  
381
        // Get the document indicated fromthe db
382
        valtext = docreader.readXMLDocument(docid);
383
      } catch (NullPointerException npe) {
384
        response.setContentType("text/html");
385
        out.println("Error getting document ID: " + 
386
                     docidstr +" (" + docid + ")");
387
      }
388
    }
339 389

  
340
      SAXParser parser = new SAXParser();           // works for both Xerces and Oracle
341
      parser.setValidationMode(true);               // Oracle
390
    SAXParser parser = new SAXParser();  // works for both Xerces and Oracle
391
    parser.setValidationMode(true);      // Oracle
392
    try {
342 393
      GenericXMLValidate gxv = new GenericXMLValidate(parser, xmlcatalogfile);
343
      boolean valid = gxv.validateString(valtext[0]);
394
      boolean valid = gxv.validateString(valtext);
344 395

  
345 396
      // set content type and other response header fields first
346 397
      response.setContentType("text/plain");
347 398
  
348 399
      if (valid) {
349 400
        out.println("The input XML is VALID!");
350
      }
351
      else {
401
      } else {
352 402
        out.println("The input XML is NOT VALID\n" + gxv.returnErrors());
403
        //response.setContentType("text/xml");
404
        //out.println(valtext);
353 405
      } 
406
    } catch (NullPointerException npe2) {
407
      // set content type and other response header fields first
408
      response.setContentType("text/html");
409
      //out.println(valtext); 
410
      out.println("Error validating document."); 
354 411
    }
412
  }
355 413

  
356 414
  /** 
357 415
   * Look up the document type from the database
358 416
   *
417
   * @param docid the id of the document to look up
359 418
   */
360
  private String getDoctype(long docid) {
361
    // Look up the System ID of the XSL sheet
419
  private String OldgetDoctype(long docid) {
362 420
    PreparedStatement pstmt;
363 421
    String doctype = null;
364 422
 
DBReader.java
144 144
   */
145 145
  public String readXMLDocument(long docid) {
146 146
    StringBuffer doc = new StringBuffer();
147
    DoctypeInfo dti = getDoctypeInfo(docid);
148
    String docname = dti.getDocname();
149
    String doctype = dti.getDoctype();
150
    String sysid = dti.getSystemID();
147 151

  
148 152
    ReaderElement element = new ReaderElement(conn, getRootNode(docid));
149 153
    doc.append("<?xml version=\"1.0\"?>\n");
154
    
155
    if (docname != null) {
156
      if ((doctype != null) && (sysid != null)) {
157
        doc.append("<!DOCTYPE " + docname + " PUBLIC \"" + doctype + 
158
                   "\" \"" + sysid + "\">\n");
159
      } else {
160
        doc.append("<!DOCTYPE " + docname + ">\n");
161
      }
162
    }
150 163
    doc.append(element.toString());
151 164

  
152 165
    return (doc.toString());
153 166
  }
167

  
168
  /**
169
   * Look up the document type information from the database
170
   *
171
   * @param docid the id of the document to look up
172
   */
173
  public DoctypeInfo getDoctypeInfo(long docid) {
174
    PreparedStatement pstmt;
175
    String doctype = null;
176
    String docname = null;
177
    String sysid   = null;
178
    DoctypeInfo dti = null;
179

  
180
    try {
181
      pstmt =
182
        conn.prepareStatement("SELECT docname,doctype " +
183
                                "FROM xml_documents " +
184
                               "WHERE docid = ?");
185
      // Bind the values to the query
186
      pstmt.setLong(1, new Long(docid).longValue());
187

  
188
      pstmt.execute();
189
      ResultSet rs = pstmt.getResultSet();
190
      boolean tableHasRows = rs.next();
191
      if (tableHasRows) {
192
        docname  = rs.getString(1);
193
        doctype  = rs.getString(2);
194
      } 
195
      pstmt.close();
196

  
197
      pstmt =
198
        conn.prepareStatement("SELECT system_id " +
199
                                "FROM xml_catalog " +
200
                               "WHERE public_id = ?");
201
      // Bind the values to the query
202
      pstmt.setString(1, doctype);
203

  
204
      pstmt.execute();
205
      rs = pstmt.getResultSet();
206
      tableHasRows = rs.next();
207
      if (tableHasRows) {
208
        sysid  = rs.getString(1);
209
      } 
210
      pstmt.close();
211
    } catch (SQLException e) {
212
      System.out.println("Error getting id: " + e.getMessage());
213
    }
214

  
215
    dti = new DoctypeInfo(docname, doctype, sysid);
216
    return dti;
217
  }
218

  
219
  /**
220
   * A utility class that encapsulates document type information
221
   */
222
  public class DoctypeInfo {
223
    private String docname = null;
224
    private String doctype = null;
225
    private String system_id = null;
226

  
227
    /**
228
     * Constructor
229
     */
230
    public DoctypeInfo(String docname, String doctype, String system_id) {
231
      this.docname = docname;
232
      this.doctype = doctype;
233
      this.system_id = system_id;
234
    }
235

  
236
    /**
237
     * get the document name
238
     */
239
    public String getDocname() {
240
      return docname;
241
    }
242

  
243
    /**
244
     * get the document type
245
     */
246
    public String getDoctype() {
247
      return doctype;
248
    }
249

  
250
    /**
251
     * get the system identifier
252
     */
253
    public String getSystemID() {
254
      return system_id;
255
    }
256
  }
257

  
154 258
}
MetaCatServlet.java
163 163
  
164 164
    String name = null;
165 165
    String[] value = null;
166
    String[] docid = new String[3];
166 167
    Hashtable params = new Hashtable();
167 168
    Enumeration paramlist = request.getParameterNames();
168 169
    while (paramlist.hasMoreElements()) {
169 170
      name = (String)paramlist.nextElement();
170 171
      value = request.getParameterValues(name);
172

  
173
      // Decode the docid and mouse click information
174
      if (name.endsWith(".y")) {
175
        docid[0] = name.substring(0,name.length()-2);
176
        //out.println("docid => " + docid[0]);
177
        params.put("docid", docid);
178
        name = "ypos";
179
      }
180
      if (name.endsWith(".x")) {
181
        name = "xpos";
182
      }
183

  
171 184
      //out.println(name + " => " + value[0]);
172 185
      params.put(name,value);
173 186
    }
174 187

  
188
    // Determine what type of request the user made
189
    // if the action parameter is set, use it as a default
190
    // but if the ypos param is set, calculate the action needed
175 191
    String action = ((String[])params.get("action"))[0];
192
    long ypos = 0;
193
    try {
194
      ypos = (new Long(((String[])params.get("ypos"))[0]).longValue());
195
      //out.println("<P>YPOS IS " + ypos);
196
      if (ypos <= 13) {
197
        action = "getdocument";
198
      } else if (ypos > 13 && ypos <= 27) {
199
        action = "validate";
200
      } else if (ypos > 27) {
201
        action = "transform";
202
      } else {
203
        action = "";
204
      }
205
    } catch (Exception npe) {
206
      //out.println("<P>Caught exception looking for Y value.");
207
    }
176 208

  
177 209
    if (action.equals("query")) {
178 210
      handleQueryAction(out, params, response);
......
180 212
      try {
181 213
        handleGetDocumentAction(out, params, response);
182 214
      } catch (ClassNotFoundException e) {
183
        System.out.println(e.getMessage());
215
        out.println(e.getMessage());
184 216
      } catch (SQLException se) {
185
        System.out.println(se.getMessage());
217
        out.println(se.getMessage());
186 218
      }
187 219
    } else if (action.equals("putdocument")) {
188 220
      handlePutDocumentAction(out, params, response);
......
292 324
        response.setContentType("text/html");
293 325

  
294 326
        // Look up the document type
295
        String sourcetype = getDoctype(docid);
327
        String sourcetype = docreader.getDoctypeInfo(docid).getDoctype();
296 328

  
297 329
        // Transform the document to the new doctype
298 330
        dbt.transformXMLDocument(doc, sourcetype, "-//W3C//HTML//EN", out);
......
333 365
   */
334 366
  private void handleValidateAction(PrintWriter out, Hashtable params, HttpServletResponse response) {
335 367

  
336
      // Get the document indicated
337
      String[] valtext = (String[])params.get("valtext");
368
    // Get the document indicated
369
    String valtext = null;
370
    try {
371
      valtext = ((String[])params.get("valtext"))[0];
372
    } catch (Exception nullpe) {
338 373

  
374
      String docidstr = null;
375
      long docid = 0;
376
      try {
377
        // Find the document id number
378
        docidstr = ((String[])params.get("docid"))[0]; 
379
        docid = (new Long(docidstr)).longValue();
380
  
381
        // Get the document indicated fromthe db
382
        valtext = docreader.readXMLDocument(docid);
383
      } catch (NullPointerException npe) {
384
        response.setContentType("text/html");
385
        out.println("Error getting document ID: " + 
386
                     docidstr +" (" + docid + ")");
387
      }
388
    }
339 389

  
340
      SAXParser parser = new SAXParser();           // works for both Xerces and Oracle
341
      parser.setValidationMode(true);               // Oracle
390
    SAXParser parser = new SAXParser();  // works for both Xerces and Oracle
391
    parser.setValidationMode(true);      // Oracle
392
    try {
342 393
      GenericXMLValidate gxv = new GenericXMLValidate(parser, xmlcatalogfile);
343
      boolean valid = gxv.validateString(valtext[0]);
394
      boolean valid = gxv.validateString(valtext);
344 395

  
345 396
      // set content type and other response header fields first
346 397
      response.setContentType("text/plain");
347 398
  
348 399
      if (valid) {
349 400
        out.println("The input XML is VALID!");
350
      }
351
      else {
401
      } else {
352 402
        out.println("The input XML is NOT VALID\n" + gxv.returnErrors());
403
        //response.setContentType("text/xml");
404
        //out.println(valtext);
353 405
      } 
406
    } catch (NullPointerException npe2) {
407
      // set content type and other response header fields first
408
      response.setContentType("text/html");
409
      //out.println(valtext); 
410
      out.println("Error validating document."); 
354 411
    }
412
  }
355 413

  
356 414
  /** 
357 415
   * Look up the document type from the database
358 416
   *
417
   * @param docid the id of the document to look up
359 418
   */
360
  private String getDoctype(long docid) {
361
    // Look up the System ID of the XSL sheet
419
  private String OldgetDoctype(long docid) {
362 420
    PreparedStatement pstmt;
363 421
    String doctype = null;
364 422
 
lib/resultset.xsl
56 56
            </xsl:attribute>
57 57

  
58 58
          <td>
59
<!--
59 60
              <input type="radio" name="docid">
60 61
                <xsl:attribute name="value">
61 62
                  <xsl:value-of select="./docid"/>
62 63
                </xsl:attribute>
63 64
              </input>
64 65
              <input type="submit" value="Display"/>
65
<!--
66
              <input border="0" type="image" name="docid" 
67
                     src="/xmltodb/images/details.png">
68
                <xsl:attribute name="value">
66
-->
67
              <input border="0" type="image">
68
                <xsl:attribute name="src">
69
                  <xsl:choose>
70
                    <xsl:when test="position() 
71
                         mod 2 = 1">/xmltodb/images/bttns-white.png</xsl:when>
72
                    <xsl:when test="position() 
73
                         mod 2 = 0">/xmltodb/images/bttns-blue.jpg</xsl:when>
74
                  </xsl:choose>
75
                </xsl:attribute>
76
                <xsl:attribute name="name">
69 77
                  <xsl:value-of select="./docid"/>
70 78
                </xsl:attribute>
71 79
              </input>
72
-->
73 80
          </td>
74 81
          <td><xsl:value-of select="./doctitle"/>
75 82
              <xsl:text>&nbsp;</xsl:text>
lib/style/resultset.xsl
56 56
            </xsl:attribute>
57 57

  
58 58
          <td>
59
<!--
59 60
              <input type="radio" name="docid">
60 61
                <xsl:attribute name="value">
61 62
                  <xsl:value-of select="./docid"/>
62 63
                </xsl:attribute>
63 64
              </input>
64 65
              <input type="submit" value="Display"/>
65
<!--
66
              <input border="0" type="image" name="docid" 
67
                     src="/xmltodb/images/details.png">
68
                <xsl:attribute name="value">
66
-->
67
              <input border="0" type="image">
68
                <xsl:attribute name="src">
69
                  <xsl:choose>
70
                    <xsl:when test="position() 
71
                         mod 2 = 1">/xmltodb/images/bttns-white.png</xsl:when>
72
                    <xsl:when test="position() 
73
                         mod 2 = 0">/xmltodb/images/bttns-blue.jpg</xsl:when>
74
                  </xsl:choose>
75
                </xsl:attribute>
76
                <xsl:attribute name="name">
69 77
                  <xsl:value-of select="./docid"/>
70 78
                </xsl:attribute>
71 79
              </input>
72
-->
73 80
          </td>
74 81
          <td><xsl:value-of select="./doctitle"/>
75 82
              <xsl:text>&nbsp;</xsl:text>
resultset.xsl
56 56
            </xsl:attribute>
57 57

  
58 58
          <td>
59
<!--
59 60
              <input type="radio" name="docid">
60 61
                <xsl:attribute name="value">
61 62
                  <xsl:value-of select="./docid"/>
62 63
                </xsl:attribute>
63 64
              </input>
64 65
              <input type="submit" value="Display"/>
65
<!--
66
              <input border="0" type="image" name="docid" 
67
                     src="/xmltodb/images/details.png">
68
                <xsl:attribute name="value">
66
-->
67
              <input border="0" type="image">
68
                <xsl:attribute name="src">
69
                  <xsl:choose>
70
                    <xsl:when test="position() 
71
                         mod 2 = 1">/xmltodb/images/bttns-white.png</xsl:when>
72
                    <xsl:when test="position() 
73
                         mod 2 = 0">/xmltodb/images/bttns-blue.jpg</xsl:when>
74
                  </xsl:choose>
75
                </xsl:attribute>
76
                <xsl:attribute name="name">
69 77
                  <xsl:value-of select="./docid"/>
70 78
                </xsl:attribute>
71 79
              </input>
72
-->
73 80
          </td>
74 81
          <td><xsl:value-of select="./doctitle"/>
75 82
              <xsl:text>&nbsp;</xsl:text>

Also available in: Unified diff