Search in sources :

Example 31 with IModelHandler

use of org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler in project webtools.sourceediting by eclipse.

the class ModelHandlerRegistry method getHandlerForContentType.

/**
 * Finds the registered IModelHandler for a given IContentType.
 *
 * @param contentType
 * @return The IModelHandler registered for the given content type. If an
 *         exact match is not found, the most-specific match according to
 *         IContentType.isKindOf() will be returned. If none are found,
 *         either a default or null will be returned.
 */
private IModelHandler getHandlerForContentType(IContentType contentType) {
    IModelHandler handler = null;
    if (contentType != null) {
        IConfigurationElement exactContentTypeElement = null;
        IConfigurationElement kindOfContentTypeElement = null;
        int kindOfContentTypeDepth = 0;
        IConfigurationElement[] elements = reader.elements;
        if (elements != null) {
            for (int i = 0; i < elements.length && exactContentTypeElement == null; i++) {
                String currentId = reader.getAssociatedContentTypeId(elements[i]);
                IContentType associatedContentType = Platform.getContentTypeManager().getContentType(currentId);
                if (contentType.equals(associatedContentType)) {
                    exactContentTypeElement = elements[i];
                } else if (contentType.isKindOf(associatedContentType)) {
                    /*
						 * Update the kindOfElement variable only if this
						 * element's content type is "deeper" (depth test
						 * ensures the first content type is remembered)
						 */
                    IContentType testContentType = associatedContentType;
                    int testDepth = 0;
                    while (testContentType != null) {
                        testDepth++;
                        testContentType = testContentType.getBaseType();
                    }
                    if (testDepth > kindOfContentTypeDepth) {
                        kindOfContentTypeElement = elements[i];
                        kindOfContentTypeDepth = testDepth;
                    }
                }
            }
        } else if (Logger.DEBUG) {
            // $NON-NLS-1$
            Logger.log(Logger.WARNING, "There were no Model Handler found in registry");
        }
        if (exactContentTypeElement != null) {
            handler = reader.getInstance(exactContentTypeElement);
        } else if (kindOfContentTypeElement != null) {
            handler = reader.getInstance(kindOfContentTypeElement);
        }
    }
    if (handler == null) {
        // temp hard coding for null content type arguments
        // $NON-NLS-1$
        handler = getHandlerExtension(INTERNAL_DEFAULT_EXTENSION);
    }
    return handler;
}
Also used : IContentType(org.eclipse.core.runtime.content.IContentType) IModelHandler(org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement)

Example 32 with IModelHandler

use of org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler in project webtools.sourceediting by eclipse.

the class ModelHandlerRegistry method getHandlerExtension.

/**
 * Finds a ModelHandler based on literal extension id. It's basically a
 * "first found first returned". No specific order is guaranteed and the
 * uniqueness of IDs is not considered.
 *
 * @param extensionId
 * @return the given extension, or null
 */
private IModelHandler getHandlerExtension(String extensionId) {
    IModelHandler found = null;
    IConfigurationElement[] elements = reader.elements;
    if (elements != null) {
        for (int i = 0; i < elements.length; i++) {
            String currentId = reader.getId(elements[i]);
            if (extensionId.equals(currentId)) {
                IModelHandler item = reader.getInstance(elements[i]);
                found = item;
            }
        }
    } else if (Logger.DEBUG) {
        // $NON-NLS-1$
        Logger.log(Logger.WARNING, "There were no Model Handler found in registry");
    }
    return found;
}
Also used : IModelHandler(org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement)

Example 33 with IModelHandler

use of org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler in project webtools.sourceediting by eclipse.

the class ModelHandlerRegistry method getHandlerFor.

/**
 * Finds the registered IModelHandler for a given named file's content
 * type.
 *
 * @param file
 * @param provideDefault should the default extension be used in the absence of other methods
 * @return The IModelHandler registered for the content type of the given
 *         file. If an exact match is not found, the most-specific match
 *         according to IContentType.isKindOf() will be returned. If none
 *         are found, either a default or null will be returned.
 * @throws CoreException
 */
public IModelHandler getHandlerFor(IFile file, boolean provideDefault) throws CoreException {
    IModelHandler modelHandler = null;
    IContentDescription contentDescription = null;
    IContentType contentType = null;
    boolean accessible = file.isAccessible();
    if (accessible) {
        /* Try the optimized method first as the description may be cached */
        contentDescription = file.getContentDescription();
        if (contentDescription != null) {
            // use the provided description
            contentType = contentDescription.getContentType();
        } else {
            /* use the more thorough discovery method to get a description */
            InputStream contents = null;
            try {
                contents = file.getContents(false);
                contentDescription = Platform.getContentTypeManager().getDescriptionFor(contents, file.getName(), IContentDescription.ALL);
                if (contentDescription != null) {
                    contentType = contentDescription.getContentType();
                }
            } catch (IOException e) {
                // nothing further can be done, but will log for debugging
                Logger.logException(e);
            } finally {
                if (contents != null) {
                    try {
                        contents.close();
                    } catch (IOException e1) {
                    // nothing can be done
                    }
                }
            }
        }
    }
    /*
		 * If we couldn't get the content type from a description, try basing
		 * it on just the filename
		 */
    if (contentType == null) {
        contentType = Platform.getContentTypeManager().findContentTypeFor(file.getName());
    }
    if (contentType != null) {
        modelHandler = getHandlerForContentType(contentType);
    } else if (contentType == null && provideDefault) {
        // hard coding for null content type
        // $NON-NLS-1$
        modelHandler = getHandlerExtension(INTERNAL_DEFAULT_EXTENSION);
    }
    return modelHandler;
}
Also used : InputStream(java.io.InputStream) IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) IModelHandler(org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler)

Example 34 with IModelHandler

use of org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler in project webtools.sourceediting by eclipse.

the class ModelHandlerRegistry method getHandlerFor.

/**
 * Finds the registered IModelHandler for a given named InputStream.
 *
 * @param inputName
 * @param inputStream
 * @return The IModelHandler registered for the content type of the given
 *         input. If an exact match is not found, the most-specific match
 *         according to IContentType.isKindOf() will be returned. If none
 *         are found, either a default or null will be returned.
 * @throws IOException
 */
public IModelHandler getHandlerFor(String inputName, InputStream inputStream) throws IOException {
    InputStream iStream = Utilities.getMarkSupportedStream(inputStream);
    IModelHandler modelHandler = null;
    IContentType contentType = null;
    if (inputStream != null) {
        try {
            iStream.mark(CodedIO.MAX_MARK_SIZE);
            contentType = Platform.getContentTypeManager().findContentTypeFor(Utilities.getLimitedStream(iStream), inputName);
        } finally {
            if (iStream != null && iStream.markSupported()) {
                iStream.reset();
            }
        }
    }
    if (contentType == null) {
        contentType = Platform.getContentTypeManager().findContentTypeFor(inputName);
    }
    // performance reasons
    if (contentType == null) {
        contentType = Platform.getContentTypeManager().findContentTypeFor(Utilities.getLimitedStream(iStream), null);
    }
    modelHandler = getHandlerForContentType(contentType);
    return modelHandler;
}
Also used : InputStream(java.io.InputStream) IContentType(org.eclipse.core.runtime.content.IContentType) IModelHandler(org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler)

Example 35 with IModelHandler

use of org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler in project webtools.sourceediting by eclipse.

the class TestModelHandlers method testXMLExists.

public void testXMLExists() {
    String id = ContentTypeIdForXML.ContentTypeID_XML;
    ModelHandlerRegistry registry = getModelHandlerRegistry();
    IModelHandler handler = registry.getHandlerForContentTypeId(id);
    assertEquals("model handler registry does not have XML type ", id, handler.getAssociatedContentTypeId());
}
Also used : ModelHandlerRegistry(org.eclipse.wst.sse.core.internal.modelhandler.ModelHandlerRegistry) IModelHandler(org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler)

Aggregations

IModelHandler (org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler)41 ModelHandlerRegistry (org.eclipse.wst.sse.core.internal.modelhandler.ModelHandlerRegistry)14 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)9 CoreException (org.eclipse.core.runtime.CoreException)6 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)6 IContentType (org.eclipse.core.runtime.content.IContentType)5 IModelLoader (org.eclipse.wst.sse.core.internal.provisional.IModelLoader)5 InputStream (java.io.InputStream)4 IFile (org.eclipse.core.resources.IFile)4 IDocumentLoader (org.eclipse.wst.sse.core.internal.document.IDocumentLoader)4 ResourceInUse (org.eclipse.wst.sse.core.internal.provisional.exceptions.ResourceInUse)4 IOException (java.io.IOException)3 IProject (org.eclipse.core.resources.IProject)3 BasicStructuredDocument (org.eclipse.wst.sse.core.internal.text.BasicStructuredDocument)3 URIResolver (org.eclipse.wst.sse.core.internal.util.URIResolver)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)2 IDocument (org.eclipse.jface.text.IDocument)2 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)2