Search in sources :

Example 1 with IExternalSchemaLocationProvider

use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.IExternalSchemaLocationProvider in project webtools.sourceediting by eclipse.

the class XMLAssociationProvider method checkExternalSchema.

protected CMElementDeclaration checkExternalSchema(Element element) {
    final Document document = element.getOwnerDocument();
    if (document instanceof IDOMDocument) {
        final String baseLocation = ((IDOMDocument) document).getModel().getBaseLocation();
        if (baseLocation != null) {
            final IPath basePath = new Path(baseLocation);
            IFile file = null;
            if (basePath.segmentCount() > 1) {
                file = ResourcesPlugin.getWorkspace().getRoot().getFile(basePath);
            }
            final URI uri = (file == null || !file.isAccessible()) ? new File(baseLocation).toURI() : file.getLocationURI();
            if (uri != null) {
                IExternalSchemaLocationProvider[] providers = ExternalSchemaLocationProviderRegistry.getInstance().getProviders();
                for (int i = 0; i < providers.length; i++) {
                    long time = _trace ? System.currentTimeMillis() : 0;
                    final Map locations = providers[i].getExternalSchemaLocation(uri);
                    if (_trace) {
                        long diff = System.currentTimeMillis() - time;
                        if (diff > 250)
                            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                            Logger.log(Logger.INFO, "Schema location provider took [" + diff + "ms] for URI [" + uri + "]");
                    }
                    if (locations != null && !locations.isEmpty()) {
                        Object location = locations.get(IExternalSchemaLocationProvider.NO_NAMESPACE_SCHEMA_LOCATION);
                        if (location != null)
                            return getCMElementDeclaration(element, NamespaceTable.getElementLineage(element), uri.toString(), location.toString());
                    }
                }
            }
        }
    }
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) Document(org.w3c.dom.Document) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IExternalSchemaLocationProvider(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.IExternalSchemaLocationProvider) URI(java.net.URI) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Map(java.util.Map)

Example 2 with IExternalSchemaLocationProvider

use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.IExternalSchemaLocationProvider in project webtools.sourceediting by eclipse.

the class ExternalSchemaTest method testExternalLocations.

public void testExternalLocations() throws Exception {
    final IExternalSchemaLocationProvider[] providers = ExternalSchemaLocationProviderRegistry.getInstance().getProviders();
    assertTrue(providers.length > 0);
    URI resource = new URI("file://uninterested/path/file.xml");
    for (int i = 0; i < providers.length; i++) {
        Map locations = providers[i].getExternalSchemaLocation(resource);
        assertTrue("[" + resource + "] should have no external schemas.", locations == null || locations.isEmpty());
    }
    resource = new URI("file://interested/path/file.xml");
    boolean foundCorrectSchema = false;
    for (int i = 0; i < providers.length; i++) {
        Map locations = providers[i].getExternalSchemaLocation(resource);
        if (locations != null) {
            String location = (String) locations.get(IExternalSchemaLocationProvider.NO_NAMESPACE_SCHEMA_LOCATION);
            if (EXTERNAL_SCHEMA_LOCATION.equals(location)) {
                foundCorrectSchema = true;
            }
        }
        assertTrue("[" + resource + "] should have an external schema.", foundCorrectSchema);
    }
}
Also used : IExternalSchemaLocationProvider(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.IExternalSchemaLocationProvider) URI(java.net.URI) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with IExternalSchemaLocationProvider

use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.IExternalSchemaLocationProvider in project webtools.sourceediting by eclipse.

the class XMLValidator method checkExternalSchemas.

private boolean checkExternalSchemas(XMLReader reader, String fileURI) throws Exception {
    boolean isGrammarEncountered = false;
    final StringBuffer schemaLocation = new StringBuffer();
    String noNamespaceSchemaLocation = null;
    // Check the schema provider extension point
    IExternalSchemaLocationProvider[] providers = ExternalSchemaLocationProviderRegistry.getInstance().getProviders();
    for (int i = 0; i < providers.length; i++) {
        URI uri = null;
        try {
            uri = URIUtil.fromString(fileURI);
        } catch (URISyntaxException e) {
            Logger.logException(e.getLocalizedMessage(), e);
        }
        if (uri != null) {
            long time = _trace ? System.currentTimeMillis() : 0;
            final Map locations = providers[i].getExternalSchemaLocation(uri);
            if (_trace) {
                long diff = System.currentTimeMillis() - time;
                if (diff > 250)
                    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    Logger.log(Logger.INFO, "Schema location provider took [" + diff + "ms] for URI [" + fileURI + "]");
            }
            if (locations != null && !locations.isEmpty()) {
                Object path = locations.get(IExternalSchemaLocationProvider.SCHEMA_LOCATION);
                if (path instanceof String) {
                    if (schemaLocation.length() > 0) {
                        schemaLocation.append(' ');
                    }
                    schemaLocation.append(path);
                }
                path = locations.get(IExternalSchemaLocationProvider.NO_NAMESPACE_SCHEMA_LOCATION);
                if (path instanceof String) {
                    noNamespaceSchemaLocation = (String) path;
                }
            }
        }
    }
    if (schemaLocation.length() > 0) {
        reader.setProperty(IExternalSchemaLocationProvider.SCHEMA_LOCATION, schemaLocation.toString());
        isGrammarEncountered = true;
    }
    if (noNamespaceSchemaLocation != null) {
        reader.setProperty(IExternalSchemaLocationProvider.NO_NAMESPACE_SCHEMA_LOCATION, noNamespaceSchemaLocation);
        isGrammarEncountered = true;
    }
    return isGrammarEncountered;
}
Also used : URISyntaxException(java.net.URISyntaxException) IExternalSchemaLocationProvider(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.IExternalSchemaLocationProvider) URI(java.net.URI) Map(java.util.Map)

Example 4 with IExternalSchemaLocationProvider

use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.IExternalSchemaLocationProvider in project webtools.sourceediting by eclipse.

the class CMDocumentLoader method checkExternalSchema.

protected boolean checkExternalSchema() {
    boolean externalSchemaLoaded = false;
    if (document instanceof IDOMDocument) {
        final String baseLocation = ((IDOMDocument) document).getModel().getBaseLocation();
        if (baseLocation == null)
            return false;
        final IPath basePath = new Path(baseLocation);
        IFile file = null;
        if (basePath.segmentCount() > 1) {
            file = ResourcesPlugin.getWorkspace().getRoot().getFile(basePath);
        }
        final URI uri = (file == null || !file.isAccessible()) ? new File(baseLocation).toURI() : file.getLocationURI();
        if (uri != null) {
            IExternalSchemaLocationProvider[] providers = ExternalSchemaLocationProviderRegistry.getInstance().getProviders();
            for (int i = 0; i < providers.length; i++) {
                long time = _trace ? System.currentTimeMillis() : 0;
                final Map locations = providers[i].getExternalSchemaLocation(uri);
                if (_trace) {
                    long diff = System.currentTimeMillis() - time;
                    if (diff > 250)
                        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        Logger.log(Logger.INFO, "Schema location provider took [" + diff + "ms] for URI [" + uri + "]");
                }
                if (locations != null && !locations.isEmpty()) {
                    // Use the externalSchemaLocation
                    if (namespaceTable != null && namespaceTable.isNamespaceEncountered()) {
                        Object schemaLocation = locations.get(IExternalSchemaLocationProvider.SCHEMA_LOCATION);
                        if (schemaLocation != null) {
                            final String location = schemaLocation.toString();
                            if (location != null) {
                                final String[] ids = StringUtils.asArray(location);
                                // namespace : location pairs
                                if (ids.length >= 2 && ids.length % 2 == 0) {
                                    if (!externalSchemaLoaded)
                                        cmDocumentManager.removeAllReferences();
                                    for (int j = 0; j < ids.length; j += 2) {
                                        // $NON-NLS-1$
                                        handleGrammar(ids[j], ids[j + 1], "XSD");
                                        externalSchemaLoaded = true;
                                    }
                                }
                            } else {
                                // $NON-NLS-1$
                                Logger.log(Logger.ERROR_DEBUG, "External schema location provider did not return an external schema location for IExternalSchemaLocationProvider.SCHEMA_LOCATION: " + providers[i].getClass().getName());
                            }
                        }
                    } else {
                        // noNamespace
                        Object schemaLocation = locations.get(IExternalSchemaLocationProvider.NO_NAMESPACE_SCHEMA_LOCATION);
                        if (schemaLocation != null) {
                            // $NON-NLS-1$
                            handleGrammar(uri.toString(), schemaLocation.toString(), "XSD");
                            externalSchemaLoaded = true;
                            break;
                        } else {
                            // $NON-NLS-1$
                            Logger.log(Logger.ERROR_DEBUG, "External schema location provider did not return an external schema location for IExternalSchemaLocationProvider.NO_NAMESPACE_SCHEMA_LOCATION: " + providers[i].getClass().getName());
                        }
                    }
                }
            }
        }
    }
    return externalSchemaLoaded;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IExternalSchemaLocationProvider(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.IExternalSchemaLocationProvider) URI(java.net.URI) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Map(java.util.Map)

Aggregations

URI (java.net.URI)4 Map (java.util.Map)4 IExternalSchemaLocationProvider (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.IExternalSchemaLocationProvider)4 File (java.io.File)2 IFile (org.eclipse.core.resources.IFile)2 IPath (org.eclipse.core.runtime.IPath)2 Path (org.eclipse.core.runtime.Path)2 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)2 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)1 Document (org.w3c.dom.Document)1