001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.tools.file;
031:
032: import java.io.File;
033: import java.io.IOException;
034: import java.io.RandomAccessFile;
035:
036: import de.intarsys.tools.string.StringTools;
037:
038: public class FilenameTest {
039:
040: private String path;
041:
042: private File file;
043:
044: private boolean readOnly = false;
045:
046: private boolean exists = false;
047:
048: private boolean legal = true;
049:
050: public FilenameTest(File pFile) {
051: this .file = pFile == null ? new File("") : pFile; //$NON-NLS-1$
052: this .path = file.getAbsolutePath();
053: check();
054: }
055:
056: public FilenameTest(String filepath) {
057: this (new File(StringTools.isEmpty(filepath) ? "" : filepath)); //$NON-NLS-1$
058: }
059:
060: private void check() {
061: checkCharacters();
062: checkFileState();
063: }
064:
065: private void checkCharacters() {
066: if (System
067: .getProperty("os.name").toLowerCase().indexOf("windows") != -1) { //$NON-NLS-1$//$NON-NLS-2$
068: String fileName = (new File(path)).getName();
069: if ((path.indexOf("<") != -1) || (path.indexOf(">") != -1)
070: || (path.indexOf("?") != -1)
071: || (path.indexOf("\"") != -1)
072: || (fileName.indexOf(":") != -1)
073: || (path.indexOf("|") != -1)
074: || (path.indexOf("*") != -1)) {
075: legal = false;
076: }
077: }
078: }
079:
080: private void checkFileState() {
081: if (!file.exists()) {
082: exists = false;
083: readOnly = false;
084: return;
085: }
086: exists = true;
087: if (!isDirectory()) {
088: checkReadOnly();
089: }
090: }
091:
092: private void checkReadOnly() {
093: // try writing, everything fails somehow
094: RandomAccessFile r = null;
095: try {
096: r = new RandomAccessFile(file, "rw"); //$NON-NLS-1$
097: readOnly = false;
098: } catch (IOException e) {
099: readOnly = true;
100: } finally {
101: if (r != null) {
102: try {
103: r.close();
104: } catch (IOException e) {
105: // ignore
106: }
107: }
108: }
109: }
110:
111: /**
112: * @return if the file exists
113: */
114: public boolean exists() {
115: return exists;
116: }
117:
118: /**
119: * @return the tested file (may be different than provided)
120: */
121: public File getFile() {
122: return file;
123: }
124:
125: public boolean isDirectory() {
126: return file.isDirectory();
127: }
128:
129: /**
130: * @return if the filename is not empty, not a directory and contains only
131: * legal characters
132: */
133: public boolean isLegal() {
134: return legal;
135: }
136:
137: /**
138: * @return if the path is a legal filename (existing or not) and is not a
139: * directory and is not read only
140: */
141: public boolean isOk() {
142: return isLegal() && !isDirectory() && !isReadOnly();
143: }
144:
145: /**
146: * @return if the file is read only
147: */
148: public boolean isReadOnly() {
149: return readOnly;
150: }
151: }
|