Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » XML Schema Definition (XSD) » specifying .xsd namespace inside XML file(proper way of specifying location of .xsd namespace inside eclipse plugin)
specifying .xsd namespace inside XML file [message #667139] Tue, 26 April 2011 23:00 Go to next message
Andrew Miga is currently offline Andrew Miga
Messages: 10
Registered: August 2009
Junior Member
Hello,

I am having difficulties deserializing XML files for a data model. The eclipse plug-in I am developing can serialize the same model to XML. I am having trouble deserializing the saved models wrt the .xsd namespace.

The errors I am getting are:

Package with uri 'null' not found. (file:/home/andrew/CSM/MagicDraw/MARTE_ActivityEx3/MARTE_Act ivityEx3-edited.csm, 2, 127)

The file the errors are referring to is as follows:

1: <?xml version="1.0" encoding="ASCII"?>
2: <CSM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" CSM:Root="file:/home/andrew/CSM/UMLActivity2CSM/resources/CSM.xsd ">

The xsd file does exist at that location.

My first question is whether an .xsd namespace file needs to be explicitly registered as part of a ResourceSet before the loading is performed.

Complicating matters is the fact that an XMLHelper method is being used. The loading is performed as follows.

CSMResourceImpl csmResource = new CSMResourceImpl( URI.createFileURI( csmPath )){
protected XMLHelper createXMLHelper() {
return new CSMXMLInputHelper(this);
}
};
is = new FileInputStream( csmFile );

try {
csmResource.load( is, null );
csmType = (CSMType)csmResource.getContents().get(0);
} catch (IOException e1) {

Most of this is code I inherited. I don't know if it is correct. Similar code to serialize the model to XML works fine.

Thank you for your help.
Re: specifying .xsd namespace inside XML file [message #667197 is a reply to message #667139] Wed, 27 April 2011 07:11 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26013
Registered: July 2009
Senior Member
Andrew Miga wrote:
> Hello,
>
> I am having difficulties deserializing XML files for a data model.
> The eclipse plug-in I am developing can serialize the same model to
> XML. I am having trouble deserializing the saved models wrt the .xsd
> namespace.
>
> The errors I am getting are:
>
> Package with uri 'null' not found.
> (file:/home/andrew/CSM/MagicDraw/MARTE_ActivityEx3/MARTE_Act
> ivityEx3-edited.csm, 2, 127)
>
> The file the errors are referring to is as follows:
>
> 1: <?xml version="1.0" encoding="ASCII"?>
> 2: <CSM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> CSM:Root="file:/home/andrew/CSM/UMLActivity2CSM/resources/CSM.xsd ">
What are you expecting from CSM:Root? You've not even defined an xmlns
prefix for CSM... This input just look kind of invalid.
>
> The xsd file does exist at that location.
>
> My first question is whether an .xsd namespace file needs to be
> explicitly registered as part of a ResourceSet before the loading is
> performed.
>
> Complicating matters is the fact that an XMLHelper method is being
> used. The loading is performed as follows.
>
> CSMResourceImpl csmResource = new CSMResourceImpl( URI.createFileURI(
> csmPath )){
> protected XMLHelper createXMLHelper() {
> return new CSMXMLInputHelper(this);
> }
> };
I have no idea why folks would need a specialized helper...
> is = new FileInputStream( csmFile );
>
> try {
> csmResource.load( is, null );
> csmType = (CSMType)csmResource.getContents().get(0);
> } catch (IOException e1) {
>
> Most of this is code I inherited. I don't know if it is correct.
> Similar code to serialize the model to XML works fine.
Is the right resource factory registered to create your specialized
resource?
>
> Thank you for your help.
>
Re: specifying .xsd namespace inside XML file [message #667329 is a reply to message #667139] Thu, 28 April 2011 03:43 Go to previous messageGo to next message
Andrew Miga is currently offline Andrew Miga
Messages: 10
Registered: August 2009
Junior Member
>I have no idea why folks would need a specialized helper...

I should have explained this in the original message. The XML Helpers simply add and remove capitalization of the first letter of elements to make the XML output prettier. The university where I work wanted to capitalize the first letter of elements to make the XML output prettier. Note the capitalization on elements Scenario, Step, Start, Refinement etc. below. Ecore does not do this capitalization and outputs all lower case. One XML Helper adds capitalization on output, another removes it on input loading. I inherited these functions and can't remove them without destroying compatibility.

You are correct in that most folks wouldn't need a helper but we are using them to add and remove capitalization.

<?xml version="1.0" encoding="ASCII"?>
<CSM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" CSM:Root="file:/home/andrew/CSM/UMLActivity2CSM/resources/CSM.xsd ">
<Scenario id="ID000000" name="BuildingSurveillanceSystem">
<Step id="ID000002" name="cycleInit" predecessor="ID000001" successor="ID000003"/>
<Step id="ID000004" name="getOneImage" predecessor="ID000003" successor="ID000066">
<Refinement parent="ID000004" sub="ID000005"/>
</Step>
<Start id="ID000001" target="ID000002">
<ClosedWorkload externalDelay="1.0" population="1"/>
</Start>


The following is sample code from the input helper which removes capitalization for deserialization

public EStructuralFeature getFeature(EClass eClass,
String namespaceURI, String name, boolean isElement) {
name = shouldMakeFirstLetterLowerCase(name)?
makeFirstLetterLowerCase(name):
name;
// TODO Auto-generated method stub
return super.getFeature(eClass, namespaceURI, name, isElement);
}

private String makeFirstLetterLowerCase(String name){
String firstLetter = name.substring(0, 1);
firstLetter = firstLetter.toLowerCase();
return firstLetter + name.substring(1);
}
private boolean shouldMakeFirstLetterLowerCase(String name){
if ((name.equals("Step"))||
(name.equals("Scenario")) ||
(name.equals("PassiveResource"))||
(name.equals("Component"))||
(name.equals("ActiveResource"))||
....

(name.equals("OutBinding"))||
(name.equals("PerfValue"))
return true;
}
return false;
}

>Is the right resource factory registered to create your specialized resource?

There was no resource factory registered at all. I registered the appropriate factory and performed several other steps and it is now working.

ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "csm", new CSMResourceFactoryImpl() );
resourceSet.getPackageRegistry().put( CSMPackage.eNS_URI, CSMPackage.eINSTANCE);
resourceSet.getResources().add( csmResource );


All of these steps were missing from the original code which may never have worked.

Thank you for your help.

Re: specifying .xsd namespace inside XML file [message #667359 is a reply to message #667329] Thu, 28 April 2011 05:50 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 26013
Registered: July 2009
Senior Member
Andrew,

Comments below.

Andrew Miga wrote:
>> I have no idea why folks would need a specialized helper...
>
> I should have explained this in the original message. The XML Helpers
> simply add and remove capitalization of the first letter of elements
> to make the XML output prettier.
Extended meta data annotations work nicely for that as well...
> The university where I work wanted to capitalize the first letter of
> elements to make the XML output prettier. Note the capitalization on
> elements Scenario, Step, Start, Refinement etc. below. Ecore does not
> do this capitalization and outputs all lower case.
Yes it uses the feature name by default but there are annotations you
can use to specify the XML name.
> One XML Helper adds capitalization on output, another removes it on
> input loading. I inherited these functions and can't remove them
> without destroying compatibility.
> You are correct in that most folks wouldn't need a helper but we are
> using them to add and remove capitalization.
>
> <?xml version="1.0" encoding="ASCII"?>
> <CSM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> CSM:Root="file:/home/andrew/CSM/UMLActivity2CSM/resources/CSM.xsd ">
> <Scenario id="ID000000" name="BuildingSurveillanceSystem">
> <Step id="ID000002" name="cycleInit" predecessor="ID000001"
> successor="ID000003"/>
> <Step id="ID000004" name="getOneImage" predecessor="ID000003"
> successor="ID000066">
> <Refinement parent="ID000004" sub="ID000005"/>
> </Step>
> <Start id="ID000001" target="ID000002">
> <ClosedWorkload externalDelay="1.0" population="1"/>
> </Start>
>
>
> The following is sample code from the input helper which removes
> capitalization for deserialization
>
> public EStructuralFeature getFeature(EClass eClass,
> String namespaceURI, String name, boolean isElement) {
> name = shouldMakeFirstLetterLowerCase(name)?
> makeFirstLetterLowerCase(name):
> name;
> // TODO Auto-generated method stub
> return super.getFeature(eClass, namespaceURI, name, isElement);
> }
>
> private String makeFirstLetterLowerCase(String name){
> String firstLetter = name.substring(0, 1);
> firstLetter = firstLetter.toLowerCase();
> return firstLetter + name.substring(1);
> }
> private boolean shouldMakeFirstLetterLowerCase(String name){
> if ((name.equals("Step"))||
> (name.equals("Scenario")) ||
> (name.equals("PassiveResource"))||
> (name.equals("Component"))||
> (name.equals("ActiveResource"))||
> ....
>
> (name.equals("OutBinding"))||
> (name.equals("PerfValue")) return true;
> }
> return false;
> }
>
>> Is the right resource factory registered to create your specialized
>> resource?
>
> There was no resource factory registered at all. I registered the
> appropriate factory and performed several other steps and it is now
> working.
>
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getResourceFactoryRegistry().getExtensionToFacto
> ryMap().put( "csm", new CSMResourceFactoryImpl() );
> resourceSet.getPackageRegistry().put( CSMPackage.eNS_URI,
> CSMPackage.eINSTANCE);
> resourceSet.getResources().add( csmResource );
>
>
> All of these steps were missing from the original code which may never
> have worked.
>
> Thank you for your help.
>
>
Previous Topic:view xml file with internal web browser
Next Topic:xml autocomplete from schema
Goto Forum:
  


Current Time: Thu Aug 28 21:44:43 EDT 2014

Powered by FUDForum. Page generated in 0.02424 seconds