Project

General

Profile

Revision 1358

Added by Jing Tao almost 18 years ago

Merge the branch for monarch.

View differences:

src/edu/ucsb/nceas/metacat/DBEntityResolver.java
44 44
import java.net.URLConnection;
45 45
import java.net.MalformedURLException;
46 46

  
47
/** 
48
 * A database aware Class implementing EntityResolver interface for the SAX 
49
 * parser to call when processing the XML stream and intercepting any 
50
 * external entities (including the external DTD subset and external 
47
/**
48
 * A database aware Class implementing EntityResolver interface for the SAX
49
 * parser to call when processing the XML stream and intercepting any
50
 * external entities (including the external DTD subset and external
51 51
 * parameter entities, if any) before including them.
52 52
 */
53 53
public class DBEntityResolver implements EntityResolver
......
59 59
  private String systemid = null;
60 60
  private Reader dtdtext = null;
61 61

  
62
  /** 
62
  /**
63 63
   * Construct an instance of the DBEntityResolver class
64 64
   *
65 65
   * @param conn the JDBC connection to which information is written
......
68 68
  {
69 69
    this.connection= conn;
70 70
  }
71
  /** 
71
  /**
72 72
   * Construct an instance of the DBEntityResolver class
73 73
   *
74 74
   * @param conn the JDBC connection to which information is written
......
81 81
    this.handler = handler;
82 82
    this.dtdtext = dtd;
83 83
  }
84
   
85
  /** 
86
   * The Parser call this method before opening any external entity 
84

  
85
  /**
86
   * The Parser call this method before opening any external entity
87 87
   * except the top-level document entity (including the external DTD subset,
88
   * external entities referenced within the DTD, and external entities 
88
   * external entities referenced within the DTD, and external entities
89 89
   * referenced within the document element)
90 90
   */
91 91
  public InputSource resolveEntity (String publicId, String systemId)
......
93 93
  {
94 94
    String dbSystemID;
95 95
    String doctype = null;
96
    
96

  
97 97
    // Won't have a handler under all cases
98 98
    if ( handler != null ) {
99 99
      if ( handler instanceof DBSAXHandler ) {
......
101 101
        dhandler = (DBSAXHandler)handler;
102 102
        if ( dhandler.processingDTD() ) {
103 103
          // public ID is doctype
104
          if (publicId != null) {   
104
          if (publicId != null) {
105 105
            doctype = publicId;
106 106
          // assume public ID (doctype) is docname
107 107
          } else if (systemId != null) {
......
113 113
        ahandler = (AccessControlList)handler;
114 114
        //if ( ahandler.processingDTD() ) {
115 115
          // public ID is doctype
116
          if (publicId != null) {   
116
          if (publicId != null) {
117 117
            doctype = publicId;
118 118
          // assume public ID (doctype) is docname
119 119
          } else if (systemId != null) {
......
125 125

  
126 126
    // get System ID for doctype
127 127
    if (doctype != null) {
128
      // look at db XML Catalog for System ID 
128
      // look at db XML Catalog for System ID
129 129
      dbSystemID = getDTDSystemID(doctype);
130 130
      boolean doctypeIsInDB = true;
131 131
      // no System ID found in db XML Catalog
......
136 136
          dbSystemID = systemId;
137 137
        }
138 138
      }
139

  
140 139
      // there are dtd text provided; try to upload on Metacat
141 140
      if ( dtdtext != null ) {
142 141
        dbSystemID = uploadDTD(dbSystemID);
......
153 152
        }
154 153
        registerDTD(doctype, dbSystemID);
155 154
      }
156
      
157 155
      // return a byte-input stream for use
158
      InputSource is = new InputSource(dbSystemID); 
156
      InputSource is = new InputSource(dbSystemID);
159 157

  
160 158
      // close and open URLConnection again
161 159
      try {
162 160
        istream.close();
163 161
      } catch (IOException e) {
164
        throw new SAXException 
162
        throw new SAXException
165 163
        ("DBEntityResolver.resolveEntity(): " + e.getMessage());
166
      }    
164
      }
167 165
      istream = checkURLConnection(dbSystemID);
168 166
      is.setByteStream(istream);
169 167
      return is;
170

  
171 168
    } else {
172 169
      // use provided systemId for the other cases
173 170
      InputStream istream = checkURLConnection(systemId);
174 171
      return null;
175
      
172

  
176 173
    }
177
    
174

  
178 175
  }
179 176

  
180
  /** 
177
  /**
181 178
   * Look at db XML Catalog to get System ID (if any) for @doctype.
182 179
   * Return null if there are no System ID found for @doctype
183 180
   */
......
192 189
      //check out DBConnection
193 190
      conn=DBConnectionPool.getDBConnection("DBEntityResolver.getDTDSystemID");
194 191
      serialNumber=conn.getCheckOutSerialNumber();
195
      
192

  
196 193
      stmt = conn.createStatement();
197
      stmt.execute("SELECT system_id FROM xml_catalog " + 
194
      stmt.execute("SELECT system_id FROM xml_catalog " +
198 195
                   "WHERE entry_type = 'DTD' AND public_id = '" +
199 196
                   doctype + "'");
200 197
      ResultSet rs = stmt.getResultSet();
......
228 225
    return systemid;
229 226
  }
230 227

  
231
  /** 
232
   * Register new DTD identified by @systemId in Metacat XML Catalog 
228
  /**
229
   * Register new DTD identified by @systemId in Metacat XML Catalog
233 230
   * . make a reference with @systemId for @doctype in Metacat DB
234 231
   */
235 232
  private void registerDTD ( String doctype, String systemId )
......
243 240
      //check out DBConnection
244 241
      //conn=DBConnectionPool.getDBConnection("DBEntityResolver.registerDTD");
245 242
      //serialNumber=conn.getCheckOutSerialNumber();
246
      
247
      
243

  
244

  
248 245
      pstmt = connection.prepareStatement(
249 246
             "INSERT INTO xml_catalog " +
250 247
             "(catalog_id, entry_type, public_id, system_id) " +
......
274 271
      }//catch
275 272
      //DBConnectionPool.returnDBConnection(conn, serialNumber);
276 273
    }//finally
277
    
274

  
278 275
  }
279 276

  
280
  /** 
277
  /**
281 278
   * Upload new DTD text identified by @systemId to Metacat file system
282 279
   */
283 280
  private String uploadDTD ( String systemId )
......
286 283
    MetaCatUtil util = new MetaCatUtil();
287 284
    String dtdPath = util.getOption("dtdPath");
288 285
    String dtdURL = util.getOption("dtdURL");
289
    
286

  
290 287
    // get filename from systemId
291 288
    String filename = systemId;
292 289
    int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
......
303 300
      // open file writer to write the input into it
304 301
      //String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
305 302
      File f = new File(dtdPath, filename);
306
     synchronized (f) { 
303
     synchronized (f) {
307 304
      try {
308 305
        if ( f.exists() ) {
309 306
          throw new IOException("File already exist: " + f.getCanonicalFile());
......
322 319
      BufferedWriter out = new BufferedWriter(fw);
323 320

  
324 321
      // read the input and write into the file writer
325
	    String inputLine;
326
	    while ( (inputLine = in.readLine()) != null ) {
327
	      out.write(inputLine, 0, inputLine.length());
328
  	    out.newLine(); //instead of out.write('\r\n');
329
	    }
322
      String inputLine;
323
      while ( (inputLine = in.readLine()) != null ) {
324
        out.write(inputLine, 0, inputLine.length());
325
        out.newLine(); //instead of out.write('\r\n');
326
      }
330 327

  
331 328
      // the input and the output streams must be closed
332
	    in.close();
333
	    out.flush();
334
	    out.close();
335
	    fw.close();
336
     } // end of synchronized      
329
      in.close();
330
      out.flush();
331
      out.close();
332
      fw.close();
333
     } // end of synchronized
337 334
    } catch (MalformedURLException e) {
338 335
      throw new SAXException
339 336
      ("DBEntityResolver.uploadDTD(): " + e.getMessage());
......
344 341
      throw new SAXException
345 342
      ("DBEntityResolver.uploadDTD(): " + e.getMessage());
346 343
    }
347
    
344

  
348 345
    //String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
349 346
    return  dtdURL + filename;
350 347
  }
351 348

  
352 349

  
353
  /** 
350
  /**
354 351
   * Upload new DTD located at outside URL to Metacat file system
355 352
   */
356 353
  private String uploadDTDFromURL(InputStream istream, String systemId)
......
359 356
    MetaCatUtil util = new MetaCatUtil();
360 357
    String dtdPath = util.getOption("dtdPath");
361 358
    String dtdURL = util.getOption("dtdURL");
362
    
359

  
363 360
    // get filename from systemId
364 361
    String filename = systemId;
365 362
    int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
......
376 373
      // open file writer to write the input into it
377 374
      //String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
378 375
      File f = new File(dtdPath, filename);
379
     synchronized (f) { 
376
     synchronized (f) {
380 377
      try {
381 378
        if ( f.exists() ) {
382 379
          throw new IOException("File already exist: " + f.getCanonicalFile());
......
395 392
      BufferedWriter out = new BufferedWriter(fw);
396 393

  
397 394
      // read the input and write into the file writer
398
	    int inputByte;
399
	    while ( (inputByte = in.read()) != -1 ) {
400
	      out.write(inputByte);
401
  	    //out.newLine(); //instead of out.write('\r\n');
402
	    }
395
      int inputByte;
396
      while ( (inputByte = in.read()) != -1 ) {
397
        out.write(inputByte);
398
        //out.newLine(); //instead of out.write('\r\n');
399
      }
403 400

  
404 401
      // the input and the output streams must be closed
405
	    in.close();
406
	    out.flush();
407
	    out.close();
408
	    fw.close();
402
      in.close();
403
      out.flush();
404
      out.close();
405
      fw.close();
409 406
     } // end of synchronized
410 407
    } catch (MalformedURLException e) {
411 408
      throw new SAXException
......
417 414
      throw new SAXException
418 415
      ("DBEntityResolver.uploadDTDFromURL(): " + e.getMessage());
419 416
    }
420
    
417

  
421 418
    //String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
422 419
    return  dtdURL + filename;
423 420
  }
424 421

  
425
  /** 
422
  /**
426 423
   * Check URL Connection for @systemId, and return an InputStream
427 424
   * that can be used to read from the systemId URL.  The parser ends
428 425
   * up using this via the InputSource to read the DTD.
......
442 439
    } catch (IOException e) {
443 440
      throw new SAXException
444 441
      ("DBEntityResolver.checkURLConnection(): " + e.getMessage());
445
    }    
446
  }   
442
    }
443
  }
447 444
}

Also available in: Unified diff