Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 37e7a9999a207956cb151b9f200af0673457ec10 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*******************************************************************************
 * Copyright (c) 2001, 2007 Oracle Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Oracle Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.common.internal;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;

/**
 * Utility JSP methods
 * @author cbateman
 *
 */
public final class JSPUtil 
{
    private final static String  CTYPE_JSPSOURCE = 
        "org.eclipse.jst.jsp.core.jspsource"; //$NON-NLS-1$
    private final static String  CTYPE_JSPFRAGMENTSOURCE = 
        "org.eclipse.jst.jsp.core.jspfragmentsource"; //$NON-NLS-1$
    /**
     * @param contentType
     * @return true if contentType is one of the content types registered
     * for JSP files
     */
    public static boolean isJSPContentType(final String contentType)
    {
        return CTYPE_JSPSOURCE.equals(contentType)
                    || CTYPE_JSPFRAGMENTSOURCE.equals(contentType);
    }
    
    /**
     * @param file
     * @return true if file is associated with a JSP or JSP fragment content type
     */
    public static boolean isJSPContentType(final IFile file)
    {
    	final boolean isJSPSource = isJSPSource(file);
        if (isJSPSource)
        {
            return true;
        }

        final boolean isJSPFragment = isJSPFragment(file);
        
        if  (isJSPFragment)
        {
        	return true;
        }
        
        return false;
    }
    
    private JSPUtil()
    {
        // no instantiation
    }

	/**
	 * @param file
	 * @return true if file is associated with the JSP source content type
	 * (returns if JSP fragment)
	 */
	public static boolean isJSPSource(IFile file) {
		return isAssociatedWithContentType(file, CTYPE_JSPSOURCE);
	}

	/**
	 * @param file
	 * @return true if the file is associated with the JSP fragment content type
	 */
	public static boolean isJSPFragment(IFile file) {
		return isAssociatedWithContentType(file, CTYPE_JSPFRAGMENTSOURCE);
	}
	
	private static boolean isAssociatedWithContentType(final IFile file, final String contentType)
	{
        final IContentTypeManager typeManager = Platform.getContentTypeManager();
        IContentType jspContentType = 
            typeManager.getContentType(contentType);
        if (jspContentType != null
                && jspContentType.isAssociatedWith(file.getName()))
        {
            return true;
        }
      
        return false;
	}
}

Back to the top