/*************** <auto-copyright.pl BEGIN do not edit this line> **************
 *
 * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
 *
 * Original Authors:
 *   Allen Bierbaum, Christopher Just,
 *   Patrick Hartling, Kevin Meinert,
 *   Carolina Cruz-Neira, Albert Baker
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * -----------------------------------------------------------------
 * File:          $RCSfile$
 * Date modified: $Date$
 * Version:       $Revision$
 * -----------------------------------------------------------------
 *
 *************** <auto-copyright.pl END do not edit this line> ***************/
package org.vrjuggler.jccl.config.io;

import java.io.File;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;

/**
 * If a SAX application needs to implement customized handling for external
 * entities, it must implement this interface and register an instance with
 * the SAX driver using the setEntityResolver method.
 *
 * The XML reader will then allow the application to intercept any external
 * entities (including the external DTD subset and external parameter
 * entities, if any) before including them.
 */
public class ConfigSchemaResolver implements EntityResolver
{
   public InputSource resolveEntity(String publicId, String systemId)
      throws org.xml.sax.SAXException
           , java.io.IOException
   {
      InputSource source;

      // If this systemId is one of the JCCL schemas, process it.
      // XXX: Is there a better way to filter this before it gets to us?
      if ( systemId.indexOf("jccl/xsd") != -1 )
      {
         // Get the schema file name from the string.
         int index = systemId.lastIndexOf('/');
         String schema_file = systemId.substring(index + 1);

         // Construct the full name of the schema file.  On UNIX, it will be
         // of the form $JCCL_BASE_DIR/share/jccl/data/<schema_file>
         String base_dir = System.getProperty("JCCL_BASE_DIR");
         String full_schema_file = base_dir + File.separator + "share" +
                                   File.separator + "jccl" + File.separator +
                                   "data" + File.separator + schema_file;

         File f = new File(full_schema_file);

         // If the schema file exists locally, use it!
         if ( f.isFile() && f.canRead() )
         {
            // Let the File object give us the absolute file name just in case
            // there is anything wonky with $JCCL_BASE_DIR.
            source = new InputSource(f.getAbsolutePath());
         }
         // Otherwise, use what we were given.
         else
         {
            source = new InputSource(systemId);
         }
      }
      // This is not a JCCL schema, so pass it through untouched.
      else
      {
         source = new InputSource(systemId);
      }

      return source;
   }
}

@@ -2671,16 +2671,282 @@
 _DIR%22);%0A
+%0A         if ( null == base_dir )%0A         %7B%0A            System.err.println(%22WARNING: JCCL_BASE_DIR not available for %22 +%0A                               %22local schema loading!%22);%0A            source = new InputSource(systemId);%0A         %7D%0A         else%0A         %7B%0A   
         
@@ -3041,24 +3041,27 @@
             
+   
 File.separat
@@ -3073,16 +3073,54 @@
 %22jccl%22 +
+%0A                                     
  File.se
@@ -3128,16 +3128,25 @@
 arator +
+ %22data%22 +
 %0A       
@@ -3173,24 +3173,18 @@
         
-%22data%22 +
+  
  File.se
@@ -3203,24 +3203,27 @@
 hema_file;%0A%0A
+   
          Fil
@@ -3253,24 +3253,27 @@
 ema_file);%0A%0A
+   
          // 
@@ -3320,24 +3320,27 @@
 t!%0A         
+   
 if ( f.isFil
@@ -3361,34 +3361,40 @@
 ad() )%0A         
-%7B%0A
+   %7B%0A   
             // L
@@ -3454,14 +3454,12 @@
 t in
- case%0A
+%0A   
     
@@ -3468,16 +3468,21 @@
       //
+ case
  there i
@@ -3519,16 +3519,19 @@
 SE_DIR.%0A
+   
         
@@ -3582,34 +3582,40 @@
 th());%0A         
+ 
-%7D%0A
+  %7D%0A   
          // Othe
@@ -3646,37 +3646,43 @@
 given.%0A         
+   
 else%0A
+   
          %7B%0A     
@@ -3668,32 +3668,220 @@
 e%0A            %7B%0A
+               System.err.println(%22WARNING: Could not read %22 +%0A                                  f.getAbsolutePath());%0A               System.err.println(%22Falling back on %22 + systemId);%0A   
             sour
@@ -3904,32 +3904,46 @@
 urce(systemId);%0A
+            %7D%0A
          %7D%0A     

Handle the case when JCCL_BASE_DIR is not available. Print a warning in the case when no local schema can be found. If a local schema isn't available, then the JCCL installation is broken, and lots of warnings will help drive that point home.