Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » XML Schema Definition (XSD) » XSDSchema Loading
XSDSchema Loading [message #72401] Mon, 18 June 2007 21:44 Go to next message
Eclipse User
Originally posted by: kemoon.tin.it

Does anybody know how to load an xml schema from a file with import and
include directives linking to other files?
I have tried this solutiom, but it still doesn't work.
String schemaURL =
"../xmLegesEditor/xsdData/NIR_XSD_completo/nirstrict.xsd";

public static XSDSchema loadSchemaUsingResourceSet(String schemaURL)
throws Exception{

XSDResourceFactoryImpl resourceFactory = new XSDResourceFactoryImpl();
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",resourceFactory);

ResourceSet resourceSet = new ResourceSetImpl();

resourceSet.getURIConverter().getURIMap().put(URI.createURI( "./xlink.xsd"),URI.createURI(" file:/home/tommaso/schemaWorkspace/xmLegesEditor/xsdData/NIR _XSD_completo/xlink.xsd "));//,URI.createURI("./xlink.xsd"));
resourceSet.getURIConverter().getURIMap().put(URI.createURI( "./h.xsd"),URI.createURI(" file:/home/tommaso/schemaWorkspace/xmLegesEditor/xsdData/NIR _XSD_completo/h.xsd "));//,URI.createURI("./h.xsd")); resourceSet.getURIConverter().getURIMap().put(URI.createURI( "./dsp.xsd"),URI.createURI(" file:/home/tommaso/schemaWorkspace/xmLegesEditor/xsdData/NIR _XSD_completo/dsp.xsd "));//,URI.createURI("./dsp.xsd")); File
file = new File(schemaURL);
if (file.isFile())
schemaURL =
URI.createFileURI(file.getCanonicalFile().toString()).toStri ng();
resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION,
Boolean.TRUE);

XSDResourceImpl xsdSchemaResource = null;
try
{
xsdSchemaResource =
(XSDResourceImpl)resourceSet.getResource(URI.createURI(schem aURL),true);
}
catch(Exception e)
{
System.err.println("Exception caught in method 'load schema using
ResourceSet' message = " + e.getMessage() + " xsdSchemaResource: " +
xsdSchemaResource);
}

for (Iterator resources =
resourceSet.getResources().iterator();resources.hasNext(); /* no-op */)
{
Resource resource = (Resource)resources.next();
if (resource instanceof XSDResourceImpl)
{
XSDResourceImpl xsdResource = (XSDResourceImpl)resource;
XSDSchema xsdSchema = xsdResource.getSchema();
for (Iterator contents = xsdSchema.getContents().iterator();
contents.hasNext(); )
{
XSDSchemaContent xsdSchemaContent =
(XSDSchemaContent)contents.next();
if (xsdSchemaContent instanceof XSDSchemaDirective)
{
XSDSchemaDirective xsdSchemaDirective =
(XSDSchemaDirective)xsdSchemaContent;
if (xsdSchemaDirective.getResolvedSchema() == null)
{
System.err.println("Unresolved schema "+
xsdSchemaDirective.getSchemaLocation() +" in " + xsdResource.getURI());

if(xsdSchemaDirective instanceof XSDImport)
{
XSDImport xsdSchemaImport = (XSDImport)xsdSchemaDirective;
XSDSchema resolvedSchema =
((XSDImportImpl)xsdSchemaImport).importSchema();
if(resolvedSchema != null)
{
System.err.println("-------> now
resolved");
xsdSchemaDirective.setResolvedSchema(resolvedSchema);
((XSDSchemaImpl)resolvedSchema).imported((XSDImport)xsdSchem aDirective);
}
}

}
else
{
System.err.println("Resolved schema "+
xsdSchemaDirective.getSchemaLocation() +" in " + xsdResource.getURI());
if(xsdSchemaDirective instanceof XSDImport)
{
XSDImport xsdSchemaImport =
(XSDImport)xsdSchemaDirective;
}
}
}
}

return xsdResource.getSchema();
}
}
System.err.println("loadSchemaUsingResourceSet(" + schemaURL + ") did
not contain any schemas!");
return null;
}
Re: XSDSchema Loading [message #72419 is a reply to message #72401] Mon, 18 June 2007 23:09 Go to previous message
Eclipse User
Originally posted by: merks.ca.ibm.com

Francesca,

If you run this under debug control (or add a print statement), what's
the value of schemaURL at the point when you call getResource? (As long
as you load using an absolute URI, all relative URIs should resolve
correctly to the same absolute URI they'd resolve to if you were loading
..html file in a browser.)


Francesca wrote:
> Does anybody know how to load an xml schema from a file with import
> and include directives linking to other files?
> I have tried this solutiom, but it still doesn't work.
> String schemaURL =
> "../xmLegesEditor/xsdData/NIR_XSD_completo/nirstrict.xsd";
>
> public static XSDSchema loadSchemaUsingResourceSet(String schemaURL)
> throws Exception{
>
> XSDResourceFactoryImpl resourceFactory = new
> XSDResourceFactoryImpl();
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",resourceFactory);
>
>
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getURIConverter().getURIMap().put(URI.createURI( "./xlink.xsd"),URI.createURI(" file:/home/tommaso/schemaWorkspace/xmLegesEditor/xsdData/NIR _XSD_completo/xlink.xsd "));//,URI.createURI("./xlink.xsd"));
>
> resourceSet.getURIConverter().getURIMap().put(URI.createURI( "./h.xsd"),URI.createURI(" file:/home/tommaso/schemaWorkspace/xmLegesEditor/xsdData/NIR _XSD_completo/h.xsd "));//,URI.createURI("./h.xsd"));
> resourceSet.getURIConverter().getURIMap().put(URI.createURI( "./dsp.xsd"),URI.createURI(" file:/home/tommaso/schemaWorkspace/xmLegesEditor/xsdData/NIR _XSD_completo/dsp.xsd "));//,URI.createURI("./dsp.xsd"));
> File file = new File(schemaURL);
> if (file.isFile())
> schemaURL =
> URI.createFileURI(file.getCanonicalFile().toString()).toStri ng();
>
> resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION,
> Boolean.TRUE);
>
> XSDResourceImpl xsdSchemaResource = null;
> try
> {
> xsdSchemaResource =
> (XSDResourceImpl)resourceSet.getResource(URI.createURI(schem aURL),true);
> }
> catch(Exception e)
> {
> System.err.println("Exception caught in method 'load schema using
> ResourceSet' message = " + e.getMessage() + " xsdSchemaResource: " +
> xsdSchemaResource);
> }
>
> for (Iterator resources =
> resourceSet.getResources().iterator();resources.hasNext(); /* no-op */)
> {
> Resource resource = (Resource)resources.next();
> if (resource instanceof XSDResourceImpl)
> {
> XSDResourceImpl xsdResource = (XSDResourceImpl)resource;
> XSDSchema xsdSchema = xsdResource.getSchema();
> for (Iterator contents = xsdSchema.getContents().iterator();
> contents.hasNext(); )
> {
> XSDSchemaContent xsdSchemaContent =
> (XSDSchemaContent)contents.next();
> if (xsdSchemaContent instanceof XSDSchemaDirective)
> { XSDSchemaDirective
> xsdSchemaDirective = (XSDSchemaDirective)xsdSchemaContent;
> if (xsdSchemaDirective.getResolvedSchema() == null)
> {
> System.err.println("Unresolved schema "+
> xsdSchemaDirective.getSchemaLocation() +" in " + xsdResource.getURI());
>
> if(xsdSchemaDirective instanceof XSDImport)
> {
> XSDImport xsdSchemaImport =
> (XSDImport)xsdSchemaDirective;
> XSDSchema resolvedSchema =
> ((XSDImportImpl)xsdSchemaImport).importSchema();
> if(resolvedSchema != null)
> {
> System.err.println("-------> now
> resolved");
> xsdSchemaDirective.setResolvedSchema(resolvedSchema);
> ((XSDSchemaImpl)resolvedSchema).imported((XSDImport)xsdSchem aDirective);
> }
> }
>
> } else {
> System.err.println("Resolved schema "+
> xsdSchemaDirective.getSchemaLocation() +" in " + xsdResource.getURI());
> if(xsdSchemaDirective instanceof XSDImport)
> {
> XSDImport xsdSchemaImport =
> (XSDImport)xsdSchemaDirective;
> }
> } }
> }
> return xsdResource.getSchema();
> }
> }
> System.err.println("loadSchemaUsingResourceSet(" + schemaURL + ")
> did not contain any schemas!");
> return null;
> }
>
>
Re: XSDSchema Loading [message #602386 is a reply to message #72401] Mon, 18 June 2007 23:09 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 26152
Registered: July 2009
Senior Member
Francesca,

If you run this under debug control (or add a print statement), what's
the value of schemaURL at the point when you call getResource? (As long
as you load using an absolute URI, all relative URIs should resolve
correctly to the same absolute URI they'd resolve to if you were loading
..html file in a browser.)


Francesca wrote:
> Does anybody know how to load an xml schema from a file with import
> and include directives linking to other files?
> I have tried this solutiom, but it still doesn't work.
> String schemaURL =
> "../xmLegesEditor/xsdData/NIR_XSD_completo/nirstrict.xsd";
>
> public static XSDSchema loadSchemaUsingResourceSet(String schemaURL)
> throws Exception{
>
> XSDResourceFactoryImpl resourceFactory = new
> XSDResourceFactoryImpl();
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",resourceFactory);
>
>
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getURIConverter().getURIMap().put(URI.createURI( "./xlink.xsd"),URI.createURI(" file:/home/tommaso/schemaWorkspace/xmLegesEditor/xsdData/NIR _XSD_completo/xlink.xsd "));//,URI.createURI("./xlink.xsd"));
>
> resourceSet.getURIConverter().getURIMap().put(URI.createURI( "./h.xsd"),URI.createURI(" file:/home/tommaso/schemaWorkspace/xmLegesEditor/xsdData/NIR _XSD_completo/h.xsd "));//,URI.createURI("./h.xsd"));
> resourceSet.getURIConverter().getURIMap().put(URI.createURI( "./dsp.xsd"),URI.createURI(" file:/home/tommaso/schemaWorkspace/xmLegesEditor/xsdData/NIR _XSD_completo/dsp.xsd "));//,URI.createURI("./dsp.xsd"));
> File file = new File(schemaURL);
> if (file.isFile())
> schemaURL =
> URI.createFileURI(file.getCanonicalFile().toString()).toStri ng();
>
> resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION,
> Boolean.TRUE);
>
> XSDResourceImpl xsdSchemaResource = null;
> try
> {
> xsdSchemaResource =
> (XSDResourceImpl)resourceSet.getResource(URI.createURI(schem aURL),true);
> }
> catch(Exception e)
> {
> System.err.println("Exception caught in method 'load schema using
> ResourceSet' message = " + e.getMessage() + " xsdSchemaResource: " +
> xsdSchemaResource);
> }
>
> for (Iterator resources =
> resourceSet.getResources().iterator();resources.hasNext(); /* no-op */)
> {
> Resource resource = (Resource)resources.next();
> if (resource instanceof XSDResourceImpl)
> {
> XSDResourceImpl xsdResource = (XSDResourceImpl)resource;
> XSDSchema xsdSchema = xsdResource.getSchema();
> for (Iterator contents = xsdSchema.getContents().iterator();
> contents.hasNext(); )
> {
> XSDSchemaContent xsdSchemaContent =
> (XSDSchemaContent)contents.next();
> if (xsdSchemaContent instanceof XSDSchemaDirective)
> { XSDSchemaDirective
> xsdSchemaDirective = (XSDSchemaDirective)xsdSchemaContent;
> if (xsdSchemaDirective.getResolvedSchema() == null)
> {
> System.err.println("Unresolved schema "+
> xsdSchemaDirective.getSchemaLocation() +" in " + xsdResource.getURI());
>
> if(xsdSchemaDirective instanceof XSDImport)
> {
> XSDImport xsdSchemaImport =
> (XSDImport)xsdSchemaDirective;
> XSDSchema resolvedSchema =
> ((XSDImportImpl)xsdSchemaImport).importSchema();
> if(resolvedSchema != null)
> {
> System.err.println("-------> now
> resolved");
> xsdSchemaDirective.setResolvedSchema(resolvedSchema);
> ((XSDSchemaImpl)resolvedSchema).imported((XSDImport)xsdSchem aDirective);
> }
> }
>
> } else {
> System.err.println("Resolved schema "+
> xsdSchemaDirective.getSchemaLocation() +" in " + xsdResource.getURI());
> if(xsdSchemaDirective instanceof XSDImport)
> {
> XSDImport xsdSchemaImport =
> (XSDImport)xsdSchemaDirective;
> }
> } }
> }
> return xsdResource.getSchema();
> }
> }
> System.err.println("loadSchemaUsingResourceSet(" + schemaURL + ")
> did not contain any schemas!");
> return null;
> }
>
>
Previous Topic:XSDSchema Loading
Next Topic:import resolution and namespaces
Goto Forum:
  


Current Time: Thu Oct 30 18:20:21 GMT 2014

Powered by FUDForum. Page generated in 0.02365 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software