Michael,

I tried to implemented LSResourceResolver different way and it seems that it is 
resolving external entities but when it tried to validate it fails.

I do not know what else I am missing ?

Please find the attached java , xsd and xml file.

Regards
Ajay.


--- On Wed, 11/12/08, Michael Glavassevich <[EMAIL PROTECTED]> wrote:

> From: Michael Glavassevich <[EMAIL PROTECTED]>
> Subject: Re: use of ResourceResolver in Schema Parsing
> To: j-users@xerces.apache.org
> Date: Wednesday, November 12, 2008, 3:09 AM
> Hi Ajay,
> 
> The NullPointerException could have come from any number of
> places. It
> would help if you posted a stack trace.
> 
> Also, though it doesn't appear it was even called your
> LSResourceResolver
> is making assumptions about the format of the systemId
> which aren't
> generally true.  It's a URI (e.g. http://www.w3.org),
> not a file path. It
> doesn't look like you're trying to do anything in
> the resolver which the
> parser wouldn't already do by default. If that's
> all you were planning to
> do there there's not much point in registering a
> resolver.
> 
> Thanks.
> 
> Michael Glavassevich
> XML Parser Development
> IBM Toronto Lab
> E-mail: [EMAIL PROTECTED]
> E-mail: [EMAIL PROTECTED]
> 
> ajay bhadauria <[EMAIL PROTECTED]> wrote on
> 11/11/2008 11:42:30 AM:
> 
> > Hi,
> >
> > I was trying to implement LSResourceResolver to load
> multiple
> > Schemas in my SchemaFactory but I am getting
> nullpointer Exception.
> > Below is the code. Please tell me what I am missing
> here ?
> >
> > Thanks
> > Ajay B.
> >
> > import java.io.*;
> > import java.util.*;
> > import org.w3c.dom.* ;
> > import org.w3c.dom.ls.* ;
> > import org.xml.sax.InputSource;
> > import org.xml.sax.EntityResolver;
> > import java.net.URL;
> > //import org.w3c.dom.ls.DOMImplementationLS;
> > import org.w3c.dom.ls.*;
> > import javax.xml.XMLConstants;
> > import javax.xml.validation.SchemaFactory;
> > import javax.xml.validation.Schema;
> > import org.w3c.dom.bootstrap.DOMImplementationRegistry
> ;
> >
> > public class SchemaReader
> > {
> >
> >   private static int errorCount = 0;
> >   String schemaName = null ;
> >   public SchemaReader(String st)
> >   {
> >      System.out.println("debug2");
> >      schemaName = st;
> >      System.out.println("debug3");
> >      Schema schema =  loadSchema( schemaName );
> >      System.out.println("debug4");
> >   }
> >
> >   public static void main(String[] a)
> >   {
> >      if (a.length<1)
> >      {
> >           System.out.println("Usage:");
> >           System.out.println("java
> XsdSchemaValidator
> > schema_file_name " + "xml_file_name");
> >      }
> >      else
> >      {
> >          //String schemaName = a[0];
> >          System.out.println("debug1");
> >          SchemaReader sr = new SchemaReader(a[0]);
> >          //Schema schema = sr.loadSchema(schemaName);
> >      }
> >    }
> >
> >   public Schema loadSchema(String name)
> >   {
> >     Schema schema = null;
> >     try
> >     {
> >       System.out.println("xxx1");
> >       String language =
> XMLConstants.W3C_XML_SCHEMA_NS_URI;
> >       System.out.println("xxx2");
> >       SchemaFactory factory =
> SchemaFactory.newInstance(language);
> >       System.out.println("xxx3");
> >       MyResourceResolver resolver = new
> MyResourceResolver();
> >       System.out.println("xxx4");
> >       factory.setResourceResolver( resolver );
> >       System.out.println("xxx5");
> >       System.out.println(name);
> >       schema = factory.newSchema(new File(name));
> >       System.out.println("xxx6");
> >
> >     }
> >     catch (Exception e)
> >     {
> >       System.out.println(e.toString());
> >     }
> >      return schema;
> >   }
> >
> >
> >   public  class MyResourceResolver implements
> LSResourceResolver
> >   {
> >            DOMImplementationLS implLS ;
> >            public MyResourceResolver()
> >            {
> >                 try
> >                 {
> >                
> System.setProperty(DOMImplementationRegistry.
> > PROPERTY,
> "org.apache.xerces.dom.DOMImplementationSourceImpl");
> >
> >                 DOMImplementationRegistry registry =
> >                         
> DOMImplementationRegistry.newInstance();
> >                 DOMImplementation domImpl =
> >                         
> registry.getDOMImplementation("LS 3.0");
> >                 DOMImplementationLS implLS =
> (DOMImplementationLS)domImpl;
> >                 }
> >                 catch ( Exception e)
> >                 {
> >                    
> System.out.println("xxx");
> >                     System.out.println(e.toString());
> >
> >                 }
> >            }
> >
> >
> >            public LSInput resolveResource(String type,
> String
> > namespaceURI, String publicId, String systemId, String
> baseURI )
> >            {
> >                 LSInput input =
> implLS.createLSInput();
> >                 System.out.println("System Id
> :" + systemId);
> >                 System.out.println("Base URI
> :" + baseURI);
> >                 FileInputStream fis =  null;
> >                 try
> >                 {
> >                      fis =  new FileInputStream(new
> File(systemId));
> >                 }
> >                 catch(Exception e)
> >                 {
> >                     
> System.out.println("debug1");
> >                      System.out.println(e.toString());
> >                 }
> >
> >                 input.setByteStream(fis);
> >                 input.setSystemId(systemId);
> >                 return input ;
> >                 //return fis ;
> >
> >             }
> >
> >
> >    }
> >
> > }
> >
> > Output
> > ========
> >
> > debug1
> > debug2
> > debug3
> > xxx1
> > xxx2
> > xxx3
> > xxx4
> > xxx5
> > clpt.001.001.01.xsd
> > java.lang.NullPointerException
> > debug4
> >
> >
> > --- On Tue, 11/11/08, ajay bhadauria
> <[EMAIL PROTECTED]> wrote:
> >
> > > From: ajay bhadauria <[EMAIL PROTECTED]>
> > > Subject: Re: use of ResourceResolver in Schema
> Parsing
> > > To: j-users@xerces.apache.org
> > > Date: Tuesday, November 11, 2008, 7:35 PM
> > > Hi,
> > >
> > > I could not figure out how to implement
> LSResourceResolver
> > > with SchemaFactory so that all external entities
> could be
> > > loaded when main schema gets loaded.
> > >
> > > Any code sample will be greatly appriciated .
> > >
> > > Thanks
> > >
> > > --- On Mon, 11/10/08, ajay bhadauria
> > > <[EMAIL PROTECTED]> wrote:
> > >
> > > > From: ajay bhadauria
> <[EMAIL PROTECTED]>
> > > > Subject: use of ResourceResolver in Schema
> Parsing
> > > > To: j-users@xerces.apache.org
> > > > Date: Monday, November 10, 2008, 8:24 PM
> > > > Hi,
> > > >
> > > > I am using SchemaFactory and then from
> factory I am
> > > setting
> > > > setResourceResolver to resolve external
> entities.
> > > while
> > > > reading schema which refers to other schemas
> through
> > > import
> > > > and include, but I am not able to construct
> LSInput in
> > > > resolveResource method of LSResourceResolver
> > > interface.
> > > >
> > > > What should I do to construct the LSInput in
> > > > resolveResource method ?
> > > >
> > > > Please find the attached java file. Please
> help me how
> > > I
> > > > should do it ?
> > > >
> > > > Regards
> > > >
> > > > Ajay
> > > >
> > >
> ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail:
> > > > [EMAIL PROTECTED]
> > > > For additional commands, e-mail:
> > > > [EMAIL PROTECTED]
> > >
> > >
> ---------------------------------------------------------------------
> > > To unsubscribe, e-mail:
> > > [EMAIL PROTECTED]
> > > For additional commands, e-mail:
> > > [EMAIL PROTECTED]
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> > For additional commands, e-mail:
> [EMAIL PROTECTED]


      
import java.io.*;
import java.util.*;
import org.w3c.dom.* ;
import org.w3c.dom.ls.* ;
import org.xml.sax.InputSource;
import org.xml.sax.EntityResolver;
import java.net.URL;
import org.w3c.dom.ls.DOMImplementationLS;
import javax.xml.XMLConstants;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;

public class SchemaReader 
{

  private static int errorCount = 0;
  public static void main(String[] a) 
  {
     if (a.length<1) 
     {
          System.out.println("Usage:");
          System.out.println("java XsdSchemaValidator schema_file_name " + 
"xml_file_name");
     } 
     else 
     {
         String schemaName = a[0];
         Schema schema = loadSchema(schemaName);
     }
   }

  public static Schema loadSchema(String name) 
  {
    Schema schema = null;
    try 
    {
      String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
      SchemaFactory factory = SchemaFactory.newInstance(language);
      MyResourceResolver resolver = new MyResourceResolver();
      factory.setResourceResolver( resolver );
      schema = factory.newSchema(new File(name));

    } 
    catch (Exception e) 
    {
      System.out.println(e.toString());
    }
     return schema;
  }


  public static class MyResourceResolver implements LSResourceResolver 
  {
           private DOMImplementationLS domImplementationLS = new 
DOMImplementationLS();
           public LSInput resolveResource(String type, String namespaceURI, 
String publicId, String systemId, String baseURI ) 
           {
                 
                LSInput ret = domImplementationLS.createLSInput();

                FileInputStream fis =  null;
                try
                {
                     fis =  new FileInputStream((new URL(systemId)).getPath());
                }
                catch(Exception e)
                {
                     System.out.println(e.toString());
                }

                ret.setSystemId(systemId);
                return ret.setByteStream(fis);

            }


   }

}

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema";
            targetNamespace="http://www.company.org";
            xmlns="http://www.company.org";
            elementFormDefault="unqualified"
            xmlns:per="http://www.person.org";
            xmlns:pro="http://www.product.org";>
    <xsd:import namespace="http://www.person.org";
                schemaLocation="Person.xsd"/>
    <xsd:import namespace="http://www.product.org";
                schemaLocation="Product.xsd"/>
    <xsd:element name="Company">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Person" type="per:PersonType" 
                             maxOccurs="unbounded"/>
                <xsd:element name="Product" type="pro:ProductType" 
                             maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema";
            targetNamespace="http://www.product.org";
            xmlns="http://www.product.org";
            elementFormDefault="unqualified">
    <xsd:complexType name="ProductType">
        <xsd:sequence>
           <xsd:element name="Type" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>


<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema";
            targetNamespace="http://www.person.org";
            xmlns="http://www.person.org";
            elementFormDefault="unqualified">
    <xsd:complexType name="PersonType">
        <xsd:sequence>
           <xsd:element name="Name" type="xsd:string"/>
           <xsd:element name="SSN" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

<?xml version="1.0"?>
<Company xmlns:c="http://www.company.org";
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
           xsi:schemaLocation= "http://www.company.org Company.xsd">
        <Person>
                <Name>John Doe</Name>
                <SSN>123-45-6789</SSN>
        </Person>
        <Product>
                <Type>Widget</Type>
        </Product>
</Company>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to