Search in sources :

Example 11 with XSDImport

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

the class UtilMockTest method testAddImport.

@Test
public void testAddImport() {
    // 2184
    // $NON-NLS-1$
    String namespace1 = "http://www.w3.org/XML/1998/namespace1";
    // $NON-NLS-1$
    String namespace2 = "http://www.w3.org/XML/1998/namespace2";
    // $NON-NLS-1$
    String namespace3 = "http://www.w3.org/XML/1998/namespace3";
    String[] namespaces = { namespace1, namespace2, namespace3 };
    // $NON-NLS-1$
    String methodToExecute = "addImport";
    XSDFactory factory = XSDFactory.eINSTANCE;
    XSDSchema xsdschema = factory.createXSDSchema();
    List<XSDImport> imports = new ArrayList<XSDImport>();
    for (int i = 0; i < 3; i++) {
        XSDImport importt = factory.createXSDImport();
        importt.setNamespace(namespaces[i]);
        imports.add(importt);
    }
    XSDImport importt = factory.createXSDImport();
    // $NON-NLS-1$
    importt.setNamespace("");
    imports.add(importt);
    xsdschema.getContents().add(imports.get(0));
    PowerMockito.mockStatic(Util.class);
    try {
        PowerMockito.when(Util.class, methodToExecute, any(XSDSchema.class), anyListOf(XSDImport.class)).thenCallRealMethod();
        Whitebox.invokeMethod(Util.class, methodToExecute, xsdschema, imports);
        EList<XSDSchemaContent> contents = xsdschema.getContents();
        assertTrue(contents.size() == 3);
        assertTrue(contents.contains(imports.get(0)));
        assertTrue(contents.contains(imports.get(1)));
        assertTrue(contents.contains(imports.get(2)));
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) ArrayList(java.util.ArrayList) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) XSDImport(org.eclipse.xsd.XSDImport) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XSDSchema(org.eclipse.xsd.XSDSchema) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with XSDImport

use of org.eclipse.xsd.XSDImport in project webtools.sourceediting by eclipse.

the class XSDSchemaDirectiveAdapter method getText.

public String getText() {
    XSDSchemaDirective directive = (XSDSchemaDirective) target;
    String result = "";
    String location = directive.getSchemaLocation();
    if (location == null || location.equals("")) {
        result = "(" + Messages._UI_LABEL_NO_LOCATION_SPECIFIED + ")";
    } else {
        result = location;
    }
    // (otherwise the namespace is obviously the same as the containing schema's)
    if (directive instanceof XSDImport) {
        XSDImport importObj = (XSDImport) directive;
        String namespace = importObj.getNamespace();
        if (namespace != null) {
            result += "  {" + namespace + "}";
        }
    }
    return result;
}
Also used : XSDSchemaDirective(org.eclipse.xsd.XSDSchemaDirective) XSDImport(org.eclipse.xsd.XSDImport)

Example 13 with XSDImport

use of org.eclipse.xsd.XSDImport in project webtools.sourceediting by eclipse.

the class XSDSchemaAdapter method notifyChanged.

public void notifyChanged(final Notification msg) {
    class CategoryNotification extends NotificationImpl {

        protected Object category;

        public CategoryNotification(Object category) {
            super(msg.getEventType(), msg.getOldValue(), msg.getNewValue(), msg.getPosition());
            this.category = category;
        }

        public Object getNotifier() {
            return category;
        }

        public Object getFeature() {
            return msg.getFeature();
        }
    }
    if (children == null) {
        getChildren();
    }
    Object newValue = msg.getNewValue();
    Object oldValue = msg.getOldValue();
    // Bug 245480 - Deletion of Include, Import and Redefine is not reflected in the Outline view
    // We only want to refresh the Directives folder for any changes to XSDDirectives.  The first case covers
    // changes to one directive, whereas the missing case as reported in bug 245480 covers changes to a list
    // of directives.
    boolean updateDirectivesCategory = false;
    if (oldValue instanceof XSDSchemaDirective) {
        updateDirectivesCategory = true;
    } else if (oldValue instanceof Collection) {
        Iterator iterator = ((Collection) oldValue).iterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            if (obj instanceof XSDSchemaDirective) {
                // if we find at least one directive, then we should refresh the folder
                updateDirectivesCategory = true;
                break;
            }
        }
    }
    if (newValue instanceof XSDInclude || newValue instanceof XSDImport || newValue instanceof XSDRedefine || // handle the case for delete directive
    (msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_Contents() && updateDirectivesCategory) || // updates to the imports/includes
    msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_IncorporatedVersions()) {
        CategoryAdapter adapter = getCategory(CategoryAdapter.DIRECTIVES);
        Assert.isTrue(adapter != null);
        XSDSchema xsdSchema = adapter.getXSDSchema();
        adapter.setChildren(getDirectives(xsdSchema));
        adapter.setAllChildren(getDirectives(xsdSchema));
        notifyListeners(new CategoryNotification(adapter), adapter.getText());
        return;
    } else if (msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_ElementDeclarations()) {
        CategoryAdapter adapter = getCategory(CategoryAdapter.ELEMENTS);
        Assert.isTrue(adapter != null);
        XSDSchema xsdSchema = adapter.getXSDSchema();
        adapter.setChildren(getGlobalElements(xsdSchema));
        adapter.setAllChildren(getGlobalElements(xsdSchema, true));
        notifyListeners(new CategoryNotification(adapter), adapter.getText());
        return;
    } else if (msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_AttributeDeclarations() || msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_AttributeGroupDefinitions()) {
        CategoryAdapter adapter = getCategory(CategoryAdapter.ATTRIBUTES);
        Assert.isTrue(adapter != null);
        XSDSchema xsdSchema = adapter.getXSDSchema();
        adapter.setChildren(getAttributeList(xsdSchema));
        adapter.setAllChildren(getAttributeList(xsdSchema, true));
        notifyListeners(new CategoryNotification(adapter), adapter.getText());
        return;
    } else if (msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_TypeDefinitions()) {
        CategoryAdapter adapter = getCategory(CategoryAdapter.TYPES);
        Assert.isTrue(adapter != null);
        XSDSchema xsdSchema = adapter.getXSDSchema();
        List types = getComplexTypes(xsdSchema);
        types.addAll(getSimpleTypes(xsdSchema));
        adapter.setChildren(types);
        adapter.setAllChildren(getTypes(xsdSchema, true));
        notifyListeners(new CategoryNotification(adapter), adapter.getText());
        return;
    } else if (msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_ModelGroupDefinitions()) {
        CategoryAdapter adapter = getCategory(CategoryAdapter.GROUPS);
        Assert.isTrue(adapter != null);
        XSDSchema xsdSchema = adapter.getXSDSchema();
        adapter.setChildren(getGroups(xsdSchema));
        adapter.setAllChildren(getGroups(xsdSchema, true));
        notifyListeners(new CategoryNotification(adapter), adapter.getText());
        return;
    } else if (msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_Annotations()) {
        return;
    } else if (msg.getFeature() == XSDPackage.eINSTANCE.getXSDSchema_SchemaLocation()) {
        notifyListeners(msg, null);
        return;
    }
    types = null;
    getTypes();
    super.notifyChanged(msg);
}
Also used : XSDRedefine(org.eclipse.xsd.XSDRedefine) NotificationImpl(org.eclipse.emf.common.notify.impl.NotificationImpl) XSDSchemaDirective(org.eclipse.xsd.XSDSchemaDirective) Iterator(java.util.Iterator) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) XSDImport(org.eclipse.xsd.XSDImport) XSDInclude(org.eclipse.xsd.XSDInclude) XSDSchema(org.eclipse.xsd.XSDSchema)

Example 14 with XSDImport

use of org.eclipse.xsd.XSDImport in project webtools.sourceediting by eclipse.

the class UpdateComponentReferenceAndManageDirectivesCommand method computeComponent.

protected XSDComponent computeComponent() {
    XSDComponent result = null;
    XSDSchema schema = concreteComponent.getSchema();
    XSDSchemaDirective directive = null;
    // lets see if the element is already visible to our schema
    result = getDefinedComponent(schema, componentName, componentNamespace);
    if (result == null) {
        // includes/imports to get to it
        if (componentNamespace != null && componentNamespace.equals(schema.getTargetNamespace())) {
            // we need to add an include
            // if the component's namespace is not null and matches the schema's
            // target namespace
            directive = XSDFactory.eINSTANCE.createXSDInclude();
        } else if (componentNamespace == null) {
            // we need to add an include
            // if the component's namespace is null, then we can just add it
            // only if the current namespace is not null
            directive = XSDFactory.eINSTANCE.createXSDInclude();
            // we have to ensure the schema for schema prefix is NOT null
            if (schema.getSchemaForSchemaQNamePrefix() == null) {
                String targetNS = schema.getTargetNamespace();
                if (targetNS == null)
                    targetNS = "";
                // this will just update the schema for schema prefix to be, say, xsd
                UpdateNamespaceInformationCommand command = new UpdateNamespaceInformationCommand("", schema, "", targetNS);
                command.execute();
            }
        } else {
            // we need to add an import
            XSDImport xsdImport = XSDFactory.eINSTANCE.createXSDImport();
            xsdImport.setNamespace(componentNamespace);
            directive = xsdImport;
        }
        String location = computeNiceLocation(schema.getSchemaLocation(), file);
        directive.setSchemaLocation(location);
        // TODO (cs) we should at the directive 'next' in the list of directives
        // for now I'm just adding as the first thing in the schema :-(
        // 
        schema.getContents().add(0, directive);
        XSDSchema resolvedSchema = directive.getResolvedSchema();
        if (resolvedSchema == null) {
            String platformLocation = "platform:/resource" + file.getFullPath();
            Resource resource = concreteComponent.eResource().getResourceSet().createResource(URI.createURI(platformLocation));
            if (resource instanceof XSDResourceImpl) {
                try {
                    resource.load(null);
                    XSDResourceImpl resourceImpl = (XSDResourceImpl) resource;
                    resolvedSchema = resourceImpl.getSchema();
                    if (resolvedSchema != null) {
                        directive.setResolvedSchema(resolvedSchema);
                    }
                } catch (Exception e) {
                }
            }
        }
        if (resolvedSchema != null) {
            result = getDefinedComponent(resolvedSchema, componentName, componentNamespace);
        } else {
        // TODO (cs) consider setting some error state so that the client can
        // provide a pop-dialog error
        // we should also remove the import/include so save from cluttering
        // the file with bogus directives
        }
    }
    return result;
}
Also used : XSDSchemaDirective(org.eclipse.xsd.XSDSchemaDirective) Resource(org.eclipse.emf.ecore.resource.Resource) XSDResourceImpl(org.eclipse.xsd.util.XSDResourceImpl) XSDImport(org.eclipse.xsd.XSDImport) XSDComponent(org.eclipse.xsd.XSDComponent) XSDSchema(org.eclipse.xsd.XSDSchema)

Aggregations

XSDImport (org.eclipse.xsd.XSDImport)14 XSDSchema (org.eclipse.xsd.XSDSchema)7 XSDInclude (org.eclipse.xsd.XSDInclude)6 ArrayList (java.util.ArrayList)4 XSDRedefine (org.eclipse.xsd.XSDRedefine)4 XSDSchemaContent (org.eclipse.xsd.XSDSchemaContent)4 XSDSchemaDirective (org.eclipse.xsd.XSDSchemaDirective)3 XSDImportImpl (org.eclipse.xsd.impl.XSDImportImpl)3 WSDataModel (com.amalto.workbench.webservices.WSDataModel)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2 EObject (org.eclipse.emf.ecore.EObject)2 TypesHelper (org.eclipse.wst.xsd.ui.internal.util.TypesHelper)2 XSDFactory (org.eclipse.xsd.XSDFactory)2 Element (org.w3c.dom.Element)2 MissingJarsException (com.amalto.workbench.service.MissingJarsException)1 InaccessibleWSDLException (com.sun.xml.internal.ws.wsdl.parser.InaccessibleWSDLException)1 IOException (java.io.IOException)1