Search in sources :

Example 6 with XSDImport

use of org.eclipse.xsd.XSDImport 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)

Example 7 with XSDImport

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

the class XSDDirectivesManager method addToUnusedImports.

/**
 * From a list of used schemas, update the unusedImports list for the given schema
 *
 * @param schema
 * @param unusedImports
 * @param usedSchemas
 */
protected void addToUnusedImports(XSDSchema schema, List usedSchemas) {
    // now that we have the list of usedSchemas, get the list of unused
    // schemas by comparing this list to what is actually in the schema
    Iterator iter = schema.getContents().iterator();
    while (iter.hasNext()) {
        Object o = iter.next();
        if (o instanceof XSDSchemaDirective) {
            XSDSchemaDirective directive = (XSDSchemaDirective) o;
            boolean isUsed = false;
            Iterator iter2 = usedSchemas.iterator();
            while (iter2.hasNext()) {
                XSDSchema usedSchema = (XSDSchema) iter2.next();
                if (directive instanceof XSDImport && directive.getResolvedSchema() == usedSchema) {
                    isUsed = true;
                    break;
                }
                if (directive instanceof XSDInclude && ((XSDInclude) directive).getIncorporatedSchema() == usedSchema) {
                    isUsed = true;
                    usedDirectIncludes.add(usedSchema);
                    break;
                }
                // blindly accept redefines as used
                if (directive instanceof XSDRedefine) {
                    isUsed = true;
                    break;
                }
            }
            // If it is an include, we need to check if it is used indirectly
            if (directive instanceof XSDInclude && !isUsed) {
                XSDInclude inc = (XSDInclude) directive;
                XSDSchema incSchema = inc.getIncorporatedSchema();
                if (incSchema != null) {
                    XSDSchema usedSchema = getUsedIncludeSchema(incSchema, inc);
                    if (usedSchema != null) {
                        usedIndirectIncludes.add(directive);
                        usedIndirectIncludesMap.put(directive, usedSchema);
                        isUsed = true;
                    } else {
                        isUsed = false;
                    }
                }
            }
            // Also any redefines should be considered used
            if (!isUsed && !unusedDirectives.contains(directive) && !(directive instanceof XSDRedefine)) {
                unusedDirectives.add(directive);
            }
        }
    }
    Iterator iter3 = usedIndirectIncludes.iterator();
    while (iter3.hasNext()) {
        Object o = iter3.next();
        if (o instanceof XSDInclude) {
            XSDInclude inc = (XSDInclude) o;
            XSDSchema targetSchema = (XSDSchema) usedIndirectIncludesMap.get(inc);
            if (usedIncludeSchemas.contains(targetSchema) && usedDirectIncludes.contains(targetSchema)) {
                unusedDirectives.add(inc);
            } else {
                usedDirectIncludes.add(targetSchema);
            }
        }
    }
}
Also used : XSDSchemaDirective(org.eclipse.xsd.XSDSchemaDirective) XSDRedefine(org.eclipse.xsd.XSDRedefine) Iterator(java.util.Iterator) EObject(org.eclipse.emf.ecore.EObject) XSDImport(org.eclipse.xsd.XSDImport) XSDSchema(org.eclipse.xsd.XSDSchema) XSDInclude(org.eclipse.xsd.XSDInclude)

Example 8 with XSDImport

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

the class DataModelMainPage method save.

public int save(String xsd) {
    try {
        WSDataModel wsObject = (WSDataModel) (xobject.getWsObject());
        wsObject.setDescription(desAntionComposite.getText());
        String schema = xsd;
        if (xsd == null) {
            schema = ((SchemaTreeContentProvider) viewer.getContentProvider()).getXSDSchemaAsString();
            xsdSchema = ((SchemaTreeContentProvider) viewer.getContentProvider()).getXsdSchema();
        }
        XSDImport xsdImport = XSDFactory.eINSTANCE.createXSDImport();
        // $NON-NLS-1$
        xsdImport.setNamespace("http://www.w3.org/2001/XMLSchema");
        if (xsdSchema == null) {
            xsdSchema = ((SchemaTreeContentProvider) viewer.getContentProvider()).getXsdSchema();
        }
        if (xsdSchema == null || (xsd != null && !xsd.equals(wsObject.getXsdSchema())) || (xsd != null && !xsd.equals(getXSDSchemaString()))) {
            // 
            xsdSchema = Util.createXsdSchema(schema, xobject);
            // 
            getSchemaContentProvider().setXsdSchema(xsdSchema);
        }
        wsObject.setXsdSchema(schema);
        EList<XSDSchemaContent> elist = xsdSchema.getContents();
        for (XSDSchemaContent cnt : elist) {
            if (cnt instanceof XSDImport) {
                XSDImport imp = (XSDImport) cnt;
                if (imp.getNamespace().equals("http://www.w3.org/2001/XMLSchema")) {
                    // $NON-NLS-1$
                    xsdImport = null;
                    break;
                }
            }
        }
        if (xsdImport != null) {
            xsdSchema.getContents().add(0, xsdImport);
            wsObject.setXsdSchema(schema);
        }
        validateSchema();
        // save to db
        doSave(wsObject);
        dirty = false;
    } catch (Exception e) {
        dirty = true;
        log.error(e.getMessage(), e);
    }
    firePropertyChange(PROP_DIRTY);
    return dirty ? 1 : 0;
}
Also used : WSDataModel(com.amalto.workbench.webservices.WSDataModel) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) XSDImport(org.eclipse.xsd.XSDImport) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) SAXParseException(org.xml.sax.SAXParseException)

Example 9 with XSDImport

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

the class DataModelMainPage method importSchema.

protected void importSchema(InputSource source, String uri) throws Exception {
    // $NON-NLS-1$
    String ns = "";
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilderFactory.setValidating(false);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(source);
    // $NON-NLS-1$
    ns = document.getDocumentElement().getAttribute("targetNamespace");
    if (xsdSchema == null) {
        xsdSchema = getXSDSchema(Util.nodeToString(document));
    } else {
        WSDataModel wsObject = (WSDataModel) (xobject.getWsObject());
        xsdSchema = Util.createXsdSchema(wsObject.getXsdSchema(), xobject);
    }
    boolean exist = false;
    for (int i = 0; i < xsdSchema.getContents().size(); i++) {
        XSDSchemaContent xsdComp = xsdSchema.getContents().get(i);
        if (ns != null && !ns.equals("")) {
            // import xsdschema
            if (xsdComp instanceof XSDImport && ((XSDImport) xsdComp).getNamespace().equals(ns)) {
                for (Map.Entry entry : xsdSchema.getQNamePrefixToNamespaceMap().entrySet()) {
                    if (entry.getValue().equals(((XSDImport) xsdComp).getNamespace())) {
                        exist = true;
                        break;
                    }
                }
                break;
            }
        } else {
            // include xsdschema
            if (xsdComp instanceof XSDInclude) {
                String xsdLocation = ((XSDInclude) xsdComp).getSchemaLocation();
                if (xsdLocation.equals(uri)) {
                    exist = true;
                    break;
                }
            }
        }
    }
    if (!exist) {
        if (ns != null && !ns.equals("")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            int last = ns.lastIndexOf("/");
            // $NON-NLS-1$//$NON-NLS-2$
            xsdSchema.getQNamePrefixToNamespaceMap().put(ns.substring(last + 1).replaceAll("[\\W]", ""), ns);
            XSDImport xsdImport = XSDFactory.eINSTANCE.createXSDImport();
            xsdImport.setNamespace(ns);
            xsdImport.setSchemaLocation(uri);
            xsdSchema.getContents().add(0, xsdImport);
        } else {
            XSDInclude xsdInclude = XSDFactory.eINSTANCE.createXSDInclude();
            xsdInclude.setSchemaLocation(uri);
            xsdSchema.getContents().add(0, xsdInclude);
        }
        String xsd = Util.nodeToString(xsdSchema.getDocument());
        setXsdSchema(xsdSchema);
        WSDataModel wsObject = (WSDataModel) (xobject.getWsObject());
        wsObject.setXsdSchema(xsd);
    }
}
Also used : WSDataModel(com.amalto.workbench.webservices.WSDataModel) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) XSDImport(org.eclipse.xsd.XSDImport) Document(org.w3c.dom.Document) Map(java.util.Map) HashMap(java.util.HashMap) XSDInclude(org.eclipse.xsd.XSDInclude)

Example 10 with XSDImport

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

the class Util method addImport.

private static void addImport(XSDSchema xsdSchema, List<XSDImport> imports) {
    Map<String, String> nsMap = xsdSchema.getQNamePrefixToNamespaceMap();
    int imp = 0;
    for (XSDImport xsdImport : imports) {
        String ns = xsdImport.getNamespace();
        if (ns.equals("")) {
            // $NON-NLS-1$
            continue;
        }
        // $NON-NLS-1$
        int last = ns.lastIndexOf("/");
        if (!nsMap.containsValue(ns)) {
            if (ns.equals("http://www.w3.org/XML/1998/namespace")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                nsMap.put("xml", ns);
            } else {
                // $NON-NLS-1$//$NON-NLS-2$
                nsMap.put(ns.substring(last + 1).replaceAll("[\\W]", ""), ns);
            }
        }
        boolean exist = false;
        for (XSDSchemaContent cnt : xsdSchema.getContents()) {
            if (cnt instanceof XSDImportImpl) {
                XSDImportImpl cntImpl = (XSDImportImpl) cnt;
                if (cntImpl.getNamespace().equals(ns)) {
                    exist = true;
                    break;
                }
            }
        }
        if (!exist) {
            xsdSchema.getContents().add(imp++, xsdImport);
        }
    }
    xsdSchema.updateElement();
}
Also used : XSDImportImpl(org.eclipse.xsd.impl.XSDImportImpl) XSDSchemaContent(org.eclipse.xsd.XSDSchemaContent) XSDImport(org.eclipse.xsd.XSDImport)

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