use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.
the class CodedStreamCreator method checkForEncodingInContents.
private EncodingMemento checkForEncodingInContents() throws CoreException, IOException {
EncodingMemento result = null;
// if encoding memento already set, and no need to get again.
if (fEncodingMemento != null) {
result = fEncodingMemento;
} else {
if (fClientSuppliedReader) {
fReader.reset();
IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
try {
IContentDescription contentDescription = contentTypeManager.getDescriptionFor(fReader, fFilename, IContentDescription.ALL);
if (contentDescription != null) {
fEncodingMemento = createMemento(contentDescription);
} else {
// $NON-NLS-1$
fEncodingMemento = CodedIO.createEncodingMemento("UTF-8");
}
} catch (NullPointerException e) {
// TODO: work around for 5/14 bug in base, should be
// removed when move up to 5/21
// just created a simple default one
// $NON-NLS-1$
fEncodingMemento = CodedIO.createEncodingMemento("UTF-8");
}
result = fEncodingMemento;
} else {
// $NON-NLS-1$
throw new IllegalStateException("unexpected state: encodingMemento was null but no input stream supplied");
}
}
// }
return result;
}
use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.
the class DebugTextEditor method detectContentType.
IContentType detectContentType(IPath location) {
IContentType type = null;
IResource resource = FileBuffers.getWorkspaceFileAtLocation(location);
if (resource != null) {
if (resource.getType() == IResource.FILE && resource.isAccessible()) {
IContentDescription d = null;
try {
// Optimized description lookup, might not succeed
d = ((IFile) resource).getContentDescription();
if (d != null) {
type = d.getContentType();
}
} catch (CoreException e) {
// Should not be possible given the accessible and file
// type check above
}
if (type == null) {
type = Platform.getContentTypeManager().findContentTypeFor(resource.getName());
}
}
} else {
File file = FileBuffers.getSystemFileAtLocation(location);
if (file != null) {
InputStream input = null;
try {
input = new FileInputStream(file);
type = Platform.getContentTypeManager().findContentTypeFor(input, location.toOSString());
} catch (FileNotFoundException e) {
} catch (IOException e) {
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e1) {
}
}
}
if (type == null) {
type = Platform.getContentTypeManager().findContentTypeFor(file.getName());
}
}
}
if (type == null) {
type = Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT);
}
return type;
}
use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.
the class DocumentRegionProcessor method getContentType.
protected String getContentType(IDocument doc) {
if (doc == null)
return null;
String contentTypeId = null;
IContentType ct = null;
try {
IContentDescription desc = Platform.getContentTypeManager().getDescriptionFor(new StringReader(doc.get()), null, IContentDescription.ALL);
if (desc != null) {
ct = desc.getContentType();
if (ct != null)
contentTypeId = ct.getId();
}
} catch (IOException e) {
// just bail
}
return contentTypeId;
}
use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.
the class TestContentDescription method getContentDescription.
/**
* Returns content description for an input stream
* Assumes it's JSP content.
* Closes the input stream when finished.
*
* @param in
* @return the IContentDescription for in
*/
private IContentDescription getContentDescription(InputStream in) {
if (in == null)
return null;
IContentDescription desc = null;
try {
IContentType contentTypeJSP = Platform.getContentTypeManager().getContentType(ContentTypeIdForJSP.ContentTypeID_JSP);
desc = contentTypeJSP.getDescriptionFor(in, IContentDescription.ALL);
} catch (IOException e) {
Logger.logException(e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
Logger.logException(e);
}
}
}
return desc;
}
use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.
the class TestContentDescription method doTestContentDescription.
private void doTestContentDescription(String filePath, HashMap expectedProperties) {
IContentDescription desc = getContentDescription(filePath);
assertNotNull("couldn't get IContentDescription for file:[" + filePath + "]", desc);
Object[] keys = expectedProperties.keySet().toArray();
for (int i = 0; i < keys.length; i++) {
Object expected = expectedProperties.get(keys[i]);
Object detected = desc.getProperty((QualifiedName) keys[i]);
assertEquals("unexpected property value", expected, detected);
}
}
Aggregations