Search in sources :

Example 11 with XSDModelGroup

use of org.eclipse.xsd.XSDModelGroup in project tmdm-studio-se by Talend.

the class XSDEditParticleAction method doAction.

@Override
public IStatus doAction() {
    try {
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        String originalXpath = getOriginalXpath();
        // $NON-NLS-1$
        String entity = originalXpath.substring(0, originalXpath.indexOf("/"));
        selParticle = (XSDParticle) selection.getFirstElement();
        if (!(selParticle.getTerm() instanceof XSDElementDeclaration)) {
            return Status.CANCEL_STATUS;
        }
        XSDElementDeclaration decl = (XSDElementDeclaration) selParticle.getContent();
        XSDElementDeclaration ref = null;
        if (decl.isElementDeclarationReference()) {
            // it is a ref
            ref = decl.getResolvedElementDeclaration();
        }
        EList eDecls = decl.getSchema().getElementDeclarations();
        ArrayList<String> elementDeclarations = new ArrayList<String>();
        for (Iterator iter = eDecls.iterator(); iter.hasNext(); ) {
            XSDElementDeclaration d = (XSDElementDeclaration) iter.next();
            if (d.getTargetNamespace() != null && d.getTargetNamespace().equals(IConstants.DEFAULT_NAME_SPACE)) {
                continue;
            }
            if (!d.getQName().equals(entity)) {
                elementDeclarations.add(// $NON-NLS-1$ //$NON-NLS-2$
                d.getQName() + (d.getTargetNamespace() != null ? " : " + d.getTargetNamespace() : ""));
            }
        }
        // $NON-NLS-1$
        elementDeclarations.add("");
        XSDIdentityConstraintDefinition identify = null;
        XSDXPathDefinition keyPath = null;
        List<Object> keyInfo = Util.getKeyInfo(decl);
        boolean isPK = false;
        if (keyInfo != null && keyInfo.size() > 0) {
            identify = (XSDIdentityConstraintDefinition) keyInfo.get(0);
            keyPath = (XSDXPathDefinition) keyInfo.get(1);
            isPK = true;
        }
        initEleName = decl.getName();
        dialog = new BusinessElementInputDialog(this, page.getSite().getShell(), Messages.XSDEditParticleAction_InputDialogTitle, decl.getName(), ref == null ? null : ref.getQName(), elementDeclarations, selParticle.getMinOccurs(), selParticle.getMaxOccurs(), false, isPK);
        dialog.setBlockOnOpen(true);
        int ret = dialog.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        if (keyPath != null) {
            identify.getFields().remove(keyPath);
        }
        // find reference
        XSDElementDeclaration newRef = null;
        if (!"".equals(refName.trim())) {
            // $NON-NLS-1$
            newRef = Util.findReference(refName, schema);
            if (newRef == null) {
                MessageDialog.openError(this.page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDEditParticleAction_ErrorMsg, refName));
                return Status.CANCEL_STATUS;
            }
        }
        // ref
        // update validation rule
        Set<String> paths = new HashSet<String>();
        Util.collectElementPaths((IStructuredContentProvider) page.getElementsViewer().getContentProvider(), page.getSite(), selParticle, paths, null);
        // 
        // $NON-NLS-1$
        decl.setName("".equals(this.elementName) ? null : this.elementName);
        if (keyPath != null) {
            XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
            XSDXPathDefinition field = factory.createXSDXPathDefinition();
            field.setVariety(keyPath.getVariety());
            field.setValue(elementName);
            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) {
            // fliu
            // no more element declarations --> we create a new Declaration with String simpleType definition
            // instead
            // FIXME: dereferecning and element is buggy
            XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
            XSDElementDeclaration newD = factory.createXSDElementDeclaration();
            newD.setName(this.elementName);
            newD.updateElement();
            XSDSimpleTypeDefinition stringType = ((SchemaTreeContentProvider) page.getTreeViewer().getContentProvider()).getXsdSchema().getSchemaForSchema().resolveSimpleTypeDefinition(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001, // $NON-NLS-1$
            "string");
            newD.setTypeDefinition(stringType);
            if (selParticle.getContainer() instanceof XSDModelGroup) {
                XSDModelGroup group = ((XSDModelGroup) selParticle.getContainer());
                ((XSDModelGroup) selParticle.getContainer()).getContents().remove(selParticle);
                selParticle = factory.createXSDParticle();
                selParticle.setContent(newD);
                group.getContents().add(selParticle);
            }
        }
        if (Util.changeElementTypeToSequence(decl, maxOccurs) == Status.CANCEL_STATUS) {
            return Status.CANCEL_STATUS;
        }
        selParticle.setMinOccurs(this.minOccurs);
        if (maxOccurs > -1) {
            selParticle.setMaxOccurs(this.maxOccurs);
        } else {
            if (selParticle.getElement().getAttributeNode("maxOccurs") != null) {
                // $NON-NLS-1$//$NON-NLS-2$
                selParticle.getElement().getAttributeNode("maxOccurs").setNodeValue("unbounded");
            } else {
                // $NON-NLS-1$//$NON-NLS-2$
                selParticle.getElement().setAttribute("maxOccurs", "unbounded");
            }
        }
        selParticle.updateElement();
        updateReference(originalXpath);
        if (elementExAdapter != null) {
            elementExAdapter.renameElement(decl.getSchema(), paths, decl.getName());
        }
        if (mapinfoExAdapter != null) {
            mapinfoExAdapter.renameElementMapinfo(paths, decl.getName());
        }
        page.refresh();
        page.markDirty();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDEditParticleAction_ErrorMsg1, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) Element(org.w3c.dom.Element) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) EList(org.eclipse.emf.common.util.EList) BusinessElementInputDialog(com.amalto.workbench.dialogs.BusinessElementInputDialog) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) Iterator(java.util.Iterator) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition) HashSet(java.util.HashSet)

Example 12 with XSDModelGroup

use of org.eclipse.xsd.XSDModelGroup in project tmdm-studio-se by Talend.

the class XSDAbstractNewXPathAction method updateElementForAddedfield.

protected void updateElementForAddedfield(XSDIdentityConstraintDefinition icd, String fieldName) {
    if (icd == null || fieldName == null)
        return;
    XSDElementDeclaration entity = (XSDElementDeclaration) icd.getContainer();
    XSDComplexTypeDefinition ctype = (XSDComplexTypeDefinition) entity.getTypeDefinition();
    if (ctype.getContent() instanceof XSDParticle) {
        XSDParticleImpl particle = (XSDParticleImpl) ctype.getContent();
        if (particle.getTerm() instanceof XSDModelGroup) {
            XSDModelGroup group = (XSDModelGroup) particle.getTerm();
            EList<XSDParticle> particles = group.getParticles();
            for (XSDParticle part : particles) {
                if (part.getTerm() instanceof XSDElementDeclaration) {
                    XSDElementDeclaration el = (XSDElementDeclaration) part.getTerm();
                    if (el.getTypeDefinition() instanceof XSDSimpleTypeDefinition) {
                        if (fieldName.equals(el.getName())) {
                            part.setMinOccurs(1);
                            part.setMaxOccurs(1);
                            break;
                        }
                    }
                }
            }
        }
    }
    entity.updateElement();
}
Also used : XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) XSDParticleImpl(org.eclipse.xsd.impl.XSDParticleImpl) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle)

Example 13 with XSDModelGroup

use of org.eclipse.xsd.XSDModelGroup in project tmdm-studio-se by Talend.

the class XSDAddComplexTypeElementAction method init.

private boolean init() {
    IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
    if (selection.getFirstElement() instanceof XSDParticle) {
        selParticle = (XSDParticle) selection.getFirstElement();
        if (!(selParticle.getContainer() instanceof XSDModelGroup)) {
            return false;
        }
        modelGroup = (XSDModelGroup) selParticle.getContainer();
    } else {
        if (selection.getFirstElement() instanceof XSDComplexTypeDefinition) {
            XSDComplexTypeDefinition ctd = (XSDComplexTypeDefinition) selection.getFirstElement();
            if (!(ctd.getContent() instanceof XSDParticle)) {
                return false;
            }
            if (!(((XSDParticle) ctd.getContent()).getTerm() instanceof XSDModelGroup)) {
                return false;
            }
            ;
            modelGroup = (XSDModelGroup) ((XSDParticle) ctd.getContent()).getTerm();
        } else if (selection.getFirstElement() instanceof XSDParticle) {
            modelGroup = (XSDModelGroup) ((XSDParticle) selection.getFirstElement()).getTerm();
        } else if (selection.getFirstElement() instanceof XSDModelGroup) {
            modelGroup = (XSDModelGroup) selection.getFirstElement();
        } else {
            log.info(Messages.bind(Messages._UnkownSection, selection.getFirstElement().getClass().getName(), selection.getFirstElement().toString()));
            return false;
        }
    }
    return true;
}
Also used : XSDModelGroup(org.eclipse.xsd.XSDModelGroup) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle)

Example 14 with XSDModelGroup

use of org.eclipse.xsd.XSDModelGroup in project tmdm-studio-se by Talend.

the class XSDAddComplexTypeElementAction method createParticle.

private XSDParticle createParticle() {
    XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
    XSDElementDeclaration resultElementDeclaration = factory.createXSDElementDeclaration();
    resultElementDeclaration.setName(elementName);
    resultElementDeclaration.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), defaultTypeName));
    XSDParticle resultParticle = factory.createXSDParticle();
    resultParticle.setContent(resultElementDeclaration);
    resultParticle.setMinOccurs(this.minOccurs);
    XSDModelGroup group = modelGroup;
    if (maxOccurs > -1) {
        resultParticle.setMaxOccurs(this.maxOccurs);
        group.getContents().add(group.getContents().size(), resultParticle);
        group.updateElement();
    } else {
        resultParticle.setMaxOccurs(this.maxOccurs);
        group.getContents().add(group.getContents().size(), resultParticle);
        group.updateElement();
        if (resultParticle.getElement().getAttributeNode("maxOccurs") != null) {
            // $NON-NLS-1$
            // $NON-NLS-1$//$NON-NLS-2$
            resultParticle.getElement().getAttributeNode("maxOccurs").setNodeValue("unbounded");
        } else {
            // $NON-NLS-1$//$NON-NLS-2$
            resultParticle.getElement().setAttribute("maxOccurs", "unbounded");
        }
    }
    Util.changeElementTypeToSequence(resultElementDeclaration, maxOccurs);
    if (dialogR.isInherit()) {
        XSDTerm totm = resultParticle.getTerm();
        XSDElementDeclaration concept = null;
        Object parent = Util.getParent(resultParticle);
        if (parent instanceof XSDElementDeclaration) {
            concept = (XSDElementDeclaration) parent;
        } else {
            concept = (XSDElementDeclaration) resultParticle.getContent();
        }
        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);
            }
        }
    }
    return resultParticle;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDAnnotationsStructure(com.amalto.workbench.utils.XSDAnnotationsStructure) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDTerm(org.eclipse.xsd.XSDTerm) XSDAnnotation(org.eclipse.xsd.XSDAnnotation) XSDParticle(org.eclipse.xsd.XSDParticle)

Example 15 with XSDModelGroup

use of org.eclipse.xsd.XSDModelGroup in project tmdm-studio-se by Talend.

the class XSDAddComplexTypeElementAction method transformToComplexType.

private boolean transformToComplexType(XSDParticle particle) {
    XSDElementDeclaration decl = (XSDElementDeclaration) particle.getContent();
    List<XSDComplexTypeDefinition> types = Util.getComplexTypes(schema);
    XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
    // $NON-NLS-1$
    boolean anonymous = (typeName == null) || ("".equals(typeName));
    boolean alreadyExists = false;
    XSDComplexTypeDefinition complexType = null;
    // the sub element created if needed
    XSDParticle subParticle = null;
    XSDParticle groupParticle = null;
    XSDElementDeclaration subElement = null;
    XSDElementDeclaration parent = null;
    Object pObject = Util.getParent(decl);
    if (pObject instanceof XSDElementDeclaration) {
        parent = (XSDElementDeclaration) pObject;
    }
    if (!anonymous) {
        if (typeName.lastIndexOf(" : ") != -1) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            typeName = typeName.substring(0, typeName.lastIndexOf(" : "));
        }
        for (XSDComplexTypeDefinition td : types) {
            if ((td.getName().equals(typeName))) {
                alreadyExists = true;
                complexType = td;
                break;
            }
        }
    } else {
        if (decl.getTypeDefinition() instanceof XSDSimpleTypeDefinition) {
            alreadyExists = false;
        }
    }
    if (alreadyExists) {
        XSDParticle partCnt = (XSDParticle) complexType.getContentType();
        partCnt.unsetMaxOccurs();
        partCnt.unsetMinOccurs();
        XSDTypeDefinition superType = null;
        for (XSDTypeDefinition type : types) {
            if (type.getName().equals(superTypeName)) {
                superType = type;
                break;
            }
        }
        if (superType != null) {
            XSDModelGroup mdlGrp = (XSDModelGroup) partCnt.getTerm();
            boolean status = updateCompositorType(superType, mdlGrp);
            if (!status) {
                return false;
            }
            complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
            complexType.setBaseTypeDefinition(superType);
        }
        if (isAbstract) {
            complexType.setAbstract(isAbstract);
        } else {
            complexType.unsetAbstract();
        }
        if (parent != null) {
            parent.updateElement();
        }
        if (complexType != null) {
            complexType.updateElement();
        }
    } else {
        // Create if does not exist
        // add an element declaration
        subElement = factory.createXSDElementDeclaration();
        // $NON-NLS-1$
        subElement.setName("subelement");
        subElement.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), defaultTypeName));
        subParticle = factory.createXSDParticle();
        subParticle.unsetMaxOccurs();
        subParticle.unsetMinOccurs();
        subParticle.setContent(subElement);
        subParticle.updateElement();
        // create group
        XSDModelGroup group = factory.createXSDModelGroup();
        if (isChoice) {
            group.setCompositor(XSDCompositor.CHOICE_LITERAL);
        } else if (isAll) {
            group.setCompositor(XSDCompositor.ALL_LITERAL);
        } else {
            group.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
        }
        group.getContents().add(0, subParticle);
        group.updateElement();
        // create the complex type
        complexType = factory.createXSDComplexTypeDefinition();
        // complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
        if (!anonymous) {
            // if (true) {
            XSDTypeDefinition superType = null;
            for (XSDTypeDefinition type : types) {
                if (type.getName().equals(superTypeName)) {
                    superType = type;
                    break;
                }
            }
            complexType.setName(typeName);
            if (superType != null) {
                boolean status = updateCompositorType(superType, group);
                if (!status) {
                    return false;
                }
                complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
                complexType.setBaseTypeDefinition(superType);
            }
            if (isAbstract) {
                complexType.setAbstract(isAbstract);
            } else {
                complexType.unsetAbstract();
            }
            schema.getContents().add(complexType);
        }
        complexType.updateElement();
        // add the group
        groupParticle = factory.createXSDParticle();
        groupParticle.unsetMaxOccurs();
        groupParticle.unsetMinOccurs();
        groupParticle.setContent(group);
        groupParticle.updateElement();
        complexType.setContent(groupParticle);
        complexType.updateElement();
    }
    // set complex type to concept
    if (anonymous) {
        decl.setAnonymousTypeDefinition(complexType);
    } else {
        decl.setTypeDefinition(complexType);
    }
    decl.updateElement();
    schema.update();
    return true;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition)

Aggregations

XSDModelGroup (org.eclipse.xsd.XSDModelGroup)119 XSDParticle (org.eclipse.xsd.XSDParticle)93 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)87 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)76 ArrayList (java.util.ArrayList)33 XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)32 XSDTerm (org.eclipse.xsd.XSDTerm)28 XSDFactory (org.eclipse.xsd.XSDFactory)26 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)26 Test (org.junit.Test)25 XSDSchema (org.eclipse.xsd.XSDSchema)23 XSDComplexTypeContent (org.eclipse.xsd.XSDComplexTypeContent)22 EList (org.eclipse.emf.common.util.EList)21 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)20 XSDModelGroupDefinition (org.eclipse.xsd.XSDModelGroupDefinition)19 List (java.util.List)18 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)18 Iterator (java.util.Iterator)16 XSDWildcard (org.eclipse.xsd.XSDWildcard)16 TreeObject (com.amalto.workbench.models.TreeObject)15