[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.webtools] Problem during replacing element from one schema to another schema using DOM

Hello,

I am trying to replace an element from one schema(source) to another schema(target) schema using DOM APIs as below :
and the replacement is not happening properly.


--------------------------------------------------------------
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.Element;
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"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"; targetNamespace="http://www.example.org/test1";
xmlns:tns1="http://www.example.org/test1"; elementFormDefault="qualified">

<element name="ele1" type="tns1:simple1" nillable="true"/>
<simpleType name="simple1">
<union memberTypes="int">
<annotation>
<documentation>
SimpleType.
</documentation>
</annotation>
</union>
</simpleType>

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


	<element name="ele1" type="tns:simple1" />

	<simpleType name="simple1">
		<union memberTypes="int">
			<annotation>
				<documentation>
					SimpleType.
				</documentation>
			</annotation>
		</union>
	</simpleType>

</schema>

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


	<element name="ele1" nillable="true" type="tns1:simple1"/>

	<simpleType name="simple1">
		<union memberTypes="int">
			<annotation>
				<documentation>
					SimpleType.
				</documentation>
			</annotation>
		</union>
	</simpleType>

</schema>

------------------------------------------------------------------
Validation falis with below message
------------------------------------------------------------------
XSD: Type reference 'tns1#simple1' is unresolved, which is true as replacechild() method should have defined "tns1" in element ?


Am i doing something wrong or is there any other way to do this.

--
Thanks and Regards,
Bhuvan Mehta