org.eclipse.core.runtime.content.IContentDescription Java Examples

The following examples show how to use org.eclipse.core.runtime.content.IContentDescription. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ActiveDocument.java    From eclipse-encoding-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Set the encoding of the active document, if supported by the editor.
 */
public void setEncoding(String encoding) {

	String contentCharset = null;
	IContentDescription contentDescription = getContentDescription();
	if (contentDescription != null) {
		contentCharset = contentDescription.getCharset();
	}
	if (contentCharset != null) {
		if (Charsets.equals(encoding, contentCharset)) {
			encoding = null;
		}
	} else if (Charsets.equals(encoding, inheritedEncoding)) {
		encoding = null;
	}
	try {
		// Null is clear for inheritance
		encodingSupport.setEncoding(encoding);
	} catch (Exception e) {
		// Ignore BackingStoreException for not sync project preferences store
		Activator.info("Failed set encoding", e);
	}
	refresh();
}
 
Example #2
Source File: EditorUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isClassFile(IFile file) {
	IContentDescription contentDescription;
	try {
		contentDescription= file.getContentDescription();
	} catch (CoreException e) {
		contentDescription= null;
	}
	if (contentDescription == null)
		return false;

	IContentType contentType= contentDescription.getContentType();
	if (contentType == null)
		return false;

	return "org.eclipse.jdt.core.javaClass".equals(contentType.getId()); //$NON-NLS-1$
}
 
Example #3
Source File: PropertiesFileDocumentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Checks whether the passed file editor input defines a Java properties file.
 * 
 * @param element the file editor input
 * @return <code>true</code> if element defines a Java properties file, <code>false</code>
 *         otherwise
 * @throws CoreException
 * 
 * @since 3.7
 */
public static boolean isJavaPropertiesFile(Object element) throws CoreException {
	if (JAVA_PROPERTIES_FILE_CONTENT_TYPE == null || !(element instanceof IFileEditorInput))
		return false;

	IFileEditorInput input= (IFileEditorInput)element;

	IFile file= input.getFile();
	if (file == null || !file.isAccessible())
		return false;

	IContentDescription description= file.getContentDescription();
	if (description == null || description.getContentType() == null || !description.getContentType().isKindOf(JAVA_PROPERTIES_FILE_CONTENT_TYPE))
		return false;

	return true;
}
 
Example #4
Source File: SVNUIPlugin.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public static IContentDescription getContentDescription(String name, InputStream stream) throws IOException  {
	// tries to obtain a description for this file contents
	IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
	try {
		return contentTypeManager.getDescriptionFor(stream, name, IContentDescription.ALL);
	} finally {
		if (stream != null)
			try {
				stream.close();
			} catch (IOException e) {
				// Ignore exceptions on close
			}
	}
}
 
Example #5
Source File: BusinessDataModelContentDescriber.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int describe(InputStream input, IContentDescription description) throws IOException {
    if (super.describe(input, description) == VALID) {
        return validBdmXML(new String(ByteStreams.toByteArray(input)));
    }
    return INVALID;
}
 
Example #6
Source File: BusinessDataModelContentDescriber.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int describe(Reader input, IContentDescription description) throws IOException {
    if (super.describe(input, description) == VALID) {
        return validBdmXML(CharStreams.toString(input));
    }
    return INVALID;
}
 
Example #7
Source File: ApplicationContentDescriber.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int describe(InputStream input, IContentDescription description) throws IOException {
    if (super.describe(input, description) == VALID) {
        return validApplicationXML(new String(ByteStreams.toByteArray(input)));
    }
    return INVALID;
}
 
Example #8
Source File: ApplicationContentDescriber.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int describe(Reader input, IContentDescription description) throws IOException {
    if (super.describe(input, description) == VALID) {
        return validApplicationXML(CharStreams.toString(input));
    }
    return INVALID;
}
 
Example #9
Source File: JavaClasspathParser.java    From sarl with Apache License 2.0 5 votes vote down vote up
private static boolean hasUTF8BOM(byte[] bytes) {
    if (bytes.length > IContentDescription.BOM_UTF_8.length) {
        for (int i = 0, length = IContentDescription.BOM_UTF_8.length; i < length; i++) {
            if (IContentDescription.BOM_UTF_8[i] != bytes[i]) {
                return false;
            }
        }
        return true;
    }
    return false;
}
 
Example #10
Source File: JavaProject.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean hasUTF8BOM(byte[] bytes) {
	if (bytes.length > IContentDescription.BOM_UTF_8.length) {
		for (int i = 0, length = IContentDescription.BOM_UTF_8.length; i < length; i++) {
			if (IContentDescription.BOM_UTF_8[i] != bytes[i])
				return false;
		}
		return true;
	}
	return false;
}
 
Example #11
Source File: TextContentDescriber.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public int describe(InputStream contents, IContentDescription description) throws IOException {
    String content = StringUtils.toString(contents);
    if (content.trim().isEmpty()) {
        return INDETERMINATE;
    }

    return isSupported(content) ? VALID : INVALID;
}
 
Example #12
Source File: GssResourceContentDescriber.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public int describe(Reader contents, IContentDescription description) throws IOException {

  // if (regularCssContentDescriber.describe(contents, description) == INVALID) {
  // return INVALID;
  // }

  return describe(ContentDescriberUtilities.resolveFileFromReader(contents));
}
 
Example #13
Source File: GssResourceContentDescriber.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public int describe(InputStream contents, IContentDescription description) throws IOException {

  // if (regularCssContentDescriber.describe(contents, description) == INVALID) {
  // return INVALID;
  // }

  return describe(ContentDescriberUtilities.resolveFileFromInputStream(contents));
}
 
Example #14
Source File: CssResourceContentDescriber.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
public int describe(InputStream contents, IContentDescription description)
    throws IOException {

  if (regularCssContentDescriber.describe(contents, description) == INVALID) {
    return INVALID;
  }

  return describe(ContentDescriberUtilities.resolveFileFromInputStream(contents));
}
 
Example #15
Source File: UiBinderXmlContentDescriber.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public int describe(Reader input, IContentDescription description)
    throws IOException {

  if (super.describe(input, description) == INVALID) {
    return INVALID;
  }

  if (rootElementContentDescriber.describe(input, description) == VALID) {
    // The root element is a UiBinder element
    return VALID;
  }

  return describe(ContentDescriberUtilities.resolveFileFromReader(input));
}
 
Example #16
Source File: CssResourceContentDescriber.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
public int describe(Reader contents, IContentDescription description)
    throws IOException {

  if (regularCssContentDescriber.describe(contents, description) == INVALID) {
    return INVALID;
  }

  return describe(ContentDescriberUtilities.resolveFileFromReader(contents));
}
 
Example #17
Source File: UiBinderXmlContentDescriber.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public int describe(InputStream input, IContentDescription description)
    throws IOException {

  if (super.describe(input, description) == INVALID) {
    return INVALID;
  }

  if (rootElementContentDescriber.describe(input, description) == VALID) {
    // The root element is a UiBinder element
    return VALID;
  }

  return describe(ContentDescriberUtilities.resolveFileFromInputStream(input));
}
 
Example #18
Source File: ExternalFileDocument.java    From eclipse-encoding-plugin with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public IContentDescription getContentDescription() {
	IContentType contentType = getContentType();
	if (contentType != null) {
		return contentType.getDefaultDescription();
	}
	return null;
}
 
Example #19
Source File: ExternalFileDocument.java    From eclipse-encoding-plugin with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected byte[] resolveBOM() {
	InputStream inputStream = getInputStream();
	if (inputStream == null) {
		return null;
	}
	try {
		int first = inputStream.read();
		if (first == 0xEF) {
			int second = inputStream.read();
			int third = inputStream.read();
			if (second == 0xBB && third == 0xBF)
				return IContentDescription.BOM_UTF_8;
		} else if (first == 0xFE) {
			if (inputStream.read() == 0xFF)
				return IContentDescription.BOM_UTF_16BE;
		} else if (first == 0xFF) {
			if (inputStream.read() == 0xFE)
				return IContentDescription.BOM_UTF_16LE;
		}
	} catch (Exception e) {
		throw new IllegalStateException(e);
	} finally {
		IOUtils.closeQuietly(inputStream);
	}
	return null;
}
 
Example #20
Source File: TextSearchVisitor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Returns a map from IFile to IDocument for all open, dirty editors. After creation this map
 * is not modified, so returning a non-synchronized map is ok.
 *
 * @return a map from IFile to IDocument for all open, dirty editors
 */
//	private Map<IFile, IDocument> evalNonFileBufferDocuments() {
//		Map<IFile, IDocument> result= new HashMap<>();
//		IWorkbench workbench= SearchPlugin.getDefault().getWorkbench();
//		IWorkbenchWindow[] windows= workbench.getWorkbenchWindows();
//		for (IWorkbenchWindow window : windows) {
//			IWorkbenchPage[] pages= window.getPages();
//			for (IWorkbenchPage page : pages) {
//				IEditorReference[] editorRefs= page.getEditorReferences();
//				for (IEditorReference editorRef : editorRefs) {
//					IEditorPart ep= editorRef.getEditor(false);
//					if (ep instanceof ITextEditor && ep.isDirty()) { // only dirty editors
//						evaluateTextEditor(result, ep);
//					}
//				}
//			}
//		}
//		return result;
//	}

//	private void evaluateTextEditor(Map<IFile, IDocument> result, IEditorPart ep) {
//		IEditorInput input= ep.getEditorInput();
//		if (input instanceof IFileEditorInput) {
//			IFile file= ((IFileEditorInput) input).getFile();
//			if (!result.containsKey(file)) { // take the first editor found
//				ITextFileBufferManager bufferManager= FileBuffers.getTextFileBufferManager();
//				ITextFileBuffer textFileBuffer= bufferManager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
//				if (textFileBuffer != null) {
//					// file buffer has precedence
//					result.put(file, textFileBuffer.getDocument());
//				} else {
//					// use document provider
//					IDocument document= ((ITextEditor) ep).getDocumentProvider().getDocument(input);
//					if (document != null) {
//						result.put(file, document);
//					}
//				}
//			}
//		}
//	}

private boolean hasBinaryContent(CharSequence seq, IFile file) throws CoreException {
	IContentDescription desc= file.getContentDescription();
	if (desc != null) {
		IContentType contentType= desc.getContentType();
		if (contentType != null && contentType.isKindOf(Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT))) {
			return false;
		}
	}

	// avoid calling seq.length() at it runs through the complete file,
	// thus it would do so for all binary files.
	try {
		int limit= FileCharSequenceProvider.BUFFER_SIZE;
		for (int i= 0; i < limit; i++) {
			if (seq.charAt(i) == '\0') {
				return true;
			}
		}
	} catch (IndexOutOfBoundsException e) {
	} catch (FileCharSequenceException ex) {
		if (ex.getCause() instanceof CharConversionException) {
			return true;
		}
		throw ex;
	}
	return false;
}
 
Example #21
Source File: FileCharSequenceProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private InputStream getInputStream(String charset) throws CoreException, IOException {
	boolean ok= false;
	InputStream contents= fFile.getContents();
	try {
		if (CHARSET_UTF_8.equals(charset)) {
			/*
			 * This is a workaround for a corresponding bug in Java readers and writer,
			 * see http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
			 * we remove the BOM before passing the stream to the reader
			 */
			IContentDescription description= fFile.getContentDescription();
			if ((description != null) && (description.getProperty(IContentDescription.BYTE_ORDER_MARK) != null)) {
				int bomLength= IContentDescription.BOM_UTF_8.length;
				byte[] bomStore= new byte[bomLength];
				int bytesRead= 0;
				do {
					int bytes= contents.read(bomStore, bytesRead, bomLength - bytesRead);
					if (bytes == -1) {
						throw new IOException();
					}
					bytesRead += bytes;
				} while (bytesRead < bomLength);

				if (!Arrays.equals(bomStore, IContentDescription.BOM_UTF_8)) {
					// discard file reader, we were wrong, no BOM -> new stream
					contents.close();
					contents= fFile.getContents();
				}
			}
		}
		ok= true;
	} finally {
		if (!ok && contents != null) {
			try {
				contents.close();
			} catch (IOException ex) {
				// ignore
			}
		}
	}
	return contents;
}
 
Example #22
Source File: TextEncodingContentDescriber.java    From xds-ide with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public int describe(InputStream contents, IContentDescription description) throws IOException {
	// here will be read only 1 byte - (used to define endianness)
    return super.describe(contents, description);
}
 
Example #23
Source File: AbstractIFileStub.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public IContentDescription getContentDescription() throws CoreException {
    throw new RuntimeException("Not implemented");
}
 
Example #24
Source File: XMLFormContentDescriber.java    From ice with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public int describe(Reader contents, IContentDescription description) throws IOException {

	// Local Declarations
	int retCode = INVALID;
	BufferedReader bufferedReader = new BufferedReader(contents);
	List<FormTextContentDescriber> otherICEEditorDescribers = null;

	try {
		otherICEEditorDescribers = FormTextContentDescriber.getFormTextContentDescribers();
	} catch (CoreException e) {
		logger.error("Could not get other FormTextContentDescribers.", e);
	}

	// The ICE XML files have a very well defined first four lines, although
	// the attribute values change. Read the first four lines of the text in
	// and then check for some common flags.
	String firstLines = "", nextLine;
	int counter = 0;
	while (((nextLine = bufferedReader.readLine()) != null) && counter < 3) {
		firstLines += nextLine;
		counter++;
	}

	// Make sure this is an XML file...
	if (!firstLines.contains("<?xml version=")) {
		return retCode;
	}

	// Now we know this is an XML file so
	// make sure that it is a valid ICEFormEditor
	if (firstLines.contains("<?xml version=") && firstLines.contains("itemType=")
			&& firstLines.contains("builderName=") && firstLines.contains("<Form") && isValidFile(firstLines)) {
		retCode = VALID;
		// Now we know this is an ICE XML Item, so
		// get a reference to its ID
		int index = firstLines.indexOf("itemID=\"");
		int endIndex = firstLines.indexOf("\"", index + 8);
		String itemIdString = firstLines.substring(index, endIndex + 1).replace("\"", "");
		itemID = Integer.valueOf(itemIdString.split("=")[1]);

		// If this is the default ICE Form Editor Describer
		// make sure there aren't better ones out there for
		// subclasses of ICE Form Editor
		if ("XMLFormContentDescriber".equals(getClass().getSimpleName())) {
			for (FormTextContentDescriber describer : otherICEEditorDescribers) {
				// Don't use the desriber that is XMLFormContentDescriber
				if (!describer.getClass().equals(getClass())) {
					// If true, this describer is better
					if (describer.isValidFile(firstLines)) {
						//logger.info("The " + describer.getClass().getSimpleName() + " describer is better than "
						//		+ getClass().getSimpleName() + " for \n\t" + firstLines);
						// We've found a better match, so exit
						// with an INVALID for this describer
						return INVALID;
					}
				}
			}
		}

		//logger.info(getClass().getSimpleName() + " is the best Describer Fit for \n\t" + firstLines);
	} else {
		retCode = INDETERMINATE;
	}

	return retCode;
}
 
Example #25
Source File: XMLFormContentDescriber.java    From ice with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public int describe(InputStream contents, IContentDescription description) throws IOException {
	// Just pass the information on to the other operation.
	InputStreamReader reader = new InputStreamReader(contents);
	return describe(reader, description);
}
 
Example #26
Source File: FakeIFile.java    From ice with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public IContentDescription getContentDescription() throws CoreException {
	// TODO Auto-generated method stub
	return null;
}
 
Example #27
Source File: ClientTester.java    From ice with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public IContentDescription getContentDescription()
		throws CoreException {
	// TODO Auto-generated method stub
	return null;
}
 
Example #28
Source File: ReportDocumentDescriber.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
public QualifiedName[] getSupportedOptions( )
{
	return IContentDescription.ALL;
}
 
Example #29
Source File: ReportDocumentDescriber.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
public int describe( InputStream contents, IContentDescription description )
		throws IOException
{
	return VALID;
}
 
Example #30
Source File: TestFile.java    From XPagesExtensionLibrary with Apache License 2.0 4 votes vote down vote up
public IContentDescription getContentDescription() throws CoreException {
        throw new RuntimeException("not implemented"); //$NON-NLS-1$
//        return null;
    }