Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

JetWebXMLReader Class Reference

Inheritance diagram for JetWebXMLReader:

Inheritance graph
[legend]
List of all members.

Detailed Description

Uses JDOM API to read a JetWeb XML file. Reads XML using a SAX parser.

Author:
S Butterworth, UCL.
Version:
Date
2006-11-23 19:07:42 +0000 (Thu, 23 Nov 2006)
Revision
1316

Definition at line 22 of file JetWebXMLReader.java.


Public Member Functions

 JetWebXMLReader (String filename) throws JetWebXMLException
 JetWebXMLReader (String fname, boolean zipped) throws JetWebXMLException

Protected Member Functions

Document getDocument ()
String getFileName ()

Static Protected Attributes

final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces"
final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation"
final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema"
final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking"
final String DYNAMIC_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/dynamic"
final String LOAD_EXTERNAL_DTD_FEATURE_ID = "http://apache.org/xml/features/nonvalidating/load-external-dtd"
final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser"
final boolean DEFAULT_NAMESPACES = true
final boolean DEFAULT_VALIDATION = false
final boolean DEFAULT_LOAD_EXTERNAL_DTD = true
final boolean DEFAULT_SCHEMA_VALIDATION = false
final boolean DEFAULT_SCHEMA_FULL_CHECKING = false
final boolean DEFAULT_DYNAMIC_VALIDATION = false
final boolean DEFAULT_CANONICAL = false

Private Member Functions

void buildAndValidate (String filename, boolean zipped, String inputParserName) throws JetWebXMLException
File getUnzippedSource (String filename) throws JetWebXMLException
void readZipped (SAXBuilder builder, String filename) throws JetWebXMLException
boolean exists (String filename)

Private Attributes

String filename
Document doc
boolean validate = false
boolean validateSchema = false

Constructor & Destructor Documentation

JetWebXMLReader String  filename  )  throws JetWebXMLException
 

Original constructor: requires xml filename default parser used JDOM SAX Builder constructs a JDOM Document validation is switched off

Definition at line 80 of file JetWebXMLReader.java.

00080                                                                       {
00081         // Load filename
00082         this.filename = filename;
00083 
00084         // Create an instance 
00085     try {
00086         //validation switched off
00087         SAXBuilder builder = new SAXBuilder(DEFAULT_PARSER_NAME,false);
00088         //read the document and read in the plot element
00089         try {
00090         builder.setFeature(LOAD_EXTERNAL_DTD_FEATURE_ID, false);
00091         builder.setFeature(VALIDATION_FEATURE_ID, false);
00092         builder.setFeature(DYNAMIC_VALIDATION_FEATURE_ID, false);
00093         builder.setFeature(SCHEMA_VALIDATION_FEATURE_ID, false);
00094 
00095         }
00096         catch (Exception e) {
00097         System.err.println("warning: Parser does not support feature ("
00098                    +LOAD_EXTERNAL_DTD_FEATURE_ID+")");
00099         }
00100         /*
00101         builder.setEntityResolver(new EntityResolver()
00102         {
00103             public InputSource resolveEntity(String publicId, String systemId)
00104             {
00105                 return new InputSource(
00106                            new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
00107             }
00108         });
00109         */
00110         doc = builder.build(filename);
00111         
00112     } catch (Exception e) {
00113         //System.out.println(e);
00114         throw new JetWebXMLException(e);
00115     }    
00116     
00117     }//JetWebXMLReader(String)

JetWebXMLReader String  fname,
boolean  zipped
throws JetWebXMLException
 

Constructor allowing zipfiles to be input and xml validation to be switched on. Validation is performed if System properties validateXML or validateSchema are 'true'

Parameters:
filename the input xml file, including path
zipped true if the xml file is intended to be zipped - in gzip format

Definition at line 125 of file JetWebXMLReader.java.

00125                                                                                   {
00126         // Load filename
00127         this.filename = fname;
00128 
00129     //first check that file exists
00130     if (!exists(filename)){
00131         throw new JetWebXMLException("XMLFileReader: file not found","Name was: "+filename);
00132     }
00133 
00134     String val =System.getProperty("validateXML");
00135     if ( val!=null && val.equalsIgnoreCase("true")){
00136         validate =true;
00137     }
00138     String valS =System.getProperty("validateSchema");
00139     if ( valS!=null && valS.equalsIgnoreCase("true")){
00140         validateSchema =true;
00141     }
00142 
00143     //testing 
00144     //validate=true;
00145 
00146     if (validate || validateSchema){
00147         String newFilename=filename;
00148         if (zipped){
00149         newFilename =getUnzippedSource(filename).getPath();
00150         } 
00151 
00152         String[] args=new String[]{"-v","-s",newFilename};
00153 
00154         System.out.println("About to call dom.Writer.main");
00155 
00156         //dom.Writer.main(args);
00157 
00158         System.out.println("Success!");
00159 
00160         //JDOM validation is really crap, I give up!
00161         //buildAndValidate(filename,zipped,DEFAULT_PARSER_NAME);
00162         //return;
00163     }
00164 
00165 
00166          
00167     try {
00168         //validation switched off
00169         SAXBuilder builder = new SAXBuilder(DEFAULT_PARSER_NAME,false);
00170         if (zipped){
00171         readZipped(builder,filename);
00172         return;
00173         }
00174         doc = builder.build(filename);
00175         
00176     } catch (Exception e) {
00177         //System.out.println(e);
00178         throw new JetWebXMLException(e);
00179     }    
00180     
00181     }//JetWebXMLReader(String,boolean,boolean)


Member Function Documentation

void buildAndValidate String  filename,
boolean  zipped,
String  inputParserName
throws JetWebXMLException [private]
 

if the validation system properties are set to true, the validation features of the parser are switched on.

parser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", "http://www.cedar.ac.uk/xml/hepdata hepdata.xsd");*

Definition at line 187 of file JetWebXMLReader.java.

00187                                                                                                                    {
00188 
00189         // Load filename 
00190         this.filename = filename;
00191 
00192     boolean validation = validate;
00193     boolean schemaValidation = validateSchema;
00194     boolean schemaFullChecking = validateSchema;
00195 
00196 
00197     boolean namespaces = DEFAULT_NAMESPACES;
00198     boolean externalDTD = DEFAULT_LOAD_EXTERNAL_DTD;
00199     boolean dynamicValidation = DEFAULT_DYNAMIC_VALIDATION;
00200     boolean canonical = DEFAULT_CANONICAL;
00201 
00202     String parserName;
00203 
00204     if (inputParserName==null){
00205         parserName=DEFAULT_PARSER_NAME;
00206     } else {
00207         parserName=inputParserName;
00208     }
00209     
00210     try {
00211 
00212         //validation switched on
00213         SAXBuilder parser = new SAXBuilder(parserName,true);
00214         
00215         // set parser features
00216         try{
00217         parser.setFeature(NAMESPACES_FEATURE_ID, namespaces);
00218         }
00219         catch (Exception e) {
00220         System.err.println
00221             ("warning: Parser does not support feature (" + 
00222              NAMESPACES_FEATURE_ID + ")");
00223         }
00224         try {
00225         parser.setFeature(VALIDATION_FEATURE_ID, validation);
00226         }
00227         catch (Exception e) {
00228         System.err.println("warning: Parser does not support feature ("+VALIDATION_FEATURE_ID+")");
00229         }
00230         try {
00231         parser.setFeature(LOAD_EXTERNAL_DTD_FEATURE_ID, externalDTD);
00232         }
00233         catch (Exception e) {
00234         System.err.println("warning: Parser does not support feature ("+LOAD_EXTERNAL_DTD_FEATURE_ID+")");
00235         }
00236         try {
00237         parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, schemaValidation);
00238         }
00239         catch (Exception e) {
00240         System.err.println("warning: Parser does not support feature ("+SCHEMA_VALIDATION_FEATURE_ID+")");
00241         }
00242         try {
00243         parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
00244         }
00245         catch (Exception e) {
00246         System.err.println("warning: Parser does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
00247         }
00248         try {
00249         parser.setFeature(DYNAMIC_VALIDATION_FEATURE_ID, dynamicValidation);
00250         }
00251         catch (Exception e) {
00252         System.err.println("warning: Parser does not support feature ("+DYNAMIC_VALIDATION_FEATURE_ID+")");
00253         }
00254 
00257          if (zipped){
00258          doc = parser.build(getUnzippedSource(filename));
00259 
00260         } else {
00261         doc = parser.build(filename);
00262         }
00263          
00264 
00265     } catch (Exception e) {
00266         System.out.println(e);
00267         throw new JetWebXMLException(e);
00268     }    
00269     
00270     }//validate

boolean exists String  filename  )  [private]
 

Definition at line 374 of file JetWebXMLReader.java.

00374                                            {
00375     boolean exists=false;
00376     JetWebFile test = new JetWebFile(filename);
00377     return test.exists();
00378 
00379     }

Document getDocument  )  [protected]
 

Definition at line 365 of file JetWebXMLReader.java.

Referenced by JetWebLogReader.fillLog(), and JetWebPlotMLReader.populateSoloPlot().

00365                                      {
00366     //if (doc==null) throw new JetWebXMLException("XML Reader not initialised: Document is null",null);
00367     return doc;
00368     }

String getFileName  )  [protected]
 

Definition at line 369 of file JetWebXMLReader.java.

Referenced by JetWebLogReader.fillLog().

00369                                     {
00370     //if (filename==null) throw new JetWebXMLException("XML Reader not initialised: No Filename provided",null);
00371     return filename;
00372     }

File getUnzippedSource String  filename  )  throws JetWebXMLException [private]
 

Seems difficult for schema file to be located when xml file is input stream from zipfile. Creating an unzipped tempfile in the xml directory gets around this. Input the zipped filename, output, a new unzipped tempfile.

Definition at line 277 of file JetWebXMLReader.java.

References JetWebFile.isZipped().

00277                                                                               {
00278 
00279     File tempFile = null;
00280     JetWebFile testFile = new JetWebFile(filename);
00281     try {
00282         tempFile = File.createTempFile("tmp",".tmp",new File("xml"));
00283         tempFile.deleteOnExit();
00284         if (testFile.isZipped()){
00285         BufferedReader reader = new BufferedReader(
00286                     new InputStreamReader(
00287                         new GZIPInputStream(
00288                     new FileInputStream(filename))));
00289 
00290         PrintWriter writer = new PrintWriter(
00291                      new BufferedWriter(
00292                      new OutputStreamWriter(
00293                      new FileOutputStream(tempFile))));         
00294 
00295         String line;
00296 
00297         while ((line=reader.readLine())!=null){
00298             //write line to temp file
00299             writer.println(line);
00300         }
00301         writer.close();
00302         reader.close();
00303 
00304             
00305         } else {
00306         throw new JetWebXMLException("XMLFile error: unzipped file.",filename);
00307         
00308         }
00309             
00310             
00311     } catch (java.io.IOException e1) {
00312         System.out.println("XMLReader: Problem reading file "+e1);  
00313         System.out.println("File should be "+filename); 
00314         System.out.println("Ought to delete this - no files exist!");   
00315         throw new JetWebXMLException(e1);
00316         //valid(false);
00317     }   
00318     
00319     return tempFile;
00320     }

void readZipped SAXBuilder  builder,
String  filename
throws JetWebXMLException [private]
 

gzipped XML file read into a JDOM Document

Definition at line 326 of file JetWebXMLReader.java.

References JetWebFile.isZipped().

00326                                                                                           {
00327 
00328     JetWebFile testFile = new JetWebFile(filename);
00329 
00330     try {
00331         if (testFile.isZipped()){
00332             // Load filename 
00333         this.filename = filename;
00334 
00335             // Create an instance 
00336             //SAXBuilder builder = new SAXBuilder(DEFAULT_PARSER_NAME,false);
00337             //read the document and read in the plot element
00338         doc = builder.build(new java.util.zip.GZIPInputStream
00339                     (new java.io.FileInputStream(filename)));
00340         } else {
00341             throw new JetWebXMLException
00342             ("XMLFile error: unzipped file.",filename);
00343             
00344         }
00345 
00346 
00347         } catch (JDOMException e) {
00348         //System.out.println(e);
00349         throw new JetWebXMLException(e);
00350         } catch (java.io.IOException e1) {
00351         System.out.println("XMLReader: Problem reading file "+e1);  
00352         System.out.println("File should be "+filename); 
00353         System.out.println("Ought to delete this - no files exist!");   
00354         //valid(false);
00355         }   
00356             
00357         
00358     
00359     }//readZipped


Member Data Documentation

final boolean DEFAULT_CANONICAL = false [static, protected]
 

Default canonical output (false).

Definition at line 65 of file JetWebXMLReader.java.

final boolean DEFAULT_DYNAMIC_VALIDATION = false [static, protected]
 

Default dynamic validation support (false).

Definition at line 62 of file JetWebXMLReader.java.

final boolean DEFAULT_LOAD_EXTERNAL_DTD = true [static, protected]
 

Default load external DTD (true).

Definition at line 53 of file JetWebXMLReader.java.

final boolean DEFAULT_NAMESPACES = true [static, protected]
 

Default namespaces support (true).

Definition at line 47 of file JetWebXMLReader.java.

final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser" [static, protected]
 

Default parser name.

Definition at line 44 of file JetWebXMLReader.java.

final boolean DEFAULT_SCHEMA_FULL_CHECKING = false [static, protected]
 

Default Schema full checking support (false).

Definition at line 59 of file JetWebXMLReader.java.

final boolean DEFAULT_SCHEMA_VALIDATION = false [static, protected]
 

Default Schema validation support (false).

Definition at line 56 of file JetWebXMLReader.java.

final boolean DEFAULT_VALIDATION = false [static, protected]
 

Default validation support (false).

Definition at line 50 of file JetWebXMLReader.java.

Document doc [private]
 

Definition at line 69 of file JetWebXMLReader.java.

final String DYNAMIC_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/dynamic" [static, protected]
 

Dynamic validation feature id (http://apache.org/xml/features/validation/dynamic).

Definition at line 36 of file JetWebXMLReader.java.

String filename [private]
 

Reimplemented in JetWebPlotMLReader.

Definition at line 68 of file JetWebXMLReader.java.

final String LOAD_EXTERNAL_DTD_FEATURE_ID = "http://apache.org/xml/features/nonvalidating/load-external-dtd" [static, protected]
 

Load external DTD feature id (http://apache.org/xml/features/nonvalidating/load-external-dtd).

Definition at line 39 of file JetWebXMLReader.java.

final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces" [static, protected]
 

Namespaces feature id (http://xml.org/sax/features/namespaces).

Definition at line 24 of file JetWebXMLReader.java.

final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking" [static, protected]
 

Schema full checking feature id (http://apache.org/xml/features/validation/schema-full-checking).

Definition at line 33 of file JetWebXMLReader.java.

final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema" [static, protected]
 

Schema validation feature id (http://apache.org/xml/features/validation/schema).

Definition at line 30 of file JetWebXMLReader.java.

boolean validate = false [private]
 

Definition at line 71 of file JetWebXMLReader.java.

boolean validateSchema = false [private]
 

Definition at line 72 of file JetWebXMLReader.java.

final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation" [static, protected]
 

Validation feature id (http://xml.org/sax/features/validation).

Definition at line 27 of file JetWebXMLReader.java.


The documentation for this class was generated from the following file:

Generated Wed Jan 17 09:14:27 GMT 2007