Search in sources :

Example 1 with XSDInclude

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

the class Util method importSchema.

private static void importSchema(XSDSchema xsdSchema, List<XSDImport> imports, Map<String, Integer> schemaMonitor) {
    EList<XSDSchemaContent> list = xsdSchema.getContents();
    for (XSDSchemaContent schemaCnt : list) {
        if (schemaCnt instanceof XSDImport) {
            XSDImportImpl xsdImpl = (XSDImportImpl) schemaCnt;
            if (xsdImpl.getNamespace() == null || xsdImpl.getNamespace().trim().equals("")) {
                // $NON-NLS-1$
                URI fileURI = URI.createFileURI(xsdImpl.getSchemaLocation());
                // $NON-NLS-1$//$NON-NLS-2$
                xsdImpl.setNamespace(fileURI.toFileString().replaceAll("[\\W]", ""));
            }
            if (xsdImpl.getSchemaLocation() == null) {
                continue;
            }
            ((XSDImportImpl) schemaCnt).importSchema();
            imports.add(((XSDImportImpl) schemaCnt));
        } else if (schemaCnt instanceof XSDInclude) {
            XSDIncludeImpl xsdInclude = (XSDIncludeImpl) schemaCnt;
            ((XSDSchemaImpl) xsdSchema).included(xsdInclude);
        }
    }
}
Also used : XSDImportImpl(org.eclipse.xsd.impl.XSDImportImpl) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) XSDImport(org.eclipse.xsd.XSDImport) XSDIncludeImpl(org.eclipse.xsd.impl.XSDIncludeImpl) URI(org.eclipse.emf.common.util.URI) XSDInclude(org.eclipse.xsd.XSDInclude)

Example 2 with XSDInclude

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

the class SchemaTreeContentProvider method getXSDSchemaChildren.

protected Object[] getXSDSchemaChildren(XSDSchema schema) {
    List<XSDElementDeclaration> declarations = new ArrayList<XSDElementDeclaration>();
    List<Object> attributeDeclarations = new ArrayList<Object>();
    if (null != xsdSchema) {
        for (XSDSchemaContent cnt : xsdSchema.getContents()) {
            if (cnt instanceof XSDInclude) {
                XSDInclude incu = (XSDInclude) cnt;
                String schemaLocation = incu.getSchemaLocation();
                XSDSchema schemaInc = createSchema(schemaLocation);
                addElementDeclarationFromSchema(schemaInc, declarations);
            } else if (cnt instanceof XSDAttributeDeclaration) {
                XSDAttributeDeclaration attriDec = (XSDAttributeDeclaration) cnt;
                attributeDeclarations.add(attriDec);
            }
        }
    }
    addElementDeclarationFromSchema(schema, declarations);
    Object[] schemaChildren = Util.filterOutDuplicatedElems(declarations.toArray(new XSDNamedComponent[declarations.size()]));
    attributeDeclarations.addAll(Arrays.asList(schemaChildren));
    return attributeDeclarations.toArray();
}
Also used : XSDNamedComponent(org.eclipse.xsd.XSDNamedComponent) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) ArrayList(java.util.ArrayList) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) TreeObject(com.amalto.workbench.models.TreeObject) XSDAttributeDeclaration(org.eclipse.xsd.XSDAttributeDeclaration) XSDInclude(org.eclipse.xsd.XSDInclude) XSDSchema(org.eclipse.xsd.XSDSchema)

Example 3 with XSDInclude

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

the class CommonDirectivesSection method doHandleEvent.

// TODO: common up code with XSDSelectIncludeFileWizard
public void doHandleEvent(Event event) {
    // $NON-NLS-1$
    errorText.setText("");
    if (event.widget == schemaLocationText) {
        // $NON-NLS-1$
        String errorMessage = "";
        isValidSchemaLocation = true;
        String xsdModelFile = schemaLocationText.getText();
        // $NON-NLS-1$
        String namespace = "";
        XSDSchema externalSchema = null;
        if (xsdModelFile.length() == 0) {
            // $NON-NLS-1$
            handleSchemaLocationChange(xsdModelFile, "", null);
            return;
        }
        try {
            IFile currentIFile = null;
            IEditorInput editorInput = getActiveEditor().getEditorInput();
            if (editorInput instanceof IFileEditorInput) {
                currentIFile = ((IFileEditorInput) editorInput).getFile();
            }
            URI newURI = URI.createURI(xsdModelFile);
            String xsdFile = URIHelper.getRelativeURI(newURI.toString(), currentIFile.getFullPath().toString());
            // $NON-NLS-1$
            final String normalizedXSDFile = URIHelper.normalize(xsdFile, currentIFile.getLocation().toString(), "");
            final String normalizedURI = URI.encodeFragment(URIHelper.addImpliedFileProtocol(normalizedXSDFile), true).toString();
            XSDParser parser = new XSDParser(new HashMap());
            parser.parse(normalizedURI);
            externalSchema = parser.getSchema();
            if (externalSchema != null) {
                String extNamespace = externalSchema.getTargetNamespace();
                // $NON-NLS-1$
                if (extNamespace == null)
                    extNamespace = "";
                namespace = extNamespace;
                if (externalSchema.getDiagnostics() != null && externalSchema.getDiagnostics().size() > 0) {
                    isValidSchemaLocation = false;
                    // $NON-NLS-1$
                    errorMessage = XSDEditorPlugin.getResourceString("_UI_INCORRECT_XML_SCHEMA", xsdModelFile);
                } else {
                    String currentNameSpace = xsdSchema.getTargetNamespace();
                    if (input instanceof XSDInclude || input instanceof XSDRedefine) {
                        // Check the namespace to make sure they are the same as current file
                        if (extNamespace != null) {
                            if (currentNameSpace != null && !extNamespace.equals(currentNameSpace)) {
                                // $NON-NLS-1$
                                errorMessage = XSDEditorPlugin.getResourceString("_UI_DIFFERENT_NAME_SPACE", xsdModelFile);
                                isValidSchemaLocation = false;
                            }
                        }
                    } else {
                        // Check the namespace to make sure they are different from the current file
                        if (extNamespace != null) {
                            if (currentNameSpace != null && extNamespace.equals(currentNameSpace)) {
                                // $NON-NLS-1$
                                errorMessage = XSDEditorPlugin.getResourceString("_UI_SAME_NAME_SPACE", xsdModelFile);
                                isValidSchemaLocation = false;
                            }
                        }
                    }
                }
            } else {
                errorMessage = Messages._UI_ERROR_INVALID_FILE;
                isValidSchemaLocation = false;
            }
        } catch (Exception e) {
            errorMessage = Messages._UI_ERROR_INVALID_FILE;
            isValidSchemaLocation = false;
        } finally {
            if (!isValidSchemaLocation) {
                errorText.setText(errorMessage);
                int length = errorText.getText().length();
                red = new Color(null, 255, 0, 0);
                StyleRange style = new StyleRange(0, length, red, schemaLocationText.getBackground());
                errorText.setStyleRange(style);
            } else {
                handleSchemaLocationChange(xsdModelFile, namespace, externalSchema);
            }
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) XSDRedefine(org.eclipse.xsd.XSDRedefine) HashMap(java.util.HashMap) Color(org.eclipse.swt.graphics.Color) StyleRange(org.eclipse.swt.custom.StyleRange) URI(org.eclipse.emf.common.util.URI) IFileEditorInput(org.eclipse.ui.IFileEditorInput) XSDParser(org.eclipse.xsd.util.XSDParser) IEditorInput(org.eclipse.ui.IEditorInput) XSDSchema(org.eclipse.xsd.XSDSchema) XSDInclude(org.eclipse.xsd.XSDInclude)

Example 4 with XSDInclude

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

the class XSDCommonUIUtils method getInputXSDAnnotation.

public static XSDAnnotation getInputXSDAnnotation(XSDConcreteComponent input, boolean createIfNotExist) {
    XSDAnnotation xsdAnnotation = null;
    XSDFactory factory = XSDFactory.eINSTANCE;
    if (input instanceof XSDAttributeDeclaration) {
        XSDAttributeDeclaration xsdComp = (XSDAttributeDeclaration) input;
        xsdAnnotation = xsdComp.getAnnotation();
        if (createIfNotExist && xsdAnnotation == null) {
            xsdAnnotation = factory.createXSDAnnotation();
            xsdComp.setAnnotation(xsdAnnotation);
        }
    } else if (input instanceof XSDAttributeGroupDefinition) {
        XSDAttributeGroupDefinition xsdComp = (XSDAttributeGroupDefinition) input;
        xsdAnnotation = xsdComp.getAnnotation();
        if (createIfNotExist && xsdAnnotation == null) {
            xsdAnnotation = factory.createXSDAnnotation();
            xsdComp.setAnnotation(xsdAnnotation);
        }
    } else if (input instanceof XSDElementDeclaration) {
        XSDElementDeclaration xsdComp = (XSDElementDeclaration) input;
        xsdAnnotation = xsdComp.getAnnotation();
        if (createIfNotExist && xsdAnnotation == null) {
            xsdAnnotation = factory.createXSDAnnotation();
            xsdComp.setAnnotation(xsdAnnotation);
        }
    } else if (input instanceof XSDNotationDeclaration) {
        XSDNotationDeclaration xsdComp = (XSDNotationDeclaration) input;
        xsdAnnotation = xsdComp.getAnnotation();
        if (createIfNotExist && xsdAnnotation == null) {
            xsdAnnotation = factory.createXSDAnnotation();
            xsdComp.setAnnotation(xsdAnnotation);
        }
    } else if (input instanceof XSDXPathDefinition) {
        XSDXPathDefinition xsdComp = (XSDXPathDefinition) input;
        xsdAnnotation = xsdComp.getAnnotation();
        if (createIfNotExist && xsdAnnotation == null) {
            xsdAnnotation = factory.createXSDAnnotation();
            xsdComp.setAnnotation(xsdAnnotation);
        }
    } else if (input instanceof XSDModelGroup) {
        XSDModelGroup xsdComp = (XSDModelGroup) input;
        xsdAnnotation = xsdComp.getAnnotation();
        if (createIfNotExist && xsdAnnotation == null) {
            xsdAnnotation = factory.createXSDAnnotation();
            xsdComp.setAnnotation(xsdAnnotation);
        }
    } else if (input instanceof XSDModelGroupDefinition) {
        XSDModelGroupDefinition xsdComp = (XSDModelGroupDefinition) input;
        xsdAnnotation = xsdComp.getAnnotation();
        if (createIfNotExist && xsdAnnotation == null) {
            xsdAnnotation = factory.createXSDAnnotation();
            xsdComp.setAnnotation(xsdAnnotation);
        }
    } else if (input instanceof XSDIdentityConstraintDefinition) {
        XSDIdentityConstraintDefinition xsdComp = (XSDIdentityConstraintDefinition) input;
        xsdAnnotation = xsdComp.getAnnotation();
        if (createIfNotExist && xsdAnnotation == null) {
            xsdAnnotation = factory.createXSDAnnotation();
            xsdComp.setAnnotation(xsdAnnotation);
        }
    } else if (input instanceof XSDWildcard) {
        XSDWildcard xsdComp = (XSDWildcard) input;
        xsdAnnotation = xsdComp.getAnnotation();
        if (createIfNotExist && xsdAnnotation == null) {
            xsdAnnotation = factory.createXSDAnnotation();
            xsdComp.setAnnotation(xsdAnnotation);
        }
    } else if (input instanceof XSDSchema) {
        XSDSchema xsdComp = (XSDSchema) input;
        List list = xsdComp.getAnnotations();
        if (list.size() > 0) {
            xsdAnnotation = (XSDAnnotation) list.get(0);
        } else {
            if (createIfNotExist && xsdAnnotation == null) {
                xsdAnnotation = factory.createXSDAnnotation();
                if (xsdComp.getContents() != null) {
                    xsdComp.getContents().add(0, xsdAnnotation);
                }
            }
        }
        return xsdAnnotation;
    } else if (input instanceof XSDFacet) {
        XSDFacet xsdComp = (XSDFacet) input;
        xsdAnnotation = xsdComp.getAnnotation();
        if (createIfNotExist && xsdAnnotation == null) {
            xsdAnnotation = factory.createXSDAnnotation();
            xsdComp.setAnnotation(xsdAnnotation);
        }
    } else if (input instanceof XSDTypeDefinition) {
        XSDTypeDefinition xsdComp = (XSDTypeDefinition) input;
        xsdAnnotation = xsdComp.getAnnotation();
        if (createIfNotExist && xsdAnnotation == null) {
            xsdAnnotation = factory.createXSDAnnotation();
            xsdComp.setAnnotation(xsdAnnotation);
        }
    } else if (input instanceof XSDInclude) {
        XSDInclude xsdComp = (XSDInclude) input;
        xsdAnnotation = xsdComp.getAnnotation();
        if (createIfNotExist && xsdAnnotation == null) {
            xsdAnnotation = factory.createXSDAnnotation();
            xsdComp.setAnnotation(xsdAnnotation);
        }
    } else if (input instanceof XSDImport) {
        XSDImport xsdComp = (XSDImport) input;
        xsdAnnotation = xsdComp.getAnnotation();
        if (createIfNotExist && xsdAnnotation == null) {
            xsdAnnotation = factory.createXSDAnnotation();
            xsdComp.setAnnotation(xsdAnnotation);
        }
    } else if (input instanceof XSDRedefine) {
        XSDRedefine xsdComp = (XSDRedefine) input;
        List contents = xsdComp.getContents();
        for (int i = 0; i < contents.size(); i++) {
            Object content = contents.get(i);
            if (content instanceof XSDAnnotation) {
                xsdAnnotation = (XSDAnnotation) content;
                break;
            }
        }
        if (createIfNotExist && xsdAnnotation == null) {
            xsdAnnotation = factory.createXSDAnnotation();
            contents.add(0, xsdAnnotation);
        }
        return xsdAnnotation;
    } else if (input instanceof XSDAnnotation) {
        xsdAnnotation = (XSDAnnotation) input;
    }
    if (createIfNotExist) {
        formatAnnotation(xsdAnnotation);
    }
    return xsdAnnotation;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDRedefine(org.eclipse.xsd.XSDRedefine) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDWildcard(org.eclipse.xsd.XSDWildcard) XSDAttributeGroupDefinition(org.eclipse.xsd.XSDAttributeGroupDefinition) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) XSDNotationDeclaration(org.eclipse.xsd.XSDNotationDeclaration) XSDFacet(org.eclipse.xsd.XSDFacet) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) EObject(org.eclipse.emf.ecore.EObject) XSDModelGroupDefinition(org.eclipse.xsd.XSDModelGroupDefinition) XSDImport(org.eclipse.xsd.XSDImport) XSDAnnotation(org.eclipse.xsd.XSDAnnotation) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition) XSDAttributeDeclaration(org.eclipse.xsd.XSDAttributeDeclaration) XSDSchema(org.eclipse.xsd.XSDSchema) XSDInclude(org.eclipse.xsd.XSDInclude)

Example 5 with XSDInclude

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

the class XSDDirectivesSchemaLocationUpdater method updateSchemaLocation.

/**
 * Modifies the schema location by opening the schema location dialog and
 * processing the results. This method refactors the code in
 * XSDImportSection$widgetSelected and SchemaLocationSection$widgetSelected
 * and the processing in handleSchemaLocationChange()
 */
public static void updateSchemaLocation(XSDSchema xsdSchema, Object selection, boolean isInclude) {
    Shell shell = Display.getCurrent().getActiveShell();
    IFile currentIFile = null;
    IEditorInput editorInput = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorInput();
    ViewerFilter filter;
    if (editorInput instanceof IFileEditorInput) {
        currentIFile = ((IFileEditorInput) editorInput).getFile();
        filter = new // $NON-NLS-1$
        ResourceFilter(// $NON-NLS-1$
        new String[] { ".xsd" }, new IFile[] { currentIFile }, null);
    } else {
        filter = new // $NON-NLS-1$
        ResourceFilter(// $NON-NLS-1$
        new String[] { ".xsd" }, null, null);
    }
    XSDSelectIncludeFileWizard fileSelectWizard = new // $NON-NLS-1$
    XSDSelectIncludeFileWizard(// $NON-NLS-1$
    xsdSchema, // $NON-NLS-1$
    isInclude, // $NON-NLS-1$
    XSDEditorPlugin.getXSDString("_UI_FILEDIALOG_SELECT_XML_SCHEMA"), // $NON-NLS-1$
    XSDEditorPlugin.getXSDString("_UI_FILEDIALOG_SELECT_XML_DESC"), filter, new StructuredSelection(selection));
    WizardDialog wizardDialog = new WizardDialog(shell, fileSelectWizard);
    wizardDialog.create();
    wizardDialog.setBlockOnOpen(true);
    int result = wizardDialog.open();
    if (result == Window.OK) {
        IFile selectedIFile = fileSelectWizard.getResultFile();
        String schemaFileString;
        if (selectedIFile != null && currentIFile != null) {
            schemaFileString = URIHelper.getRelativeURI(selectedIFile.getLocation(), currentIFile.getLocation());
        } else if (selectedIFile != null && currentIFile == null) {
            schemaFileString = selectedIFile.getLocationURI().toString();
        } else {
            schemaFileString = fileSelectWizard.getURL();
        }
        // $NON-NLS-1$
        String attributeSchemaLocation = "schemaLocation";
        if (selection instanceof XSDImport) {
            XSDImport xsdImport = (XSDImport) selection;
            // $NON-NLS-1$
            xsdImport.getElement().setAttribute(attributeSchemaLocation, schemaFileString);
            String namespace = fileSelectWizard.getNamespace();
            if (namespace == null)
                // $NON-NLS-1$
                namespace = "";
            XSDSchema externalSchema = fileSelectWizard.getExternalSchema();
            java.util.Map map = xsdSchema.getQNamePrefixToNamespaceMap();
            Element schemaElement = xsdSchema.getElement();
            // update the xmlns in the schema element first, and then update the
            // import element next so that the last change will be in the import element. This keeps the
            // selection on the import element
            TypesHelper helper = new TypesHelper(externalSchema);
            String prefix = helper.getPrefix(namespace, false);
            if (map.containsKey(prefix)) {
                prefix = null;
            }
            if (prefix == null || (prefix != null && prefix.length() == 0)) {
                // $NON-NLS-1$
                StringBuffer newPrefix = new StringBuffer("pref");
                int prefixExtension = 1;
                while (map.containsKey(newPrefix.toString()) && prefixExtension < 100) {
                    // $NON-NLS-1$
                    newPrefix = new StringBuffer("pref" + String.valueOf(prefixExtension));
                    prefixExtension++;
                }
                prefix = newPrefix.toString();
            }
            // $NON-NLS-1$
            String attributeNamespace = "namespace";
            if (namespace.length() > 0) {
                // if ns already in map, use its corresponding prefix
                if (map.containsValue(namespace)) {
                    TypesHelper typesHelper = new TypesHelper(xsdSchema);
                    prefix = typesHelper.getPrefix(namespace, false);
                } else // otherwise add to the map
                {
                    // $NON-NLS-1$
                    schemaElement.setAttribute("xmlns:" + prefix, namespace);
                }
                // prefixText.setText(prefix);
                xsdImport.getElement().setAttribute(attributeNamespace, namespace);
            }
            // into the resource set
            if (// redundant
            selection instanceof XSDImportImpl) {
                XSDImportImpl xsdImportImpl = (XSDImportImpl) selection;
                xsdImportImpl.importSchema();
            }
        } else if (selection instanceof XSDInclude) {
            XSDInclude xsdInclude = (XSDInclude) selection;
            xsdInclude.getElement().setAttribute(attributeSchemaLocation, schemaFileString);
        } else if (selection instanceof XSDRedefine) {
            XSDRedefine xsdRedefine = (XSDRedefine) selection;
            xsdRedefine.getElement().setAttribute(attributeSchemaLocation, schemaFileString);
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) XSDRedefine(org.eclipse.xsd.XSDRedefine) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) Element(org.w3c.dom.Element) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) Shell(org.eclipse.swt.widgets.Shell) XSDImportImpl(org.eclipse.xsd.impl.XSDImportImpl) XSDSelectIncludeFileWizard(org.eclipse.wst.xsd.ui.internal.wizards.XSDSelectIncludeFileWizard) TypesHelper(org.eclipse.wst.xsd.ui.internal.util.TypesHelper) IFileEditorInput(org.eclipse.ui.IFileEditorInput) XSDImport(org.eclipse.xsd.XSDImport) WizardDialog(org.eclipse.jface.wizard.WizardDialog) IEditorInput(org.eclipse.ui.IEditorInput) XSDSchema(org.eclipse.xsd.XSDSchema) XSDInclude(org.eclipse.xsd.XSDInclude)

Aggregations

XSDInclude (org.eclipse.xsd.XSDInclude)11 XSDSchema (org.eclipse.xsd.XSDSchema)7 XSDImport (org.eclipse.xsd.XSDImport)6 XSDRedefine (org.eclipse.xsd.XSDRedefine)6 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 EObject (org.eclipse.emf.ecore.EObject)3 XSDSchemaContent (org.eclipse.xsd.XSDSchemaContent)3 HashMap (java.util.HashMap)2 List (java.util.List)2 IFile (org.eclipse.core.resources.IFile)2 URI (org.eclipse.emf.common.util.URI)2 IEditorInput (org.eclipse.ui.IEditorInput)2 IFileEditorInput (org.eclipse.ui.IFileEditorInput)2 XSDAttributeDeclaration (org.eclipse.xsd.XSDAttributeDeclaration)2 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)2 XSDSchemaDirective (org.eclipse.xsd.XSDSchemaDirective)2 XSDImportImpl (org.eclipse.xsd.impl.XSDImportImpl)2 TreeObject (com.amalto.workbench.models.TreeObject)1 WSDataModel (com.amalto.workbench.webservices.WSDataModel)1