Search in sources :

Example 16 with IContentDescription

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

the class JSPValidator method isFragment.

/**
 * Determines if file is jsp fragment or not (does a deep, indepth check,
 * looking into contents of file)
 *
 * @param file
 *            assumes file is not null and exists
 * @return true if file is jsp fragment, false otherwise
 */
private boolean isFragment(IFile file) {
    boolean isFragment = false;
    InputStream is = null;
    try {
        IContentDescription contentDescription = file.getContentDescription();
        // it can be null
        if (contentDescription == null) {
            is = file.getContents();
            contentDescription = Platform.getContentTypeManager().getDescriptionFor(is, file.getName(), new QualifiedName[] { IContentDescription.CHARSET });
        }
        if (contentDescription != null) {
            String fileCtId = contentDescription.getContentType().getId();
            isFragment = (fileCtId != null && ContentTypeIdForJSP.ContentTypeID_JSPFRAGMENT.equals(fileCtId));
        }
    } catch (IOException e) {
    // ignore, assume it's invalid JSP
    } catch (CoreException e) {
    // ignore, assume it's invalid JSP
    } finally {
        // must close input stream in case others need it
        if (is != null)
            try {
                is.close();
            } catch (Exception e) {
            // not sure how to recover at this point
            }
    }
    return isFragment;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) InputStream(java.io.InputStream) QualifiedName(org.eclipse.core.runtime.QualifiedName) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) ValidationException(org.eclipse.wst.validation.internal.core.ValidationException) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException)

Example 17 with IContentDescription

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

the class FormatActionDelegate method processorAvailable.

/* (non-Javadoc)
	 * @see org.eclipse.wst.sse.ui.internal.actions.ResourceActionDelegate#processorAvailable(org.eclipse.core.resources.IResource)
	 */
protected boolean processorAvailable(IResource resource) {
    boolean result = false;
    if (resource.isAccessible()) {
        try {
            if (resource instanceof IFile) {
                IFile file = (IFile) resource;
                IStructuredFormatProcessor formatProcessor = null;
                IContentDescription contentDescription = file.getContentDescription();
                if (contentDescription != null) {
                    IContentType contentType = contentDescription.getContentType();
                    formatProcessor = getFormatProcessor(contentType.getId());
                }
                if (formatProcessor != null)
                    result = true;
            } else if (resource instanceof IContainer) {
                IContainer container = (IContainer) resource;
                IResource[] members;
                members = container.members();
                for (int i = 0; i < members.length; i++) {
                    boolean available = processorAvailable(members[i]);
                    if (available) {
                        result = true;
                        break;
                    }
                }
            }
        } catch (CoreException e) {
            Logger.logException(e);
        }
    }
    return result;
}
Also used : IStructuredFormatProcessor(org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IContentType(org.eclipse.core.runtime.content.IContentType) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) IContainer(org.eclipse.core.resources.IContainer)

Example 18 with IContentDescription

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

the class StorageModelProvider method getPersistedEncoding.

/* (non-Javadoc)
	 * @see org.eclipse.ui.editors.text.StorageDocumentProvider#getPersistedEncoding(java.lang.Object)
	 */
protected String getPersistedEncoding(Object element) {
    String charset = super.getPersistedEncoding(element);
    if (charset == null && element instanceof IStorageEditorInput) {
        IStorage storage;
        try {
            storage = ((IStorageEditorInput) element).getStorage();
            if (storage != null && !(storage instanceof IEncodedStorage)) {
                InputStream contents = null;
                try {
                    contents = storage.getContents();
                    if (contents != null) {
                        QualifiedName[] detectionOptions = new QualifiedName[] { IContentDescription.BYTE_ORDER_MARK, IContentDescription.CHARSET };
                        IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(contents, storage.getName(), detectionOptions);
                        if (description != null) {
                            charset = description.getCharset();
                        }
                    }
                } catch (IOException e) {
                } finally {
                    if (contents != null)
                        try {
                            contents.close();
                        } catch (IOException e) {
                        // do nothing
                        }
                }
            }
        } catch (CoreException e) {
            Logger.logException(e);
        }
    }
    return charset;
}
Also used : IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) CoreException(org.eclipse.core.runtime.CoreException) IEncodedStorage(org.eclipse.core.resources.IEncodedStorage) InputStream(java.io.InputStream) QualifiedName(org.eclipse.core.runtime.QualifiedName) IOException(java.io.IOException) IStorage(org.eclipse.core.resources.IStorage) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 19 with IContentDescription

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

the class JSPBatchValidator method isFragment.

/**
 * Determines if file is jsp fragment or not (does a deep, indepth check,
 * looking into contents of file)
 *
 * @param file
 *            assumes file is not null and exists
 * @return true if file is jsp fragment, false otherwise
 */
private boolean isFragment(IFile file) {
    boolean isFragment = false;
    InputStream is = null;
    try {
        IContentDescription contentDescription = file.getContentDescription();
        // it can be null
        if (contentDescription == null) {
            is = file.getContents();
            contentDescription = Platform.getContentTypeManager().getDescriptionFor(is, file.getName(), new QualifiedName[] { IContentDescription.CHARSET });
        }
        if (contentDescription != null) {
            String fileCtId = contentDescription.getContentType().getId();
            isFragment = (fileCtId != null && ContentTypeIdForJSP.ContentTypeID_JSPFRAGMENT.equals(fileCtId));
        }
    } catch (IOException e) {
    // ignore, assume it's invalid JSP
    } catch (CoreException e) {
    // ignore, assume it's invalid JSP
    } finally {
        /*
			 * must close input stream in case others need it
			 * (IFile.getContents() requirement as well)
			 */
        if (is != null)
            try {
                is.close();
            } catch (Exception e) {
            // not sure how to recover at this point
            }
    }
    return isFragment;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) InputStream(java.io.InputStream) QualifiedName(org.eclipse.core.runtime.QualifiedName) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) ValidationException(org.eclipse.wst.validation.internal.core.ValidationException) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

Example 20 with IContentDescription

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

the class JSPActionSourceValidator method isFragment.

/**
 * Determines if file is jsp fragment or not (does a deep, indepth check,
 * looking into contents of file)
 *
 * @param file
 *            assumes file is not null and exists
 * @return true if file is jsp fragment, false otherwise
 */
private boolean isFragment(IFile file) {
    // copied from JSPValidator
    boolean isFragment = false;
    InputStream is = null;
    try {
        IContentDescription contentDescription = file.getContentDescription();
        // it can be null
        if (contentDescription == null) {
            is = file.getContents();
            contentDescription = Platform.getContentTypeManager().getDescriptionFor(is, file.getName(), new QualifiedName[] { IContentDescription.CHARSET });
        }
        if (contentDescription != null) {
            String fileCtId = contentDescription.getContentType().getId();
            isFragment = (fileCtId != null && ContentTypeIdForJSP.ContentTypeID_JSPFRAGMENT.equals(fileCtId));
        }
    } catch (IOException e) {
    // ignore, assume it's invalid JSP
    } catch (CoreException e) {
    // ignore, assume it's invalid JSP
    } finally {
        // must close input stream in case others need it
        if (is != null)
            try {
                is.close();
            } catch (Exception e) {
            // not sure how to recover at this point
            }
    }
    return isFragment;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) InputStream(java.io.InputStream) QualifiedName(org.eclipse.core.runtime.QualifiedName) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

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