Home » Archived » XML Schema Definition (XSD) » How to clone annotations
| | | | | | | | | | | | | | |
Re: How to clone annotations [message #77208 is a reply to message #76636] |
Thu, 19 February 2009 10:28 |
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
<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 |
Ed Merks Messages: 33263 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
> <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;
>> }
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: How to clone annotations [message #77239 is a reply to message #77224] |
Thu, 19 February 2009 13:24 |
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
>> <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 |
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
>>> <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 |
Ed Merks Messages: 33263 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
>>> <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;
>>>> }
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: How to clone annotations [message #77288 is a reply to message #77255] |
Thu, 19 February 2009 15:34 |
Ed Merks Messages: 33263 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
>>>> <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;
>>>>> }
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: How to clone annotations [message #77304 is a reply to message #77271] |
Thu, 19 February 2009 16:42 |
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 #77349 is a reply to message #77335] |
Fri, 20 February 2009 12:01 |
Ed Merks Messages: 33263 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>
<xsd:element name="ele1" type="xsd:boolean"/>
<br>
<br>
now i try to replace the element in target schema from source schema
which has the content of element as below :
<br>
<x:element name="ele1" type="x:boolean">
<br>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Re: How to clone annotations [message #77394 is a reply to message #77349] |
Wed, 25 February 2009 12:22 |
Bhuvan Mehta Messages: 58 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 #77451 is a reply to message #77394] |
Wed, 25 February 2009 16:04 |
Ed Merks Messages: 33263 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.
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| | | | | |
Re: How to clone annotations [message #77623 is a reply to message #77615] |
Thu, 16 April 2009 14:55 |
Ed Merks Messages: 33263 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
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: How to clone annotations [message #603537 is a reply to message #76536] |
Tue, 23 December 2008 12:59 |
Ed Merks Messages: 33263 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
>
>
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Re: How to clone annotations [message #603557 is a reply to message #76620] |
Wed, 24 December 2008 09:34 |
Ed Merks Messages: 33263 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
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| | | | | |
Re: How to clone annotations [message #603642 is a reply to message #76996] |
Wed, 18 February 2009 12:24 |
Ed Merks Messages: 33263 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
>
>
>
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: How to clone annotations [message #603646 is a reply to message #77005] |
Wed, 18 February 2009 12:29 |
Ed Merks Messages: 33263 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.
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| | |
Re: How to clone annotations [message #603657 is a reply to message #77040] |
Wed, 18 February 2009 15:31 |
Ed Merks Messages: 33263 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...
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Re: How to clone annotations [message #604042 is a reply to message #76636] |
Thu, 19 February 2009 10:28 |
Jacek Pospychala Messages: 159 Registered: July 2009 |
Senior Member |
|
|
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
<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 #604046 is a reply to message #77208] |
Thu, 19 February 2009 11:52 |
Ed Merks Messages: 33263 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
> <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;
>> }
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: How to clone annotations [message #604051 is a reply to message #77224] |
Thu, 19 February 2009 13:24 |
Jacek Pospychala Messages: 159 Registered: July 2009 |
Senior Member |
|
|
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
>> <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 #604057 is a reply to message #77239] |
Thu, 19 February 2009 15:16 |
Jacek Pospychala Messages: 159 Registered: July 2009 |
Senior Member |
|
|
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
>>> <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 #604062 is a reply to message #77239] |
Thu, 19 February 2009 15:32 |
Ed Merks Messages: 33263 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
>>> <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;
>>>> }
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: How to clone annotations [message #604066 is a reply to message #77255] |
Thu, 19 February 2009 15:34 |
Ed Merks Messages: 33263 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
>>>> <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;
>>>>> }
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: How to clone annotations [message #604072 is a reply to message #77271] |
Thu, 19 February 2009 16:42 |
Jacek Pospychala Messages: 159 Registered: July 2009 |
Senior Member |
|
|
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 #604084 is a reply to message #77335] |
Fri, 20 February 2009 12:01 |
Ed Merks Messages: 33263 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>
<xsd:element name="ele1" type="xsd:boolean"/>
<br>
<br>
now i try to replace the element in target schema from source schema
which has the content of element as below :
<br>
<x:element name="ele1" type="x:boolean">
<br>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Re: How to clone annotations [message #604097 is a reply to message #77349] |
Wed, 25 February 2009 12:22 |
Bhuvan Mehta Messages: 58 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 #604101 is a reply to message #77381] |
Wed, 25 February 2009 15:18 |
Ed Merks Messages: 33263 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
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: How to clone annotations [message #604111 is a reply to message #77394] |
Wed, 25 February 2009 16:04 |
Ed Merks Messages: 33263 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.
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| | | |
Re: How to clone annotations [message #604157 is a reply to message #77583] |
Wed, 15 April 2009 10:51 |
Ed Merks Messages: 33263 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
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Re: How to clone annotations [message #604167 is a reply to message #77615] |
Thu, 16 April 2009 14:55 |
Ed Merks Messages: 33263 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
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Goto Forum:
Current Time: Wed Dec 11 21:16:27 GMT 2024
Powered by FUDForum. Page generated in 0.13149 seconds
|