Search in sources :

Example 6 with IContentTypeManager

use of org.eclipse.core.runtime.content.IContentTypeManager in project webtools.sourceediting by eclipse.

the class ValidatorMetaData method calculateParentContentTypeIds.

/**
 * TODO: This exact method is also in ValidatorStrategy. Should be in a common place.
 *
 * @param contentTypeId
 * @return
 */
private String[] calculateParentContentTypeIds(String contentTypeId) {
    Set parentTypes = new HashSet();
    IContentTypeManager ctManager = Platform.getContentTypeManager();
    IContentType ct = ctManager.getContentType(contentTypeId);
    String id = contentTypeId;
    while (ct != null && id != null) {
        parentTypes.add(id);
        ct = ctManager.getContentType(id);
        if (ct != null) {
            IContentType baseType = ct.getBaseType();
            id = (baseType != null) ? baseType.getId() : null;
        }
    }
    return (String[]) parentTypes.toArray(new String[parentTypes.size()]);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) IContentType(org.eclipse.core.runtime.content.IContentType) HashSet(java.util.HashSet)

Example 7 with IContentTypeManager

use of org.eclipse.core.runtime.content.IContentTypeManager in project webtools.sourceediting by eclipse.

the class CodedReaderCreator method checkForEncodingInContents.

private EncodingMemento checkForEncodingInContents(InputStream limitedStream) throws CoreException, IOException {
    EncodingMemento result = null;
    // have been set, and no need to get again.
    if (fEncodingMemento != null) {
        result = fEncodingMemento;
    } else {
        if (fClientSuppliedStream) {
            try {
                limitedStream.reset();
                IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
                IContentDescription contentDescription = contentTypeManager.getDescriptionFor(limitedStream, fFilename, IContentDescription.ALL);
                if (contentDescription != null) {
                    fEncodingMemento = createMemento(contentDescription);
                }
                result = fEncodingMemento;
            } finally {
                limitedStream.reset();
            }
        } else {
            // throw new IllegalStateException("unexpected state:
            // encodingMemento was null but no input stream supplied by
            // client"); //$NON-NLS-1$
            result = null;
        }
    }
    if (result != null && !result.isValid() && !forceDefault()) {
        throw new UnsupportedCharsetExceptionWithDetail(result);
    }
    return result;
}
Also used : IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) UnsupportedCharsetExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 8 with IContentTypeManager

use of org.eclipse.core.runtime.content.IContentTypeManager in project webtools.sourceediting by eclipse.

the class CodedReaderCreator method findMementoFromStreamCase.

/**
 * The primary method which contains the highest level rules for how to
 * decide appropriate decoding rules: 1. first check for unicode stream 2.
 * then looked for encoding specified in content (according to the type of
 * content that is it ... xml, html, jsp, etc. 3. then check for various
 * settings: file settings first, if null check project settings, if null,
 * check user preferences. 4. lastly (or, what is the last user
 * preference) is to use "workbench defaults".
 *
 * @throws IOException
 * @throws CoreException
 */
private EncodingMemento findMementoFromStreamCase() throws CoreException, IOException {
    EncodingMemento result = null;
    InputStream resettableLimitedStream = null;
    try {
        resettableLimitedStream = getLimitedStream(getResettableStream());
        if (resettableLimitedStream != null) {
            // first check for unicode stream
            result = checkStreamForBOM(resettableLimitedStream);
            // if not that, then check contents
            if (result == null) {
                resettableLimitedStream.reset();
                result = checkForEncodingInContents(resettableLimitedStream);
            }
        } else {
            // stream null, may name's not.
            if (fFilename != null) {
                // filename not null
                IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
                IContentDescription contentDescription = contentTypeManager.getDescriptionFor(new NullInputStream(), fFilename, IContentDescription.ALL);
                if (contentDescription != null) {
                    result = createMemento(contentDescription);
                }
            }
        }
    } finally {
        if (resettableLimitedStream != null) {
            handleStreamClose(resettableLimitedStream);
        }
    }
    return result;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) NullInputStream(org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream) InputStream(java.io.InputStream) IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) NullInputStream(org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream)

Example 9 with IContentTypeManager

use of org.eclipse.core.runtime.content.IContentTypeManager in project webtools.sourceediting by eclipse.

the class StructuredTextEditor method installCharacterPairing.

private void installCharacterPairing() {
    IStructuredModel model = getInternalModel();
    if (model != null) {
        // $NON-NLS-1$
        IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(SSEUIPlugin.ID, "characterPairing");
        IContentTypeManager mgr = Platform.getContentTypeManager();
        IContentType type = mgr.getContentType(model.getContentTypeIdentifier());
        if (type != null) {
            for (int i = 0; i < elements.length; i++) {
                // Create the inserter
                IConfigurationElement element = elements[i];
                try {
                    IConfigurationElement[] contentTypes = element.getChildren("contentTypeIdentifier");
                    for (int j = 0; j < contentTypes.length; j++) {
                        String id = contentTypes[j].getAttribute("id");
                        if (id != null) {
                            IContentType targetType = mgr.getContentType(id);
                            int priority = calculatePriority(type, targetType, 0);
                            if (priority >= 0) {
                                final CharacterPairing pairing = new CharacterPairing();
                                pairing.priority = priority;
                                String[] partitions = StringUtils.unpack(contentTypes[j].getAttribute("partitions"));
                                pairing.partitions = new HashSet(partitions.length);
                                // Only add the inserter if there is at least one partition for the content type
                                for (int k = 0; k < partitions.length; k++) {
                                    pairing.partitions.add(partitions[k]);
                                }
                                pairing.inserter = (AbstractCharacterPairInserter) element.createExecutableExtension("class");
                                if (pairing.inserter != null && partitions.length > 0) {
                                    fPairInserter.addInserter(pairing);
                                    /* use a SafeRunner since this method is also invoked during Part creation */
                                    SafeRunner.run(new ISafeRunnable() {

                                        public void run() throws Exception {
                                            pairing.inserter.initialize();
                                        }

                                        public void handleException(Throwable exception) {
                                        // rely on default logging
                                        }
                                    });
                                }
                            }
                        }
                    }
                } catch (CoreException e) {
                    Logger.logException(e);
                }
            }
            fPairInserter.prioritize();
        }
    }
}
Also used : IContentType(org.eclipse.core.runtime.content.IContentType) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) Point(org.eclipse.swt.graphics.Point) BadLocationException(org.eclipse.jface.text.BadLocationException) IOException(java.io.IOException) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) ExecutionException(org.eclipse.core.commands.ExecutionException) CoreException(org.eclipse.core.runtime.CoreException) IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 10 with IContentTypeManager

use of org.eclipse.core.runtime.content.IContentTypeManager in project webtools.sourceediting by eclipse.

the class TestContentTypeHandlers method testJSPExistsByFileExtension.

public void testJSPExistsByFileExtension() throws IOException {
    String filename = "test.jsp";
    IContentTypeManager registry = getContentTypeRegistry();
    IContentType identifier = registry.getDescriptionFor(new NullStream(), filename, IContentDescription.ALL).getContentType();
    assertTrue("content type identifier for " + filename + " does not have JSP type ", identifier != null);
}
Also used : IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) IContentType(org.eclipse.core.runtime.content.IContentType)

Aggregations

IContentTypeManager (org.eclipse.core.runtime.content.IContentTypeManager)33 IContentType (org.eclipse.core.runtime.content.IContentType)27 IContentDescription (org.eclipse.core.runtime.content.IContentDescription)8 CoreException (org.eclipse.core.runtime.CoreException)5 InputStream (java.io.InputStream)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)3 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 BufferedInputStream (java.io.BufferedInputStream)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IFileInfo (org.eclipse.core.filesystem.IFileInfo)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 ProjectScope (org.eclipse.core.resources.ProjectScope)1 IExtensionPoint (org.eclipse.core.runtime.IExtensionPoint)1