use of org.eclipse.xsd.XSDXPathDefinition 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.XSDXPathDefinition 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.XSDXPathDefinition in project tmdm-common by Talend.
the class MetadataRepository method visitElement.
@Override
public void visitElement(XSDElementDeclaration element) {
if (currentTypeStack.isEmpty()) {
// "top level" elements means new MDM entity type
String typeName = element.getName();
if (getComplexType(typeName) != null) {
// Don't process twice type
return;
}
// If entity's type is abstract
boolean isAbstract = false;
XSDTypeDefinition typeDefinition = element.getTypeDefinition();
if (typeDefinition != null && typeDefinition instanceof XSDComplexTypeDefinition) {
isAbstract = ((XSDComplexTypeDefinition) typeDefinition).isAbstract();
}
// Id fields
Map<String, XSDXPathDefinition> idFields = new LinkedHashMap<String, XSDXPathDefinition>();
EList<XSDIdentityConstraintDefinition> constraints = element.getIdentityConstraintDefinitions();
for (XSDIdentityConstraintDefinition constraint : constraints) {
EList<XSDXPathDefinition> fields = constraint.getFields();
for (XSDXPathDefinition field : fields) {
idFields.put(field.getValue(), field);
}
}
XmlSchemaAnnotationProcessorState state;
try {
XSDAnnotation annotation = element.getAnnotation();
state = new XmlSchemaAnnotationProcessorState();
for (XmlSchemaAnnotationProcessor processor : XML_ANNOTATIONS_PROCESSORS) {
processor.process(this, null, annotation, state);
}
} catch (Exception e) {
throw new RuntimeException("Annotation processing exception while parsing info for type '" + typeName + "'.", e);
}
// If write is not allowed for everyone, at least add "administration".
if (state.getAllowWrite().isEmpty()) {
state.getAllowWrite().add(ICoreConstants.ADMIN_PERMISSION);
}
ComplexTypeMetadata type = new ComplexTypeMetadataImpl(targetNamespace, typeName, state.getAllowWrite(), state.getDenyCreate(), state.getHide(), state.getDenyPhysicalDelete(), state.getDenyLogicalDelete(), state.getSchematron(), state.getPrimaryKeyInfo(), state.getLookupFields(), true, isAbstract, state.getWorkflowAccessRights());
// Register parsed localized labels
Map<Locale, String> localeToLabel = state.getLocaleToLabel();
for (Map.Entry<Locale, String> entry : localeToLabel.entrySet()) {
type.registerName(entry.getKey(), entry.getValue());
}
// Register parsed localized descriptions
Map<Locale, String> localeToDescription = state.getLocaleToDescription();
for (Map.Entry<Locale, String> entry : localeToDescription.entrySet()) {
type.registerDescription(entry.getKey(), entry.getValue());
}
// Keep line and column of definition
type.setData(XSD_LINE_NUMBER, XSDParser.getStartLine(element.getElement()));
type.setData(XSD_COLUMN_NUMBER, XSDParser.getStartColumn(element.getElement()));
type.setData(XSD_ELEMENT, element);
type.setData(XSD_DOM_ELEMENT, element.getElement());
addTypeMetadata(type);
// Keep usage information
entityTypeUsage.get(element.getType()).add(type);
// Walk the fields
currentTypeStack.push(type);
{
XmlSchemaWalker.walk(element.getType(), this);
}
currentTypeStack.pop();
// Super types
XSDElementDeclaration substitutionGroup = element.getSubstitutionGroupAffiliation();
if (substitutionGroup != null && !XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(substitutionGroup.getTargetNamespace()) && !Types.ANY_TYPE.equals(substitutionGroup.getName())) {
if (!substitutionGroup.getResolvedElementDeclaration().equals(element)) {
SoftTypeRef superType = new SoftTypeRef(this, substitutionGroup.getTargetNamespace(), substitutionGroup.getName(), true);
type.addSuperType(superType);
}
}
// Register keys (TMDM-4470).
for (Map.Entry<String, XSDXPathDefinition> unresolvedId : idFields.entrySet()) {
SoftIdFieldRef keyField = new SoftIdFieldRef(this, type.getName(), unresolvedId.getKey());
// Keep line and column of definition
keyField.setData(XSD_LINE_NUMBER, XSDParser.getStartLine(unresolvedId.getValue().getElement()));
keyField.setData(XSD_COLUMN_NUMBER, XSDParser.getStartColumn(unresolvedId.getValue().getElement()));
keyField.setData(XSD_DOM_ELEMENT, unresolvedId.getValue().getElement());
type.registerKey(keyField);
}
// entity.
if (type.getKeyFields().isEmpty() && type.getSuperTypes().isEmpty()) {
Map<String, TypeMetadata> userEntityTypes = entityTypes.get(getUserNamespace());
if (userEntityTypes != null) {
userEntityTypes.remove(type.getName());
}
}
} else {
// Non "top level" elements means fields for the MDM entity type being parsed
FieldMetadata fieldMetadata;
int minOccurs = ((XSDParticle) element.getContainer()).getMinOccurs();
int maxOccurs = ((XSDParticle) element.getContainer()).getMaxOccurs();
if (element.isElementDeclarationReference() && currentTypeStack.peek().getName().equals(element.getResolvedElementDeclaration().getName())) {
return;
}
if (element.getResolvedElementDeclaration() != null && element.getResolvedElementDeclaration().getTargetNamespace() == null) {
fieldMetadata = createFieldMetadata(element.getResolvedElementDeclaration(), currentTypeStack.peek(), minOccurs, maxOccurs);
} else {
fieldMetadata = createFieldMetadata(element, currentTypeStack.peek(), minOccurs, maxOccurs);
}
currentTypeStack.peek().addField(fieldMetadata);
}
}
use of org.eclipse.xsd.XSDXPathDefinition in project tmdm-studio-se by Talend.
the class XSDNewXPathAction method doAction.
@Override
public IStatus doAction() {
try {
int index = 0;
IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
if (selection.getFirstElement() instanceof XSDIdentityConstraintDefinition) {
icd = (XSDIdentityConstraintDefinition) selection.getFirstElement();
} else if (selection.getFirstElement() instanceof XSDXPathDefinition) {
XSDXPathDefinition xpath = (XSDXPathDefinition) selection.getFirstElement();
icd = (XSDIdentityConstraintDefinition) xpath.getContainer();
if (xpath.getVariety().equals(XSDXPathVariety.FIELD_LITERAL))
index = icd.getFields().indexOf(xpath) + 1;
else
index = 0;
} else {
MessageDialog.openError(this.page.getSite().getShell(), Messages._Error, Messages.XSDNewXPathAction_Huhhh + selection.getFirstElement().getClass().getName());
return Status.CANCEL_STATUS;
}
// InputDialog id = new InputDialog(
// page.getSite().getShell(),
// "New XPath",
// "Enter a new XPath to the field",
// null,
// new IInputValidator() {
// public String isValid(String newText) {
// if ((newText==null) || "".equals(newText)) return "The XPath cannot be empty";
// return null;
// };
// }
// );
// $NON-NLS-1$
List<String> childNames = Util.getChildElementNames("", (XSDElementDeclaration) icd.getContainer());
// filter the non top level fields
List<String> topChilds = new ArrayList<String>();
for (String child : childNames) {
if (child.indexOf('/') == -1) {
topChilds.add(child);
}
}
// forbid to add already exists field
EList<XSDXPathDefinition> fields = icd.getFields();
for (XSDXPathDefinition fd : fields) {
if (topChilds.contains(fd.getValue()))
topChilds.remove(fd.getValue());
}
SelectFieldDialog id = new SelectFieldDialog(page.getSite().getShell(), Messages.XSDNewXPathAction_SelectOnField, topChilds, null);
id.create();
id.setBlockOnOpen(true);
int ret = id.open();
if (ret == Dialog.CANCEL) {
return Status.CANCEL_STATUS;
}
String field = id.getField();
if (field.length() == 0)
return Status.CANCEL_STATUS;
XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
XSDXPathDefinition xpath = factory.createXSDXPathDefinition();
xpath.setValue(field);
xpath.setVariety(XSDXPathVariety.FIELD_LITERAL);
icd.getFields().add(index, xpath);
icd.updateElement();
updateElementForAddedfield(icd, field);
page.refresh();
page.getTreeViewer().setSelection(new StructuredSelection(xpath), true);
page.markDirty();
} catch (Exception e) {
log.error(e.getMessage(), e);
MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.ErrorMsg, e.getLocalizedMessage()));
return Status.CANCEL_STATUS;
}
return Status.OK_STATUS;
}
use of org.eclipse.xsd.XSDXPathDefinition in project tmdm-studio-se by Talend.
the class XSDDeleteParticleAction method doAction.
public IStatus doAction() {
try {
// xsdPartle is to support the multiple delete action on key press,
// which each delete action on particle must be explicit passed a xsd particle to
// delete
XSDParticle particle = (XSDParticle) xsdPartle;
if (particle == null) {
ISelection selection = page.getTreeViewer().getSelection();
particle = (XSDParticle) ((IStructuredSelection) selection).getFirstElement();
}
XSDTerm term = particle.getTerm();
XSDElementDeclaration decl = null;
if (term instanceof XSDElementDeclaration) {
decl = (XSDElementDeclaration) particle.getTerm();
}
if (particle.getContainer() == null) {
return Status.CANCEL_STATUS;
}
XSDIdentityConstraintDefinition identify = null;
XSDXPathDefinition keyPath = null;
List<Object> keyInfo = Util.getKeyInfo(decl);
if (keyInfo != null && keyInfo.size() > 0) {
identify = (XSDIdentityConstraintDefinition) keyInfo.get(0);
keyPath = (XSDXPathDefinition) keyInfo.get(1);
identify.getFields().remove(keyPath);
if (identify.getFields().size() == 0) {
XSDElementDeclaration top = (XSDElementDeclaration) Util.getParent(decl);
top.getIdentityConstraintDefinitions().remove(identify);
}
}
if (!(particle.getContainer() instanceof XSDModelGroup))
throw new XtentisException(Messages.bind(Messages.XSDDeleteParticleAction_ExceptionInfo, particle.getContainer().getClass().getName()));
XSDModelGroup group = (XSDModelGroup) particle.getContainer();
group.getContents().remove(particle);
// if (term instanceof XSDElementDeclaration) {
// //remove type definition is no more used and type is not built in
// XSDTypeDefinition typeDef = decl.getTypeDefinition();
// if ( (typeDef.getName()!=null) && //anonymous type
// (!typeDef.getSchema().getSchemaForSchemaNamespace().equals(typeDef.getTargetNamespace()))
// ){
// if (Util.findElementsUsingType(group.getSchema(),typeDef.getTargetNamespace(),
// typeDef.getName()).size()==0)
// group.getSchema().getContents().remove(typeDef);
// }
// }
group.updateElement();
xsdPartle = null;
page.refresh();
page.markDirty();
} catch (Exception e) {
log.error(e.getMessage(), e);
MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDDeleteParticleAction_ErrorMsg, e.getLocalizedMessage()));
return Status.CANCEL_STATUS;
}
return Status.OK_STATUS;
}
Aggregations