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

JetWebXMLReader.java

Go to the documentation of this file.
00001 package cedar.jetweb.xml;
00002 
00003 import cedar.jetweb.util.JetWebFile;
00004 import org.jdom.Document;
00005 import org.jdom.JDOMException;
00006 import org.jdom.input.*;
00007 import org.xml.sax.*;
00008     
00009 //import org.jdom.input.SAXBuilder;;
00010 
00011 
00012 import java.io.*;
00013 import java.util.zip.GZIPInputStream;
00014 
00022 public class JetWebXMLReader {
00024     protected static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces";
00025 
00027     protected static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation";
00028 
00030     protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema";
00031 
00033     protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking";
00034     
00036     protected static final String DYNAMIC_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/dynamic";
00037     
00039     protected static final String LOAD_EXTERNAL_DTD_FEATURE_ID = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
00040 
00041     // default settings
00042 
00044     protected static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";
00045 
00047     protected static final boolean DEFAULT_NAMESPACES = true;
00048 
00050     protected static final boolean DEFAULT_VALIDATION = false;
00051     
00053     protected static final boolean DEFAULT_LOAD_EXTERNAL_DTD = true;
00054 
00056     protected static final boolean DEFAULT_SCHEMA_VALIDATION = false;
00057 
00059     protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false;
00060     
00062     protected static final boolean DEFAULT_DYNAMIC_VALIDATION = false;
00063 
00065     protected static final boolean DEFAULT_CANONICAL = false;
00066 
00067   
00068     private String filename;
00069     private Document doc;
00070 
00071     private boolean validate = false;
00072     private boolean validateSchema = false;
00073 
00080     public JetWebXMLReader(String filename) throws JetWebXMLException {
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)
00118 
00125     public JetWebXMLReader(String fname,boolean zipped) throws JetWebXMLException {
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)
00182 
00187     private void buildAndValidate(String filename,boolean zipped,String inputParserName) throws JetWebXMLException {
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
00271 
00277     private File getUnzippedSource(String filename) throws JetWebXMLException {
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     }
00321     
00326     private void readZipped(SAXBuilder builder,String filename) throws JetWebXMLException {
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
00360 
00361     
00362     
00363     
00364 
00365     protected Document getDocument() {
00366     //if (doc==null) throw new JetWebXMLException("XML Reader not initialised: Document is null",null);
00367     return doc;
00368     }
00369     protected String getFileName()  {
00370     //if (filename==null) throw new JetWebXMLException("XML Reader not initialised: No Filename provided",null);
00371     return filename;
00372     }
00373 
00374     private boolean exists(String filename){
00375     boolean exists=false;
00376     JetWebFile test = new JetWebFile(filename);
00377     return test.exists();
00378 
00379     }
00380 }//JetWebXMLReader

Generated Wed Jan 17 09:14:27 GMT 2007