Search in sources :

Example 46 with XSDFactory

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

the class XSDNewComplexTypeDefinition method doAction.

public IStatus doAction() {
    List<XSDComplexTypeDefinition> list = new ArrayList<XSDComplexTypeDefinition>();
    EList<XSDTypeDefinition> types = schema.getTypeDefinitions();
    for (XSDTypeDefinition type : types) {
        if (type instanceof XSDComplexTypeDefinition)
            list.add((XSDComplexTypeDefinition) type);
    }
    // $NON-NLS-1$
    dialog = new ComplexTypeInputDialog(this, page.getSite().getShell(), "", schema, null, list, false);
    dialog.setBlockOnOpen(true);
    int ret = dialog.open();
    if (ret == Dialog.CANCEL) {
        return Status.CANCEL_STATUS;
    }
    XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
    XSDModelGroup group = factory.createXSDModelGroup();
    XSDParticle subParticle = null;
    XSDElementDeclaration subElement = null;
    subElement = factory.createXSDElementDeclaration();
    // $NON-NLS-1$
    subElement.setName("subelement");
    // $NON-NLS-1$
    subElement.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), "string"));
    subParticle = factory.createXSDParticle();
    subParticle.unsetMaxOccurs();
    subParticle.unsetMinOccurs();
    subParticle.setContent(subElement);
    subParticle.updateElement();
    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();
    XSDComplexTypeDefinition complexType = factory.createXSDComplexTypeDefinition();
    // complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
    XSDTypeDefinition superType = null;
    for (XSDTypeDefinition type : list) {
        if (type.getName().equals(superTypeName)) {
            superType = type;
            break;
        }
    }
    complexType.setName(typeName);
    if (superType != null) {
        if (!checkParentType(superType, group)) {
            return Status.CANCEL_STATUS;
        }
        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
    XSDParticle groupParticle = factory.createXSDParticle();
    groupParticle.unsetMaxOccurs();
    groupParticle.unsetMinOccurs();
    groupParticle.setContent(group);
    groupParticle.updateElement();
    complexType.setContent(groupParticle);
    complexType.updateElement();
    page.refresh();
    page.markDirty();
    return Status.OK_STATUS;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) ArrayList(java.util.ArrayList) ComplexTypeInputDialog(com.amalto.workbench.dialogs.ComplexTypeInputDialog) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition)

Example 47 with XSDFactory

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

the class XSDNewElementAction method widgetSelected.

/**
 * author: fliu this fun is to support button click event invoked from the new expansion of Element creation dialog
 */
public void widgetSelected(SelectionEvent e) {
    // $NON-NLS-1$
    NewConceptOrElementDialog dlg = (NewConceptOrElementDialog) ((Widget) e.getSource()).getData("dialog");
    if (dlg.getReturnCode() == Window.OK) {
        XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
        XSDElementDeclaration decl = factory.createXSDElementDeclaration();
        decl.setName(dlg.getTypeName());
        // $NON-NLS-1$
        decl.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), "string"));
        schema.getContents().add(decl);
        // schema.getElementDeclarations().add(decl);
        decl.updateElement();
        // page.markDirty();
        UndoAction changeAction = null;
        if (dlg.isComplexType()) {
            changeAction = new XSDChangeToComplexTypeAction(page, decl, dlg.getComplexType(), dlg.isChoice(), dlg.isAll(), dlg.isAbstract(), dlg.getSuperTypeName());
        } else {
            changeAction = new XSDChangeToSimpleTypeAction(page, decl, dlg.getElementType(), dlg.isBuildIn());
        }
        dlg.close();
        changeAction.setOmitTrack(true);
        IStatus status = changeAction.execute();
        if (status == Status.CANCEL_STATUS) {
            schema.getContents().remove(decl);
        }
        page.refresh();
    }
}
Also used : NewConceptOrElementDialog(com.amalto.workbench.dialogs.NewConceptOrElementDialog) IStatus(org.eclipse.core.runtime.IStatus) XSDFactory(org.eclipse.xsd.XSDFactory) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration)

Example 48 with XSDFactory

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

the class XSDNewGroupFromParticleAction 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 iter = group.getContents().iterator(); iter.hasNext(); ) {
            XSDParticle p = (XSDParticle) iter.next();
            if (p.equals(selParticle)) {
                index = i;
                break;
            }
            i++;
        }
        dialog = new NewGroupDialog(this, page.getSite().getShell());
        dialog.setBlockOnOpen(true);
        int ret = dialog.open();
        if (ret == Dialog.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
        // add an element declaration
        XSDElementDeclaration subElement = factory.createXSDElementDeclaration();
        // $NON-NLS-1$
        subElement.setName("subelement");
        // $NON-NLS-1$
        subElement.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), "string"));
        XSDParticle subParticle = factory.createXSDParticle();
        subParticle.setMinOccurs(1);
        subParticle.setMaxOccurs(1);
        subParticle.setContent(subElement);
        subParticle.updateElement();
        XSDModelGroup newGroup = factory.createXSDModelGroup();
        if (isChoice)
            newGroup.setCompositor(XSDCompositor.CHOICE_LITERAL);
        else if (isAll)
            newGroup.setCompositor(XSDCompositor.ALL_LITERAL);
        else
            newGroup.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
        newGroup.getContents().add(0, subParticle);
        newGroup.updateElement();
        XSDParticle particle = factory.createXSDParticle();
        particle.setContent(newGroup);
        particle.setMinOccurs(this.minOccurs);
        particle.setMaxOccurs(this.maxOccurs);
        group.getContents().add(index + 1, particle);
        group.updateElement();
        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.XSDNewGroupFromParticleAction_ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.CANCEL_STATUS;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) Iterator(java.util.Iterator) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) XSDParticle(org.eclipse.xsd.XSDParticle) NewGroupDialog(com.amalto.workbench.dialogs.NewGroupDialog)

Example 49 with XSDFactory

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

the class XSDNewGroupFromTypeAction method doAction.

public IStatus doAction() {
    try {
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        if (selection.getFirstElement() instanceof XSDComplexTypeDefinition) {
            XSDComplexTypeDefinition ctd = (XSDComplexTypeDefinition) selection.getFirstElement();
            if (!(ctd.getContent() instanceof XSDParticle))
                return Status.CANCEL_STATUS;
            if (!(((XSDParticle) ctd.getContent()).getTerm() instanceof XSDModelGroup))
                return Status.CANCEL_STATUS;
            group = (XSDModelGroup) ((XSDParticle) ctd.getContent()).getTerm();
        } else if (selection.getFirstElement() instanceof XSDParticle) {
            group = (XSDModelGroup) ((XSDParticle) selection.getFirstElement()).getTerm();
        } else {
            return Status.CANCEL_STATUS;
        }
        dialog = new NewGroupDialog(this, page.getSite().getShell());
        dialog.setBlockOnOpen(true);
        int ret = dialog.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
        // add an element declaration
        XSDElementDeclaration subElement = factory.createXSDElementDeclaration();
        // $NON-NLS-1$
        subElement.setName("subelement");
        // $NON-NLS-1$
        subElement.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), "string"));
        XSDParticle subParticle = factory.createXSDParticle();
        subParticle.setMinOccurs(1);
        subParticle.setMaxOccurs(1);
        subParticle.setContent(subElement);
        subParticle.updateElement();
        XSDModelGroup newGroup = factory.createXSDModelGroup();
        if (isChoice)
            newGroup.setCompositor(XSDCompositor.CHOICE_LITERAL);
        else if (isAll)
            newGroup.setCompositor(XSDCompositor.ALL_LITERAL);
        else
            newGroup.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
        newGroup.getContents().add(0, subParticle);
        newGroup.updateElement();
        XSDParticle particle = factory.createXSDParticle();
        particle.setContent(newGroup);
        particle.setMinOccurs(this.minOccurs);
        particle.setMaxOccurs(this.maxOccurs);
        group.getContents().add(0, particle);
        group.updateElement();
        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.XSDNewGroupFromTypeAction_ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) NewGroupDialog(com.amalto.workbench.dialogs.NewGroupDialog)

Example 50 with XSDFactory

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

the class XSDNewIdentityConstraintAction method doAction.

@Override
public IStatus doAction() {
    try {
        int index = -1;
        // EList list = schema.getIdentityConstraintDefinitions();
        // schema.getElement();
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        List<String> childNames = new ArrayList<String>();
        if (selection.getFirstElement() instanceof XSDElementDeclaration) {
            decl = (XSDElementDeclaration) selection.getFirstElement();
        // childNames = Util.getChildElementNames(decl.getElement());
        } else if (selection.getFirstElement() instanceof XSDIdentityConstraintDefinition) {
            XSDIdentityConstraintDefinition selIcd = (XSDIdentityConstraintDefinition) selection.getFirstElement();
            decl = (XSDElementDeclaration) (selIcd.getContainer());
            // get position of the selected Identity Constraint in the container (Element Declaration)
            int i = 0;
            for (Iterator iter = decl.getIdentityConstraintDefinitions().iterator(); iter.hasNext(); ) {
                XSDIdentityConstraintDefinition ic = (XSDIdentityConstraintDefinition) iter.next();
                if (ic.equals(selIcd)) {
                    index = i;
                    break;
                }
                i++;
            }
        } else if (selection.getFirstElement() instanceof XSDParticle) {
            XSDParticle selParticle = (XSDParticle) selection.getFirstElement();
            if (!(selParticle.getTerm() instanceof XSDElementDeclaration))
                return Status.CANCEL_STATUS;
            decl = (XSDElementDeclaration) selParticle.getTerm();
        // childNames.add(decl.getName());
        } else {
            MessageDialog.openError(this.page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDNewIdentityConstraintAction_ErrorMsg, selection.getFirstElement().getClass().getName()));
            return Status.CANCEL_STATUS;
        }
        // $NON-NLS-1$
        childNames = Util.getChildElementNames("", decl);
        // filter the non top level fields
        List<String> topChilds = new ArrayList<String>();
        for (String child : childNames) {
            if (child.indexOf('/') == -1) {
                topChilds.add(child);
            }
        }
        dialog = new IdentityConstraintInputDialog(decl, page.getSite().getShell(), Messages.XSDNewIdentityConstraintAction_AddANewKey, topChilds, decl.getName());
        dialog.setBlockOnOpen(true);
        int ret = dialog.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        keyName = dialog.getKeyName();
        fieldName = dialog.getFieldName();
        type = dialog.getType();
        XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
        XSDIdentityConstraintDefinition icd = factory.createXSDIdentityConstraintDefinition();
        icd.setName(keyName);
        icd.setIdentityConstraintCategory(type);
        XSDXPathDefinition selector = factory.createXSDXPathDefinition();
        selector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
        // $NON-NLS-1$
        selector.setValue(".");
        icd.setSelector(selector);
        XSDXPathDefinition field = factory.createXSDXPathDefinition();
        field.setVariety(XSDXPathVariety.FIELD_LITERAL);
        // $NON-NLS-1$
        field.setValue(".");
        // if complex content set name of first field
        if (fieldName == null || fieldName.trim().length() == 0) {
            if (decl.getTypeDefinition() instanceof XSDComplexTypeDefinition) {
                XSDComplexTypeContent ctc = ((XSDComplexTypeDefinition) decl.getTypeDefinition()).getContent();
                if (ctc instanceof XSDParticle) {
                    if (((XSDParticle) ctc).getTerm() instanceof XSDModelGroup) {
                        XSDModelGroup mg = (XSDModelGroup) ((XSDParticle) ctc).getTerm();
                        if (mg.getContents().size() > 0)
                            if (mg.getContents().get(0).getTerm() instanceof XSDElementDeclaration)
                                field.setValue(((XSDElementDeclaration) (mg.getContents().get(0).getTerm())).getName());
                    }
                }
            }
        } else {
            field.setValue(fieldName);
        }
        icd.getFields().add(field);
        decl.getIdentityConstraintDefinitions().add(index + 1, icd);
        decl.updateElement();
        updateElementForAddedfield(icd, fieldName);
        page.refresh();
        page.getTreeViewer().setSelection(new StructuredSelection(icd), true);
        page.markDirty();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDNewIdentityConstraintAction_ErrorMsg2, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDComplexTypeContent(org.eclipse.xsd.XSDComplexTypeContent) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) ArrayList(java.util.ArrayList) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IdentityConstraintInputDialog(com.amalto.workbench.dialogs.IdentityConstraintInputDialog) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) Iterator(java.util.Iterator) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle)

Aggregations

XSDFactory (org.eclipse.xsd.XSDFactory)50 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)37 XSDParticle (org.eclipse.xsd.XSDParticle)27 XSDModelGroup (org.eclipse.xsd.XSDModelGroup)26 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)23 Test (org.junit.Test)19 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)15 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)13 ArrayList (java.util.ArrayList)12 XSDSchema (org.eclipse.xsd.XSDSchema)12 XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)12 XSDXPathDefinition (org.eclipse.xsd.XSDXPathDefinition)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)9 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)8 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)6 HashSet (java.util.HashSet)5 Iterator (java.util.Iterator)5 List (java.util.List)5