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;
}
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;
}
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;
}
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;
}
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());
}
Aggregations