• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Pathname Validation - Please help out!!!

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm trying to validate pathnames in an OS independant way. The individual directory names should follow the usual naming convention (no special characters than '_', must contain an alphabet or number, etc) My code looks like the following :



This is the error i'm getting...

java.util.regex.PatternSyntaxException , at the line with str.split()

I guess using File.separator to do the split doesnt work...

I could use 'str.split("\\\\|\\/");' but is there a better way?
Or a regular expression to validate pathnames, for both Windows and Unix?
 
Pallavi Kudigrama
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
p.s - this is a situation where I want to create a directory, not validate an existing directory, hence the name checking.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pallavi,
Welcome to the Ranch.

While posting code, please use the code tags.
You can incorporate the tags in your existing post by clicking the button.

Are you aware that the File class provides a File#exists() method? The file object can either be a file or even a folder. Can you use that for your requirement?
 
Pallavi Kudigrama
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I do know about the exists() method...my task involves accepting a location from the user and ensuring that it exists; if it doesnt exist, create it.
However, to create I need to validate the directory names first. For eg. if the user inputs c:\abc\def , and it doesnt exist, then i need to ensure that 'abc' and 'def' are valid names...the user cannot be allowed to enter names 'ab$%c'.

So, in order to do this, I'm splitting the pathname into the individual directory names and storing them in an array, on which i do the validation. When I try to use 'File.separator' to do the split, I get the exception.
However I dont know any other OS independant way to do the split, and split("\\\\|\\/") seems primitive to me...is there a neater way to do it?

It seems easier to split and check individual names than to come up with a regular expression that would validate the path as a whole...but if there is such an expression, please let me know...
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So maybe you could try to create the file/directory and catch and process the exception if the creation fails? The OS won't allow to create a file with a bad name, so you'll get an exception - no validation required.
 
Pallavi Kudigrama
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm...maybe I should have explained my problem a little more in detail. Creation takes place in the final step; there are lots of other intermediate steps in between, some of which allow backtracking, like, say an installation wizard.

So if the directory is created at the validation step and the user decides to backtrack or cancel the task, it will need to be deleted and the system will have to return to it's original state. That's why I'm separating validation from creation.

My requirement is that the user should not be allowed to proceed if he has entered an invalid path, but if he has entered a valid one, then he continues till the last step which is when the directory is created.
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try File.getCanonicalPath(). If it returns, the file path is OK, otherwise it throws an exception, eg.

 
Pallavi Kudigrama
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, i'll try that...
 
Pallavi Kudigrama
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used getAbsolutePath() and getName() along with getParentFile() to do the validation for the whole path...I did away with split() altogether...it works now!

Thanks for pitching me in the right direction!
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic