关于Eclipse中校验输入文件名的源代码

时间:2024-01-08 21:00:32

Eclipse中测试文件名的方法.

也没有单独的分操作系统.在Talend时解决一个在文本框中输入名字有Bug的一个问题,这个是Eclipse中解决输入名字,对名字校验的部分源码.

     public IStatus validateName(String segment, int type) {
         String message;

         /* segment must not be null */
         if (segment == null) {
             message = Messages.resources_nameNull;
             return new ResourceStatus(IResourceStatus.INVALID_VALUE, null, message);
         }

         // cannot be an empty string
         if (segment.length() == 0) {
             message = Messages.resources_nameEmpty;
             return new ResourceStatus(IResourceStatus.INVALID_VALUE, null, message);
         }

         /* test invalid characters */
         char[] chars = OS.INVALID_RESOURCE_CHARACTERS;
         for (int i = 0; i < chars.length; i++)
             if (segment.indexOf(chars[i]) != -1) {
                 message = NLS.bind(Messages.resources_invalidCharInName, String.valueOf(chars[i]), segment);
                 return new ResourceStatus(IResourceStatus.INVALID_VALUE, null, message);
             }

         /* test invalid OS names */
         if (!OS.isNameValid(segment)) {
             message = NLS.bind(Messages.resources_invalidName, segment);
             return new ResourceStatus(IResourceStatus.INVALID_VALUE, null, message);
         }
         return Status.OK_STATUS;
     }