use of org.eclipse.xsd.XSDAnnotation in project tmdm-studio-se by Talend.
the class XSDUtil method getAnnotationValue.
public static String getAnnotationValue(XSDElementDeclaration elementDesc, String key) {
XSDAnnotation annotation = elementDesc.getAnnotation();
if (annotation != null) {
EList<Element> applicationInformation = annotation.getApplicationInformation();
if (applicationInformation != null) {
for (Element element : applicationInformation) {
if (element.getLocalName().equalsIgnoreCase("appinfo")) {
// $NON-NLS-1$
// $NON-NLS-1$
String source = element.getAttribute("source");
if (key.equalsIgnoreCase(source)) {
NodeList childNodes = element.getChildNodes();
String nodeValue = null;
if (childNodes.getLength() > 0) {
Node node = childNodes.item(0);
nodeValue = node.getNodeValue();
}
return nodeValue;
}
}
}
}
}
return null;
}
use of org.eclipse.xsd.XSDAnnotation in project tmdm-studio-se by Talend.
the class Util method retrieveXSDComponentPath.
public static List<String> retrieveXSDComponentPath(Object component, XSDSchema schema, List<String> buffer) {
String name = null;
if (component instanceof XSDElementDeclaration) {
XSDElementDeclaration decl = (XSDElementDeclaration) component;
name = decl.getName();
// $NON-NLS-1$//$NON-NLS-2$
buffer.add("//xsd:element[@name='" + name + "']");
if (decl.getContainer() instanceof XSDSchemaImpl) {
return buffer;
} else {
return retrieveXSDComponentPath(decl.getContainer(), schema, buffer);
}
} else if (component instanceof XSDParticle) {
XSDParticle particle = (XSDParticle) component;
XSDTerm term = particle.getTerm();
if (term instanceof XSDElementDeclaration && !(((XSDElementDeclaration) term).getContainer() instanceof XSDParticle)) {
String prefix = null;
String ns = ((XSDElementDeclaration) term).getTargetNamespace();
Iterator<Map.Entry<String, String>> iter = schema.getQNamePrefixToNamespaceMap().entrySet().iterator();
while (iter.hasNext() && ns != null) {
Map.Entry<String, String> entry = iter.next();
if (entry.getValue().equals(ns)) {
prefix = entry.getKey();
}
}
name = ((XSDElementDeclaration) term).getName();
buffer.add(// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
"//xsd:element[@name='" + name + "' or @ref='" + (prefix != null ? (prefix + ":" + name) : name) + "']");
return retrieveXSDComponentPath(particle.getContainer(), schema, buffer);
} else {
retrieveXSDComponentPath(particle.getContainer(), schema, buffer);
}
} else if (component instanceof XSDComplexTypeDefinition) {
XSDComplexTypeDefinition type = (XSDComplexTypeDefinition) component;
name = type.getName();
// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
buffer.add("//xsd:complexType" + (name != null ? "[@name='" + name + "']" : ""));
if (type.getContainer() instanceof XSDSchemaImpl) {
return buffer;
}
return retrieveXSDComponentPath(type.getContainer(), schema, buffer);
} else if (component instanceof XSDSimpleTypeDefinition) {
// TreePath tPath=((TreeSelection)selection).getPaths()[0];
// Object elem = tPath.getSegment(0);
// return retrieveXSDComponentPath(elem, schema, buffer, selection);
String typeName = ((XSDSimpleTypeDefinition) component).getName();
// $NON-NLS-1$//$NON-NLS-2$
buffer.add("//xsd:simpleType[@name='" + typeName + "']");
return buffer;
} else if (component instanceof XSDModelGroup) {
XSDModelGroup group = (XSDModelGroup) component;
String literal = group.getCompositor().getLiteral();
// $NON-NLS-1$
buffer.add("//xsd:" + literal);
return retrieveXSDComponentPath(group.getContainer(), schema, buffer);
} else if (component instanceof XSDIdentityConstraintDefinition) {
XSDIdentityConstraintDefinition identify = (XSDIdentityConstraintDefinition) component;
XSDConcreteComponent c = identify.getContainer();
// $NON-NLS-1$//$NON-NLS-2$
buffer.add("//xsd:unique[@name='" + identify.getName() + "']");
return retrieveXSDComponentPath(c, schema, buffer);
} else if (component instanceof XSDXPathDefinition) {
XSDXPathDefinition path = (XSDXPathDefinition) component;
// $NON-NLS-1$//$NON-NLS-2$
buffer.add("//xsd:field[@xpath='" + path.getValue() + "']");
return retrieveXSDComponentPath(path.getContainer(), schema, buffer);
} else if (component instanceof XSDAnnotation) {
XSDAnnotation annon = (XSDAnnotation) component;
// $NON-NLS-1$
buffer.add("//xsd:annotation");
return retrieveXSDComponentPath(annon.getContainer(), schema, buffer);
} else {
return buffer;
}
return buffer;
}
use of org.eclipse.xsd.XSDAnnotation in project tmdm-studio-se by Talend.
the class UtilTest method testGetforeignKeyOfElement.
@Test
public void testGetforeignKeyOfElement() {
// $NON-NLS-1$
String attKey = "source";
// $NON-NLS-1$
String attValue = "X_ForeignKey";
// $NON-NLS-1$
String namespaceURI = "http://www.w3.org/XML/1998/namespace";
// $NON-NLS-1$
String qualifiedName = "appinfo";
Set<String> list = new HashSet<String>();
try {
Util.getforeignKeyOfElement(list, null);
assertTrue(list.isEmpty());
//
Document doc = getEmptyDocument();
Element appinfoElement1 = doc.createElementNS(namespaceURI, qualifiedName);
appinfoElement1.setAttribute(attKey, attValue);
// $NON-NLS-1$
appinfoElement1.appendChild(doc.createTextNode("StoreA/Id"));
// $NON-NLS-1$
Element appinfoElement2 = doc.createElementNS(namespaceURI, "appinfosssss");
appinfoElement2.setAttribute(attKey, attValue);
// $NON-NLS-1$
appinfoElement2.appendChild(doc.createTextNode("StoreB/Id"));
XSDAnnotation xsdAnnotation = XSDFactory.eINSTANCE.createXSDAnnotation();
EList<Element> applicationInformations = xsdAnnotation.getApplicationInformation();
applicationInformations.add(appinfoElement1);
applicationInformations.add(appinfoElement2);
// prepare a
XSDAnnotation xsdAnnotation_a = XSDFactory.eINSTANCE.createXSDAnnotation();
Element appElement_a = doc.createElementNS(namespaceURI, qualifiedName);
// $NON-NLS-1$
appElement_a.setAttribute(attKey, attValue);
// $NON-NLS-1$
appElement_a.appendChild(doc.createTextNode("StoreC/Id"));
xsdAnnotation_a.getApplicationInformation().add(appElement_a);
XSDElementDeclaration xsdElementDeclaration_a = XSDFactory.eINSTANCE.createXSDElementDeclaration();
xsdElementDeclaration_a.setAnnotation(xsdAnnotation_a);
XSDParticle xsdParticle_a = XSDFactory.eINSTANCE.createXSDParticle();
xsdParticle_a.setTerm(xsdElementDeclaration_a);
// prepare b
XSDAnnotation xsdAnnotation_b = XSDFactory.eINSTANCE.createXSDAnnotation();
Element appElement_b = doc.createElementNS(namespaceURI, qualifiedName);
// $NON-NLS-1$
appElement_b.setAttribute(attKey, attValue);
// $NON-NLS-1$
appElement_b.appendChild(doc.createTextNode("StoreD/Id"));
xsdAnnotation_b.getApplicationInformation().add(appElement_b);
XSDElementDeclaration xsdElementDeclaration_b = XSDFactory.eINSTANCE.createXSDElementDeclaration();
xsdElementDeclaration_b.setAnnotation(xsdAnnotation_b);
XSDParticle xsdParticle_b = XSDFactory.eINSTANCE.createXSDParticle();
xsdParticle_b.setTerm(xsdElementDeclaration_b);
// prepare c
XSDAnnotation xsdAnnotation_c = XSDFactory.eINSTANCE.createXSDAnnotation();
Element appElement_c = doc.createElementNS(namespaceURI, qualifiedName);
// $NON-NLS-1$
appElement_c.setAttribute(attKey, attValue);
// $NON-NLS-1$
appElement_c.appendChild(doc.createTextNode("StoreE/Id"));
xsdAnnotation_c.getApplicationInformation().add(appElement_c);
XSDElementDeclaration xsdElementDeclaration_c = XSDFactory.eINSTANCE.createXSDElementDeclaration();
xsdElementDeclaration_c.setAnnotation(xsdAnnotation_c);
XSDComplexTypeDefinition xsdComplexTypeDefinition_c = XSDFactory.eINSTANCE.createXSDComplexTypeDefinition();
xsdElementDeclaration_c.setTypeDefinition(xsdComplexTypeDefinition_c);
XSDParticle xsdParticle_c_1 = XSDFactory.eINSTANCE.createXSDParticle();
xsdComplexTypeDefinition_c.setContent(xsdParticle_c_1);
XSDModelGroup xsdModelGroup_c_1 = XSDFactory.eINSTANCE.createXSDModelGroup();
xsdParticle_c_1.setTerm(xsdModelGroup_c_1);
XSDParticle xsdParticle_c_2 = XSDFactory.eINSTANCE.createXSDParticle();
xsdModelGroup_c_1.getContents().add(xsdParticle_c_2);
XSDElementDeclaration xsdElementDeclaration_c_2 = XSDFactory.eINSTANCE.createXSDElementDeclaration();
xsdParticle_c_2.setTerm(xsdElementDeclaration_c_2);
XSDAnnotation xsdAnnotation_c_2 = XSDFactory.eINSTANCE.createXSDAnnotation();
Element appElement_c_2 = doc.createElementNS(namespaceURI, qualifiedName);
// $NON-NLS-1$
appElement_c_2.setAttribute(attKey, attValue);
// $NON-NLS-1$
appElement_c_2.appendChild(doc.createTextNode("StoreF/Id"));
xsdAnnotation_c_2.getApplicationInformation().add(appElement_c_2);
xsdElementDeclaration_c_2.setAnnotation(xsdAnnotation_c_2);
XSDParticle xsdParticle_c = XSDFactory.eINSTANCE.createXSDParticle();
xsdParticle_c.setTerm(xsdElementDeclaration_c);
XSDModelGroup xsdModelGroup_a = XSDFactory.eINSTANCE.createXSDModelGroup();
xsdModelGroup_a.getContents().add(xsdParticle_a);
// referecened
xsdModelGroup_a.getContents().add(xsdParticle_b);
xsdModelGroup_a.getContents().add(xsdParticle_c);
XSDParticle xsdParticle_child = XSDFactory.eINSTANCE.createXSDParticle();
xsdParticle_child.setTerm(xsdModelGroup_a);
XSDComplexTypeDefinition xsdComplexTypeDefinition = XSDFactory.eINSTANCE.createXSDComplexTypeDefinition();
xsdComplexTypeDefinition.setContent(xsdParticle_child);
XSDElementDeclaration xsdElementDeclaration = xsdElementDeclaration_b;
xsdElementDeclaration.setAnnotation(xsdAnnotation);
xsdElementDeclaration.setTypeDefinition(xsdComplexTypeDefinition);
// run and check
Util.getforeignKeyOfElement(list, xsdElementDeclaration);
assertTrue(list.size() == 4);
// $NON-NLS-1$
assertTrue(list.contains("StoreA"));
// $NON-NLS-1$
assertTrue(list.contains("StoreC"));
// $NON-NLS-1$
assertTrue(list.contains("StoreE"));
// $NON-NLS-1$
assertTrue(list.contains("StoreF"));
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
use of org.eclipse.xsd.XSDAnnotation in project tmdm-studio-se by Talend.
the class UtilTest method testRetrieveXSDComponentPath.
@Test
public void testRetrieveXSDComponentPath() {
// $NON-NLS-1$
String conceptName = "Product";
// $NON-NLS-1$
String complextypeName = "ctype";
// $NON-NLS-1$
String simpletypeName = "stype";
// $NON-NLS-1$
String identityName = "identityName";
// $NON-NLS-1$
String xsdxpathValue = "Id";
// $NON-NLS-1$
String childelementName = "childelementName";
List<String> xsdComponentPath = null;
XSDFactory factory = XSDFactory.eINSTANCE;
XSDSchema xschema = factory.createXSDSchema();
XSDElementDeclaration concept = factory.createXSDElementDeclaration();
concept.setName(conceptName);
xschema.getContents().add(concept);
XSDComplexTypeDefinition complexTypeDefinition = factory.createXSDComplexTypeDefinition();
complexTypeDefinition.setBaseTypeDefinition(// $NON-NLS-1$ );
xschema.resolveComplexTypeDefinition(xschema.getSchemaForSchemaNamespace(), "anyType"));
xschema.getContents().add(complexTypeDefinition);
XSDSimpleTypeDefinition simpleTypeDefinition = factory.createXSDSimpleTypeDefinition();
simpleTypeDefinition.setName(simpletypeName);
XSDModelGroup modelGroup = factory.createXSDModelGroup();
modelGroup.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
XSDParticle typeparticle = factory.createXSDParticle();
complexTypeDefinition.setContent(typeparticle);
typeparticle.setContent(modelGroup);
XSDParticle childParticle = factory.createXSDParticle();
XSDElementDeclaration childelement = factory.createXSDElementDeclaration();
childelement.setName(childelementName);
childParticle.setContent(childelement);
childParticle.setTerm(childelement);
modelGroup.getContents().add(childParticle);
XSDIdentityConstraintDefinition IdConsDef = factory.createXSDIdentityConstraintDefinition();
IdConsDef.setName(identityName);
concept.getIdentityConstraintDefinitions().add(IdConsDef);
XSDXPathDefinition xsdXPathDefinition = factory.createXSDXPathDefinition();
xsdXPathDefinition.setValue(xsdxpathValue);
IdConsDef.getFields().add(xsdXPathDefinition);
XSDAnnotation conceptAnnotation = factory.createXSDAnnotation();
concept.setAnnotation(conceptAnnotation);
XSDElementDeclaration anotherConcept = factory.createXSDElementDeclaration();
// $NON-NLS-1$
String concept2name = "anotherConcept";
anotherConcept.setName(concept2name);
xschema.getContents().add(anotherConcept);
//
List<String> buffer = new ArrayList<String>();
xsdComponentPath = Util.retrieveXSDComponentPath(concept, xschema, buffer);
assertEquals(1, xsdComponentPath.size());
// $NON-NLS-1$ //$NON-NLS-2$
assertEquals("//xsd:element[@name='" + conceptName + "']", xsdComponentPath.get(0));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(complexTypeDefinition, xschema, buffer);
assertEquals(1, xsdComponentPath.size());
// $NON-NLS-1$
assertEquals("//xsd:complexType", xsdComponentPath.get(0));
//
buffer.clear();
complexTypeDefinition.setName(complextypeName);
xsdComponentPath = Util.retrieveXSDComponentPath(complexTypeDefinition, xschema, buffer);
assertEquals(1, xsdComponentPath.size());
// $NON-NLS-1$ //$NON-NLS-2$
assertEquals("//xsd:complexType[@name='" + complextypeName + "']", xsdComponentPath.get(0));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(simpleTypeDefinition, xschema, buffer);
assertEquals(1, xsdComponentPath.size());
// $NON-NLS-1$ //$NON-NLS-2$
assertEquals("//xsd:simpleType[@name='" + simpletypeName + "']", xsdComponentPath.get(0));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(modelGroup, xschema, buffer);
assertEquals(2, xsdComponentPath.size());
// $NON-NLS-1$
assertTrue(xsdComponentPath.contains("//xsd:" + XSDCompositor.SEQUENCE_LITERAL.getLiteral()));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:complexType[@name='" + complextypeName + "']"));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(IdConsDef, xschema, buffer);
assertEquals(2, xsdComponentPath.size());
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:element[@name='" + conceptName + "']"));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:unique[@name='" + identityName + "']"));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(xsdXPathDefinition, xschema, buffer);
assertEquals(3, xsdComponentPath.size());
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:element[@name='" + conceptName + "']"));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:unique[@name='" + identityName + "']"));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:field[@xpath='" + xsdxpathValue + "']"));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(conceptAnnotation, xschema, buffer);
assertEquals(2, xsdComponentPath.size());
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:element[@name='" + conceptName + "']"));
// $NON-NLS-1$
assertTrue(xsdComponentPath.contains("//xsd:annotation"));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(childParticle, xschema, buffer);
assertEquals(2, xsdComponentPath.size());
// $NON-NLS-1$
assertTrue(xsdComponentPath.contains("//xsd:" + XSDCompositor.SEQUENCE_LITERAL.getLiteral()));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:complexType[@name='" + complextypeName + "']"));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(childelement, xschema, buffer);
assertEquals(3, xsdComponentPath.size());
// $NON-NLS-1$
assertTrue(xsdComponentPath.contains("//xsd:" + XSDCompositor.SEQUENCE_LITERAL.getLiteral()));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:complexType[@name='" + complextypeName + "']"));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:element[@name='" + childelementName + "']"));
//
buffer.clear();
childelement.setResolvedElementDeclaration(anotherConcept);
xsdComponentPath = Util.retrieveXSDComponentPath(childParticle, xschema, buffer);
assertEquals(3, xsdComponentPath.size());
// $NON-NLS-1$
assertTrue(xsdComponentPath.contains("//xsd:" + XSDCompositor.SEQUENCE_LITERAL.getLiteral()));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:complexType[@name='" + complextypeName + "']"));
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertTrue(xsdComponentPath.contains("//xsd:element[@name='" + concept2name + "' or @ref='" + concept2name + "']"));
//
buffer.clear();
// $NON-NLS-1$
String targetNamespace = "targetnamespace";
// $NON-NLS-1$
String key = "prefix";
anotherConcept.setTargetNamespace(targetNamespace);
xschema.getQNamePrefixToNamespaceMap().put(key, targetNamespace);
xsdComponentPath = Util.retrieveXSDComponentPath(childParticle, xschema, buffer);
assertEquals(3, xsdComponentPath.size());
// $NON-NLS-1$
assertTrue(xsdComponentPath.contains("//xsd:" + XSDCompositor.SEQUENCE_LITERAL.getLiteral()));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:complexType[@name='" + complextypeName + "']"));
assertTrue(xsdComponentPath.contains(// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"//xsd:element[@name='" + concept2name + "' or @ref='" + key + ":" + concept2name + "']"));
}
use of org.eclipse.xsd.XSDAnnotation in project tmdm-studio-se by Talend.
the class UtilTest method testGetForeignKeyofParcle.
@Test
public void testGetForeignKeyofParcle() {
Set<String> list = new HashSet<String>();
try {
Util.getforeignKeyOfElement(list, null);
assertTrue(list.isEmpty());
//
Document doc = getEmptyDocument();
// $NON-NLS-1$ //$NON-NLS-2$
Element appinfoElement1 = doc.createElementNS("http://www.w3.org/XML/1998/namespace", "appinfo");
// $NON-NLS-1$ //$NON-NLS-2$
appinfoElement1.setAttribute("source", "X_ForeignKey");
// $NON-NLS-1$
appinfoElement1.appendChild(doc.createTextNode("Store/Id"));
// $NON-NLS-1$ //$NON-NLS-2$
Element appinfoElement2 = doc.createElementNS("http://www.w3.org/XML/1998/namespace", "appinfosssss");
// $NON-NLS-1$ //$NON-NLS-2$
appinfoElement2.setAttribute("source", "X_ForeignKey");
// $NON-NLS-1$
appinfoElement2.appendChild(doc.createTextNode("ProductFamily/Id"));
XSDAnnotation xsdAnnotation = XSDFactory.eINSTANCE.createXSDAnnotation();
EList<Element> applicationInformations = xsdAnnotation.getApplicationInformation();
applicationInformations.add(appinfoElement1);
applicationInformations.add(appinfoElement2);
Util.getForeignKeyofParcle(list, xsdAnnotation);
assertTrue(list.size() == 1);
// $NON-NLS-1$
assertTrue(list.contains("Store"));
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
Aggregations