Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » XML Schema Definition (XSD) » How to clone annotations
How to clone annotations [message #76536] Tue, 23 December 2008 12:36 Go to next message
shobana is currently offline shobana
Messages: 13
Registered: July 2009
Junior Member
Hi,

I need to copy a xsd component from a schema to another schema. I have
done it by using cloneConcreteComponent() method. But annotations of the
component is not getting cloned if I use this method.

I tried cloneNode(), even that didnt work.

Could any of you please help me in this.

Thanks in advance.

Shobana
Re: How to clone annotations [message #76552 is a reply to message #76536] Tue, 23 December 2008 12:59 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26058
Registered: July 2009
Senior Member
Shobana,

Did you try what's in XSDPrototypicalSchema.cloneComponent with
preserveDOM true? I.e., clone the DOM and create a new component to
wrap that DOM...


shobana wrote:
> Hi,
>
> I need to copy a xsd component from a schema to another schema. I have
> done it by using cloneConcreteComponent() method. But annotations of
> the component is not getting cloned if I use this method.
> I tried cloneNode(), even that didnt work.
>
> Could any of you please help me in this.
>
> Thanks in advance.
>
> Shobana
>
>
>
Re: How to clone annotations [message #76620 is a reply to message #76552] Wed, 24 December 2008 06:45 Go to previous messageGo to next message
shobana is currently offline shobana
Messages: 13
Registered: July 2009
Junior Member
Hi ed,

Thanks for quick reply.

Yes I tried that too. But it just returns the empty tag of the xsd
component.
Is there any other way to copy the xsd component to another schema without
any data loss?

Thanks,
Shobana
Re: How to clone annotations [message #76636 is a reply to message #76620] Wed, 24 December 2008 09:34 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26058
Registered: July 2009
Senior Member
Shobana,

You're sure? Did you look at the implementation of that method?

Element element = xsdConcreteComponent./*{@link
XSDConcreteComponent#getElement() /*}*/getElement();
if (element != null)
{
// Clone the DOM using the DOM API, and create the same type of
component to hold it.
//
Element clonedElement = (Element)element.cloneNode(true);
XSDConcreteComponent result =
(XSDConcreteComponent)XSDFactory.eINSTANCE.create(xsdConcret eComponent.eClass());
result.setElement(clonedElement);
return result;
}

If you're moving between schemas, you need to do
xsdSchema.getDocument().importNode(element) instead of just cloning it,
because a clone produces an Element that can only be used in the same
Document...


shobana wrote:
> Hi ed,
>
> Thanks for quick reply.
>
> Yes I tried that too. But it just returns the empty tag of the xsd
> component.
> Is there any other way to copy the xsd component to another schema
> without any data loss?
>
> Thanks,
> Shobana
>
Re: How to clone annotations [message #76667 is a reply to message #76636] Fri, 26 December 2008 09:03 Go to previous messageGo to next message
shobana is currently offline shobana
Messages: 13
Registered: July 2009
Junior Member
Hi Ed,

Thanks a lot. I used importNode() it worked. :)

But yes I tried the method u mentioned in the earlier post, but it didnt
work.

Thanks,
Shobana
Re: How to clone annotations [message #76996 is a reply to message #76667] Wed, 18 February 2009 10:56 Go to previous messageGo to next message
rollo is currently offline rollo
Messages: 117
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------080305090109090806010008
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

I also want to clone an existing annotation and move it to another
schema, but I didn't get it working.

For example for this schema "schema.xsd" in the attachment.

In the moment I do it like this:

I read the content of the old schema. Create for example a new
Simpletype. I put the annotation of the old Simpletype in a map.
At the end I add all new Types to the new schema.

normalizedSchema.getContents().add( type );

if ( annotations.containsKey( type.getName() ) ) {
XSDAnnotation annotation = annotations.get( type.getName() );
type.setAnnotation( annotation );
schema.getDocument().importNode( annotation.getElement(), true );
}

But it did not work, like you can see in schema2.xsd.

Can somebody help me?

Roland





--------------080305090109090806010008
Content-Type: text/plain; x-mac-type="0"; x-mac-creator="0";
name="schema.xsd"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename="schema.xsd"

PHhzZDpzY2hlbWEgeG1sbnM6eHNkPSJodHRwOi8vd3d3LnczLm9yZy8yMDAx L1hNTFNjaGVt
YSIKCXhtbG5zOm5zPSJodHRwOi8vZXhhbXBsZS5jb20vdGVzdCIgeG1sbnM6 bXlNZXRhPSJo
dHRwOi8vZXhhbXBsZS5jb20vbWV0YUluZm8iCgl0YXJnZXROYW1lc3BhY2U9 Imh0dHA6Ly9l
eGFtcGxlLmNvbS90ZXN0Ij4KCgk8eHNkOmVsZW1lbnQgbmFtZT0iQSIgdHlw ZT0ibnM6QSIg
Lz4KCgk8eHNkOmNvbXBsZXhUeXBlIG5hbWU9IkEiPgoJCTx4c2Q6YW5ub3Rh dGlvbj4KCQkJ
PHhzZDphcHBpbmZvIHNvdXJjZT0iaHR0cDovL2V4YW1wbGUuY29tL21ldGFJ bmZvIj4KCQkJ
CTxNZXRhSW5mb01hcCB4bWxucz0iaHR0cDovL2V4YW1wbGUuY29tL21ldGFJ bmZvIj4KCQkJ
CQk8bWV0YUluZm8ga2V5PSJkZXNjcmlwdGlvbiIgdmFsdWU9IkEiIC8+CgkJ CQk8L01ldGFJ
bmZvTWFwPgoJCQk8L3hzZDphcHBpbmZvPgoJCTwveHNkOmFubm90YXRpb24+ CgkJPHhzZDpz
ZXF1ZW5jZT4KCQkJPHhzZDplbGVtZW50IG5hbWU9IkIiIHR5cGU9Im5zOkIi IC8+CgkJPC94
c2Q6c2VxdWVuY2U+Cgk8L3hzZDpjb21wbGV4VHlwZT4KCgk8eHNkOnNpbXBs ZVR5cGUgbmFt
ZT0iQiI+CgkJPHhzZDphbm5vdGF0aW9uPgoJCQk8eHNkOmFwcGluZm8gc291 cmNlPSJodHRw
Oi8vZXhhbXBsZS5jb20vbWV0YUluZm8iPgoJCQkJPE1ldGFJbmZvTWFwIHht bG5zPSJodHRw
Oi8vZXhhbXBsZS5jb20vbWV0YUluZm8iPgoJCQkJCTxtZXRhSW5mbyBrZXk9 ImRlc2NyaXB0
aW9uIiB2YWx1ZT0iQiIgLz4KCQkJCTwvTWV0YUluZm9NYXA+CgkJCTwveHNk OmFwcGluZm8+
CgkJPC94c2Q6YW5ub3RhdGlvbj4KCQk8eHNkOnJlc3RyaWN0aW9uIGJhc2U9 InhzZDpzdHJp
bmciIC8+Cgk8L3hzZDpzaW1wbGVUeXBlPgoKPC94c2Q6c2NoZW1hPg==
--------------080305090109090806010008
Content-Type: text/xml; x-mac-type="0"; x-mac-creator="0";
name="schema2.xsd"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="schema2.xsd"

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:myMeta="http://example.com/metaInfo"
xmlns:ns="http://www.arlanis.com/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified" targetNamespace="http://www.arlanis.com/test">
<xsd:element name="A" type="ns:A" />
<xsd:complexType name="A">
<xsd:sequence maxOccurs="1" minOccurs="1">
<xsd:element maxOccurs="1" minOccurs="1" name="B" type="ns:B" />
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="B">
<xsd:annotation />
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
</xsd:schema>

--------------080305090109090806010008--
Re: How to clone annotations [message #77005 is a reply to message #76636] Wed, 18 February 2009 11:00 Go to previous messageGo to next message
Bhuvan Mehta is currently offline Bhuvan Mehta
Messages: 38
Registered: July 2009
Member
Hello,

When i use the below code snippet as suggested by you :

Element element = srcComponent.getElement();
if (element != null) {
Element clonedElement = (Element)element.cloneNode(true);
XSDConcreteComponent result =
(XSDConcreteComponent)XSDFactory.eINSTANCE.create(srcCompone nt.eClass());
result.setElement(clonedElement);

Node newComponent =
targetSchema.getDocument().importNode(result.getElement(), true);
targetSchema.getElement().replaceChild(newComponent,
oldComponent.getElement());

}

then it updates the schema in following way :
Ex:
Original Schema :-
Say i change the type from "int" to "boolean"

<xsd:element name="ele1" type="xsd:int"/>

<element name="ele1" type="boolean"
xmlns="http://www.w3.org/2001/XMLSchema"/>

Which is valid but when i use the validate() method from
XSDConcreteComponent
to check schema validation then it says schema is invalid with following
message.

"Invalid XML schema for namespace 'http://www.example.org/test1/'.
Diagnostics:
XSD: Type reference '#int' is unresolved".
Re: How to clone annotations [message #77014 is a reply to message #76667] Wed, 18 February 2009 12:12 Go to previous messageGo to next message
Bhuvan Mehta is currently offline Bhuvan Mehta
Messages: 38
Registered: July 2009
Member
Hello Ed,

When i use the below code snippet as suggested by you :

Element element = srcComponent.getElement();
if (element != null) {
Element clonedElement = (Element)element.cloneNode(true);
XSDConcreteComponent result =
(XSDConcreteComponent)XSDFactory.eINSTANCE.create(srcCompone nt.eClass());
result.setElement(clonedElement);
Node newComponent =
targetSchema.getDocument().importNode(result.getElement(), true);
targetSchema.getElement().replaceChild(newComponent,
oldComponent.getElement());

}

then it updates the schema in following way :
Ex:
Original Schema :-
Say i change the type from "int" to "boolean"

<xsd:element name="ele1" type="xsd:int"/>

<element name="ele1" type="boolean"
xmlns="http://www.w3.org/2001/XMLSchema"/>

Which is valid but when i use the validate() method from
XSDConcreteComponent
to check schema validation then it says schema is invalid with following
message.

"Invalid XML schema for namespace 'http://www.example.org/test1/'.
Diagnostics:
XSD: Type reference '#int' is unresolved".
Re: How to clone annotations [message #77023 is a reply to message #76667] Wed, 18 February 2009 12:13 Go to previous messageGo to next message
Bhuvan Mehta is currently offline Bhuvan Mehta
Messages: 38
Registered: July 2009
Member
Hello,

When i use the below code snippet as suggested by you :

Element element = srcComponent.getElement();
if (element != null) {
Element clonedElement = (Element)element.cloneNode(true);
XSDConcreteComponent result =
(XSDConcreteComponent)XSDFactory.eINSTANCE.create(srcCompone nt.eClass());
result.setElement(clonedElement);
Node newComponent =
targetSchema.getDocument().importNode(result.getElement(), true);
targetSchema.getElement().replaceChild(newComponent,
oldComponent.getElement());

}

then it updates the schema in following way :
Ex:
Original Schema :-
Say i change the type from "int" to "boolean"

<xsd:element name="ele1" type="xsd:int"/>

<element name="ele1" type="boolean"
xmlns="http://www.w3.org/2001/XMLSchema"/>

Which is valid but when i use the validate() method from
XSDConcreteComponent
to check schema validation then it says schema is invalid with following
message.

"Invalid XML schema for namespace 'http://www.example.org/test1/'.
Diagnostics:
XSD: Type reference '#int' is unresolved".
Re: How to clone annotations [message #77029 is a reply to message #76996] Wed, 18 February 2009 12:24 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26058
Registered: July 2009
Senior Member
Roland,

Comments below.

Roland Siebert wrote:
> Hi,
>
> I also want to clone an existing annotation and move it to another
> schema, but I didn't get it working.
>
> For example for this schema "schema.xsd" in the attachment.
>
> In the moment I do it like this:
>
> I read the content of the old schema. Create for example a new
> Simpletype. I put the annotation of the old Simpletype in a map.
> At the end I add all new Types to the new schema.
>
> normalizedSchema.getContents().add( type );
>
> if ( annotations.containsKey( type.getName() ) ) {
> XSDAnnotation annotation = annotations.get( type.getName() );
> type.setAnnotation( annotation );
So here you are working with the existing annotation and moving it across.
> schema.getDocument().importNode( annotation.getElement(), true );
Now you clone its element but do nothing with it so it's garbage collected.
> }
>
> But it did not work, like you can see in schema2.xsd.
>
> Can somebody help me?
How about trying what I've already shown to clone the annotations
Element and using that as the element of a newly created annotation?
>
> Roland
>
>
>
>
Re: How to clone annotations [message #77033 is a reply to message #77005] Wed, 18 February 2009 12:29 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26058
Registered: July 2009
Senior Member
Bhuvan,

Comments below.

Bhuvan wrote:
> Hello,
>
> When i use the below code snippet as suggested by you :
>
> Element element = srcComponent.getElement();
> if (element != null) {
> Element clonedElement = (Element)element.cloneNode(true);
So that creates a clone you can use in the same Document.
> XSDConcreteComponent result =
> (XSDConcreteComponent)XSDFactory.eINSTANCE.create(srcCompone nt.eClass());
> result.setElement(clonedElement);
> Node newComponent =
> targetSchema.getDocument().importNode(result.getElement(), true);
Cloning the element after you've already put it in a component seems
odd. Why not reverse to two statements? And why not use the original
element instead of importing a clone?
>
> targetSchema.getElement().replaceChild(newComponent,
> oldComponent.getElement());
>
> }
>
> then it updates the schema in following way :
> Ex:
> Original Schema :-
> Say i change the type from "int" to "boolean"
>
> <xsd:element name="ele1" type="xsd:int"/>
>
> <element name="ele1" type="boolean"
> xmlns="http://www.w3.org/2001/XMLSchema"/>
>
> Which is valid but when i use the validate() method from
> XSDConcreteComponent
> to check schema validation then it says schema is invalid with
> following message.
>
> "Invalid XML schema for namespace 'http://www.example.org/test1/'.
> Diagnostics:
> XSD: Type reference '#int' is unresolved".
I'm not sure there is a xmlns:xsd anywhere.
Re: How to clone annotations [message #77037 is a reply to message #77033] Wed, 18 February 2009 13:44 Go to previous messageGo to next message
Bhuvan Mehta is currently offline Bhuvan Mehta
Messages: 38
Registered: July 2009
Member
Hello Ed,

Thanks for the quick reply.

I actually need to change in other document so i have used cloning.

and xmlns:xsd i defined in <schema> tag, so missed out to mention.

after the changing files using the snippet and validating using validate()
method, it returns the WSDL is invalid which is not the case
and i checked it with WTP editor which shows file is valid.
Re: How to clone annotations [message #77040 is a reply to message #77033] Wed, 18 February 2009 14:31 Go to previous messageGo to next message
Bhuvan Mehta is currently offline Bhuvan Mehta
Messages: 38
Registered: July 2009
Member
Hello Ed,

Thanks for the quick reply.
Case is : I need to update one XSDConcreteComponent from one document to
another
document.

There are actually two problems
1. If i use the below code snippet for this (which puts the proper prefix
when changing in component in other schema)

XSDConcreteComponent cloneConcreteComponent =
newComponent.cloneConcreteComponent(true, false);
targetSchema.getContents().remove(oldComponent);
targetSchema.getContents().add((XSDSchemaContent) cloneConcreteComponent);

But this code does not copies the annotations in target schema so i used
the below snippet to get annotations copied, as i need to copy to other
document
so i used importNode()

2. Then i used below code

Node new = targetSchema.getDocument().importNode(newComponent, true);
targetSchema.getElement().replaceChild(new,oldComponent.getE lement());

which does proper copy for annotations but ignores the defied prefixes in
schema and adds "xmlns" inside changed component, but this we can ignore
as long as schema is valid.

But when i validate the schema after changes using validate() method of
XSDConcreteComponent then it says schema is invalid with below message.
"Invalid XML schema for namespace 'http://www.example.org/test1/'.
Diagnostics:
XSD: Type reference '#int' is unresolved".

then after that when i save the EMF model and validate the file with WTP
editor and which says it is valid.

and "xmlns:xsd" prefix i have actually defined in <schema> tag so missed
out to mention this in earlier post.
Re: How to clone annotations [message #77129 is a reply to message #77040] Wed, 18 February 2009 15:31 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26058
Registered: July 2009
Senior Member
Bhuvan,

Comments below.

Bhuvan wrote:
> Hello Ed,
>
> Thanks for the quick reply.
> Case is : I need to update one XSDConcreteComponent from one document
> to another
> document.
>
> There are actually two problems 1. If i use the below code snippet for
> this (which puts the proper prefix when changing in component in other
> schema)
>
> XSDConcreteComponent cloneConcreteComponent =
> newComponent.cloneConcreteComponent(true, false);
> targetSchema.getContents().remove(oldComponent);
> targetSchema.getContents().add((XSDSchemaContent)
> cloneConcreteComponent);
>
> But this code does not copies the annotations in target schema so i
> used the below snippet to get annotations copied, as i need to copy to
> other document
> so i used importNode()
>
> 2. Then i used below code
> Node new = targetSchema.getDocument().importNode(newComponent,
> true);
Here newComponent is an Element but above it's an XSDConcreteComponent...
>
> targetSchema.getElement().replaceChild(new,oldComponent.getE lement());
>
>
> which does proper copy for annotations but ignores the defied prefixes
> in schema and adds "xmlns" inside changed component, but this we can
> ignore as long as schema is valid.
That kind of sucks though. Are you sure the prefixes being used are the
same between the two schemas? Or maybe one uses "xsd" and the other
uses no prefix?
>
> But when i validate the schema after changes using validate() method
> of XSDConcreteComponent then it says schema is invalid with below
> message.
> "Invalid XML schema for namespace 'http://www.example.org/test1/'.
> Diagnostics:
> XSD: Type reference '#int' is unresolved".
>
> then after that when i save the EMF model and validate the file with
> WTP editor and which says it is valid.
>
> and "xmlns:xsd" prefix i have actually defined in <schema> tag so
> missed out to mention this in earlier post.
I'm not sure what to say. Perhaps there's a bug in updating the model
from the DOM, but I'd need a test case to reproduce that...
Re: How to clone annotations [message #77161 is a reply to message #77129] Wed, 18 February 2009 17:11 Go to previous messageGo to next message
Bhuvan Mehta is currently offline Bhuvan Mehta
Messages: 38
Registered: July 2009
Member
"Here newComponent is an Element but above it's an
XSDConcreteComponent...",
TRUE, it;s just same name that i somehow used at two different places.

yaa Ed you got it right, so one schema does not uses any prefix and the
other one uses "xsd", but for the same case "(1)"st code snippet puts the
prefix correctly.


This behaviour is very strange, once we save the updated EMF model to file
and then validate the file using WTP or get the schema from updated file
and validate it using validate() method both of the cases it is said VALID.

The problem is only when the model is not saved and we try to validate it,
and so is the usecase if schema is InValid then there is no point in
saving the changes into files.

--
Thanks and Best Regards,
Bhuvan Mehta
Re: How to clone annotations [message #77208 is a reply to message #76636] Thu, 19 February 2009 10:28 Go to previous messageGo to next message
Eclipse User
Originally posted by: jacek.pospychala.pl.ibm.com

Ed,
I don't get this in Javadoc: "A component can be cloned directly or by
cloning the underlying DOM. By cloning the DOM you ensure that
&lt;annotation>s, non-schema namespace attributes, and formatting are
preserved."

When I clone just the DOM, how is component recreated? based on DOM?

I'm copying between schemas, so I use importNode instead of cloneNode.

When I do XSDFactory.eINSTANCE.create(xsdConcreteComponent.eClass()) and
importNode, all the component attributes (e.g getAnnotation()) are Null.

When I do cloneConcreteComponent(true,true) and deep importNode, then
DOM and component model seem to be correct, but they're not synchronized.
So if I change a value in component model and then print XSD contents,
then I still see old DOM.




Ed Merks pisze:
> Shobana,
>
> You're sure? Did you look at the implementation of that method?
> Element element = xsdConcreteComponent./*{@link
> XSDConcreteComponent#getElement() /*}*/getElement();
> if (element != null)
> {
> // Clone the DOM using the DOM API, and create the same type of
> component to hold it.
> //
> Element clonedElement = (Element)element.cloneNode(true);
> XSDConcreteComponent result =
> (XSDConcreteComponent)XSDFactory.eINSTANCE.create(xsdConcret eComponent.eClass());
>
> result.setElement(clonedElement);
> return result;
> }
Re: How to clone annotations [message #77224 is a reply to message #77208] Thu, 19 February 2009 11:52 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26058
Registered: July 2009
Senior Member
Jacek,

Comments below.

Jacek Pospychala wrote:
> Ed,
> I don't get this in Javadoc: "A component can be cloned directly or by
> cloning the underlying DOM. By cloning the DOM you ensure that
> &lt;annotation>s, non-schema namespace attributes, and formatting are
> preserved."
>
> When I clone just the DOM, how is component recreated? based on DOM?
It seems like the code snippet below shows that. I.e., create a
component of the appropriate type and set its element...
>
> I'm copying between schemas, so I use importNode instead of cloneNode.
>
> When I do XSDFactory.eINSTANCE.create(xsdConcreteComponent.eClass())
> and importNode, all the component attributes (e.g getAnnotation()) are
> Null.
It's best you show exactly the code. Maybe you've missed a step...
>
> When I do cloneConcreteComponent(true,true) and deep importNode, then
> DOM and component model seem to be correct, but they're not synchronized.
Sharing the DOM is use within the model for things like chameleon
namespace includes.
> So if I change a value in component model and then print XSD contents,
> then I still see old DOM.
Yes it's intended as a read-only view and is not likely to be useful
outside of the framework.
>
>
>
>
> Ed Merks pisze:
>> Shobana,
>>
>> You're sure? Did you look at the implementation of that method?
>> Element element = xsdConcreteComponent./*{@link
>> XSDConcreteComponent#getElement() /*}*/getElement();
>> if (element != null)
>> {
>> // Clone the DOM using the DOM API, and create the same type
>> of component to hold it.
>> //
>> Element clonedElement = (Element)element.cloneNode(true);
>> XSDConcreteComponent result =
>> (XSDConcreteComponent)XSDFactory.eINSTANCE.create(xsdConcret eComponent.eClass());
>>
>> result.setElement(clonedElement);
>> return result;
>> }
Re: How to clone annotations [message #77239 is a reply to message #77224] Thu, 19 February 2009 13:24 Go to previous messageGo to next message
Eclipse User
Originally posted by: jacek.pospychala.pl.ibm.com

Ed,
below is my sample code.

What I'm trying to do, is copy an element from one XSD to another
without loosing any information, whether it's kept in DOM or XSD model
itself and then normally work on changed XSD.

Possible?

btw. I discovered the XSDPrototypicalSchema class. It's very helpful :-)


import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import junit.framework.TestCase;

import org.eclipse.xsd.XSDAnnotation;
import org.eclipse.xsd.XSDFactory;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.XSDTypeDefinition;
import org.eclipse.xsd.util.XSDConstants;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Test extends TestCase {

public void testCopy() throws TransformerFactoryConfigurationError,
TransformerException, IOException {
// create source schema
XSDSchema srcSchema = XSDFactory.eINSTANCE.createXSDSchema();
srcSchema.setSchemaForSchemaQNamePrefix("xs");
srcSchema.setTargetNamespace("http://abc");
Map<String, String> qNamePrefixToNamespaceMap =
srcSchema.getQNamePrefixToNamespaceMap();
qNamePrefixToNamespaceMap.put("tns", srcSchema.getTargetNamespace());

qNamePrefixToNamespaceMap.put(srcSchema.getSchemaForSchemaQN amePrefix(),
XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);

// add complex type in source schema
XSDTypeDefinition type =
XSDFactory.eINSTANCE.createXSDComplexTypeDefinition();
srcSchema.getContents().add(type);
type.setName("someType");
XSDAnnotation annotation = XSDFactory.eINSTANCE.createXSDAnnotation();
type.setAnnotation(annotation);

Element e = annotation.createUserInformation(null);
e.appendChild(e.getOwnerDocument().createTextNode("Some text"));
annotation.getElement().appendChild(e);

srcSchema.updateElement();

assertEquals("Some text",
type.getAnnotation().getUserInformation().get(0).getTextCont ent());

// create target schema
XSDSchema dstSchema = XSDFactory.eINSTANCE.createXSDSchema();
dstSchema.setSchemaForSchemaQNamePrefix("xs");
dstSchema.setTargetNamespace("http://xyz");
qNamePrefixToNamespaceMap = dstSchema.getQNamePrefixToNamespaceMap();
qNamePrefixToNamespaceMap.put("tns", dstSchema.getTargetNamespace());

qNamePrefixToNamespaceMap.put(dstSchema.getSchemaForSchemaQN amePrefix(),
XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);
dstSchema.updateElement();

// copy complex type from srcSchema to dstSchema
XSDTypeDefinition srcType = srcSchema.getTypeDefinitions().get(0);
XSDTypeDefinition dstType = (XSDTypeDefinition)
srcType.cloneConcreteComponent(true, false);
dstSchema.getContents().add(dstType);

Element dstElement = (Element)
dstSchema.getDocument().importNode(srcType.getElement(), true);
dstType.getElement().getParentNode().replaceChild(dstElement ,
dstType.getElement());

dstSchema.updateElement();

assertEquals("Some text",
dstType.getAnnotation().getUserInformation().get(0).getTextC ontent());


System.out.println(convertToString(dstSchema.getDocument())) ;
}


Ed Merks pisze:
> Jacek,
>
> Comments below.
>
> Jacek Pospychala wrote:
>> Ed,
>> I don't get this in Javadoc: "A component can be cloned directly or by
>> cloning the underlying DOM. By cloning the DOM you ensure that
>> &lt;annotation>s, non-schema namespace attributes, and formatting are
>> preserved."
>>
>> When I clone just the DOM, how is component recreated? based on DOM?
> It seems like the code snippet below shows that. I.e., create a
> component of the appropriate type and set its element...
>>
>> I'm copying between schemas, so I use importNode instead of cloneNode.
>>
>> When I do XSDFactory.eINSTANCE.create(xsdConcreteComponent.eClass())
>> and importNode, all the component attributes (e.g getAnnotation()) are
>> Null.
> It's best you show exactly the code. Maybe you've missed a step...
>>
>> When I do cloneConcreteComponent(true,true) and deep importNode, then
>> DOM and component model seem to be correct, but they're not synchronized.
> Sharing the DOM is use within the model for things like chameleon
> namespace includes.
>> So if I change a value in component model and then print XSD contents,
>> then I still see old DOM.
> Yes it's intended as a read-only view and is not likely to be useful
> outside of the framework.
>>
>>
>>
>>
>> Ed Merks pisze:
>>> Shobana,
>>>
>>> You're sure? Did you look at the implementation of that method?
>>> Element element = xsdConcreteComponent./*{@link
>>> XSDConcreteComponent#getElement() /*}*/getElement();
>>> if (element != null)
>>> {
>>> // Clone the DOM using the DOM API, and create the same type
>>> of component to hold it.
>>> //
>>> Element clonedElement = (Element)element.cloneNode(true);
>>> XSDConcreteComponent result =
>>> (XSDConcreteComponent)XSDFactory.eINSTANCE.create(xsdConcret eComponent.eClass());
>>>
>>> result.setElement(clonedElement);
>>> return result;
>>> }
Re: How to clone annotations [message #77255 is a reply to message #77239] Thu, 19 February 2009 15:16 Go to previous messageGo to next message
Eclipse User
Originally posted by: jacek.pospychala.pl.ibm.com

Haha! Found! :-)

I have to care only about DOM model and XSD model will be built on top
of that automatically = let dstSchema create the XSDTypeDefinition
instead of creating it by hand.

So do copy as following:

XSDTypeDefinition srcType = srcSchema.getTypeDefinitions().get(0);
Element dstElement = (Element)
dstSchema.getDocument().importNode(srcType.getElement(), true);
dstSchema.getElement().appendChild(dstElement);


Jacek Pospychala pisze:
> Ed,
> below is my sample code.
>
> What I'm trying to do, is copy an element from one XSD to another
> without loosing any information, whether it's kept in DOM or XSD model
> itself and then normally work on changed XSD.
>
> Possible?
>
> btw. I discovered the XSDPrototypicalSchema class. It's very helpful :-)
[...]
>
> Ed Merks pisze:
>> Jacek,
>>
>> Comments below.
>>
>> Jacek Pospychala wrote:
>>> Ed,
>>> I don't get this in Javadoc: "A component can be cloned directly or
>>> by cloning the underlying DOM. By cloning the DOM you ensure that
>>> &lt;annotation>s, non-schema namespace attributes, and formatting are
>>> preserved."
>>>
>>> When I clone just the DOM, how is component recreated? based on DOM?
>> It seems like the code snippet below shows that. I.e., create a
>> component of the appropriate type and set its element...
>>>
>>> I'm copying between schemas, so I use importNode instead of cloneNode.
>>>
>>> When I do XSDFactory.eINSTANCE.create(xsdConcreteComponent.eClass())
>>> and importNode, all the component attributes (e.g getAnnotation())
>>> are Null.
>> It's best you show exactly the code. Maybe you've missed a step...
>>>
>>> When I do cloneConcreteComponent(true,true) and deep importNode, then
>>> DOM and component model seem to be correct, but they're not
>>> synchronized.
>> Sharing the DOM is use within the model for things like chameleon
>> namespace includes.
>>> So if I change a value in component model and then print XSD
>>> contents, then I still see old DOM.
>> Yes it's intended as a read-only view and is not likely to be useful
>> outside of the framework.
>>>
>>>
>>>
>>>
>>> Ed Merks pisze:
>>>> Shobana,
>>>>
>>>> You're sure? Did you look at the implementation of that
>>>> method? Element element = xsdConcreteComponent./*{@link
>>>> XSDConcreteComponent#getElement() /*}*/getElement();
>>>> if (element != null)
>>>> {
>>>> // Clone the DOM using the DOM API, and create the same type
>>>> of component to hold it.
>>>> //
>>>> Element clonedElement = (Element)element.cloneNode(true);
>>>> XSDConcreteComponent result =
>>>> (XSDConcreteComponent)XSDFactory.eINSTANCE.create(xsdConcret eComponent.eClass());
>>>>
>>>> result.setElement(clonedElement);
>>>> return result;
>>>> }
Re: How to clone annotations [message #77271 is a reply to message #77239] Thu, 19 February 2009 15:32 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26058
Registered: July 2009
Senior Member
Jacek,

Comments below.

Jacek Pospychala wrote:
> Ed,
> below is my sample code.
>
> What I'm trying to do, is copy an element from one XSD to another
> without loosing any information, whether it's kept in DOM or XSD model
> itself and then normally work on changed XSD.
>
> Possible?
Should be.
>
> btw. I discovered the XSDPrototypicalSchema class. It's very helpful :-)
I tried to have an example of pretty much everything...
>
>
> import java.io.IOException;
> import java.io.StringWriter;
> import java.util.Map;
>
> import javax.xml.transform.OutputKeys;
> import javax.xml.transform.Transformer;
> import javax.xml.transform.TransformerException;
> import javax.xml.transform.TransformerFactory;
> import javax.xml.transform.TransformerFactoryConfigurationError;
> import javax.xml.transform.dom.DOMSource;
> import javax.xml.transform.stream.StreamResult;
>
> import junit.framework.TestCase;
>
> import org.eclipse.xsd.XSDAnnotation;
> import org.eclipse.xsd.XSDFactory;
> import org.eclipse.xsd.XSDSchema;
> import org.eclipse.xsd.XSDTypeDefinition;
> import org.eclipse.xsd.util.XSDConstants;
> import org.w3c.dom.Document;
> import org.w3c.dom.Element;
> import org.w3c.dom.Node;
>
> public class Test extends TestCase {
>
> public void testCopy() throws
> TransformerFactoryConfigurationError, TransformerException, IOException {
> // create source schema
> XSDSchema srcSchema = XSDFactory.eINSTANCE.createXSDSchema();
> srcSchema.setSchemaForSchemaQNamePrefix("xs");
> srcSchema.setTargetNamespace("http://abc");
> Map<String, String> qNamePrefixToNamespaceMap =
> srcSchema.getQNamePrefixToNamespaceMap();
> qNamePrefixToNamespaceMap.put("tns",
> srcSchema.getTargetNamespace());
>
> qNamePrefixToNamespaceMap.put(srcSchema.getSchemaForSchemaQN amePrefix(),
> XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);
>
> // add complex type in source schema
> XSDTypeDefinition type =
> XSDFactory.eINSTANCE.createXSDComplexTypeDefinition();
> srcSchema.getContents().add(type);
> type.setName("someType");
> XSDAnnotation annotation =
> XSDFactory.eINSTANCE.createXSDAnnotation();
> type.setAnnotation(annotation);
>
> Element e = annotation.createUserInformation(null);
> e.appendChild(e.getOwnerDocument().createTextNode("Some text"));
> annotation.getElement().appendChild(e);
>
> srcSchema.updateElement();
>
> assertEquals("Some text",
> type.getAnnotation().getUserInformation().get(0).getTextCont ent());
>
> // create target schema
> XSDSchema dstSchema = XSDFactory.eINSTANCE.createXSDSchema();
> dstSchema.setSchemaForSchemaQNamePrefix("xs");
> dstSchema.setTargetNamespace("http://xyz");
> qNamePrefixToNamespaceMap =
> dstSchema.getQNamePrefixToNamespaceMap();
> qNamePrefixToNamespaceMap.put("tns",
> dstSchema.getTargetNamespace());
>
> qNamePrefixToNamespaceMap.put(dstSchema.getSchemaForSchemaQN amePrefix(),
> XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);
> dstSchema.updateElement();
>
> // copy complex type from srcSchema to dstSchema
> XSDTypeDefinition srcType =
> srcSchema.getTypeDefinitions().get(0);
> XSDTypeDefinition dstType = (XSDTypeDefinition)
> srcType.cloneConcreteComponent(true, false);
> dstSchema.getContents().add(dstType);
>
> Element dstElement = (Element)
> dstSchema.getDocument().importNode(srcType.getElement(), true);
> dstType.getElement().getParentNode().replaceChild(dstElement ,
> dstType.getElement());
I'd expect this to work, but the dstType just isn't reconciled properly
even though the DOM itself looks correct. The last line shouldn't be
needed.

XSDTypeDefinition srcType = srcSchema.getTypeDefinitions().get(0);
XSDTypeDefinition dstType =
(XSDTypeDefinition)EcoreUtil.create(srcType.eClass());
Element dstElement = (Element)
dstSchema.getDocument().importNode(srcType.getElement(), true);
dstType.setElement(dstElement);
dstSchema.getContents().add(dstType);

dstType.elementChanged(dstType.getElement());

I think this patch is needed. Please open a bugzilla.

### Eclipse Workspace Patch 1.0
#P org.eclipse.xsd
Index: src/org/eclipse/xsd/impl/XSDConcreteComponentImpl.java
============================================================ =======
RCS file:
/cvsroot/modeling/org.eclipse.mdt/org.eclipse.xsd/plugins/or g.eclipse.xsd/src/org/eclipse/xsd/impl/XSDConcreteComponentI mpl.java,v
retrieving revision 1.29
diff -u -r1.29 XSDConcreteComponentImpl.java
--- src/org/eclipse/xsd/impl/XSDConcreteComponentImpl.java 20 Jan
2009 17:02:57 -0000 1.29
+++ src/org/eclipse/xsd/impl/XSDConcreteComponentImpl.java 19 Feb
2009 15:31:19 -0000
@@ -1361,11 +1361,13 @@
}
}

- if (childElement == null)
- {
- ((XSDConcreteComponentImpl)xsdConcreteComponent).isReconcili ng =
true;
- childElement =
((XSDConcreteComponentImpl)xsdConcreteComponent).createEleme nt();
- ((XSDConcreteComponentImpl)xsdConcreteComponent).isReconcili ng =
false;
+ XSDConcreteComponentImpl xsdConcreteComponentImpl =
(XSDConcreteComponentImpl)xsdConcreteComponent;
+ boolean newChild = childElement == null;
+ if (newChild)
+ {
+ xsdConcreteComponentImpl.isReconciling = true;
+ childElement = xsdConcreteComponentImpl.createElement();
+ xsdConcreteComponentImpl.isReconciling = false;
if (childElement == null)
{
System.out.println("not created! " + xsdConcreteComponent);
@@ -1418,6 +1420,12 @@
}

niceInsertBefore(adoptionParent, childElement, referencedElement);
+ if (!newChild)
+ {
+ xsdConcreteComponentImpl.isReconciling = true;
+ xsdConcreteComponentImpl.reconcile(childElement);
+ xsdConcreteComponentImpl.isReconciling = false;
+ }
}
else
{

>
> dstSchema.updateElement();
>
> assertEquals("Some text",
> dstType.getAnnotation().getUserInformation().get(0).getTextC ontent());
>
>
> System.out.println(convertToString(dstSchema.getDocument())) ;
> }
>
>
> Ed Merks pisze:
>> Jacek,
>>
>> Comments below.
>>
>> Jacek Pospychala wrote:
>>> Ed,
>>> I don't get this in Javadoc: "A component can be cloned directly or
>>> by cloning the underlying DOM. By cloning the DOM you ensure that
>>> &lt;annotation>s, non-schema namespace attributes, and formatting
>>> are preserved."
>>>
>>> When I clone just the DOM, how is component recreated? based on DOM?
>> It seems like the code snippet below shows that. I.e., create a
>> component of the appropriate type and set its element...
>>>
>>> I'm copying between schemas, so I use importNode instead of cloneNode.
>>>
>>> When I do XSDFactory.eINSTANCE.create(xsdConcreteComponent.eClass())
>>> and importNode, all the component attributes (e.g getAnnotation())
>>> are Null.
>> It's best you show exactly the code. Maybe you've missed a step...
>>>
>>> When I do cloneConcreteComponent(true,true) and deep importNode,
>>> then DOM and component model seem to be correct, but they're not
>>> synchronized.
>> Sharing the DOM is use within the model for things like chameleon
>> namespace includes.
>>> So if I change a value in component model and then print XSD
>>> contents, then I still see old DOM.
>> Yes it's intended as a read-only view and is not likely to be useful
>> outside of the framework.
>>>
>>>
>>>
>>>
>>> Ed Merks pisze:
>>>> Shobana,
>>>>
>>>> You're sure? Did you look at the implementation of that
>>>> method? Element element = xsdConcreteComponent./*{@link
>>>> XSDConcreteComponent#getElement() /*}*/getElement();
>>>> if (element != null)
>>>> {
>>>> // Clone the DOM using the DOM API, and create the same type
>>>> of component to hold it.
>>>> //
>>>> Element clonedElement = (Element)element.cloneNode(true);
>>>> XSDConcreteComponent result =
>>>> (XSDConcreteComponent)XSDFactory.eINSTANCE.create(xsdConcret eComponent.eClass());
>>>>
>>>> result.setElement(clonedElement);
>>>> return result;
>>>> }
Re: How to clone annotations [message #77288 is a reply to message #77255] Thu, 19 February 2009 15:34 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26058
Registered: July 2009
Senior Member
Jacek,

Yes, manipulating the DOM directly does the trick because components
will automatically be created, but the other way should work too (at
least the one I showed in the other note).


Jacek Pospychala wrote:
> Haha! Found! :-)
>
> I have to care only about DOM model and XSD model will be built on top
> of that automatically = let dstSchema create the XSDTypeDefinition
> instead of creating it by hand.
>
> So do copy as following:
>
> XSDTypeDefinition srcType = srcSchema.getTypeDefinitions().get(0);
> Element dstElement = (Element)
> dstSchema.getDocument().importNode(srcType.getElement(), true);
> dstSchema.getElement().appendChild(dstElement);
>
>
> Jacek Pospychala pisze:
>> Ed,
>> below is my sample code.
>>
>> What I'm trying to do, is copy an element from one XSD to another
>> without loosing any information, whether it's kept in DOM or XSD
>> model itself and then normally work on changed XSD.
>>
>> Possible?
>>
>> btw. I discovered the XSDPrototypicalSchema class. It's very helpful :-)
> [...]
>>
>> Ed Merks pisze:
>>> Jacek,
>>>
>>> Comments below.
>>>
>>> Jacek Pospychala wrote:
>>>> Ed,
>>>> I don't get this in Javadoc: "A component can be cloned directly or
>>>> by cloning the underlying DOM. By cloning the DOM you ensure that
>>>> &lt;annotation>s, non-schema namespace attributes, and formatting
>>>> are preserved."
>>>>
>>>> When I clone just the DOM, how is component recreated? based on DOM?
>>> It seems like the code snippet below shows that. I.e., create a
>>> component of the appropriate type and set its element...
>>>>
>>>> I'm copying between schemas, so I use importNode instead of cloneNode.
>>>>
>>>> When I do
>>>> XSDFactory.eINSTANCE.create(xsdConcreteComponent.eClass()) and
>>>> importNode, all the component attributes (e.g getAnnotation()) are
>>>> Null.
>>> It's best you show exactly the code. Maybe you've missed a step...
>>>>
>>>> When I do cloneConcreteComponent(true,true) and deep importNode,
>>>> then DOM and component model seem to be correct, but they're not
>>>> synchronized.
>>> Sharing the DOM is use within the model for things like chameleon
>>> namespace includes.
>>>> So if I change a value in component model and then print XSD
>>>> contents, then I still see old DOM.
>>> Yes it's intended as a read-only view and is not likely to be useful
>>> outside of the framework.
>>>>
>>>>
>>>>
>>>>
>>>> Ed Merks pisze:
>>>>> Shobana,
>>>>>
>>>>> You're sure? Did you look at the implementation of that
>>>>> method? Element element = xsdConcreteComponent./*{@link
>>>>> XSDConcreteComponent#getElement() /*}*/getElement();
>>>>> if (element != null)
>>>>> {
>>>>> // Clone the DOM using the DOM API, and create the same
>>>>> type of component to hold it.
>>>>> //
>>>>> Element clonedElement = (Element)element.cloneNode(true);
>>>>> XSDConcreteComponent result =
>>>>> (XSDConcreteComponent)XSDFactory.eINSTANCE.create(xsdConcret eComponent.eClass());
>>>>>
>>>>> result.setElement(clonedElement);
>>>>> return result;
>>>>> }
Re: How to clone annotations [message #77304 is a reply to message #77271] Thu, 19 February 2009 16:42 Go to previous messageGo to next message
Eclipse User
Originally posted by: jacek.pospychala.pl.ibm.com

Ed,
Thanks for looking at this.
I have created https://bugs.eclipse.org/bugs/show_bug.cgi?id=265485

This synchronization with DOM stuff is really cool.


Ed Merks pisze:
[...]
> I think this patch is needed. Please open a bugzilla.
>
> ### Eclipse Workspace Patch 1.0
> #P org.eclipse.xsd
> Index: src/org/eclipse/xsd/impl/XSDConcreteComponentImpl.java
> ============================================================ =======
> RCS file:
> /cvsroot/modeling/org.eclipse.mdt/org.eclipse.xsd/plugins/or g.eclipse.xsd/src/org/eclipse/xsd/impl/XSDConcreteComponentI mpl.java,v
>
> retrieving revision 1.29
> diff -u -r1.29 XSDConcreteComponentImpl.java
> --- src/org/eclipse/xsd/impl/XSDConcreteComponentImpl.java 20 Jan
> 2009 17:02:57 -0000 1.29
> +++ src/org/eclipse/xsd/impl/XSDConcreteComponentImpl.java 19 Feb
> 2009 15:31:19 -0000
> @@ -1361,11 +1361,13 @@
> }
> }
>
> - if (childElement == null)
> - {
> - ((XSDConcreteComponentImpl)xsdConcreteComponent).isReconcili ng =
> true;
> - childElement =
> ((XSDConcreteComponentImpl)xsdConcreteComponent).createEleme nt();
> - ((XSDConcreteComponentImpl)xsdConcreteComponent).isReconcili ng =
> false;
> + XSDConcreteComponentImpl xsdConcreteComponentImpl =
> (XSDConcreteComponentImpl)xsdConcreteComponent;
> + boolean newChild = childElement == null;
> + if (newChild)
> + {
> + xsdConcreteComponentImpl.isReconciling = true;
> + childElement = xsdConcreteComponentImpl.createElement();
> + xsdConcreteComponentImpl.isReconciling = false;
> if (childElement == null)
> {
> System.out.println("not created! " + xsdConcreteComponent);
> @@ -1418,6 +1420,12 @@
> }
>
> niceInsertBefore(adoptionParent, childElement, referencedElement);
> + if (!newChild)
> + {
> + xsdConcreteComponentImpl.isReconciling = true;
> + xsdConcreteComponentImpl.reconcile(childElement);
> + xsdConcreteComponentImpl.isReconciling = false;
> + }
> }
> else
> {
>
>> dstSchema.updateElement();
>> assertEquals("Some text",
>> dstType.getAnnotation().getUserInformation().get(0).getTextC ontent());
>>
>> System.out.println(convertToString(dstSchema.getDocument())) ;
>> }
>>
>>
Re: How to clone annotations [message #77335 is a reply to message #77304] Fri, 20 February 2009 10:08 Go to previous messageGo to next message
Bhuvan Mehta is currently offline Bhuvan Mehta
Messages: 38
Registered: July 2009
Member
Hello Ed,

But this still does not help in case where source and target schema uses
the different prefixes, and in this case the components in target schema
are updated in very pathetic way, below is the explanation :-

I am using the below code snippet for this :-
targetSchema.getContents().remove(oldComponent);
Element dstElement = (Element)
targetSchema.getDocument().importNode(srcComponent.getElemen t(), true);
targetSchema.getElement().appendChild(dstElement);


target schema intial state (this uses "xsd" as prefix) :
<xsd:element name="ele1" type="xsd:boolean"/>

now i try to replace the element in target schema from source schema which
has the content of element as below :
<x:element name="ele1" type="x:boolean">
<x:annotation>
<x:documentation>
Test Doc.
</x:documentation>
</x:annotation>
</x:element>

now in target schema the contents are updated as below which makes the
complete schema InValid, (and updation is completely wrong)

<xsd:schema xmlns:Q1="x" targetNamespace="http://www.example.org/test1/">
<x:element name="ele1" type="Q1:boolean"
xmlns:x="http://www.w3.org/2001/XMLSchema">
<x:annotation>
<x:documentation>
Test Doc.
</x:documentation>
</x:annotation>
</x:element>
</xsd:schema>

Am i doing something wrong ?? Please suggest me how to update components
from one schema to another schema when both schemas use different prefix
to define type attribute.

--
Thanks and Regards,
Bhuvan Mehta
Re: How to clone annotations [message #77349 is a reply to message #77335] Fri, 20 February 2009 12:01 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26058
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------030102020108060009070902
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Bhuvan,

Comments below.

Bhuvan wrote:
> Hello Ed,
>
> But this still does not help in case where source and target schema
> uses the different prefixes, and in this case the components in target
> schema are updated in very pathetic way, below is the explanation :-
It's DOM, of course it's going to be pathetic. :-P
>
> I am using the below code snippet for this :-
> targetSchema.getContents().remove(oldComponent);
How are oldComponent and srcComponent related?
> Element dstElement = (Element)
> targetSchema.getDocument().importNode(srcComponent.getElemen t(), true);
At this point, you can do arbitrary processing on the DOM to control
what prefixes are used...
> targetSchema.getElement().appendChild(dstElement);
>
>
> target schema intial state (this uses "xsd" as prefix) :
> <xsd:element name="ele1" type="xsd:boolean"/>
>
> now i try to replace the element in target schema from source schema
> which has the content of element as below :
> <x:element name="ele1" type="x:boolean">
> <x:annotation>
> <x:documentation>
> Test Doc.
> </x:documentation>
> </x:annotation>
> </x:element>
>
> now in target schema the contents are updated as below which makes the
> complete schema InValid, (and updation is completely wrong)
>
> <xsd:schema xmlns:Q1="x" targetNamespace="http://www.example.org/test1/">
> <x:element name="ele1" type="Q1:boolean"
> xmlns:x="http://www.w3.org/2001/XMLSchema">
> <x:annotation>
> <x:documentation>
> Test Doc.
> </x:documentation>
> </x:annotation>
> </x:element>
> </xsd:schema>
>
> Am i doing something wrong ??
You'll note that Jacek provided a complete running example and that as a
result I was able to help him with his explicit issues. In what you're
showing, there seems to be some confusion over whether "x" is a prefix
or a namespace. But all you've done apparently is purely DOM manipulation:

targetSchema.getElement().appendChild(dstElement);

So if things are going wrong at this level, that's not something I can
do something about. I suspect there's more going on than you've shown.
> Please suggest me how to update components from one schema to another
> schema when both schemas use different prefix to define type attribute.
I'm not sure the best way. There's an updatePrefix method in the
implementation classes, but other than
XSDSchema.setSchemaForSchemaQNamePrefix there's no public access to it.
So perhaps you can put the thing into a schema element that's using the
same prefix and then use setSchemaForSchemaQNamePrefix to update it to
what you actually want in the target, and the import that version into
the target... Maybe you'll need to call updateElement. Consider
providing a self contained sample like Jacek did if you need more help.
>
> --
> Thanks and Regards,
> Bhuvan Mehta
>

--------------030102020108060009070902
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Bhuvan,<br>
<br>
Comments below.<br>
<br>
Bhuvan wrote:
<blockquote
cite="mid:1fc733db5020e50424aa83ff9b09b3d5$1@www.eclipse.org"
type="cite">Hello Ed,
<br>
<br>
But this still does not help in case where source and target schema
uses the different prefixes, and in this case the components in target
schema are updated in very pathetic way, below is the explanation :-
<br>
</blockquote>
It's DOM, of course it's going to be pathetic. :-P<br>
<blockquote
cite="mid:1fc733db5020e50424aa83ff9b09b3d5$1@www.eclipse.org"
type="cite"><br>
I am using the below code snippet for this :-
<br>
targetSchema.getContents().remove(oldComponent);
<br>
</blockquote>
How are oldComponent and srcComponent related?<br>
<blockquote
cite="mid:1fc733db5020e50424aa83ff9b09b3d5$1@www.eclipse.org"
type="cite">Element dstElement = (Element)
targetSchema.getDocument().importNode(srcComponent.getElemen t(), true);
<br>
</blockquote>
At this point, you can do arbitrary processing on the DOM to control
what prefixes are used...<br>
<blockquote
cite="mid:1fc733db5020e50424aa83ff9b09b3d5$1@www.eclipse.org"
type="cite">targetSchema.getElement().appendChild(dstElement);
<br>
<br>
<br>
target schema intial state (this uses "xsd" as prefix) :
<br>
&lt;xsd:element name="ele1" type="xsd:boolean"/&gt;
<br>
<br>
now i try to replace the element in target schema from source schema
which has the content of element as below :
<br>
&lt;x:element name="ele1" type="x:boolean"&gt;
<br>
Re: How to clone annotations [message #77381 is a reply to message #77029] Wed, 25 February 2009 09:35 Go to previous messageGo to next message
rollo is currently offline rollo
Messages: 117
Registered: July 2009
Senior Member
Is this the right way to clone Annotations and use them in a different
schema?

newSchema.getContents().add( newTypeDef );
newSchema.updateElement();

XSDAnnotation oldAnnotation = oldTypeDef.getAnnotation();

XSDAnnotation newAnnotation = FACTORY.createXSDAnnotation();

Element newElement = (Element)
newTypeDef.getSchema().getDocument().importNode(
oldAnnotation.getElement(), true );

newAnnotation.setElement( newElement );

newTypeDef.setAnnotation( newAnnotation );

Or did I forget something?

Roland


Ed Merks schrieb:
> Roland,
>
> Comments below.
>
> Roland Siebert wrote:
>> Hi,
>>
>> I also want to clone an existing annotation and move it to another
>> schema, but I didn't get it working.
>>
>> For example for this schema "schema.xsd" in the attachment.
>>
>> In the moment I do it like this:
>>
>> I read the content of the old schema. Create for example a new
>> Simpletype. I put the annotation of the old Simpletype in a map.
>> At the end I add all new Types to the new schema.
>>
>> normalizedSchema.getContents().add( type );
>>
>> if ( annotations.containsKey( type.getName() ) ) {
>> XSDAnnotation annotation = annotations.get( type.getName() );
>> type.setAnnotation( annotation );
> So here you are working with the existing annotation and moving it across.
>> schema.getDocument().importNode( annotation.getElement(), true );
> Now you clone its element but do nothing with it so it's garbage collected.
>> }
>>
>> But it did not work, like you can see in schema2.xsd.
>>
>> Can somebody help me?
> How about trying what I've already shown to clone the annotations
> Element and using that as the element of a newly created annotation?
>>
>> Roland
Re: How to clone annotations [message #77394 is a reply to message #77349] Wed, 25 February 2009 12:22 Go to previous messageGo to next message
Bhuvan Mehta is currently offline Bhuvan Mehta
Messages: 38
Registered: July 2009
Member
Hello Ed,
Thanks a lot for reply, the suggestion worked and it puts the prefix in
such a way that "atleast" Schemas remains valid, but still we have one
strange problem that when i validate the EMF model of schema after making
the changes then it is said InValid :(, when i save changes to file system
then validate the schema then it is said valid.
I have prepared the below example which explains the usecase :-
------------------------------------------------------------ --
TestContentCopy.java
------------------------------------------------------------ --
package test.schema;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.xsd.XSDDiagnostic;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.util.XSDResourceImpl;
import org.w3c.dom.Node;

public class TestContentCopy implements IObjectActionDelegate {

public void setActivePart(IAction action, IWorkbenchPart targetPart) {
}

public void run(IAction action) {

String sourceLoc = "C:\\test1.xsd";
String targetLoc = "C:\\test2.xsd";

File file1 = new File(sourceLoc);
File file2 = new File(targetLoc);

final ResourceSet resourceSet = new ResourceSetImpl();
final Resource res1 =
resourceSet.getResource(URI.createURI(file1.toURI().toString ()), true);
final Resource res2 =
resourceSet.getResource(URI.createURI(file2.toURI().toString ()), true);

XSDSchema sourceSchema = ((XSDResourceImpl)res1).getSchema();
XSDSchema targetSchema = ((XSDResourceImpl)res2).getSchema();

XSDElementDeclaration sourceElement =
sourceSchema.getElementDeclarations().get(0);
XSDElementDeclaration targetElement =
targetSchema.getElementDeclarations().get(0);

String prefix = targetSchema.getSchemaForSchemaQNamePrefix();
Node newComponent =
targetSchema.getDocument().importNode(sourceElement.getEleme nt(),
true);
targetSchema.getElement().replaceChild(newComponent,
targetElement.getElement());

if(prefix!=null)
targetSchema.setSchemaForSchemaQNamePrefix(prefix);

targetSchema.validate();
final Collection<XSDDiagnostic> diag = targetSchema.getAllDiagnostics();
if(diag != null && !diag.isEmpty()) {
for (final XSDDiagnostic dia: diag){
System.out.println(dia.getMessage());
}
}
Resource resource = targetSchema.eResource();
try {
resource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
e.printStackTrace();
}
}

public void selectionChanged(IAction action, ISelection selection) {
}

}

------------------------------------------------------------ --
Test1.xsd
------------------------------------------------------------ --
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<x2:schema xmlns:tns="http://www.example.org/test1"
xmlns:x2="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.example.org/test1">


<x2:element name="ele1" type="x2:string">
<x2:annotation>
<x2:documentation>
Test.
</x2:documentation>
</x2:annotation>
</x2:element>
</x2:schema>

------------------------------------------------------------ --
Test2.xsd (Intial State)
------------------------------------------------------------ --
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<x1:schema xmlns:tns="http://www.example.org/test1"
elementFormDefault="qualified"
targetNamespace="http://www.example.org/test1"
xmlns:x1="http://www.w3.org/2001/XMLSchema">


<x1:element name="ele1" type="x1:int">
</x1:element>

</x1:schema>

------------------------------------------------------------ --
Test2.xsd (State After Executing Code)
------------------------------------------------------------ --
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<x1:schema xmlns:tns="http://www.example.org/test1"
xmlns:x1="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://www.example.org/test1">


<x2:element name="ele1" type="x2:string"
xmlns:x2="http://www.w3.org/2001/XMLSchema">
<x2:annotation>
<x2:documentation>
Test.
</x2:documentation>
</x2:annotation>
</x2:element>

</x1:schema>
Re: How to clone annotations [message #77408 is a reply to message #77381] Wed, 25 February 2009 15:18 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26058
Registered: July 2009
Senior Member
Roland,

Comments below.

Roland Siebert wrote:
> Is this the right way to clone Annotations and use them in a different
> schema?
>
> newSchema.getContents().add( newTypeDef );
So you cloned this one? Which of course loses the details on the
annotation...
> newSchema.updateElement();
>
> XSDAnnotation oldAnnotation = oldTypeDef.getAnnotation();
>
> XSDAnnotation newAnnotation = FACTORY.createXSDAnnotation();
>
> Element newElement = (Element)
> newTypeDef.getSchema().getDocument().importNode(
> oldAnnotation.getElement(), true );
>
> newAnnotation.setElement( newElement );
>
> newTypeDef.setAnnotation( newAnnotation );
And now you clone just the annotation and replace the blank one with that.
>
> Or did I forget something?
It sounds like a reasonable approach...
>
> Roland
>
>
> Ed Merks schrieb:
>> Roland,
>>
>> Comments below.
>>
>> Roland Siebert wrote:
>>> Hi,
>>>
>>> I also want to clone an existing annotation and move it to another
>>> schema, but I didn't get it working.
>>>
>>> For example for this schema "schema.xsd" in the attachment.
>>>
>>> In the moment I do it like this:
>>>
>>> I read the content of the old schema. Create for example a new
>>> Simpletype. I put the annotation of the old Simpletype in a map.
>>> At the end I add all new Types to the new schema.
>>>
>>> normalizedSchema.getContents().add( type );
>>>
>>> if ( annotations.containsKey( type.getName() ) ) {
>>> XSDAnnotation annotation = annotations.get( type.getName() );
>>> type.setAnnotation( annotation );
>> So here you are working with the existing annotation and moving it
>> across.
>>> schema.getDocument().importNode( annotation.getElement(), true );
>> Now you clone its element but do nothing with it so it's garbage
>> collected.
>>> }
>>>
>>> But it did not work, like you can see in schema2.xsd.
>>>
>>> Can somebody help me?
>> How about trying what I've already shown to clone the annotations
>> Element and using that as the element of a newly created annotation?
>>>
>>> Roland
Re: How to clone annotations [message #77451 is a reply to message #77394] Wed, 25 February 2009 16:04 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26058
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------080607030301050308040101
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Bhuvan,

It's very strange. The imported node is in a weird state. Although an
xmlns:x2 is serialized, if you query the DOM itself, there's no such
attribute present.


It seems as if serializing auto corrects the ill-formed DOM but that
doesn't do much good when analyzing it... I did some Javadoc hunting
and found Document.normalizeDocument() and tested that adding this
solves the problem:

targetSchema.getDocument().normalizeDocument();

Jeesh...


Bhuvan wrote:
> Hello Ed,
> Thanks a lot for reply, the suggestion worked and it puts the prefix
> in such a way that "atleast" Schemas remains valid, but still we have
> one strange problem that when i validate the EMF model of schema after
> making the changes then it is said InValid :(, when i save changes to
> file system then validate the schema then it is said valid.
> I have prepared the below example which explains the usecase :-
> ------------------------------------------------------------ --
> TestContentCopy.java
> ------------------------------------------------------------ --
> package test.schema;
>
> import java.io.File;
> import java.io.IOException;
> import java.util.Collection;
> import java.util.Collections;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.jface.action.IAction;
> import org.eclipse.jface.viewers.ISelection;
> import org.eclipse.ui.IObjectActionDelegate;
> import org.eclipse.ui.IWorkbenchPart;
> import org.eclipse.xsd.XSDDiagnostic;
> import org.eclipse.xsd.XSDElementDeclaration;
> import org.eclipse.xsd.XSDSchema;
> import org.eclipse.xsd.util.XSDResourceImpl;
> import org.w3c.dom.Node;
>
> public class TestContentCopy implements IObjectActionDelegate {
>
> public void setActivePart(IAction action, IWorkbenchPart
> targetPart) {
> }
>
> public void run(IAction action) {
>
> String sourceLoc = "C:\\test1.xsd";
> String targetLoc = "C:\\test2.xsd";
>
> File file1 = new File(sourceLoc);
> File file2 = new File(targetLoc);
>
> final ResourceSet resourceSet = new ResourceSetImpl();
> final Resource res1 =
> resourceSet.getResource(URI.createURI(file1.toURI().toString ()), true);
> final Resource res2 =
> resourceSet.getResource(URI.createURI(file2.toURI().toString ()), true);
>
> XSDSchema sourceSchema = ((XSDResourceImpl)res1).getSchema();
> XSDSchema targetSchema = ((XSDResourceImpl)res2).getSchema();
>
> XSDElementDeclaration sourceElement =
> sourceSchema.getElementDeclarations().get(0);
> XSDElementDeclaration targetElement =
> targetSchema.getElementDeclarations().get(0);
>
> String prefix = targetSchema.getSchemaForSchemaQNamePrefix();
> Node newComponent =
> targetSchema.getDocument().importNode(sourceElement.getEleme nt(),
> true);
> targetSchema.getElement().replaceChild(newComponent,
> targetElement.getElement());
>
> if(prefix!=null)
> targetSchema.setSchemaForSchemaQNamePrefix(prefix);
>
> targetSchema.validate();
> final Collection<XSDDiagnostic> diag =
> targetSchema.getAllDiagnostics();
> if(diag != null && !diag.isEmpty()) {
> for (final XSDDiagnostic dia: diag){
> System.out.println(dia.getMessage());
> }
> }
> Resource resource = targetSchema.eResource();
> try {
> resource.save(Collections.EMPTY_MAP);
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
>
> public void selectionChanged(IAction action, ISelection selection) {
> }
>
> }
>
> ------------------------------------------------------------ --
> Test1.xsd
> ------------------------------------------------------------ --
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <x2:schema xmlns:tns="http://www.example.org/test1"
> xmlns:x2="http://www.w3.org/2001/XMLSchema"
> elementFormDefault="qualified"
> targetNamespace="http://www.example.org/test1">
>
>
> <x2:element name="ele1" type="x2:string">
> <x2:annotation>
> <x2:documentation>
> Test.
> </x2:documentation>
> </x2:annotation>
> </x2:element>
> </x2:schema>
>
> ------------------------------------------------------------ --
> Test2.xsd (Intial State)
> ------------------------------------------------------------ --
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <x1:schema xmlns:tns="http://www.example.org/test1"
> elementFormDefault="qualified"
> targetNamespace="http://www.example.org/test1"
> xmlns:x1="http://www.w3.org/2001/XMLSchema">
>
>
> <x1:element name="ele1" type="x1:int">
> </x1:element>
>
> </x1:schema>
>
> ------------------------------------------------------------ --
> Test2.xsd (State After Executing Code)
> ------------------------------------------------------------ --
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <x1:schema xmlns:tns="http://www.example.org/test1"
> xmlns:x1="http://www.w3.org/2001/XMLSchema"
> elementFormDefault="qualified"
> targetNamespace="http://www.example.org/test1">
>
>
> <x2:element name="ele1" type="x2:string"
> xmlns:x2="http://www.w3.org/2001/XMLSchema">
> <x2:annotation>
> <x2:documentation>
> Test.
> </x2:documentation>
> </x2:annotation>
> </x2:element>
>
> </x1:schema>
>
>
>

--------------080607030301050308040101
Content-Type: multipart/related;
boundary="------------020201090004060201020000"


--------------020201090004060201020000
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Bhuvan,<br>
<br>
It's very strange.
Re: How to clone annotations [message #77466 is a reply to message #77451] Thu, 26 February 2009 07:13 Go to previous messageGo to next message
Bhuvan Mehta is currently offline Bhuvan Mehta
Messages: 38
Registered: July 2009
Member
Thanks a lot , Ed, it really helped a lot.

--
Thanks and Best Regards,
Bhuvan Mehta
Re: How to clone annotations [message #77481 is a reply to message #77408] Thu, 26 February 2009 18:23 Go to previous messageGo to next message
rollo is currently offline rollo
Messages: 117
Registered: July 2009
Senior Member
I generate a new TypeDefinition, because I have to change several parts
of the old one. I only want to add the Annotations of the old
TypeDefinition to the new one.

See also comments below.


>> newSchema.getContents().add( newTypeDef );
> So you cloned this one? Which of course loses the details on the
> annotation...
"newTypeDef" is a new generated TypeDefinion.

>> newSchema.updateElement();
>>
>> XSDAnnotation oldAnnotation = oldTypeDef.getAnnotation();
And this (oldTypeDef) is the old original TypeDefinition.
>>
>> XSDAnnotation newAnnotation = FACTORY.createXSDAnnotation();
>>
>> Element newElement = (Element)
>> newTypeDef.getSchema().getDocument().importNode(
>> oldAnnotation.getElement(), true );
>>
>> newAnnotation.setElement( newElement );
>>
>> newTypeDef.setAnnotation( newAnnotation );
> And now you clone just the annotation and replace the blank one with that.
>>
>> Or did I forget something?
> It sounds like a reasonable approach...
Re: How to clone annotations [message #77583 is a reply to message #77451] Wed, 15 April 2009 06:49 Go to previous messageGo to next message
Bhuvan Mehta is currently offline Bhuvan Mehta
Messages: 38
Registered: July 2009
Member
Hello Ed,

targetSchema.getDocument().normalizeDocument();
Only helpes till the structure of element is simple, as structure get more
deeper/complicated, let's say element is like below in first schema:
------------------------------------------------------------ -----
<p1:element name="ele1">
<p1:complexType>
<p1:sequence>
<p1:element name="inner1" type="p1:date"/>
</p1:sequence>
</p1:complexType>
</p1:element>
------------------------------------------------------------ -----
and then we modify type "of inner1" from other schema the validation again
fails with :
"Invalid XML schema for namespace 'http://www.example.org/1'. Diagnostics:
XSD: Type reference 'p1#date' is unresolved"

Is there any way to normalize the document which make document valid for
xsd schema validator, irrespective to the depth of components inside the
schema ??

--
Thanks and Regards,
Bhuvan Mehta
Re: How to clone annotations [message #77599 is a reply to message #77583] Wed, 15 April 2009 10:51 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26058
Registered: July 2009
Senior Member
Bhuvan,

Comments below.

Bhuvan wrote:
> Hello Ed,
>
> targetSchema.getDocument().normalizeDocument();
> Only helpes till the structure of element is simple, as structure get
> more deeper/complicated, let's say element is like below in first schema:
> ------------------------------------------------------------ -----
> <p1:element name="ele1">
> <p1:complexType>
> <p1:sequence>
> <p1:element name="inner1" type="p1:date"/>
> </p1:sequence>
> </p1:complexType>
> </p1:element>
> ------------------------------------------------------------ -----
> and then we modify type "of inner1" from other schema the validation
> again fails with :
> "Invalid XML schema for namespace 'http://www.example.org/1'.
> Diagnostics:
> XSD: Type reference 'p1#date' is unresolved"
It's hard to comment about things I cannot reproduce.
>
> Is there any way to normalize the document which make document valid
> for xsd schema validator, irrespective to the depth of components
> inside the schema ??
I'm really not sure how depth is related to the issue. The normalize
method should normalize the full depth.
>
>
> --
> Thanks and Regards,
> Bhuvan Mehta
>
Re: How to clone annotations [message #77615 is a reply to message #77599] Thu, 16 April 2009 04:52 Go to previous messageGo to next message
Bhuvan Mehta is currently offline Bhuvan Mehta
Messages: 38
Registered: July 2009
Member
Hello Ed,
Same was the wondering part for me as well that when we normalize the
docuemnt then it should normalize the complete document irrespective of
depth of the components but it actually behave differently in different
cases.

Actually the same code snippet can be used to reproduce the scenario. It
goes as below :

------------------------------------------------------------ --
TestContentCopy.java
------------------------------------------------------------ --
package test.schema;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.xsd.XSDDiagnostic;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.util.XSDResourceImpl;
import org.w3c.dom.Node;

public class TestContentCopy implements IObjectActionDelegate {

public void setActivePart(IAction action, IWorkbenchPart targetPart) {
}

public void run(IAction action) {

String sourceLoc = "C:\\test1.xsd";
String targetLoc = "C:\\test2.xsd";

File file1 = new File(sourceLoc);
File file2 = new File(targetLoc);

final ResourceSet resourceSet = new ResourceSetImpl();
final Resource res1 =
resourceSet.getResource(URI.createURI(file1.toURI().toString ()), true);
final Resource res2 =
resourceSet.getResource(URI.createURI(file2.toURI().toString ()), true);

XSDSchema sourceSchema = ((XSDResourceImpl)res1).getSchema();
XSDSchema targetSchema = ((XSDResourceImpl)res2).getSchema();

XSDElementDeclaration srcComponent=
sourceSchema.getElementDeclarations().get(0);
XSDElementDeclaration oldComponent=
targetSchema.getElementDeclarations().get(0);

Element element = srcComponent.getElement();
if (element != null) {
final Node newComponent = targetSchema.getDocument().
importNode(srcComponent.getElement(),
true);
targetSchema.getElement().
replaceChild(newComponent, oldComponent.getElement());
targetSchema.getDocument().normalizeDocument();
}


targetSchema.validate();
final Collection<XSDDiagnostic> diag = targetSchema.getAllDiagnostics();
if(diag != null && !diag.isEmpty()) {
for (final XSDDiagnostic dia: diag){
System.out.println(dia.getMessage());
}
}
Resource resource = targetSchema.eResource();
try {
resource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
e.printStackTrace();
}
}

public void selectionChanged(IAction action, ISelection selection) {
}

}

------------------------------------------------------------ --
Test1.xsd
------------------------------------------------------------ --
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<x2:schema xmlns:tns="http://www.example.org/test1"
xmlns:x2="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://www.example.org/test1">


<x2:element name="ele1">
<x2:complexType>
<x2:sequence>
<x2:element name="inner1" type="x2:date"/>
</x2:sequence>
</x2:complexType>
</x2:element>

------------------------------------------------------------ --
Test2.xsd
------------------------------------------------------------ --
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<x1:schema xmlns:tns="http://www.example.org/test1"
elementFormDefault="qualified"
targetNamespace="http://www.example.org/test1"
xmlns:x1="http://www.w3.org/2001/XMLSchema">


<x1:element name="ele1">
<x1:complexType>
<x1:sequence>
<x1:element name="inner1" type="x1:string"/>
</x1:sequence>
</x1:complexType>
</x1:element>
------------------------------------------------------------ --

So here i want to copy element ele1 from test2.xsd to test1.xsd but when i
validate the XSDSchema after copying then validation fails with below
message.

"Invalid XML schema for namespace 'http://www.example.org/1'. Diagnostics:
XSD: Type reference 'p1#date' is unresolved".

--
Thanks and Regards,
Bhuvan Mehta
Re: How to clone annotations [message #77623 is a reply to message #77615] Thu, 16 April 2009 14:55 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 26058
Registered: July 2009
Senior Member
Bhuvan,

You can make use targetSchema.update() after calling normalizeDocument
to force QNames to resolve again.


Bhuvan wrote:
> Hello Ed,
> Same was the wondering part for me as well that when we normalize the
> docuemnt then it should normalize the complete document irrespective
> of depth of the components but it actually behave differently in
> different cases.
>
> Actually the same code snippet can be used to reproduce the scenario.
> It goes as below :
> ------------------------------------------------------------ --
> TestContentCopy.java
> ------------------------------------------------------------ --
> package test.schema;
>
> import java.io.File;
> import java.io.IOException;
> import java.util.Collection;
> import java.util.Collections;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.jface.action.IAction;
> import org.eclipse.jface.viewers.ISelection;
> import org.eclipse.ui.IObjectActionDelegate;
> import org.eclipse.ui.IWorkbenchPart;
> import org.eclipse.xsd.XSDDiagnostic;
> import org.eclipse.xsd.XSDElementDeclaration;
> import org.eclipse.xsd.XSDSchema;
> import org.eclipse.xsd.util.XSDResourceImpl;
> import org.w3c.dom.Node;
>
> public class TestContentCopy implements IObjectActionDelegate {
>
> public void setActivePart(IAction action, IWorkbenchPart targetPart) {
> }
>
> public void run(IAction action) {
>
> String sourceLoc = "C:\\test1.xsd";
> String targetLoc = "C:\\test2.xsd";
>
> File file1 = new File(sourceLoc);
> File file2 = new File(targetLoc);
>
> final ResourceSet resourceSet = new ResourceSetImpl();
> final Resource res1 =
> resourceSet.getResource(URI.createURI(file1.toURI().toString ()), true);
> final Resource res2 =
> resourceSet.getResource(URI.createURI(file2.toURI().toString ()), true);
>
> XSDSchema sourceSchema = ((XSDResourceImpl)res1).getSchema();
> XSDSchema targetSchema = ((XSDResourceImpl)res2).getSchema();
>
> XSDElementDeclaration srcComponent=
> sourceSchema.getElementDeclarations().get(0);
> XSDElementDeclaration oldComponent=
> targetSchema.getElementDeclarations().get(0);
>
> Element element = srcComponent.getElement();
> if (element != null) {
> final Node newComponent = targetSchema.getDocument().
> importNode(srcComponent.getElement(),
> true);
> targetSchema.getElement().
> replaceChild(newComponent, oldComponent.getElement());
> targetSchema.getDocument().normalizeDocument();
> }
>
> targetSchema.validate();
> final Collection<XSDDiagnostic> diag = targetSchema.getAllDiagnostics();
> if(diag != null && !diag.isEmpty()) {
> for (final XSDDiagnostic dia: diag){
> System.out.println(dia.getMessage());
> }
> }
> Resource resource = targetSchema.eResource();
> try {
> resource.save(Collections.EMPTY_MAP);
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
>
> public void selectionChanged(IAction action, ISelection selection) {
> }
>
> }
>
> ------------------------------------------------------------ --
> Test1.xsd
> ------------------------------------------------------------ --
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <x2:schema xmlns:tns="http://www.example.org/test1"
> xmlns:x2="http://www.w3.org/2001/XMLSchema"
> elementFormDefault="qualified"
> targetNamespace="http://www.example.org/test1">
>
>
> <x2:element name="ele1">
> <x2:complexType>
> <x2:sequence>
> <x2:element name="inner1" type="x2:date"/>
> </x2:sequence>
> </x2:complexType>
> </x2:element>
>
> ------------------------------------------------------------ --
> Test2.xsd ------------------------------------------------------------ --
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <x1:schema xmlns:tns="http://www.example.org/test1"
> elementFormDefault="qualified"
> targetNamespace="http://www.example.org/test1"
> xmlns:x1="http://www.w3.org/2001/XMLSchema">
>
>
> <x1:element name="ele1">
> <x1:complexType>
> <x1:sequence>
> <x1:element name="inner1" type="x1:string"/>
> </x1:sequence>
> </x1:complexType>
> </x1:element>
> ------------------------------------------------------------ --
>
> So here i want to copy element ele1 from test2.xsd to test1.xsd but
> when i validate the XSDSchema after copying then validation fails with
> below message.
>
> "Invalid XML schema for namespace 'http://www.example.org/1'.
> Diagnostics:
> XSD: Type reference 'p1#date' is unresolved".
>
> --
> Thanks and Regards,
> Bhuvan Mehta
>
Re: How to clone annotations [message #603537 is a reply to message #76536] Tue, 23 December 2008 12:59 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 26058
Registered: July 2009
Senior Member
Shobana,

Did you try what's in XSDPrototypicalSchema.cloneComponent with
preserveDOM true? I.e., clone the DOM and create a new component to
wrap that DOM...


shobana wrote:
> Hi,
>
> I need to copy a xsd component from a schema to another schema. I have
> done it by using cloneConcreteComponent() method. But annotations of
> the component is not getting cloned if I use this method.
> I tried cloneNode(), even that didnt work.
>
> Could any of you please help me in this.
>
> Thanks in advance.
>
> Shobana
>
>
>
Re: How to clone annotations [message #603553 is a reply to message #76552] Wed, 24 December 2008 06:45 Go to previous message
shobana is currently offline shobana
Messages: 13
Registered: July 2009
Junior Member
Hi ed,

Thanks for quick reply.

Yes I tried that too. But it just returns the empty tag of the xsd
component.
Is there any other way to copy the xsd component to another schema without
any data loss?

Thanks,
Shobana
Re: How to clone annotations [message #603557 is a reply to message #76620] Wed, 24 December 2008 09:34 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 26058
Registered: July 2009
Senior Member
Shobana,

You're sure? Did you look at the implementation of that method?

Element element = xsdConcreteComponent./*{@link
XSDConcreteComponent#getElement() /*}*/getElement();
if (element != null)
{
// Clone the DOM using the DOM API, and create the same type of
component to hold it.
//
Element clonedElement = (Element)element.cloneNode(true);
XSDConcreteComponent result =
(XSDConcreteComponent)XSDFactory.eINSTANCE.create(xsdConcret eComponent.eClass());
result.setElement(clonedElement);
return result;
}

If you're moving between schemas, you need to do
xsdSchema.getDocument().importNode(element) instead of just cloning it,
because a clone produces an Element that can only be used in the same
Document...


shobana wrote:
> Hi ed,
>
> Thanks for quick reply.
>
> Yes I tried that too. But it just returns the empty tag of the xsd
> component.
> Is there any other way to copy the xsd component to another schema
> without any data loss?
>
> Thanks,
> Shobana
>
Re: How to clone annotations [message #603563 is a reply to message #76636] Fri, 26 December 2008 09:03 Go to previous message
shobana is currently offline shobana
Messages: 13
Registered: July 2009
Junior Member
Hi Ed,

Thanks a lot. I used importNode() it worked. :)

But yes I tried the method u mentioned in the earlier post, but it didnt
work.

Thanks,
Shobana
Re: How to clone annotations [message #603630 is a reply to message #76667] Wed, 18 February 2009 10:56 Go to previous message
rollo is currently offline rollo
Messages: 117
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------080305090109090806010008
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

I also want to clone an existing annotation and move it to another
schema, but I didn't get it working.

For example for this schema "schema.xsd" in the attachment.

In the moment I do it like this:

I read the content of the old schema. Create for example a new
Simpletype. I put the annotation of the old Simpletype in a map.
At the end I add all new Types to the new schema.

normalizedSchema.getContents().add( type );

if ( annotations.containsKey( type.getName() ) ) {
XSDAnnotation annotation = annotations.get( type.getName() );
type.setAnnotation( annotation );
schema.getDocument().importNode( annotation.getElement(), true );
}

But it did not work, like you can see in schema2.xsd.

Can somebody help me?

Roland





--------------080305090109090806010008
Content-Type: text/plain; x-mac-type="0"; x-mac-creator="0";
name="schema.xsd"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename="schema.xsd"

PHhzZDpzY2hlbWEgeG1sbnM6eHNkPSJodHRwOi8vd3d3LnczLm9yZy8yMDAx L1hNTFNjaGVt
YSIKCXhtbG5zOm5zPSJodHRwOi8vZXhhbXBsZS5jb20vdGVzdCIgeG1sbnM6 bXlNZXRhPSJo
dHRwOi8vZXhhbXBsZS5jb20vbWV0YUluZm8iCgl0YXJnZXROYW1lc3BhY2U9 Imh0dHA6Ly9l
eGFtcGxlLmNvbS90ZXN0Ij4KCgk8eHNkOmVsZW1lbnQgbmFtZT0iQSIgdHlw ZT0ibnM6QSIg
Lz4KCgk8eHNkOmNvbXBsZXhUeXBlIG5hbWU9IkEiPgoJCTx4c2Q6YW5ub3Rh dGlvbj4KCQkJ
PHhzZDphcHBpbmZvIHNvdXJjZT0iaHR0cDovL2V4YW1wbGUuY29tL21ldGFJ bmZvIj4KCQkJ
CTxNZXRhSW5mb01hcCB4bWxucz0iaHR0cDovL2V4YW1wbGUuY29tL21ldGFJ bmZvIj4KCQkJ
CQk8bWV0YUluZm8ga2V5PSJkZXNjcmlwdGlvbiIgdmFsdWU9IkEiIC8+CgkJ CQk8L01ldGFJ
bmZvTWFwPgoJCQk8L3hzZDphcHBpbmZvPgoJCTwveHNkOmFubm90YXRpb24+ CgkJPHhzZDpz
ZXF1ZW5jZT4KCQkJPHhzZDplbGVtZW50IG5hbWU9IkIiIHR5cGU9Im5zOkIi IC8+CgkJPC94
c2Q6c2VxdWVuY2U+Cgk8L3hzZDpjb21wbGV4VHlwZT4KCgk8eHNkOnNpbXBs ZVR5cGUgbmFt
ZT0iQiI+CgkJPHhzZDphbm5vdGF0aW9uPgoJCQk8eHNkOmFwcGluZm8gc291 cmNlPSJodHRw
Oi8vZXhhbXBsZS5jb20vbWV0YUluZm8iPgoJCQkJPE1ldGFJbmZvTWFwIHht bG5zPSJodHRw
Oi8vZXhhbXBsZS5jb20vbWV0YUluZm8iPgoJCQkJCTxtZXRhSW5mbyBrZXk9 ImRlc2NyaXB0
aW9uIiB2YWx1ZT0iQiIgLz4KCQkJCTwvTWV0YUluZm9NYXA+CgkJCTwveHNk OmFwcGluZm8+
CgkJPC94c2Q6YW5ub3RhdGlvbj4KCQk8eHNkOnJlc3RyaWN0aW9uIGJhc2U9 InhzZDpzdHJp
bmciIC8+Cgk8L3hzZDpzaW1wbGVUeXBlPgoKPC94c2Q6c2NoZW1hPg==
--------------080305090109090806010008
Content-Type: text/xml; x-mac-type="0"; x-mac-creator="0";
name="schema2.xsd"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="schema2.xsd"

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:myMeta="http://example.com/metaInfo"
xmlns:ns="http://www.arlanis.com/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified" targetNamespace="http://www.arlanis.com/test">
<xsd:element name="A" type="ns:A" />
<xsd:complexType name="A">
<xsd:sequence maxOccurs="1" minOccurs="1">
<xsd:element maxOccurs="1" minOccurs="1" name="B" type="ns:B" />
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="B">
<xsd:annotation />
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
</xsd:schema>

--------------080305090109090806010008--
Re: How to clone annotations [message #603632 is a reply to message #76636] Wed, 18 February 2009 11:00 Go to previous message
Bhuvan Mehta is currently offline Bhuvan Mehta
Messages: 38
Registered: July 2009
Member
Hello,

When i use the below code snippet as suggested by you :

Element element = srcComponent.getElement();
if (element != null) {
Element clonedElement = (Element)element.cloneNode(true);
XSDConcreteComponent result =
(XSDConcreteComponent)XSDFactory.eINSTANCE.create(srcCompone nt.eClass());
result.setElement(clonedElement);

Node newComponent =
targetSchema.getDocument().importNode(result.getElement(), true);
targetSchema.getElement().replaceChild(newComponent,
oldComponent.getElement());

}

then it updates the schema in following way :
Ex:
Original Schema :-
Say i change the type from "int" to "boolean"

<xsd:element name="ele1" type="xsd:int"/>

<element name="ele1" type="boolean"
xmlns="http://www.w3.org/2001/XMLSchema"/>

Which is valid but when i use the validate() method from
XSDConcreteComponent
to check schema validation then it says schema is invalid with following
message.

"Invalid XML schema for namespace 'http://www.example.org/test1/'.
Diagnostics:
XSD: Type reference '#int' is unresolved".
Previous Topic:EMF/XSD Activities Example
Next Topic:Cloning annotations
Goto Forum:
  


Current Time: Mon Sep 22 16:25:18 GMT 2014

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

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