001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.util;
011:
012: import org.eclipse.core.runtime.IStatus;
013:
014: import org.eclipse.jdt.core.IJavaElement;
015: import org.eclipse.jdt.core.IJavaProject;
016: import org.eclipse.jdt.core.JavaConventions;
017: import org.eclipse.jdt.core.JavaCore;
018:
019: /**
020: * Provides methods for checking Java-specific conventions such as name syntax.
021: * <p>
022: * This is a wrapper for {@link JavaConventions} with more convenient
023: * <code>validate*(..)</code> methods. If multiple validations are planned on
024: * the same element, please use {@link JavaConventions} directly (with the
025: * arguments from {@link #getSourceComplianceLevels(IJavaElement)}).
026: * </p>
027: */
028: public class JavaConventionsUtil {
029:
030: /**
031: * @param context an {@link IJavaElement} or <code>null</code>
032: * @return a <code>String[]</code> whose <code>[0]</code> is the
033: * {@link JavaCore#COMPILER_SOURCE} and whose <code>[1]</code> is
034: * the {@link JavaCore#COMPILER_COMPLIANCE} level at the given
035: * <code>context</code>.
036: */
037: public static String[] getSourceComplianceLevels(
038: IJavaElement context) {
039: if (context != null) {
040: IJavaProject javaProject = context.getJavaProject();
041: if (javaProject != null) {
042: return new String[] {
043: javaProject.getOption(JavaCore.COMPILER_SOURCE,
044: true),
045: javaProject.getOption(
046: JavaCore.COMPILER_COMPLIANCE, true) };
047: }
048: }
049: return new String[] {
050: JavaCore.getOption(JavaCore.COMPILER_SOURCE),
051: JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE) };
052: }
053:
054: /**
055: * @param name the name to validate
056: * @param context an {@link IJavaElement} or <code>null</code>
057: * @return validation status in <code>context</code>'s project or in the workspace
058: *
059: * @see JavaConventions#validateCompilationUnitName(String, String, String)
060: */
061: public static IStatus validateCompilationUnitName(String name,
062: IJavaElement context) {
063: String[] sourceComplianceLevels = getSourceComplianceLevels(context);
064: return JavaConventions.validateCompilationUnitName(name,
065: sourceComplianceLevels[0], sourceComplianceLevels[1]);
066: }
067:
068: /**
069: * @param name the name to validate
070: * @param context an {@link IJavaElement} or <code>null</code>
071: * @return validation status in <code>context</code>'s project or in the workspace
072: *
073: * @see JavaConventions#validateClassFileName(String, String, String)
074: */
075: public static IStatus validateClassFileName(String name,
076: IJavaElement context) {
077: String[] sourceComplianceLevels = getSourceComplianceLevels(context);
078: return JavaConventions.validateClassFileName(name,
079: sourceComplianceLevels[0], sourceComplianceLevels[1]);
080: }
081:
082: /**
083: * @param name the name to validate
084: * @param context an {@link IJavaElement} or <code>null</code>
085: * @return validation status in <code>context</code>'s project or in the workspace
086: *
087: * @see JavaConventions#validateFieldName(String, String, String)
088: */
089: public static IStatus validateFieldName(String name,
090: IJavaElement context) {
091: String[] sourceComplianceLevels = getSourceComplianceLevels(context);
092: return JavaConventions.validateFieldName(name,
093: sourceComplianceLevels[0], sourceComplianceLevels[1]);
094: }
095:
096: /**
097: * @param name the name to validate
098: * @param context an {@link IJavaElement} or <code>null</code>
099: * @return validation status in <code>context</code>'s project or in the workspace
100: *
101: * @see JavaConventions#validateIdentifier(String, String, String)
102: */
103: public static IStatus validateIdentifier(String name,
104: IJavaElement context) {
105: String[] sourceComplianceLevels = getSourceComplianceLevels(context);
106: return JavaConventions.validateIdentifier(name,
107: sourceComplianceLevels[0], sourceComplianceLevels[1]);
108: }
109:
110: /**
111: * @param name the name to validate
112: * @param context an {@link IJavaElement} or <code>null</code>
113: * @return validation status in <code>context</code>'s project or in the workspace
114: *
115: * @see JavaConventions#validateImportDeclaration(String, String, String)
116: */
117: public static IStatus validateImportDeclaration(String name,
118: IJavaElement context) {
119: String[] sourceComplianceLevels = getSourceComplianceLevels(context);
120: return JavaConventions.validateImportDeclaration(name,
121: sourceComplianceLevels[0], sourceComplianceLevels[1]);
122: }
123:
124: /**
125: * @param name the name to validate
126: * @param context an {@link IJavaElement} or <code>null</code>
127: * @return validation status in <code>context</code>'s project or in the workspace
128: *
129: * @see JavaConventions#validateJavaTypeName(String, String, String)
130: */
131: public static IStatus validateJavaTypeName(String name,
132: IJavaElement context) {
133: String[] sourceComplianceLevels = getSourceComplianceLevels(context);
134: return JavaConventions.validateJavaTypeName(name,
135: sourceComplianceLevels[0], sourceComplianceLevels[1]);
136: }
137:
138: /**
139: * @param name the name to validate
140: * @param context an {@link IJavaElement} or <code>null</code>
141: * @return validation status in <code>context</code>'s project or in the workspace
142: *
143: * @see JavaConventions#validateMethodName(String, String, String)
144: */
145: public static IStatus validateMethodName(String name,
146: IJavaElement context) {
147: String[] sourceComplianceLevels = getSourceComplianceLevels(context);
148: return JavaConventions.validateMethodName(name,
149: sourceComplianceLevels[0], sourceComplianceLevels[1]);
150: }
151:
152: /**
153: * @param name the name to validate
154: * @param context an {@link IJavaElement} or <code>null</code>
155: * @return validation status in <code>context</code>'s project or in the workspace
156: *
157: * @see JavaConventions#validatePackageName(String, String, String)
158: */
159: public static IStatus validatePackageName(String name,
160: IJavaElement context) {
161: String[] sourceComplianceLevels = getSourceComplianceLevels(context);
162: return JavaConventions.validatePackageName(name,
163: sourceComplianceLevels[0], sourceComplianceLevels[1]);
164: }
165:
166: /**
167: * @param name the name to validate
168: * @param context an {@link IJavaElement} or <code>null</code>
169: * @return validation status in <code>context</code>'s project or in the workspace
170: *
171: * @see JavaConventions#validateTypeVariableName(String, String, String)
172: */
173: public static IStatus validateTypeVariableName(String name,
174: IJavaElement context) {
175: String[] sourceComplianceLevels = getSourceComplianceLevels(context);
176: return JavaConventions.validateTypeVariableName(name,
177: sourceComplianceLevels[0], sourceComplianceLevels[1]);
178: }
179: }
|