Hi Ioana,

Ioana Grigoropol wrote, On 14.03.2013 10:06:
[Yocto #4008]

Signed-off-by: Ioana Grigoropol <[email protected]>
---
  .../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

_______________________________________________
yocto mailing list
[email protected]
https://lists.yoctoproject.org/listinfo/yocto

Reply via email to