use of org.eclipse.xsd.XSDModelGroup 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.XSDModelGroup in project tmdm-studio-se by Talend.
the class Util method isReferrenced.
private static boolean isReferrenced(XSDElementDeclaration element, XSDElementDeclaration term) {
if (element == term) {
return true;
}
if (term.getTypeDefinition() instanceof XSDComplexTypeDefinition) {
XSDComplexTypeContent fromcomplexType = ((XSDComplexTypeDefinition) term.getTypeDefinition()).getContent();
if (fromcomplexType instanceof XSDParticle) {
XSDParticle particle = (XSDParticle) fromcomplexType;
if (particle.getTerm() instanceof XSDModelGroup) {
XSDModelGroup modelGroup = ((XSDModelGroup) particle.getTerm());
EList<?> fromlist = modelGroup.getContents();
for (Object obj : fromlist) {
XSDTerm t = ((XSDParticle) obj).getTerm();
if (t == element) {
return true;
}
}
}
}
}
return false;
}
use of org.eclipse.xsd.XSDModelGroup 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.XSDModelGroup in project tmdm-studio-se by Talend.
the class UtilTest method testGetParticleReferenceName.
@Test
public void testGetParticleReferenceName() {
XSDComplexTypeDefinition complexType = (XSDComplexTypeDefinition) schema.getElementDeclarations().get(0).getTypeDefinition();
XSDModelGroup group = (XSDModelGroup) ((XSDParticle) complexType.getContent()).getTerm();
XSDParticle curXSDParticle = group.getParticles().get(0);
String name = Util.getParticleReferenceName(curXSDParticle);
// $NON-NLS-1$
assertEquals(name, "");
}
use of org.eclipse.xsd.XSDModelGroup 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);
}
}
Aggregations