All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Timo Müller" <mail@timomueller.eu>
To: Ioana Grigoropol <ioanax.grigoropol@intel.com>
Cc: yocto@yoctoproject.org
Subject: Re: [PATCH] [eclipse-poky][master]Add more comprehensive error message for invalid project name
Date: Thu, 14 Mar 2013 11:36:28 +0100	[thread overview]
Message-ID: <5141A82C.5040400@timomueller.eu> (raw)
In-Reply-To: <5141A68D.5020601@timomueller.eu>

Hi Ioana,

Timo Müller wrote, On 14.03.2013 11:29:
> Hi Ioana,
>
> Ioana Grigoropol wrote, On 14.03.2013 10:06:
>> [Yocto #4008]
>>
>> Signed-off-by: Ioana Grigoropol <ioanax.grigoropol@intel.com>
>> ---
>>   .../sdk/ide/wizard/NewYoctoCProjectTemplate.java   |   34
>> +++++++++++++++++---
>>   1 file changed, 29 insertions(+), 5 deletions(-)
>>
>> diff --git
>> a/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/wizard/NewYoctoCProjectTemplate.java
>> b/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/wizard/NewYoctoCProjectTemplate.java
>>
>> index 5ffd6b7..8ab5972 100644
>> ---
>> a/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/wizard/NewYoctoCProjectTemplate.java
>>
>> +++
>> b/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/wizard/NewYoctoCProjectTemplate.java
>>
>> @@ -10,6 +10,8 @@
>>
>> *******************************************************************************/
>>
>>   package org.yocto.sdk.ide.wizard;
>>
>> +import java.util.ArrayList;
>> +import java.util.Arrays;
>>   import java.util.LinkedHashMap;
>>   import java.util.List;
>>
>> @@ -46,11 +48,11 @@ import org.yocto.sdk.ide.YoctoProfileElement;
>>   import org.yocto.sdk.ide.YoctoSDKChecker;
>>   import org.yocto.sdk.ide.YoctoSDKChecker.SDKCheckRequestFrom;
>>   import org.yocto.sdk.ide.YoctoSDKChecker.SDKCheckResults;
>> +import org.yocto.sdk.ide.YoctoSDKPlugin;
>> +import org.yocto.sdk.ide.YoctoUIElement;
>>   import org.yocto.sdk.ide.natures.YoctoSDKEmptyProjectNature;
>>   import org.yocto.sdk.ide.natures.YoctoSDKProjectNature;
>>   import org.yocto.sdk.ide.utils.YoctoSDKUtils;
>> -import org.yocto.sdk.ide.YoctoSDKPlugin;
>> -import org.yocto.sdk.ide.YoctoUIElement;
>>
>>
>>   @SuppressWarnings("restriction")
>> @@ -58,11 +60,20 @@ public class NewYoctoCProjectTemplate extends
>> ProcessRunner {
>>       protected boolean savedAutoBuildingValue;
>>       protected ProjectCreatedActions pca;
>>       protected IManagedBuildInfo info;
>> +    protected List<Character> illegalChars = Arrays.asList('$',
>> '"','#','%','&','\'','(',')','*', '+',
>> ',','.','/',':',';','<','=','>','?','@','[','\\',']','^','`','{','|','}','~');
>>
>>
>>       public NewYoctoCProjectTemplate() {
>>           pca = new ProjectCreatedActions();
>>       }
>> -
>> +    private String printIllegalChars(){
>> +        String print = "";
>> +        for (int i = 0; i < illegalChars.size(); i++) {
>> +            print += illegalChars.get(i);
>> +            if (i != illegalChars.size() - 1)
>> +                print += " ,";
>> +        }
>> +        return print;
>> +    }
>
> I think the contained "if" isn't really necessary. You can remove the
> last ", " afterwards. Then you can also use a for each loop to append
> the characters.
>
> private String printIllegalChars(){
>      String print = "";
>
>      for (Character character : illegalChars) {
>          print += character + ", ";
>      }
>
>      if (!illegalChars.isEmpty()) {
>          print = print.substring(0, print.lastIndexOf(",") - 1);
>      }
>
>      return print;
> }
>
>>       public void process(TemplateCore template, ProcessArgument[]
>> args, String processId, IProgressMonitor monitor) throws
>> ProcessFailureException {
>>
>>           String projectName = args[0].getSimpleValue();
>> @@ -74,9 +85,10 @@ public class NewYoctoCProjectTemplate extends
>> ProcessRunner {
>>           boolean isEmptryProject =
>> Boolean.valueOf(isEmptyProjetValue).booleanValue();
>>           IProject project =
>> ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
>>           try {
>> -            if (projectName.contains(" ")) {
>> +            if (!isValidProjectName(projectName)) {
>>                   project.delete(true, null);
>> -                throw new ProcessFailureException(projectName + "
>> contains space(s).  Project name can't contain space(s)");
>> +                throw new ProcessFailureException("Project name " +
>> "\""+ projectName +"\"" +" is invalid! " +
>> +                        "\nNone of these characters are accepted
>> inside project names: whitespaces, " + printIllegalChars());
>
> Would be nice if we can wrap this up with the YoctoSDKMessages to be
> able to internationalize the string later.
>
>>               }
>>               if (!project.exists()) {
>>                   IWorkspace workspace = ResourcesPlugin.getWorkspace();
>> @@ -166,6 +178,18 @@ public class NewYoctoCProjectTemplate extends
>> ProcessRunner {
>>               throw new
>> OperationCanceledException(Messages.getString("NewManagedProject.3") +
>> e.getMessage());
>>           }
>>       }
>> +    private boolean isValidProjectName(String projectName) {
>> +        if (projectName.contains("\\s+"))
>> +            return false;
>> +
>> +        char[] chars = projectName.toCharArray();
>> +        if (!Character.isJavaIdentifierStart(chars[0]))
>> +            return false;
>> +        for (int i = 1; i < chars.length; i++)
>> +            if (illegalChars.contains(chars[i]))
>> +                return false;
>> +        return true;
>> +}
>
> I think it would be better to use functionality from java.util.regex to
> do the name checking. The Pattern could be compiled once as a static
> member and we could than use the matcher to check whether the project
> name is fine.
>
> Something like this:
>
> private static Pattern pattern = Pattern.compile(".*(\\$|\").*");
>
> private boolean isValidProjectName(String projectName) {
>    Matcher matcher = pattern.matcher(projectName);
>
>    if (matcher.matches()) {
>      return false;
>    }
>
>    return true;
> }
>
>>
>>       protected final void turnOffAutoBuild(IWorkspace workspace)
>> throws CoreException {
>>           IWorkspaceDescription workspaceDesc =
>> workspace.getDescription();
>>
>
> Best regards,
> Timo


I forgot to mention. I found a way to create projects with invalid names.

# Create a new Yocto C Project
# Enter a wrong name eg. Test"
# Press next
# Press finish
# --> Error Message is shown, press ok
# Press back
# Change the name to an also invalid name eg. Test$
# Click finish

Best regards,
Timo



  reply	other threads:[~2013-03-14 10:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-14  9:06 [PATCH] [eclipse-poky][master]Add more comprehensive error message for invalid project name Ioana Grigoropol
2013-03-14 10:29 ` Timo Müller
2013-03-14 10:36   ` Timo Müller [this message]
2013-03-14 12:08   ` Grigoropol, IoanaX

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5141A82C.5040400@timomueller.eu \
    --to=mail@timomueller.eu \
    --cc=ioanax.grigoropol@intel.com \
    --cc=yocto@yoctoproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.