plugins/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/Util.java
author Eugene Ostroukhov <eugeneo@symbian.org>
Thu, 02 Sep 2010 10:50:38 -0700
changeset 483 109da596fa9d
parent 470 d4809db37847
child 484 f5df819c1852
permissions -rw-r--r--
Fixed relevant FindBug messages
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
     1
/**
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
     2
 * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies).
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
     3
 * All rights reserved.
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
     4
 * This component and the accompanying materials are made available
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
     6
 * which accompanies this distribution, and is available
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
     8
 *
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
     9
 * Initial Contributors:
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
    10
 * Symbian Foundation - initial contribution.
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
    11
 * Contributors:
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
    12
 * Description:
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
    13
 * Overview:
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
    14
 * Details:
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
    15
 * Platforms/Drives/Compatibility:
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
    16
 * Assumptions/Requirement/Pre-requisites:
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
    17
 * Failures and causes:
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
    18
 */
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
    19
package org.symbian.tools.wrttools.util;
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
    20
50
0560e98b9bf6 Added widget project nature and WRT validators
tasneems@symbian.org
parents: 17
diff changeset
    21
17
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
    22
public class Util {
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
    23
439
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    24
    public static String removeSpaces(String widgetName) {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    25
        return widgetName != null ? widgetName.replace(" ", "") : "";
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    26
    }
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    27
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    28
    /* Validation tests for both Windows & Mac OS */
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    29
    private static String commonValidate(String argName) {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    30
        if (argName.length() == 0) {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    31
            return ("Can not be empty");
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    32
        }
50
0560e98b9bf6 Added widget project nature and WRT validators
tasneems@symbian.org
parents: 17
diff changeset
    33
439
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    34
        // filenames starting with dot are not valid for both Widget name & UID
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    35
        if (argName.charAt(0) == '.') {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    36
            return ("Can not begin with a dot");
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    37
        }
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    38
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    39
        final char lastChar = argName.charAt(argName.length() - 1);
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    40
        // filenames ending in dot are not valid for both Widget name & UID
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    41
        if (lastChar == '.') {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    42
            return ("Can not end with dot");
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    43
        }
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    44
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    45
        return null;
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    46
    }
50
0560e98b9bf6 Added widget project nature and WRT validators
tasneems@symbian.org
parents: 17
diff changeset
    47
439
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    48
    public static String validateWidgetName(String widgetName) {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    49
        String strError = null;
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    50
        if ((strError = commonValidate(widgetName)) != null) {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    51
            return "Invalid Widget name. " + strError;
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    52
        }
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    53
        if (widgetName.indexOf("<") > -1 || widgetName.indexOf(">") > -1) {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    54
            return ("Invalid Widget name. Angle brackets are not allowed");
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    55
        }
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    56
        final char lastChar = widgetName.charAt(widgetName.length() - 1);
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    57
        // trailing or beginning space is not valid in filenames for Widget name
483
109da596fa9d Fixed relevant FindBug messages
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
    58
        if ((Character.isWhitespace(widgetName.charAt(0)) || Character.isWhitespace(lastChar))) {
439
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    59
            return ("Invalid Widget name. Beginning or trailing spaces are not allowed");
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    60
        }
50
0560e98b9bf6 Added widget project nature and WRT validators
tasneems@symbian.org
parents: 17
diff changeset
    61
439
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    62
        if (widgetName.indexOf('\n') != -1 || widgetName.indexOf('\t') != -1) {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    63
            return ("Invalid Widget name. newline character is not allowed");
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    64
        }
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    65
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    66
        return null;
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    67
    }
50
0560e98b9bf6 Added widget project nature and WRT validators
tasneems@symbian.org
parents: 17
diff changeset
    68
439
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    69
    public static String validateWidgetID(String widgetID) {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    70
        String strError = null;
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    71
        if ((strError = commonValidate(widgetID)) != null) {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    72
            return "Invalid Widget identifier. " + strError;
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    73
        }
50
0560e98b9bf6 Added widget project nature and WRT validators
tasneems@symbian.org
parents: 17
diff changeset
    74
439
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    75
        // file names with white spaces are not allowed for Widget Identifier
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    76
        // (UID)
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    77
        if (widgetID.indexOf(" ") > -1) {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    78
            return ("Invalid Widget identifier. Whitespaces are not allowed");
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    79
        }
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    80
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    81
        if (widgetID.length() > 78) {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    82
            return ("Invalid Widget identifier. Maximum string length exceeded");
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    83
        }
50
0560e98b9bf6 Added widget project nature and WRT validators
tasneems@symbian.org
parents: 17
diff changeset
    84
439
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    85
        /* test invalid characters, allows only alphanumeric and '.' for UID */
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    86
        String alphnum = ".0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    87
        for (int i = 0; i < widgetID.length(); i++) {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    88
            if (alphnum.indexOf(widgetID.charAt(i), 0) == -1) {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    89
                return ("Invalid Widget identifier. Only alphanumeric or '.' is allowed");
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    90
            }
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    91
        }
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    92
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    93
        if (widgetID.matches(".*[.]{2,}.*")) {
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    94
            return ("Invalid Widget identifier. Consecutive dots are not allowed");
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    95
        }
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    96
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    97
        return null;
57fff6202b74 Bug 3184 - Reorganize new project wizard
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 50
diff changeset
    98
    }
17
f1b0259bb410 Validation was added to new project creation wizard
Eugene Ostroukhov <eostroukhov@symbian.org>
parents:
diff changeset
    99
}