Project

General

Profile

« Previous | Next » 

Revision 694

Added by bojilova about 23 years ago

reject writing DTD file if it already exists

View differences:

DBEntityResolver.java
35 35
import java.io.File;
36 36
import java.io.Reader;
37 37
import java.io.BufferedReader;
38
import java.io.BufferedInputStream;
38 39
import java.io.FileWriter;
39 40
import java.io.BufferedWriter;
40 41
import java.io.InputStream;
......
141 142
        dbSystemID = uploadDTD(dbSystemID);
142 143
      }
143 144

  
144
      // check URLConnection first
145
      // open URLConnection to check first
145 146
      InputStream istream = checkURLConnection(dbSystemID);
146 147

  
147 148
      // need to register System ID in db XML Catalog if not yet
148 149
      if ( !doctypeIsInDB ) {
150
        // new DTD from outside URL location; try to upload on Metacat
151
        if ( dtdtext == null ) {
152
          dbSystemID = uploadDTDFromURL(istream, dbSystemID);
153
        }
149 154
        registerDTD(doctype, dbSystemID);
150 155
      }
151 156
      
152 157
      // return a byte-input stream for use
153 158
      InputSource is = new InputSource(dbSystemID); 
154
      //is.setPublicId(doctype);
155
      //is.setSystemId(dbSystemID);
159

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

  
159 171
    } else {
160
      // use the provided systemId for other cases
172
      // use provided systemId for the other cases
161 173
      InputStream istream = checkURLConnection(systemId);
162 174
      return null;
163 175
      
......
221 233
  }
222 234

  
223 235
  /** 
224
   * Upload new DTD text identified by @systemId on Metacat file system
236
   * Upload new DTD text identified by @systemId to Metacat file system
225 237
   */
226 238
  private String uploadDTD ( String systemId )
227 239
                 throws SAXException
......
246 258
      // open file writer to write the input into it
247 259
      //String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
248 260
      File f = new File(dtdPath, filename);
261
     synchronized (f) { 
249 262
      try {
250
        if ( f.exists() && !f.canWrite() ) {
251
          throw new IOException("Not writable: " + f.getCanonicalFile());
263
        if ( f.exists() ) {
264
          throw new IOException("File already exist: " + f.getCanonicalFile());
265
        //if ( f.exists() && !f.canWrite() ) {
266
        //  throw new IOException("Not writable: " + f.getCanonicalFile());
252 267
        }
253 268
      } catch (SecurityException se) {
254 269
        // if a security manager exists,
......
273 288
	    out.flush();
274 289
	    out.close();
275 290
	    fw.close();
276
      
291
     } // end of synchronized      
277 292
    } catch (MalformedURLException e) {
278 293
      throw new SAXException
279 294
      ("DBEntityResolver.uploadDTD(): " + e.getMessage());
......
289 304
    return  dtdURL + filename;
290 305
  }
291 306

  
307

  
292 308
  /** 
309
   * Upload new DTD located at outside URL to Metacat file system
310
   */
311
  private String uploadDTDFromURL(InputStream istream, String systemId)
312
                 throws SAXException
313
  {
314
    MetaCatUtil util = new MetaCatUtil();
315
    String dtdPath = util.getOption("dtdPath");
316
    String dtdURL = util.getOption("dtdURL");
317
    
318
    // get filename from systemId
319
    String filename = systemId;
320
    int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
321
    if ( slash > -1 ) {
322
      filename = filename.substring(slash + 1);
323
    }
324

  
325
    // writing dtd text on Metacat file system as filename
326
    try {
327
      // create a buffering character-input stream
328
      // that uses a default-sized input buffer
329
      BufferedInputStream in = new BufferedInputStream(istream);
330

  
331
      // open file writer to write the input into it
332
      //String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
333
      File f = new File(dtdPath, filename);
334
     synchronized (f) { 
335
      try {
336
        if ( f.exists() ) {
337
          throw new IOException("File already exist: " + f.getCanonicalFile());
338
        //if ( f.exists() && !f.canWrite() ) {
339
        //  throw new IOException("Not writable: " + f.getCanonicalFile());
340
        }
341
      } catch (SecurityException se) {
342
        // if a security manager exists,
343
        // its checkRead method is called for f.exist()
344
        // or checkWrite method is called for f.canWrite()
345
        throw se;
346
      }
347
      // create a buffered character-output stream
348
      // that uses a default-sized output buffer
349
      FileWriter fw = new FileWriter(f);
350
      BufferedWriter out = new BufferedWriter(fw);
351

  
352
      // read the input and write into the file writer
353
	    int inputByte;
354
	    while ( (inputByte = in.read()) != -1 ) {
355
	      out.write(inputByte);
356
  	    //out.newLine(); //instead of out.write('\r\n');
357
	    }
358

  
359
      // the input and the output streams must be closed
360
	    in.close();
361
	    out.flush();
362
	    out.close();
363
	    fw.close();
364
     } // end of synchronized
365
    } catch (MalformedURLException e) {
366
      throw new SAXException
367
      ("DBEntityResolver.uploadDTDFromURL(): " + e.getMessage());
368
    } catch (IOException e) {
369
      throw new SAXException
370
      ("DBEntityResolver.uploadDTDFromURL(): " + e.getMessage());
371
    } catch (SecurityException e) {
372
      throw new SAXException
373
      ("DBEntityResolver.uploadDTDFromURL(): " + e.getMessage());
374
    }
375
    
376
    //String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
377
    return  dtdURL + filename;
378
  }
379

  
380
  /** 
293 381
   * Check URL Connection for @systemId, and return an InputStream
294 382
   * that can be used to read from the systemId URL.  The parser ends
295 383
   * up using this via the InputSource to read the DTD.

Also available in: Unified diff