use of org.eclipse.xsd.XSDFactory in project tmdm-studio-se by Talend.
the class XSDNewParticleFromParticleAction method doAction.
public IStatus doAction() {
try {
IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
selParticle = (XSDParticle) selection.getFirstElement();
if (!(selParticle.getContainer() instanceof XSDModelGroup))
return Status.CANCEL_STATUS;
;
XSDModelGroup group = (XSDModelGroup) selParticle.getContainer();
// get position of the selected particle in the container
int index = 0;
int i = 0;
for (Iterator<XSDParticle> iter = group.getContents().iterator(); iter.hasNext(); ) {
XSDParticle p = (XSDParticle) iter.next();
if (p.equals(selParticle)) {
index = i;
break;
}
i++;
}
EList<XSDElementDeclaration> eDecls = schema.getElementDeclarations();
List<String> elementDeclarations = new ArrayList<String>();
for (Iterator<XSDElementDeclaration> iter = eDecls.iterator(); iter.hasNext(); ) {
XSDElementDeclaration d = (XSDElementDeclaration) iter.next();
if (d.getTargetNamespace() != null && d.getTargetNamespace().equals(IConstants.DEFAULT_NAME_SPACE))
continue;
// $NON-NLS-1$//$NON-NLS-2$
elementDeclarations.add(d.getQName() + (d.getTargetNamespace() != null ? " : " + d.getTargetNamespace() : ""));
}
// $NON-NLS-1$
elementDeclarations.add("");
dialog = new BusinessElementInputDialog(this, page.getSite().getShell(), Messages._AddANewBusinessElement, null, null, elementDeclarations, 0, 1, true, false);
dialog.setBlockOnOpen(true);
int ret = dialog.open();
if (ret == Dialog.CANCEL) {
return Status.CANCEL_STATUS;
}
XSDElementDeclaration elem = (XSDElementDeclaration) selParticle.getContent();
if (Util.changeElementTypeToSequence(elem, maxOccurs) == Status.CANCEL_STATUS) {
return Status.CANCEL_STATUS;
}
XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
XSDElementDeclaration decl = factory.createXSDElementDeclaration();
decl.setName(this.elementName);
if (!refName.equals("")) {
// $NON-NLS-1$
XSDElementDeclaration ref = Util.findReference(refName, schema);
if (ref != null) {
decl.setResolvedElementDeclaration(ref);
}
} else {
decl.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), simpleTypeName));
}
XSDParticle particle = factory.createXSDParticle();
particle.setContent(decl);
particle.setMinOccurs(this.minOccurs);
if (maxOccurs > -1) {
particle.setMaxOccurs(this.maxOccurs);
} else {
particle.setMaxOccurs(this.maxOccurs);
group.getContents().add(group.getContents().size(), particle);
group.updateElement();
if (// $NON-NLS-1$
particle.getElement().getAttributeNode("maxOccurs") != null)
// $NON-NLS-1$//$NON-NLS-2$
particle.getElement().getAttributeNode("maxOccurs").setNodeValue("unbounded");
else {
// $NON-NLS-1$//$NON-NLS-2$
particle.getElement().setAttribute("maxOccurs", "unbounded");
}
}
if (maxOccurs > -1) {
group.getContents().add(group.getContents().size(), particle);
group.updateElement();
}
if (dialog.isInherit()) {
XSDTerm totm = particle.getTerm();
XSDElementDeclaration concept = null;
if (Util.getParent(selParticle) instanceof XSDElementDeclaration)
concept = (XSDElementDeclaration) Util.getParent(selParticle);
else if (Util.getParent(selParticle) instanceof XSDComplexTypeDefinition) {
if (selParticle instanceof XSDParticle)
concept = (XSDElementDeclaration) ((XSDParticle) selParticle).getContent();
else if (selParticle instanceof XSDElementDeclaration)
concept = (XSDElementDeclaration) selParticle;
}
XSDAnnotation fromannotation = null;
if (concept != null)
fromannotation = concept.getAnnotation();
if (fromannotation != null) {
XSDAnnotationsStructure struc = new XSDAnnotationsStructure(totm);
if (((XSDElementDeclaration) totm).getType() != null)
addAnnotion(struc, fromannotation);
}
}
page.refresh();
page.getTreeViewer().setSelection(new StructuredSelection(particle), true);
page.markDirty();
} catch (Exception e) {
log.error(e.getMessage(), e);
MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages._ErrorCreatBusinessElement, e.getLocalizedMessage()));
return Status.CANCEL_STATUS;
}
return Status.OK_STATUS;
}
use of org.eclipse.xsd.XSDFactory in project tmdm-studio-se by Talend.
the class ElementWrapperCommitHandler method doSubmit.
@Override
protected boolean doSubmit() throws CommitException {
try {
String originalName = getCommitedObj().getSourceName();
XSDElementDeclaration decl = getCommitedObj().getSourceXSDContent();
XSDElementDeclaration ref = null;
if (decl.isElementDeclarationReference()) {
ref = decl.getResolvedElementDeclaration();
}
XSDAnnotationsStructure struct = new XSDAnnotationsStructure(getCommitedObj().getSourceElement());
// remove first
// struct.setAutoExpand(null);
struct.setAutoExpand(String.valueOf(getCommitedObj().isAutoExpand()));
// update validation rule
Set<String> paths = new HashSet<String>();
DataModelMainPage page = getPage();
Util.collectElementPaths((IStructuredContentProvider) page.getElementsViewer().getContentProvider(), page.getSite(), getCommitedObj().getSourceElement(), paths, null);
XSDElementDeclaration newRef = Util.findReference(getCommitedObj().getNewReference(), getCommitedObj().getSchema());
XSDIdentityConstraintDefinition identify = null;
XSDXPathDefinition keyPath = null;
List<Object> keyInfo = Util.getKeyInfo(getCommitedObj().getSourceXSDContent());
if (keyInfo != null && keyInfo.size() > 0) {
identify = (XSDIdentityConstraintDefinition) keyInfo.get(0);
keyPath = (XSDXPathDefinition) keyInfo.get(1);
identify.getFields().remove(keyPath);
}
getCommitedObj().getSourceXSDContent().setName(// $NON-NLS-1$
"".equals(getCommitedObj().getNewName()) ? null : getCommitedObj().getNewName());
if (keyPath != null) {
XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
XSDXPathDefinition field = factory.createXSDXPathDefinition();
field.setVariety(keyPath.getVariety());
field.setValue(getCommitedObj().getNewName());
identify.getFields().add(field);
}
if (newRef != null) {
decl.setResolvedElementDeclaration(newRef);
decl.setTypeDefinition(null);
Element elem = decl.getElement();
if (elem.getAttributes().getNamedItem("type") != null) {
// $NON-NLS-1$
elem.getAttributes().removeNamedItem("type");
}
decl.updateElement();
} else if (ref != null) {
XSDElementDeclaration sourceXSDContent = getCommitedObj().getSourceXSDContent();
sourceXSDContent.setTypeDefinition(getCommitedObj().getSchema().getSchemaForSchema().resolveSimpleTypeDefinition(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001, // $NON-NLS-1$
"string"));
sourceXSDContent.setResolvedElementDeclaration(sourceXSDContent);
// XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
// XSDElementDeclaration newD = (XSDElementDeclaration) factory.createXSDElementDeclaration();
// newD.setName(getCommitedObj().getNewName());
// newD.updateElement();
// XSDSimpleTypeDefinition stringType = getCommitedObj().getSchema().getSchemaForSchema()
// .resolveSimpleTypeDefinition(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001, "string");
//
// newD.setTypeDefinition(stringType);
// if (getCommitedObj().getSourceElement().getContainer() instanceof XSDModelGroup) {
// XSDModelGroup group = ((XSDModelGroup) getCommitedObj().getSourceElement().getContainer());
// ((XSDModelGroup) getCommitedObj().getSourceElement().getContainer()).getContents().remove(
// getCommitedObj().getSourceElement());
// getCommitedObj().setSourceElement(factory.createXSDParticle());
// getCommitedObj().getSourceElement().setContent(newD);
// group.getContents().add(getCommitedObj().getSourceElement());
// }
}
int newMaxOcur = getCommitedObj().getNewMaxOcur();
if (Util.changeElementTypeToSequence(decl, newMaxOcur) == Status.CANCEL_STATUS) {
return false;
}
int newMinOcur = getCommitedObj().getNewMinOcur();
getCommitedObj().getSourceElement().setMinOccurs(newMinOcur);
if (newMaxOcur == -1 || (newMaxOcur == 0 & newMinOcur == 0)) {
if (!"unbounded".equals(getCommitedObj().getSourceElement().getElement().getAttribute("maxOccurs"))) {
// $NON-NLS-1$//$NON-NLS-2$
getCommitedObj().getSourceElement().getElement().setAttribute("maxOccurs", "unbounded");
}
} else {
getCommitedObj().getSourceElement().setMaxOccurs(newMaxOcur);
}
getCommitedObj().getSourceElement().updateElement();
updateReference(originalName);
if (elementExAdapter != null) {
elementExAdapter.renameElement(getCommitedObj().getSchema(), paths, getCommitedObj().getNewName());
}
if (mapinfoExAdapter != null) {
mapinfoExAdapter.renameElementMapinfo(paths, decl.getName());
}
} catch (Exception e) {
throw new CommitException(e.getMessage(), e);
}
return true;
}
use of org.eclipse.xsd.XSDFactory in project tmdm-studio-se by Talend.
the class EntityCommitHandler method commitKeyAddition.
private boolean commitKeyAddition() {
boolean hasChanges = false;
for (KeyWrapper eachKeyWrapper : getCommitedObj().getKeys()) {
if (!eachKeyWrapper.isUserCreated()) {
continue;
}
hasChanges = true;
XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
XSDIdentityConstraintDefinition newKey = factory.createXSDIdentityConstraintDefinition();
newKey.setName(eachKeyWrapper.getName());
newKey.setIdentityConstraintCategory(eachKeyWrapper.getType());
XSDXPathDefinition selector = factory.createXSDXPathDefinition();
selector.setValue(eachKeyWrapper.getSelector());
selector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
newKey.setSelector(selector);
for (FieldWrapper newFields : eachKeyWrapper.getFields()) {
XSDXPathDefinition field = factory.createXSDXPathDefinition();
field.setValue(newFields.getXPath());
field.setVariety(XSDXPathVariety.FIELD_LITERAL);
newKey.getFields().add(field);
}
getCommitedObj().getSourceEntity().getIdentityConstraintDefinitions().add(newKey);
getCommitedObj().getSourceEntity().updateElement();
}
return hasChanges;
}
use of org.eclipse.xsd.XSDFactory in project tmdm-studio-se by Talend.
the class UtilTest method testFindReference.
@Test
public void testFindReference() {
XSDFactory factory = XSDFactory.eINSTANCE;
// $NON-NLS-1$ //$NON-NLS-2$
String conceptName1 = "Product", conceptName2 = "Store";
String refName = conceptName2;
XSDSchema xschema = factory.createXSDSchema();
XSDElementDeclaration referencedEntity = Util.findReference(null, null);
assertNull(referencedEntity);
referencedEntity = Util.findReference(refName, null);
assertNull(referencedEntity);
referencedEntity = Util.findReference(null, xschema);
assertNull(referencedEntity);
referencedEntity = Util.findReference(refName, xschema);
assertNull(referencedEntity);
XSDElementDeclaration concept1 = factory.createXSDElementDeclaration();
concept1.setName(conceptName1);
XSDElementDeclaration concept2 = factory.createXSDElementDeclaration();
concept2.setName(conceptName2);
xschema.getContents().add(concept1);
xschema.getContents().add(concept2);
referencedEntity = Util.findReference(refName, xschema);
assertNotNull(referencedEntity);
assertSame(concept2, referencedEntity);
// $NON-NLS-1$//$NON-NLS-2$
refName = refName + " : " + "fair";
referencedEntity = Util.findReference(refName, xschema);
assertNotNull(referencedEntity);
assertSame(concept2, referencedEntity);
// $NON-NLS-1$
refName = "ProductFamily";
referencedEntity = Util.findReference(refName, xschema);
assertNull(referencedEntity);
}
use of org.eclipse.xsd.XSDFactory in project tmdm-studio-se by Talend.
the class UtilTest method testGetTopElement.
@Test
public void testGetTopElement() {
try {
XSDFactory factory = XSDFactory.eINSTANCE;
//
XSDElementDeclaration concept = factory.createXSDElementDeclaration();
// concept with three children
// $NON-NLS-1$
String element1 = "Id";
// $NON-NLS-1$
String element2 = "code";
// $NON-NLS-1$
String element3 = "address";
XSDSchema xsdSchema = factory.createXSDSchema();
xsdSchema.getContents().add(concept);
XSDComplexTypeDefinition xsdComplexTypeDef = factory.createXSDComplexTypeDefinition();
xsdComplexTypeDef.setBaseTypeDefinition(// $NON-NLS-1$
xsdSchema.resolveComplexTypeDefinition(xsdSchema.getSchemaForSchemaNamespace(), "anyType"));
xsdSchema.getContents().add(xsdComplexTypeDef);
XSDParticle xsdParticle = factory.createXSDParticle();
xsdComplexTypeDef.setContent(xsdParticle);
XSDModelGroup xsdModelGroup = factory.createXSDModelGroup();
xsdParticle.setContent(xsdModelGroup);
concept.setAnonymousTypeDefinition(xsdComplexTypeDef);
XSDParticle childParticle1 = factory.createXSDParticle();
XSDElementDeclaration childElement1 = factory.createXSDElementDeclaration();
childElement1.setName(element1);
childParticle1.setContent(childElement1);
XSDParticle childParticle2 = factory.createXSDParticle();
XSDElementDeclaration childElement2 = factory.createXSDElementDeclaration();
childElement2.setName(element2);
childParticle2.setContent(childElement2);
XSDParticle childParticle3 = factory.createXSDParticle();
XSDElementDeclaration childElement3 = factory.createXSDElementDeclaration();
childElement3.setName(element3);
childParticle3.setContent(childElement3);
xsdModelGroup.getContents().add(childParticle1);
xsdModelGroup.getContents().add(childParticle2);
xsdModelGroup.getContents().add(childParticle3);
Object primaryKey = Util.getTopElement(concept, childElement1);
assertEquals(element1, primaryKey);
XSDElementDeclaration elementDecl = factory.createXSDElementDeclaration();
elementDecl.setTypeDefinition(factory.createXSDSimpleTypeDefinition());
primaryKey = Util.getTopElement(elementDecl, childElement1);
assertNull(primaryKey);
//
XSDComplexTypeDefinition xsdComplexTypeDef2 = factory.createXSDComplexTypeDefinition();
xsdComplexTypeDef2.setBaseTypeDefinition(xsdComplexTypeDef);
XSDParticle xsdParticle2 = factory.createXSDParticle();
xsdComplexTypeDef2.setContent(xsdParticle2);
XSDModelGroup xsdModelGroup2 = factory.createXSDModelGroup();
xsdParticle2.setContent(xsdModelGroup2);
XSDParticle childParticle_2 = factory.createXSDParticle();
XSDElementDeclaration childElement_2 = factory.createXSDElementDeclaration();
childElement_2.setName(element1);
childParticle_2.setContent(childElement_2);
xsdModelGroup2.getContents().add(childParticle_2);
xsdSchema.getContents().add(xsdComplexTypeDef2);
concept.setTypeDefinition(xsdComplexTypeDef2);
primaryKey = Util.getTopElement(concept, childElement1);
assertEquals(element1, primaryKey);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
Aggregations