Site Sponsors:
Fiark: Unicode & Valid File / Folder Names 
Sitting down to update Fiark this weekend, once again I came across the notion of detecting invalid directory & filenames. With a heavy sigh, I realize the advent of Unicode meant that there would be far, far more to check for than the usual "{}", "]", "/", "\", "&", "*" and others from our ASCII / UTF8 past.

If you have run across a similar problem, then you probably need no further explanation of the problem domain. Submitted for your fair use enjoyment therefore, here is what will be coming soon to a Fiark near you:


package com.soft9000.file;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
*
* @author profnagy
*/
public class LiveNodeName {

private String nodePathName = "";
private boolean isDirectory = false;

public LiveNodeName() {

}

public LiveNodeName(String nodePathName, boolean isDirectory) {
this.nodePathName = nodePathName;
this.isDirectory = isDirectory;

}

public String getName() {
return this.nodePathName;
}

public boolean isDirectory() {
return this.isDirectory;
}

public boolean isNull() {
return nodePathName.isEmpty();
}

public void asNull() {
this.nodePathName = "";
}

public static List<LiveNodeName> GetNodes(final LiveNodeName ref) {
String zName = ref.getName();
if (com.soft9000.Text.Contains(zName, "\\".getBytes())) {
zName = com.soft9000.Text.ReplaceAll(zName, "\\", "/"); // No RE's
}
while (zName.startsWith(".")) {
zName = zName.substring(1);
}
while (zName.startsWith("/")) {
zName = zName.substring(1);
}
String[] array = com.soft9000.Text.Split(zName, '/');
List<LiveNodeName> results = new ArrayList<LiveNodeName>();
for (String leaf : array) {
results.add(new LiveNodeName(leaf, true));
}
if (results.size() > 0 && ref.isDirectory == false) {
results.get(results.size() - 1).isDirectory = false;
}
return results;
}

/**
* With the advent of Unicode, the range of valid / invalid file names has
* become truly infinite! Rather than crafting a plethora of OS-centric RE
* tests, surely the best strategy is to simply let the operating system
* itself tell us the final score?
*
* @param ref a LiveNodeName
* @return True if the node can be hosted on this O.S!
*/
public static boolean CanCreate(final LiveNodeName nodei) {
if (nodei == null || nodei.isNull()) {
return false;
}
List<LiveNodeName> array = LiveNodeName.GetNodes(nodei);
try {
for (LiveNodeName oneSeg : array) {
if (oneSeg.isNull()) {
return false;
}
if (oneSeg.isDirectory) {
Directory dir = new Directory(oneSeg.getName());
if (dir.exists()) {
return true;
}
if (dir.mkdir() == true) {
return dir.delete();
}
} else {
File file = new File(oneSeg.nodePathName);
if (file.exists()) {
return true;
}
File pFile = file.getParentFile();
if (pFile == null) {
if (file.createNewFile() == true) {
file.delete();
return true;
}
}
boolean bShoulRemove = false;
Directory dir = new Directory(pFile);
if (dir.exists() == false) {
if (dir.mkdir() == false) {
return false;
}
bShoulRemove = true;
}
if (file.createNewFile() == true) {
if (file.delete() == false) {
return false;
}
if (bShoulRemove) {
return dir.delete();
} else {
return true;
}
}
}
}
} catch (Exception ex) {
}

return false;
}

/**
* Check a list of LiveNodeName, assigning any error-nodes to the result
* list.
*
* @param input List for LiveNodeName to test.
* @param results List of invalid LiveNodeName (rejects.)
* @return True of all NodeNames are okay - false if there were rejects.
*/
public static boolean CanCreate(final List<LiveNodeName> input, List<LiveNodeName> results) {
boolean br = true;
for (LiveNodeName ref : input) {
if (CanCreate(ref) == false) {
results.add(ref);
br = false;
}
}
return br;
}

/**
* For my problem domain (ONLY :)
* @param foo
*/
public static void main(String... foo) {
LiveNodeName node = new LiveNodeName("/foo/bar", true);
if (CanCreate(node) == false) {
System.out.println("Error 1000001!");
}
node = new LiveNodeName("foo", true);
if (CanCreate(node) == false) {
System.out.println("Error 1000002!");
}
node = new LiveNodeName("foo", false);
if (CanCreate(node) == false) {
System.out.println("Error 1000003!");
}
node = new LiveNodeName("/foo/bar.txt", false);
if (CanCreate(node) == false) {
System.out.println("Error 1000004!");
}
node = new LiveNodeName("/foo/\\bar.txt", false);
if (CanCreate(node) == false) {
System.out.println("Error 1000005!");
}
}
}


Enjoy the journey!

-Randall


[ view entry ] ( 517 views )   |  permalink  |  related link

<<First <Back | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | Next> Last>>