Search in sources :

Example 11 with IContentDescription

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

the class PageDirectiveAdapterImpl 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, or null if in is null
 */
private IContentDescription getContentDescription(Reader 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 {
        try {
            in.close();
        } catch (IOException e) {
            Logger.logException(e);
        }
    }
    return desc;
}
Also used : IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 12 with IContentDescription

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

the class PageDirectiveAdapterImpl method setCachedContentType.

/**
 * Sets the cachedContentType.
 *
 * @param cachedContentType
 *            The cachedContentType to set
 */
public void setCachedContentType(String newContentType) {
    /*
		 * if the passed in value is the same as existing, there's nothing to
		 * do. if its different, then we need to change the contentHandler as
		 * well and, more to the point, signal a re-initializtation is needed.
		 * 
		 * Note: if the value we're getting set to does not have a handler in
		 * the registry, we'll actually not set it to null or anything, we'll
		 * just continue on with the one we have. This is pretty important to
		 * avoid re-initializing on every key stroke if someone is typing in a
		 * new content type, but haven't yet finished the whole "word".
		 * However, if an contentType is not recognized, the registry returns
		 * the one for XML.
		 */
    /* set the actual value first, the rest is "side effect" */
    this.cachedContentType = newContentType;
    /* just safety check, can be removed later, early in release cycle */
    if (model == null) {
        // throw IllegalStateException("model should never be null in
        // PageDirective Adapter");
        Logger.log(Logger.ERROR, "model should never be null in PageDirective Adapter");
        return;
    }
    EmbeddedTypeHandler potentialNewandler = null;
    IContentDescription contentDescription = getContentDescription(model.getStructuredDocument());
    Object prop = contentDescription.getProperty(IContentDescriptionForJSP.CONTENT_FAMILY_ATTRIBUTE);
    if (prop != null) {
        if (ContentTypeFamilyForHTML.HTML_FAMILY.equals(prop)) {
            potentialNewandler = EmbeddedTypeRegistryImpl.getInstance().getTypeFor("text/html");
        }
    }
    if (potentialNewandler == null) {
        /*
			 * getHandler should always return something (never null), based
			 * on the rules in the factory.
			 */
        potentialNewandler = getHandlerFor(this.cachedContentType);
    }
    /*
		 * we do this check for re-init here, instead of in setEmbeddedType,
		 * since setEmbeddedType is called during the normal initializtion
		 * process, when re-init is not needed (since there is no content)
		 */
    if (embeddedTypeHandler == null) {
        setEmbeddedType(potentialNewandler);
    } else if (potentialNewandler != null && embeddedTypeHandler != potentialNewandler) {
        /*
			 * changing this embedded handler here may be in the middle of a
			 * notify loop. That's why we set that "it's needed". Then the
			 * model decides when its "safe" to actually do the re-init.
			 * 
			 * be sure to hold oldHandler in temp var or else setEmbeddedType
			 * will "reset" it before modelReinitNeeded(oldHandler, handler)
			 * is called
			 * 
			 */
        EmbeddedTypeHandler oldHandler = embeddedTypeHandler;
        setEmbeddedType(potentialNewandler);
        modelReinitNeeded(oldHandler, potentialNewandler);
    }
}
Also used : EmbeddedTypeHandler(org.eclipse.wst.sse.core.internal.ltk.modelhandler.EmbeddedTypeHandler) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 13 with IContentDescription

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

the class WorkspaceFileHyperlink method getEditorLabel.

private String getEditorLabel() throws CoreException {
    final IContentDescription description = fFile.getContentDescription();
    final IEditorDescriptor defaultEditor = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(fFile.getName(), description != null ? description.getContentType() : null);
    return defaultEditor != null ? defaultEditor.getLabel() : null;
}
Also used : IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 14 with IContentDescription

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

the class ModelManagerImpl method _doCommonCreateModel.

private void _doCommonCreateModel(InputStream inputStream, String id, IModelHandler handler, URIResolver resolver, ReadEditType rwType, String encoding, String lineDelimiter, SharedObject sharedObject) throws IOException {
    boolean doRemove = true;
    try {
        synchronized (sharedObject) {
            IStructuredModel model = null;
            try {
                model = _commonCreateModel(id, handler, resolver);
                IModelLoader loader = handler.getModelLoader();
                if (inputStream == null) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    Logger.log(Logger.WARNING, "model was requested for id " + id + " without a content InputStream");
                }
                loader.load(id, Utilities.getMarkSupportedStream(inputStream), model, encoding, lineDelimiter);
            } catch (ResourceInUse e) {
                // impossible, since we've already found
                handleProgramError(e);
            }
            if (model != null) {
                /**
                 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=264228
                 *
                 * Ensure that the content type identifier field of the model
                 * is properly set. This is normally handled by the
                 * FileBufferModelManager when working with files as it knows
                 * the content type in advance; here is where we handle it for
                 * streams.
                 */
                if (model instanceof AbstractStructuredModel) {
                    DocumentReader reader = new DocumentReader(model.getStructuredDocument());
                    IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(reader, id, new QualifiedName[0]);
                    reader.close();
                    if (description != null && description.getContentType() != null) {
                        ((AbstractStructuredModel) model).setContentTypeIdentifier(description.getContentType().getId());
                    }
                }
                sharedObject.theSharedModel = model;
                _initCount(sharedObject, rwType);
                doRemove = false;
            }
        }
    } finally {
        if (doRemove) {
            SYNC.acquire();
            // remove it if we didn't get one back
            fManagedObjects.remove(id);
            SYNC.release();
        }
        sharedObject.setLoaded();
    }
}
Also used : IModelLoader(org.eclipse.wst.sse.core.internal.provisional.IModelLoader) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) DocumentReader(org.eclipse.wst.sse.core.internal.document.DocumentReader) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) ResourceInUse(org.eclipse.wst.sse.core.internal.provisional.exceptions.ResourceInUse)

Example 15 with IContentDescription

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

the class JSPModelLoader method getContentDescription.

/**
 * Returns content description for an input stream Assumes it's JSP
 * content. Closes the input stream when finished.
 *
 * @param reader
 * @return the IContentDescription for in, or null if in is null
 */
private IContentDescription getContentDescription(Reader reader) {
    if (reader == null)
        return null;
    IContentDescription desc = null;
    try {
        IContentType contentTypeJSP = Platform.getContentTypeManager().getContentType(ContentTypeIdForJSP.ContentTypeID_JSP);
        desc = contentTypeJSP.getDescriptionFor(reader, IContentDescription.ALL);
    } catch (IOException e) {
        Logger.logException(e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                Logger.logException(e);
            }
        }
    }
    return desc;
}
Also used : IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Aggregations

IContentDescription (org.eclipse.core.runtime.content.IContentDescription)58 CoreException (org.eclipse.core.runtime.CoreException)27 IOException (java.io.IOException)24 IContentType (org.eclipse.core.runtime.content.IContentType)22 InputStream (java.io.InputStream)19 IFile (org.eclipse.core.resources.IFile)14 QualifiedName (org.eclipse.core.runtime.QualifiedName)13 IContentTypeManager (org.eclipse.core.runtime.content.IContentTypeManager)8 Reader (java.io.Reader)6 IStatus (org.eclipse.core.runtime.IStatus)6 Status (org.eclipse.core.runtime.Status)6 BufferedReader (java.io.BufferedReader)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStreamReader (java.io.InputStreamReader)4 IStructuredFormatProcessor (org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor)4 SequenceInputStream (java.io.SequenceInputStream)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 IResource (org.eclipse.core.resources.IResource)3