bug-classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SystemProperty is not used while creating instance of SchemaFactory()


From: avadhoot kulkarni
Subject: SystemProperty is not used while creating instance of SchemaFactory()
Date: Wed, 16 Aug 2006 15:21:29 +0530

Hi,

I am using Class path for my development work.

When I am creating a SchemaFactory, I want the code to return instance
of MyShemaFactoryImpl

<+Source code +>

System.setProperty("javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema";,
                                    "MySchemaFactoryImpl");

SchemaFactory sf =
SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema";);
<+Source code +>

When I run it under sun java it return MySchemaFactoryImpl instance
but Gnu Classptah does not do that.

Instead it returns instance of
"gnu.xml.validation.xmlschema.XMLSchemaSchemaFactory", when my Schema
language is "http://www.w3.org/2001/XMLSchema";

and "gnu.xml.validation.relaxng.RELAXNGSchemaFactory", when it is
"http://relaxng.org/ns/structure/1.0";.

This happens as the Class names are hard coded in the implementation
of "newInstance()" of "javax.xml.validation.SchemaFactory" Class.\

//Current implementation

<+Source code +>
if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schemaLanguage)) {
           return new gnu.xml.validation.xmlschema.XMLSchemaSchemaFactory();
       }
if (XMLConstants.RELAXNG_NS_URI.equals(schemaLanguage)) {
            return new gnu.xml.validation.relaxng.RELAXNGSchemaFactory();
       }
<+Source code +>

We can change the implementation to check the system property and then
return the proper instance of factory.

<+Source code +>

public static final SchemaFactory newInstance(String schemaLanguage)

 {
  //START Changes by avadhoot Kulkarni
   //check which schema language is specified
    if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schemaLanguage)) {
              //for schema language = http://www.w3.org/2001/XMLSchema
String className =
System.getProperty("javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema";);

// is the property set in environment variables???
    if (className != null && className != "") {
       try {
                 java.lang.ClassLoader cl = ClassLoader.getSystemClassLoader();
                 Class cls = cl.loadClass(className);

          //load the class and create instance as schemaFactory

                 SchemaFactory sf = (SchemaFactory) cls.newInstance();
 if (sf.isSchemaLanguageSupported
                                 (XMLConstants.W3C_XML_SCHEMA_NS_URI)== false){

//check if the instantiated schemafactory supports the provided
schema language..
 throw new IllegalArgumentException("Schema Language Not Supported in
SchemaFactory Class :"+ className);

  }
          return sf;
  } catch (Exception e) {
// classDefinationNotFound, IllegalCastException etc.
throw new IllegalArgumentException(e);
     }
} else {
     //Default behaviour
                     return new
gnu.xml.validation.xmlschema.XMLSchemaSchemaFactory();
                   }
                 }
               if (XMLConstants.RELAXNG_NS_URI.equals(schemaLanguage)) {
                //for schema language =http://relaxng.org/ns/structure/1.0
                       String className = System.getProperty

("javax.xml.validation.SchemaFactory:http://relaxng.org/ns/structure/1.0";);

                      // does the property is set in environment variables???
                       if (className != "" && className != null) {
                           try {
                                    java.lang.ClassLoader cl =
ClassLoader.getSystemClassLoader();

                               Class cls = cl.loadClass(className);

                               //load the class and create instance
as schemaFactory

                               SchemaFactory sf = (SchemaFactory)
cls.newInstance();

                               if
(sf.isSchemaLanguageSupported(XMLConstants.RELAXNG_NS_URI) == false) {

                                   //check if the instantiated
schemafactory supports the provided schema language..

                       throw new IllegalArgumentException("Schema
Language Not Supported in SchemaFactory Class :" + className);

                               }

                               return sf;

                           } catch (Exception e) {

                               throw new IllegalArgumentException(e);

                           }

                       } else {

                       // classDefinationNotFound, IlligleCastException etc.
                          return new
gnu.xml.validation.relaxng.RELAXNGSchemaFactory();
                       }
                   }else{
                       //Default behaviour
                       throw new IllegalArgumentException(schemaLanguage);
                   }
                   //END
}
<+Source code +>

<NOTE: This change is partially tested. Needs more rigorous testing >

Please look for other Factory implementations as well, Same thing has
been done in most of the Factory Classes "newInstance()" method
implementation.

E.g. "javax.xml.parsers.SAXParserFactory".

Thanks,
Avadhoot




reply via email to

[Prev in Thread] Current Thread [Next in Thread]