Search in sources :

Example 1 with XSDSchemaLocationResolverAdapterFactory

use of org.eclipse.wst.xsd.ui.internal.util.XSDSchemaLocationResolverAdapterFactory in project webtools.sourceediting by eclipse.

the class XSDModelAdapter method createSchema.

public XSDSchema createSchema(Document document) {
    try {
        // (cs) note that we always want to ensure that a
        // schema model object get's returned
        schema = XSDFactory.eINSTANCE.createXSDSchema();
        resourceSet = XSDSchemaImpl.createResourceSet();
        resourceSet.getAdapterFactories().add(new XSDSchemaLocationResolverAdapterFactory());
        IDOMNode domNode = (IDOMNode) document;
        String baseLocation = domNode.getModel().getBaseLocation();
        // TODO... gotta pester SSE folks to provide 'useful' baseLocations
        // 
        URI uri = getURI(baseLocation);
        Resource resource = new XSDResourceImpl();
        resource.setURI(uri);
        schema = XSDFactory.eINSTANCE.createXSDSchema();
        resource.getContents().add(schema);
        resourceSet.getResources().add(resource);
        schema.setDocument(document);
        final Element element = document.getDocumentElement();
        if (element != null) {
            // Force the loading of the "meta" schema for schema instance instance.
            // 
            String schemaForSchemaNamespace = element.getNamespaceURI();
            XSDSchemaImpl.getSchemaForSchema(schemaForSchemaNamespace);
        }
        IRunnableWithProgress setElementOperation = new IRunnableWithProgress() {

            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                // Use the animated flavour as we don't know beforehand how many ticks we need.
                // The task name will be displayed by the code in XSDResourceImpl.
                // $NON-NLS-1$
                monitor.beginTask("", IProgressMonitor.UNKNOWN);
                Map loadOptions = resourceSet.getLoadOptions();
                loadOptions.put(XSDResourceImpl.XSD_PROGRESS_MONITOR, monitor);
                schema.setElement(element);
                loadOptions.remove(XSDResourceImpl.XSD_PROGRESS_MONITOR);
            }
        };
        IProgressService progressService = PlatformUI.getWorkbench().getProgressService();
        try {
            progressService.busyCursorWhile(setElementOperation);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // attach an adapter to keep the XSD model and DOM in sync
        // 
        modelReconcileAdapter = new XSDModelReconcileAdapter(document, schema);
        domNode.getModel().addModelStateListener(modelReconcileAdapter);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return schema;
}
Also used : Element(org.w3c.dom.Element) Resource(org.eclipse.emf.ecore.resource.Resource) XSDResourceImpl(org.eclipse.xsd.util.XSDResourceImpl) XSDSchemaLocationResolverAdapterFactory(org.eclipse.wst.xsd.ui.internal.util.XSDSchemaLocationResolverAdapterFactory) URI(org.eclipse.emf.common.util.URI) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IProgressService(org.eclipse.ui.progress.IProgressService) Map(java.util.Map)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Map (java.util.Map)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 URI (org.eclipse.emf.common.util.URI)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)1 IProgressService (org.eclipse.ui.progress.IProgressService)1 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)1 XSDSchemaLocationResolverAdapterFactory (org.eclipse.wst.xsd.ui.internal.util.XSDSchemaLocationResolverAdapterFactory)1 XSDResourceImpl (org.eclipse.xsd.util.XSDResourceImpl)1 Element (org.w3c.dom.Element)1