001: /*
002: * File : $Source: /usr/local/cvs/alkacon/com.alkacon.opencms.formgenerator/src/com/alkacon/opencms/formgenerator/database/CmsFileUtil.java,v $
003: * Date : $Date: 2007-12-21 14:34:01 $
004: * Version: $Revision: 1.1 $
005: *
006: * This file is part of the Alkacon OpenCms Add-On Module Package
007: *
008: * Copyright (c) 2007 Alkacon Software GmbH (http://www.alkacon.com)
009: *
010: * The Alkacon OpenCms Add-On Module Package is free software:
011: * you can redistribute it and/or modify
012: * it under the terms of the GNU General Public License as published by
013: * the Free Software Foundation, either version 3 of the License, or
014: * (at your option) any later version.
015: *
016: * The Alkacon OpenCms Add-On Module Package is distributed
017: * in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with the Alkacon OpenCms Add-On Module Package.
024: * If not, see http://www.gnu.org/licenses/.
025: *
026: * For further information about Alkacon Software GmbH, please see the
027: * company website: http://www.alkacon.com.
028: *
029: * For further information about OpenCms, please see the
030: * project website: http://www.opencms.org.
031: */
032: package com.alkacon.opencms.formgenerator.database;
033:
034: import org.opencms.util.CmsRfsException;
035:
036: import java.io.File;
037: import java.io.IOException;
038:
039: /**
040: * <code>{@link java.io.File}</code> handling utilities with extended verification
041: * for files and folders in the RFS.
042: * <p>
043: *
044: * @author Achim Westermann
045: *
046: * @version $Revision: 1.1 $
047: *
048: * @since 7.0.4
049: *
050: */
051: public final class CmsFileUtil {
052:
053: /** Constant for read access to files. */
054: public static final int MODE_READ = 1;
055:
056: /** Constant for write access to files. */
057: public static final int MODE_WRITE = 3;
058:
059: /**
060: * Utility class constructor: No instances is needed.
061: * <p>
062: *
063: */
064: private CmsFileUtil() {
065:
066: super ();
067: }
068:
069: /**
070: * Asserts that the given file is not null, points to a valid file (no folder) and is accessible
071: * in the given mode.
072: * <p>
073: *
074: * @param file the file to assert access to.
075: *
076: * @param mode {@link #MODE_READ}, {@link #MODE_WRITE} or
077: * <code>{@link #MODE_READ} | {@link #MODE_WRITE}</code> (binary OR).
078: *
079: * @param create if true this call will create the file if non-existant.
080: *
081: * @throws CmsRfsException if the file does not exist, cannot be read, is no file (but a
082: * folder), or cannot be accessed in the given mode.
083: */
084: public static void assertFile(File file, int mode, boolean create)
085: throws CmsRfsException {
086:
087: if (file == null) {
088: throw new CmsRfsException(Messages.get().container(
089: Messages.ERR_FILE_ARG_NULL_0));
090: }
091: if (!file.exists()) {
092: if (create) {
093: try {
094: file.createNewFile();
095: } catch (IOException ioex) {
096: throw new CmsRfsException(Messages.get().container(
097: Messages.ERR_FILE_ARG_CREATE_1,
098: file.getAbsolutePath()), ioex);
099: }
100: } else {
101: throw new CmsRfsException(Messages.get().container(
102: Messages.ERR_FILE_ARG_EXISTS_1,
103: file.getAbsolutePath()));
104: }
105:
106: }
107: if (file.isDirectory()) {
108: throw new CmsRfsException(Messages.get().container(
109: Messages.ERR_FILE_ARG_IS_FOLDER_1,
110: new Object[] { file.getAbsolutePath() }));
111: } else if (!file.isFile()) {
112: throw new CmsRfsException(Messages.get().container(
113: Messages.ERR_FILE_ARG_NOT_FOUND_1,
114: new Object[] { file.getAbsolutePath() }));
115:
116: }
117:
118: if ((mode & MODE_READ) != 0) {
119: if (!file.canRead()) {
120: throw new CmsRfsException(Messages.get().container(
121: Messages.ERR_FILE_ARG_NOT_READ_1,
122: new Object[] { String.valueOf(file
123: .getAbsolutePath()) }));
124:
125: }
126: }
127: if ((mode & MODE_WRITE) != 0) {
128: if (!file.canWrite()) {
129:
130: throw new CmsRfsException(Messages.get().container(
131: Messages.ERR_FILE_ARG_NOT_WRITE_1,
132: new Object[] { String.valueOf(file
133: .getAbsolutePath()) }));
134: }
135: }
136: }
137:
138: /**
139: * Asserts that the given file is not null, points to a valid folder (no file) and is accessible
140: * in the given mode.
141: * <p>
142: *
143: * @param file the file to assert access to.
144: *
145: * @param create if true this call will create the file if non-existant.
146: *
147: * @param mode {@link #MODE_READ}, {@link #MODE_WRITE} or
148: * <code>{@link #MODE_READ} | {@link #MODE_WRITE}</code> (binary OR).
149: *
150: * @throws CmsRfsException if the file does not exist, cannot be read, is no file (but a
151: * folder), or cannot be accessed in the given mode.
152: */
153: public static void assertFolder(File file, int mode, boolean create)
154: throws CmsRfsException {
155:
156: if (file == null) {
157: throw new CmsRfsException(Messages.get().container(
158: Messages.ERR_FILE_ARG_NULL_0));
159: }
160: if (!file.exists()) {
161: if (create) {
162: try {
163: if (!file.mkdirs()) {
164: throw new CmsRfsException(Messages.get()
165: .container(
166: Messages.ERR_FILE_ARG_CREATE_1,
167: file.getAbsolutePath()));
168: }
169: } catch (SecurityException secex) {
170: throw new CmsRfsException(Messages.get().container(
171: Messages.ERR_FILE_ARG_CREATE_1,
172: file.getAbsolutePath()), secex);
173: }
174: } else {
175: throw new CmsRfsException(Messages.get().container(
176: Messages.ERR_FILE_ARG_EXISTS_1,
177: file.getAbsolutePath()));
178: }
179:
180: }
181: if (file.isFile()) {
182: throw new CmsRfsException(Messages.get().container(
183: Messages.ERR_FILE_ARG_IS_FILE_1,
184: new Object[] { file.getAbsolutePath() }));
185: }
186: if ((mode & MODE_READ) != 0) {
187: if (!file.canRead()) {
188: throw new CmsRfsException(Messages.get().container(
189: Messages.ERR_FILE_ARG_NOT_READ_1,
190: new Object[] { String.valueOf(file
191: .getAbsolutePath()) }));
192:
193: }
194: }
195: if ((mode & MODE_WRITE) != 0) {
196: if (!file.canWrite()) {
197:
198: throw new CmsRfsException(Messages.get().container(
199: Messages.ERR_FILE_ARG_NOT_WRITE_1,
200: new Object[] { String.valueOf(file
201: .getAbsolutePath()) }));
202: }
203: }
204: }
205: }
|