Example usage for org.eclipse.jdt.core JavaConventions validatePackageName

List of usage examples for org.eclipse.jdt.core JavaConventions validatePackageName

Introduction

In this page you can find the example usage for org.eclipse.jdt.core JavaConventions validatePackageName.

Prototype

public static IStatus validatePackageName(String name, String sourceLevel, String complianceLevel) 

Source Link

Document

Validate the given package name for the given source and compliance levels.

Usage

From source file:com.android.ide.eclipse.adt.internal.wizards.newproject.ApplicationInfoPage.java

License:Open Source License

public static IStatus validatePackage(String packageFieldContents) {
    // Validate package
    if (packageFieldContents == null || packageFieldContents.length() == 0) {
        return new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID, "Package name must be specified.");
    } else if (packageFieldContents.equals(DUMMY_PACKAGE)) {
        // The dummy package name is just a placeholder package (which isn't even valid
        // because it contains the reserved Java keyword "package") but we want to
        // make the error message say that a proper package should be entered rather than
        // what's wrong with this specific package. (And the reason we provide a dummy
        // package rather than a blank line is to make it more clear to beginners what
        // we're looking for.
        return new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID, "Package name must be specified.");
    }/*from   ww w . j a  v  a2s.c  om*/
    // Check it's a valid package string
    IStatus status = JavaConventions.validatePackageName(packageFieldContents, JDK_15, JDK_15);
    if (!status.isOK()) {
        return status;
    }

    // The Android Activity Manager does not accept packages names with only one
    // identifier. Check the package name has at least one dot in them (the previous rule
    // validated that if such a dot exist, it's not the first nor last characters of the
    // string.)
    if (packageFieldContents.indexOf('.') == -1) {
        return new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID,
                "Package name must have at least two identifiers.");
    }

    return null;
}

From source file:com.android.ide.eclipse.adt.internal.wizards.newproject.ApplicationInfoPage.java

License:Open Source License

/**
 * Validates the given activity name/*  w ww .j  a v a2s.c  o  m*/
 *
 * @param activityFieldContents the activity name to validate
 * @return a status for whether the activity name is valid
 */
public static IStatus validateActivity(String activityFieldContents) {
    // Validate activity field
    if (activityFieldContents == null || activityFieldContents.length() == 0) {
        return new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID, "Activity name must be specified.");
    } else if (ACTIVITY_NAME_SUFFIX.equals(activityFieldContents)) {
        return new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID, "Enter a valid activity name");
    } else if (activityFieldContents.contains("..")) { //$NON-NLS-1$
        return new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID,
                "Package segments in activity name cannot be empty (..)");
    }
    // The activity field can actually contain part of a sub-package name
    // or it can start with a dot "." to indicates it comes from the parent package
    // name.
    String packageName = ""; //$NON-NLS-1$
    int pos = activityFieldContents.lastIndexOf('.');
    if (pos >= 0) {
        packageName = activityFieldContents.substring(0, pos);
        if (packageName.startsWith(".")) { //$NON-NLS-1$
            packageName = packageName.substring(1);
        }

        activityFieldContents = activityFieldContents.substring(pos + 1);
    }

    // the activity field can contain a simple java identifier, or a
    // package name or one that starts with a dot. So if it starts with a dot,
    // ignore this dot -- the rest must look like a package name.
    if (activityFieldContents.length() > 0 && activityFieldContents.charAt(0) == '.') {
        activityFieldContents = activityFieldContents.substring(1);
    }

    // Check it's a valid activity string
    IStatus status = JavaConventions.validateTypeVariableName(activityFieldContents, JDK_15, JDK_15);
    if (!status.isOK()) {
        return status;
    }

    // Check it's a valid package string
    if (packageName.length() > 0) {
        status = JavaConventions.validatePackageName(packageName, JDK_15, JDK_15);
        if (!status.isOK()) {
            return new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID,
                    status.getMessage() + " (in the activity name)");
        }
    }

    return null;
}

From source file:com.android.ide.eclipse.adt.internal.wizards.newproject.NewProjectCreationPage.java

License:Open Source License

/**
 * Validates the activity name field.//from  w  ww . j  a va  2  s  . c o  m
 *
 * @return The wizard message type, one of MSG_ERROR, MSG_WARNING or MSG_NONE.
 */
private int validateActivityField() {
    // Disregard if not creating an activity
    if (!mInfo.isCreateActivity()) {
        return MSG_NONE;
    }

    // Validate activity field
    String activityFieldContents = mInfo.getActivityName();
    if (activityFieldContents.length() == 0) {
        return setStatus("Activity name must be specified.", MSG_ERROR);
    }

    // The activity field can actually contain part of a sub-package name
    // or it can start with a dot "." to indicates it comes from the parent package name.
    String packageName = ""; //$NON-NLS-1$
    int pos = activityFieldContents.lastIndexOf('.');
    if (pos >= 0) {
        packageName = activityFieldContents.substring(0, pos);
        if (packageName.startsWith(".")) { //$NON-NLS-1$
            packageName = packageName.substring(1);
        }

        activityFieldContents = activityFieldContents.substring(pos + 1);
    }

    // the activity field can contain a simple java identifier, or a
    // package name or one that starts with a dot. So if it starts with a dot,
    // ignore this dot -- the rest must look like a package name.
    if (activityFieldContents.charAt(0) == '.') {
        activityFieldContents = activityFieldContents.substring(1);
    }

    // Check it's a valid activity string
    int result = MSG_NONE;
    IStatus status = JavaConventions.validateTypeVariableName(activityFieldContents, "1.5", "1.5"); //$NON-NLS-1$ $NON-NLS-2$
    if (!status.isOK()) {
        result = setStatus(status.getMessage(),
                status.getSeverity() == IStatus.ERROR ? MSG_ERROR : MSG_WARNING);
    }

    // Check it's a valid package string
    if (result != MSG_ERROR && packageName.length() > 0) {
        status = JavaConventions.validatePackageName(packageName, "1.5", "1.5"); //$NON-NLS-1$ $NON-NLS-2$
        if (!status.isOK()) {
            result = setStatus(status.getMessage() + " (in the activity name)",
                    status.getSeverity() == IStatus.ERROR ? MSG_ERROR : MSG_WARNING);
        }
    }

    return result;
}

From source file:com.android.ide.eclipse.adt.internal.wizards.newproject.NewProjectCreationPage.java

License:Open Source License

/**
 * Validates the package name field./* w w  w  .j  a va  2 s  .co m*/
 *
 * @return The wizard message type, one of MSG_ERROR, MSG_WARNING or MSG_NONE.
 */
private int validatePackageField() {
    // Validate package field
    String packageFieldContents = mInfo.getPackageName();
    if (packageFieldContents.length() == 0) {
        return setStatus("Package name must be specified.", MSG_ERROR);
    }

    // Check it's a valid package string
    int result = MSG_NONE;
    IStatus status = JavaConventions.validatePackageName(packageFieldContents, "1.5", "1.5"); //$NON-NLS-1$ $NON-NLS-2$
    if (!status.isOK()) {
        result = setStatus(status.getMessage(),
                status.getSeverity() == IStatus.ERROR ? MSG_ERROR : MSG_WARNING);
    }

    // The Android Activity Manager does not accept packages names with only one
    // identifier. Check the package name has at least one dot in them (the previous rule
    // validated that if such a dot exist, it's not the first nor last characters of the
    // string.)
    if (result != MSG_ERROR && packageFieldContents.indexOf('.') == -1) {
        return setStatus("Package name must have at least two identifiers.", MSG_ERROR);
    }

    return result;
}

From source file:com.android.ide.eclipse.adt.internal.wizards.newproject.NewTestProjectCreationPage.java

License:Open Source License

/**
 * Validates the package name field.// w  w  w.j  av a  2s.  com
 *
 * @return The wizard message type, one of MSG_ERROR, MSG_WARNING or MSG_NONE.
 */
private int validatePackageField() {
    // Validate package field
    String packageName = mInfo.getPackageName();
    if (packageName.length() == 0) {
        return setStatus("Project package name must be specified.", MSG_ERROR);
    }

    // Check it's a valid package string
    int result = MSG_NONE;
    IStatus status = JavaConventions.validatePackageName(packageName, "1.5", "1.5"); //$NON-NLS-1$ $NON-NLS-2$
    if (!status.isOK()) {
        result = setStatus(String.format("Project package: %s", status.getMessage()),
                status.getSeverity() == IStatus.ERROR ? MSG_ERROR : MSG_WARNING);
    }

    // The Android Activity Manager does not accept packages names with only one
    // identifier. Check the package name has at least one dot in them (the previous rule
    // validated that if such a dot exist, it's not the first nor last characters of the
    // string.)
    if (result != MSG_ERROR && packageName.indexOf('.') == -1) {
        return setStatus("Project package name must have at least two identifiers.", MSG_ERROR);
    }

    // Check that the target package name is valid too
    packageName = mInfo.getTargetPackageName();
    if (packageName.length() == 0) {
        return setStatus("Target package name must be specified.", MSG_ERROR);
    }

    // Check it's a valid package string
    status = JavaConventions.validatePackageName(packageName, "1.5", "1.5"); //$NON-NLS-1$ $NON-NLS-2$
    if (!status.isOK()) {
        result = setStatus(String.format("Target package: %s", status.getMessage()),
                status.getSeverity() == IStatus.ERROR ? MSG_ERROR : MSG_WARNING);
    }

    if (result != MSG_ERROR && packageName.indexOf('.') == -1) {
        return setStatus("Target name must have at least two identifiers.", MSG_ERROR);
    }

    return result;
}

From source file:com.android.ide.eclipse.adt.wizards.newproject.NewProjectCreationPage.java

License:Open Source License

/**
 * Validates the activity name field./*  w ww .  j  a v a 2s  .  c  o  m*/
 *
 * @return The wizard message type, one of MSG_ERROR, MSG_WARNING or MSG_NONE.
 */
private int validateActivityField() {
    // Disregard if not creating an activity
    if (!isCreateActivity()) {
        return MSG_NONE;
    }

    // Validate activity field
    String activityFieldContents = getActivityName();
    if (activityFieldContents.length() == 0) {
        return setStatus("Activity name must be specified.", MSG_ERROR);
    }

    // The activity field can actually contain part of a sub-package name
    // or it can start with a dot "." to indicates it comes from the parent package name.
    String packageName = "";
    int pos = activityFieldContents.lastIndexOf('.');
    if (pos >= 0) {
        packageName = activityFieldContents.substring(0, pos);
        if (packageName.startsWith(".")) { //$NON-NLS-1$
            packageName = packageName.substring(1);
        }

        activityFieldContents = activityFieldContents.substring(pos + 1);
    }

    // the activity field can contain a simple java identifier, or a
    // package name or one that starts with a dot. So if it starts with a dot,
    // ignore this dot -- the rest must look like a package name.
    if (activityFieldContents.charAt(0) == '.') {
        activityFieldContents = activityFieldContents.substring(1);
    }

    // Check it's a valid activity string
    int result = MSG_NONE;
    IStatus status = JavaConventions.validateTypeVariableName(activityFieldContents, "1.5", "1.5"); //$NON-NLS-1$ $NON-NLS-2$
    if (!status.isOK()) {
        result = setStatus(status.getMessage(),
                status.getSeverity() == IStatus.ERROR ? MSG_ERROR : MSG_WARNING);
    }

    // Check it's a valid package string
    if (result != MSG_ERROR && packageName.length() > 0) {
        status = JavaConventions.validatePackageName(packageName, "1.5", "1.5"); //$NON-NLS-1$ $NON-NLS-2$
        if (!status.isOK()) {
            result = setStatus(status.getMessage() + " (in the activity name)",
                    status.getSeverity() == IStatus.ERROR ? MSG_ERROR : MSG_WARNING);
        }
    }

    return result;
}

From source file:com.android.ide.eclipse.adt.wizards.newproject.NewProjectCreationPage.java

License:Open Source License

/**
 * Validates the package name field.//www  .ja v a  2  s  .com
 *
 * @return The wizard message type, one of MSG_ERROR, MSG_WARNING or MSG_NONE.
 */
private int validatePackageField() {
    // Validate package field
    String packageFieldContents = getPackageName();
    if (packageFieldContents.length() == 0) {
        return setStatus("Package name must be specified.", MSG_ERROR);
    }

    // Check it's a valid package string
    int result = MSG_NONE;
    IStatus status = JavaConventions.validatePackageName(packageFieldContents, "1.5", "1.5"); //$NON-NLS-1$ $NON-NLS-2$
    if (!status.isOK()) {
        result = setStatus(status.getMessage(),
                status.getSeverity() == IStatus.ERROR ? MSG_ERROR : MSG_WARNING);
    }

    // The Android Activity Manager does not accept packages names with only one
    // identifier. Check the package name has at least one dot in them (the previous rule
    // validated that if such a dot exist, it's not the first nor last characters of the
    // string.)
    if (result != MSG_ERROR && packageFieldContents.indexOf('.') == -1) {
        return setStatus("Package name must have at least two identifiers.", MSG_ERROR);
    }

    return result;
}

From source file:com.ebmwebsourcing.petals.components.wizards.ComponentNewWizardPage.java

License:Open Source License

/**
 * Validates the page fields.//  w  w w  . j a va 2  s  .  c  o m
 */
private void validate() {

    // Validate fields
    if (StringUtils.isEmpty(this.name)) {
        updateStatus("You have to provide the component name.");
        return;
    }

    if (!this.name.toLowerCase().equals(this.name)) {
        updateStatus("The component name should be completely in lower case.");
        return;
    }

    if (this.isAtDefaultLocation) {
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(this.name);
        if (project.exists()) {
            updateStatus("A component project with a similar name already exists in the workspace.");
            return;
        }
    }

    if (StringUtils.isEmpty(this.groupId)) {
        updateStatus("You have to provide the component group ID.");
        return;
    }

    if (!JavaConventions.validatePackageName(this.groupId, JavaCore.VERSION_1_5, JavaCore.VERSION_1_5).isOK()) {
        updateStatus("The component group ID does not respect the Java package syntax.");
        return;
    }

    if (StringUtils.isEmpty(this.rootPackage)) {
        updateStatus("You have to provide the Java root package.");
        return;
    }

    if (!JavaConventions.validatePackageName(this.rootPackage, JavaCore.VERSION_1_5, JavaCore.VERSION_1_5)
            .isOK()) {
        updateStatus("The Java root package name does not respect the Java package syntax.");
        return;
    }

    // Project location
    File projectFile;
    if (!this.isAtDefaultLocation) {

        if (StringUtils.isEmpty(this.location)) {
            updateStatus("You have to specify the project location.");
            return;
        }

        try {
            projectFile = new File(this.location, this.name);
            this.projectLocationURI = projectFile.toURI();

        } catch (Exception e) {
            updateStatus("The specified location is not a valid file location.");
            return;
        }

        IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(this.name);
        IStatus status = ResourcesPlugin.getWorkspace().validateProjectLocationURI(p, this.projectLocationURI);
        if (status.getCode() != IStatus.OK) {
            updateStatus(status.getMessage());
            return;
        }

    } else {
        this.projectLocationURI = null;
        projectFile = new File(ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile(), this.name);
    }

    // Does the file exists?
    if (projectFile.exists()) {
        updateStatus("There is already an existing file at this location.");
        return;
    }

    updateStatus(null);
}

From source file:com.ebmwebsourcing.petals.components.wizards.SharedLibraryNewWizardPage.java

License:Open Source License

/**
 * Validates the page fields.//from w w w . ja  v a2s. co  m
 */
private void validate() {

    // Validate fields
    if (StringUtils.isEmpty(this.name)) {
        updateStatus("You have to provide the shared-library's name.");
        return;
    }

    if (!this.name.toLowerCase().equals(this.name)) {
        updateStatus("The shared library's name should be completely in lower case.");
        return;
    }

    if (this.isAtDefaultLocation) {
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(this.name);
        if (project.exists()) {
            updateStatus("A project with a similar name already exists in the workspace.");
            return;
        }
    }

    if (StringUtils.isEmpty(this.groupId)) {
        updateStatus("You have to provide the group ID.");
        return;
    }

    if (!JavaConventions.validatePackageName(this.groupId, JavaCore.VERSION_1_5, JavaCore.VERSION_1_5).isOK()) {
        updateStatus("The group ID does not respect the Java package syntax.");
        return;
    }

    // Project location
    File projectFile;
    if (!this.isAtDefaultLocation) {

        if (this.location == null || this.location.length() == 0) {
            updateStatus("You have to specify the project location.");
            return;
        }

        try {
            projectFile = new File(this.location, this.name);
            this.projectLocationURI = projectFile.toURI();

        } catch (Exception e) {
            updateStatus("The specified location is not a valid file location.");
            return;
        }

        IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(this.name);
        IStatus status = ResourcesPlugin.getWorkspace().validateProjectLocationURI(p, this.projectLocationURI);
        if (status.getCode() != IStatus.OK) {
            updateStatus(status.getMessage());
            return;
        }

    } else {
        this.projectLocationURI = null;
        projectFile = new File(ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile(), this.name);
    }

    // Does the file exists?
    if (projectFile.exists()) {
        updateStatus("There is already an existing file at this location.");
        return;
    }

    updateStatus(null);
}

From source file:com.google.cloud.tools.eclipse.appengine.newproject.JavaPackageValidator.java

License:Apache License

/**
 * Check if a string is a legal Java package name.
 *///  w  w w.j av a2 s  .c o  m
public static IStatus validate(String packageName) {
    if (packageName == null) {
        return new Status(IStatus.ERROR, PLUGIN_ID, 45, "null package name", null);
    } else if (packageName.isEmpty()) { // default package is allowed
        return Status.OK_STATUS;
    } else if (packageName.endsWith(".")) {
        // todo or allow this and strip the period
        return new Status(IStatus.ERROR, PLUGIN_ID, 46,
                MessageFormat.format("{0} ends with a period.", packageName), null);
    } else if (containsWhitespace(packageName)) {
        // very weird condition because validatePackageName allows internal white space
        return new Status(IStatus.ERROR, PLUGIN_ID, 46,
                MessageFormat.format("{0} contains whitespace.", packageName), null);
    } else {
        return JavaConventions.validatePackageName(packageName, JavaCore.VERSION_1_4, JavaCore.VERSION_1_4);
    }
}