1 16 package org.apache.commons.vfs.provider.local; 17 18 import org.apache.commons.vfs.FileName; 19 import org.apache.commons.vfs.FileSystemException; 20 import org.apache.commons.vfs.FileType; 21 22 27 public class WindowsFileNameParser 28 extends LocalFileNameParser 29 { 30 33 protected String extractRootPrefix(final String uri, 34 final StringBuffer name) 35 throws FileSystemException 36 { 37 return extractWindowsRootPrefix(uri, name); 38 } 39 40 protected FileName createFileName(String scheme, final String rootFile, final String path, final FileType type) 41 { 42 return new WindowsFileName(scheme, rootFile, path, type); 43 } 44 45 48 private String extractWindowsRootPrefix(final String uri, 49 final StringBuffer name) 50 throws FileSystemException 51 { 52 56 int startPos = 0; 58 int maxlen = Math.min(4, name.length()); 59 for (; startPos < maxlen && name.charAt(startPos) == '/'; startPos++) 60 { 61 } 62 if (startPos == maxlen && name.length() > startPos && name.charAt(startPos + 1) == '/') 63 { 64 throw new FileSystemException("vfs.provider.local/not-absolute-file-name.error", uri); 66 } 67 name.delete(0, startPos); 68 69 String driveName = extractDrivePrefix(name); 71 if (driveName != null) 72 { 73 return driveName; 74 } 75 76 if (startPos < 2) 78 { 79 throw new FileSystemException("vfs.provider.local/not-absolute-file-name.error", uri); 80 } 81 82 return "//" + extractUNCPrefix(uri, name); 83 } 84 85 88 private String extractDrivePrefix(final StringBuffer name) 89 { 90 if (name.length() < 3) 92 { 93 return null; 95 } 96 char ch = name.charAt(0); 97 if (ch == '/' || ch == ':') 98 { 99 return null; 101 } 102 if (name.charAt(1) != ':') 103 { 104 return null; 106 } 107 if (name.charAt(2) != '/') 108 { 109 return null; 111 } 112 113 String prefix = name.substring(0, 2); 114 name.delete(0, 2); 115 return prefix; 116 } 117 118 121 private String extractUNCPrefix(final String uri, 122 final StringBuffer name) 123 throws FileSystemException 124 { 125 127 int maxpos = name.length(); 129 int pos = 0; 130 for (; pos < maxpos && name.charAt(pos) != '/'; pos++) 131 { 132 } 133 pos++; 134 if (pos >= maxpos) 135 { 136 throw new FileSystemException("vfs.provider.local/missing-share-name.error", uri); 137 } 138 139 int startShareName = pos; 141 for (; pos < maxpos && name.charAt(pos) != '/'; pos++) 142 { 143 } 144 if (pos == startShareName) 145 { 146 throw new FileSystemException("vfs.provider.local/missing-share-name.error", uri); 147 } 148 149 String prefix = name.substring(0, pos); 151 name.delete(0, pos); 152 return prefix; 153 } 154 } 155 | Popular Tags |