Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » still struggling with namespace packages.
still struggling with namespace packages. [message #415110] Fri, 30 November 2007 00:49 Go to next message
Jason Henriksen is currently offline Jason HenriksenFriend
Messages: 231
Registered: July 2009
Senior Member
Hi again,

I'm trying to generate java from XSD that appears in many name spaces.
I have all these namespaces because I need the generated java classes
to be separated into a reasonable package structure.

The current java-only data model contains about 3000 different model
objects, so I'm imagining that I'll have hundreds of namespaces in play
once the whole thing is converted to EMF.

The crux of this is the need a utility that converts arbitrary SOAP XML
in to EObjects. Here's my current code to do that:
(I may have objects in the database, hence the Teneo factory)


//---------------------------------------------------------
static URI xmlURI = URI.createURI("*.xml");

public Object getObjectFromSoap(SOAPElement element)
{
ResourceSet resourceSet = new ResourceSetImpl();

// By default pull things from the database
resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap()
.put(Resource.Factory.Registry.DEFAULT_EXTENSION, hrf);

// CONCERN 1: With one global namespace I have one
// global factory. If my whole enterprise starts using EMF
// I'll end up with literally hundreds of java packages and
// thus hundreds of namespaces and factories.
// It seems that since this utility doesn't know what's in
// the SOAP xml payload I need to register all of those packages.
// Is that correct? Is it possible to set up the package registry
// only once and re-use it between resource sets?

resourceSet.getPackageRegistry()
.put(packageImpl.getNsURI(),packageImpl);

// CONCERN 2: With one global namespace I know which resource factory
// to use. Do I need to somehow use the tag name as a key into
// determining which resource factory contains the object for
// the given tag and from that figure out which factory to use?
// If I have say 100 resourceFactories, do I need to create 100
// extensions and map them some how? Or does EMF have some kind of
// Class.forName("foo.bar.Class") that I just haven't found yet?

resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap()
.put("xml", resourceFactory);

XMLResourceImpl resource =
(XMLResourceImpl)resourceSet.createResource(xmlURI);

String foo = element.toString();

// CONCERN 3: Since I don't know what object is in the SOAP payload,
// I'm not sure what namespace to specify. Is there a way to make it
// search the registered namespaces to find a tag that matches?
// Or is there a more elegant solution to this I haven't found yet?
// This is ugly, but I don't know how else to tell EMF what package
// to look in to find the tags/features. I've only found the outer
// tag to have it's namespace messed with so I don't think the inner
// tags need this
// (Maybe I just need to somehow prevent the namespace scrambling)

if (foo.indexOf("xmlns='http://vsp.com/'") == -1) {
foo = foo.substring(0, foo.indexOf(' '))
+ " xmlns='http://vsp.com/' xmlns:" + defaultPackage
+ "='http://vsp.com/' " + foo.substring(foo.indexOf(' '));
}

try {
resource.load(new ByteArrayInputStream(foo.getBytes()), null);
} catch (java.io.IOException ioe) {
ioe.printStackTrace();
return null;
}
return resource.getContents().get(0);
}
//---------------------------------------------------------

As always: thanks for our advice,

Jason Henriksen
Re: still struggling with namespace packages. [message #415112 is a reply to message #415110] Fri, 30 November 2007 01:07 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Jason,

Comments below.

jason henriksen wrote:
> Hi again,
>
> I'm trying to generate java from XSD that appears in many name spaces.
> I have all these namespaces because I need the generated java
> classes to be separated into a reasonable package structure.
>
> The current java-only data model contains about 3000 different model
> objects, so I'm imagining that I'll have hundreds of namespaces in
> play once the whole thing is converted to EMF.
Wow. That kind of hurts the brain...
>
> The crux of this is the need a utility that converts arbitrary SOAP
> XML in to EObjects. Here's my current code to do that:
> (I may have objects in the database, hence the Teneo factory)
>
>
> //---------------------------------------------------------
> static URI xmlURI = URI.createURI("*.xml");
>
> public Object getObjectFromSoap(SOAPElement element)
> {
> ResourceSet resourceSet = new ResourceSetImpl();
>
> // By default pull things from the database
> resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap()
> .put(Resource.Factory.Registry.DEFAULT_EXTENSION, hrf);
>
> // CONCERN 1: With one global namespace I have one
> // global factory. If my whole enterprise starts using EMF
> // I'll end up with literally hundreds of java packages and
> // thus hundreds of namespaces and factories.
Hmmm. But EMF generates one package per namespace. So I'd expect on
mondo package rather than hundreds of them.
> // It seems that since this utility doesn't know what's in
> // the SOAP xml payload I need to register all of those packages.
> // Is that correct? Is it possible to set up the package registry
> // only once and re-use it between resource sets?
ResourceSet has a setPackageRegistry method so you could share them
between resource set.
>
> resourceSet.getPackageRegistry()
> .put(packageImpl.getNsURI(),packageImpl);
I don't really like seeing PackageImpls do much. Can't use use the
interface only?
>
> // CONCERN 2: With one global namespace I know which resource factory
> // to use. Do I need to somehow use the tag name as a key into
> // determining which resource factory contains the object for
> // the given tag and from that figure out which factory to use?
The parser does that for you. It used
EPackage.Registry.INSTANCE.getEPackage(<namespace>) to find the package
for any particular qualified name.
> // If I have say 100 resourceFactories, do I need to create 100
> // extensions and map them some how? Or does EMF have some kind of
> // Class.forName("foo.bar.Class") that I just haven't found yet?
Hmm. Resource factories. Why so many? You only have one resource
you're reading right?
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap()
> .put("xml", resourceFactory);
You can use "*" to register against a wildcard. You can also turn
resource factory generation off for a package. If you look at what's
generated, they all look pretty much the same, i.e., use the same options.
>
> XMLResourceImpl resource =
> (XMLResourceImpl)resourceSet.createResource(xmlURI);
>
> String foo = element.toString();
>
> // CONCERN 3: Since I don't know what object is in the SOAP payload,
> // I'm not sure what namespace to specify. Is there a way to make it
> // search the registered namespaces to find a tag that matches?
In EMF 2.4 there is support for content handlers that can determine the
resource type to use based on the content itself. I suspect though that
a one-side-fits-all resource will be sufficient for you.
> // Or is there a more elegant solution to this I haven't found yet?
> // This is ugly, but I don't know how else to tell EMF what package
> // to look in to find the tags/features.
It will automatically find a package as I described above. The package
namespace is register in the package registry so the tag will be matched
to the right package base on that.
> I've only found the outer
> // tag to have it's namespace messed with so I don't think the inner
> // tags need this
> // (Maybe I just need to somehow prevent the namespace scrambling)
Scrambling is good for eggs, but not much else.
>
> if (foo.indexOf("xmlns='http://vsp.com/'") == -1) {
> foo = foo.substring(0, foo.indexOf(' '))
> + " xmlns='http://vsp.com/' xmlns:" + defaultPackage
> + "='http://vsp.com/' " + foo.substring(foo.indexOf(' '));
> }
>
> try {
> resource.load(new ByteArrayInputStream(foo.getBytes()), null);
If you have a string already, best not to convert it to byte. Look at
URIConverter.ReadableInputStream as a way to wrap the string. This
approach might use a system encoding to encode something that will thing
it should be using a UTF-8 encoding.
> } catch (java.io.IOException ioe) {
> ioe.printStackTrace();
> return null;
> }
> return resource.getContents().get(0);
> }
> //---------------------------------------------------------
>
> As always: thanks for our advice,
Hopefully I've helped a tiny bit. It's pretty late...
>
> Jason Henriksen


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:TransactionalEditingDomain in EMF generated editors
Next Topic:Mac users: Leopard Upgrade warning
Goto Forum:
  


Current Time: Thu Apr 25 01:59:10 GMT 2024

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

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

Back to the top