Michael, In the attached java file, it resolves all the dependent schemas but while validation it fails. If I move any dependent schmemas, it clearly throws error but I do not know why validation fails ?
java SchemaReader Company.xsd Company.xml throws following error org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Company'. What should I do to get working ? Regards Ajay --- On Thu, 11/13/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: Thursday, November 13, 2008, 3:39 AM > Michael, > > When I run the SchemaReader I got the below response. > LSResourceResolver is getting called but I do not know why > validation is failing. What I am missing ? > > Thanks > Ajay > > debug1 > Schema File Name : Company.xsd > System Id:Person.xsd > baseURI:file:///tmp/ajay/xslt/Company.xsd > File Path /tmp/ajay/xslt/Person.xsd > XXXXXXX : /tmp/ajay/xslt/Person.xsd > System Id:Product.xsd > baseURI:file:///tmp/ajay/xslt/Company.xsd > File Path /tmp/ajay/xslt/Product.xsd > XXXXXXX : /tmp/ajay/xslt/Product.xsd > File Name : Company.xml > GGGGGG > org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the > declaration of element 'Company'. > > > > --- On Wed, 11/12/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: Wednesday, November 12, 2008, 8:43 PM > > 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] > > > > > > > > > --------------------------------------------------------------------- > > 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 javax.xml.XMLConstants; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Schema; import javax.xml.validation.Validator; import javax.xml.parsers.DocumentBuilderFactory ; import javax.xml.parsers.DocumentBuilder ; import javax.xml.parsers.ParserConfigurationException ; import javax.xml.transform.dom.DOMSource; import org.xml.sax.* ; import org.w3c.dom.ls.* ; public class SchemaReader { private static int errorCount = 0; String schemaName = null ; public SchemaReader(String st, String st1) { schemaName = st; Schema schema = loadSchema( schemaName ); Validator validator = schema.newValidator(); Document document= null; DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); dFactory.setNamespaceAware(true); DocumentBuilder dBuilder =null; try { dBuilder = dFactory.newDocumentBuilder(); } catch( Exception e) { System.out.println(e.toString()); } try { System.out.println("File Name : " + st1); document = dBuilder.parse( new FileInputStream(st1)); } catch( Exception e) { System.out.println(e.toString()); } DOMSource source = new DOMSource(document); try { validator.validate(source); } catch (Exception e) { System.out.println("In the Validation Exception"); System.out.println(e.toString()); } } public static void main(String[] a) { if (a.length<2) { 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],a[1]); //Schema schema = sr.loadSchema(schemaName); } } public Schema loadSchema(String name) { Schema schema = null; try { String language = XMLConstants.W3C_XML_SCHEMA_NS_URI; MyErrorHandler handler = new MyErrorHandler(); SchemaFactory factory = SchemaFactory.newInstance(language); factory.setErrorHandler(handler); MyResourceResolver resolver = new MyResourceResolver(); factory.setResourceResolver( resolver ); System.out.println("Schema File Name : " + name); schema = factory.newSchema(new File(name)); } catch (Exception e) { System.out.println("debug2 "); System.out.println(e.toString()); } return schema; } public class MyErrorHandler implements ErrorHandler { public void error(SAXParseException exception ) { System.out.println("In the error"); System.out.println(exception.toString()); } public void fatalError(SAXParseException exception ) { System.out.println(exception.toString()); } public void warning(SAXParseException exception ) { System.out.println("Warning :"); System.out.println(exception.toString()); } } public class MyResourceResolver implements LSResourceResolver { public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI ) { System.out.println("System Id:" + systemId); System.out.println("baseURI:" + baseURI); String path = "/tmp/ajay/xslt/" + systemId; System.out.println("File Path " + path); return new ResourceInput(path); } } private static class ResourceInput implements LSInput { private final String resource; private ResourceInput(String resource) { this.resource = resource; } public Reader getCharacterStream() { return null; } public void setCharacterStream(Reader characterStream) { } public InputStream getByteStream() { FileInputStream fis = null; try { System.out.println("Resource : " + resource); fis = new FileInputStream(new File(resource)); return fis ; } catch (IOException e) { return null; } } public void setByteStream(InputStream byteStream) { } public String getStringData() { return null; } public void setStringData(String stringData) { } public String getSystemId() { return this.resource; } public void setSystemId(String systemId) { } public String getPublicId() { return null; } public void setPublicId(String publicId) { } public String getBaseURI() { return null; } public void setBaseURI(String baseURI) { } public String getEncoding() { return null; } public void setEncoding(String encoding) { } public boolean getCertifiedText() { return false; } public void setCertifiedText(boolean certifiedText) { } } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]