use of org.eclipse.xsd.XSDSimpleTypeDefinition in project tmdm-studio-se by Talend.
the class Util method findElementsUsingType.
/**
* Find elementDeclarations that use any types derived from a named type.
*
* <p>
* This shows one way to query the schema for elementDeclarations and then how to find specific kinds of
* typeDefinitions.
* </p>
*
* @param objList collection set to search for elemDecls
* @param localName for the type used
* @return Boolean indicate any XSDElementDeclarations is found or not
*/
public static boolean findElementsUsingType(List<Object> objList, XSDTypeDefinition localTypedef) {
for (Object obj : objList) {
if (obj == localTypedef) {
continue;
}
if (obj instanceof XSDParticle || obj instanceof XSDElementDeclaration || obj instanceof XSDTypeDefinition) {
XSDTypeDefinition typedef = null;
if (obj instanceof XSDParticle) {
XSDParticle xsdParticle = (XSDParticle) obj;
if (xsdParticle.getTerm() instanceof XSDElementDeclaration) {
obj = xsdParticle.getTerm();
}
}
if (obj instanceof XSDElementDeclaration) {
XSDElementDeclaration elem = (XSDElementDeclaration) obj;
if (elem.getAnonymousTypeDefinition() != null) {
typedef = elem.getAnonymousTypeDefinition();
} else if (elem.getTypeDefinition() != null) {
typedef = elem.getTypeDefinition();
} else {
// thus it's not using our type
continue;
}
} else {
typedef = (XSDTypeDefinition) obj;
}
if (typedef instanceof XSDComplexTypeDefinition) {
XSDComplexTypeDefinition type = (XSDComplexTypeDefinition) typedef;
if (localTypedef.getName().equals(type.getName()) && (localTypedef instanceof XSDComplexTypeDefinition)) {
return true;
}
if (type.getContent() instanceof XSDParticle) {
XSDParticle particle = (XSDParticle) type.getContent();
if (particle.getTerm() instanceof XSDModelGroup) {
XSDModelGroup group = (XSDModelGroup) particle.getTerm();
EList<XSDParticle> elist = group.getContents();
for (XSDParticle pt : elist) {
if (pt.getContent() instanceof XSDElementDeclaration) {
XSDTypeDefinition typeDef = ((XSDElementDeclaration) pt.getContent()).getTypeDefinition();
boolean sameType = (typeDef instanceof XSDComplexTypeDefinition && localTypedef instanceof XSDComplexTypeDefinition) || (typeDef instanceof XSDSimpleTypeDefinition && localTypedef instanceof XSDSimpleTypeDefinition);
if (typeDef != null && typeDef.getName() != null && sameType) {
if ((localTypedef.getName().equals(typeDef.getName()))) {
return true;
}
}
}
}
}
}
} else if (typedef instanceof XSDSimpleTypeDefinition) {
XSDSimpleTypeDefinition type = (XSDSimpleTypeDefinition) typedef;
XSDSimpleTypeDefinition baseType = type.getBaseTypeDefinition();
if (baseType != null && baseType.getName().equals(localTypedef.getName()) && localTypedef instanceof XSDSimpleTypeDefinition) {
return true;
}
}
}
}
return false;
}
use of org.eclipse.xsd.XSDSimpleTypeDefinition 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.XSDSimpleTypeDefinition in project tmdm-studio-se by Talend.
the class Util method getComplexTypeDefinitionChildren.
public static ArrayList<Object> getComplexTypeDefinitionChildren(XSDComplexTypeDefinition complexTypeDefinition, boolean ignoreRestriction) {
XSDComplexTypeContent xsdComplexTypeContent = complexTypeDefinition.getContent();
ArrayList<Object> list = new ArrayList<Object>();
// Now determine whether ref. If ref look at the base Type definition
XSDTypeDefinition baseTypeDefinition = complexTypeDefinition.getBaseTypeDefinition();
if (xsdComplexTypeContent == null) {
XSDTypeDefinition typeDefinition = baseTypeDefinition;
// if a simple type return the simple type
if (typeDefinition instanceof XSDSimpleTypeDefinition) {
list.add(typeDefinition);
return list;
} else {
}
// it is a complex Type
xsdComplexTypeContent = ((XSDComplexTypeDefinition) typeDefinition).getContent();
}
// check if we are extending a complex Definition
if ("extension".equals(complexTypeDefinition.getDerivationMethod().getName())) {
if (baseTypeDefinition instanceof XSDComplexTypeDefinition && baseTypeDefinition != complexTypeDefinition) {
String name = ((XSDComplexTypeDefinition) baseTypeDefinition).getDerivationMethod().getName();
if (name.equals("restriction") || ignoreRestriction) {
// $NON-NLS-1$
list.addAll(getComplexTypeDefinitionChildren((XSDComplexTypeDefinition) baseTypeDefinition, ignoreRestriction));
//
}
}
}
// Attributes
if (complexTypeDefinition.getAttributeContents() != null) {
list.addAll(complexTypeDefinition.getAttributeContents());
}
// Annotations
if (complexTypeDefinition.getAnnotations() != null) {
list.addAll(complexTypeDefinition.getAnnotations());
}
// simple type return the simple type
if (xsdComplexTypeContent instanceof XSDSimpleTypeDefinition) {
list.add(xsdComplexTypeContent);
return list;
}
// xsd Particle: we have a model group
if (xsdComplexTypeContent instanceof XSDParticle) {
// log.info("Model Group?: "+((XSDParticle)xsdComplexTypeContent).getTerm());
if (((XSDParticle) xsdComplexTypeContent).getTerm() instanceof XSDModelGroup) {
// return the model group
list.add(((XSDParticle) xsdComplexTypeContent).getTerm());
return list;
} else {
// wild card or element declaration '?)
list.add(((XSDParticle) xsdComplexTypeContent).getTerm());
return list;
}
}
// what else could it be ?
list.add(xsdComplexTypeContent);
return list;
}
use of org.eclipse.xsd.XSDSimpleTypeDefinition in project tmdm-studio-se by Talend.
the class UtilTest method testGetComponentName.
@Test
public void testGetComponentName() {
// $NON-NLS-1$ //$NON-NLS-2$
String prefix = "name=\"", suffix = "\"";
String[] objNames = { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"product_elementdeclaration", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"product_particle", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"p_complextype", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"p_simpletype", "product_identityconstraintdef", // $NON-NLS-1$ //$NON-NLS-2$
"p_xpathdef" };
String[] expectedObjNames = new String[objNames.length];
for (int i = 0; i < objNames.length - 1; i++) {
expectedObjNames[i] = prefix + objNames[i] + suffix;
}
// $NON-NLS-1$
expectedObjNames[objNames.length - 1] = "xpath=\"" + objNames[objNames.length - 1] + suffix;
XSDFactory factory = XSDFactory.eINSTANCE;
try {
XSDElementDeclaration xsdElementDeclaration = factory.createXSDElementDeclaration();
xsdElementDeclaration.setName(objNames[0]);
XSDParticle xsdParticle = factory.createXSDParticle();
XSDElementDeclaration xsdParticleDeclaration = factory.createXSDElementDeclaration();
xsdParticleDeclaration.setName(objNames[1]);
xsdParticle.setTerm(xsdParticleDeclaration);
XSDComplexTypeDefinition xsdComplexTypeDefinition = factory.createXSDComplexTypeDefinition();
xsdComplexTypeDefinition.setName(objNames[2]);
XSDSimpleTypeDefinition xsdSimpleTypeDefinition = factory.createXSDSimpleTypeDefinition();
xsdSimpleTypeDefinition.setName(objNames[3]);
XSDIdentityConstraintDefinition xsdIdConsDef = factory.createXSDIdentityConstraintDefinition();
xsdIdConsDef.setName(objNames[4]);
XSDXPathDefinition xsdPathDefinition = factory.createXSDXPathDefinition();
xsdPathDefinition.setValue(objNames[5]);
String name = Util.getComponentName(xsdElementDeclaration);
assertEquals(expectedObjNames[0], name);
name = Util.getComponentName(xsdParticle);
assertEquals(expectedObjNames[1], name);
name = Util.getComponentName(xsdComplexTypeDefinition);
assertEquals(expectedObjNames[2], name);
name = Util.getComponentName(xsdSimpleTypeDefinition);
assertEquals(expectedObjNames[3], name);
name = Util.getComponentName(xsdIdConsDef);
assertEquals(expectedObjNames[4], name);
name = Util.getComponentName(xsdPathDefinition);
assertEquals(expectedObjNames[5], name);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
use of org.eclipse.xsd.XSDSimpleTypeDefinition 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 + "']"));
}
Aggregations