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:
035: import de.intarsys.tools.string.StringTools;
036:
037: /**
038: * A utility class to simplify the task of loading files and / or directories.
039: *
040: */
041: abstract public class Loader {
042: public static final String PATH_SEPARATOR = "/"; //$NON-NLS-1$
043:
044: public static final String DEFAULT_LANGUAGE = "en"; //$NON-NLS-1$
045:
046: protected static final String PROP_USERLANGUAGE = "user.language"; //$NON-NLS-1$
047:
048: public Loader() {
049: }
050:
051: abstract protected boolean basicLoadFile(File file,
052: boolean readOnly, String path) throws IOException;
053:
054: public boolean load(File file, boolean readOnly, boolean recursive)
055: throws IOException {
056: if (file == null || !file.exists()) {
057: return false;
058: }
059: return basicLoad(file, readOnly, recursive, StringTools.EMPTY);
060: }
061:
062: protected boolean basicLoad(File file, boolean readOnly,
063: boolean recursive, String path) throws IOException {
064: if (file.isDirectory()) {
065: return basicLoadDirectory(file, readOnly, recursive, path);
066: } else {
067: return basicLoadFile(file, readOnly, path);
068: }
069: }
070:
071: protected boolean basicLoadDirectory(File file, boolean readOnly,
072: boolean recursive, String path) throws IOException {
073: File[] files = file.listFiles();
074: for (int i = 0; i < files.length; i++) {
075: File childFile = files[i];
076: if (recursive) {
077: String newPath = path;
078: if (childFile.isDirectory()) {
079: newPath = path + childFile.getName()
080: + PATH_SEPARATOR;
081: }
082: basicLoad(childFile, readOnly, recursive, newPath);
083: } else {
084: if (childFile.isFile()) {
085: basicLoadFile(childFile, readOnly, path);
086: }
087: }
088: }
089: return true;
090: }
091:
092: public boolean load(File parent, String filename, boolean readOnly,
093: boolean recursive) throws IOException {
094: if (filename == null) {
095: return false;
096: }
097: File file = new File(filename);
098: if (!file.isAbsolute()) {
099: file = new File(parent, filename);
100: }
101: return load(file, readOnly, recursive);
102: }
103:
104: public boolean loadNLS(File file, boolean readOnly,
105: boolean recursive) throws IOException {
106: if (file == null || !file.exists()) {
107: return false;
108: }
109: if (file.isDirectory()) {
110: String language = System.getProperty(PROP_USERLANGUAGE);
111: File languageDir = new File(file, language);
112: if (load(languageDir, readOnly, recursive)) {
113: return true;
114: }
115: File defaultDir = new File(file, DEFAULT_LANGUAGE);
116: return load(defaultDir, readOnly, recursive);
117: } else {
118: File parent = file.getParentFile();
119: if (parent == null) {
120: return false;
121: }
122: String basename = FileTools.getBaseName(file);
123: String extension = FileTools.getExtension(file);
124: String language = System.getProperty(PROP_USERLANGUAGE);
125: File languageFile = new File(parent, basename
126: + "_" + language //$NON-NLS-1$
127: + "." + extension); //$NON-NLS-1$
128: if (file.exists()
129: && basicLoadFile(languageFile, readOnly,
130: StringTools.EMPTY)) {
131: return true;
132: }
133: return basicLoadFile(file, readOnly, StringTools.EMPTY);
134: }
135: }
136:
137: public boolean loadNLS(File parent, String filename,
138: boolean readOnly, boolean recursive) throws IOException {
139: if (filename == null) {
140: return false;
141: }
142: File file = new File(filename);
143: if (!file.isAbsolute()) {
144: file = new File(parent, filename);
145: }
146: return loadNLS(file, readOnly, recursive);
147: }
148:
149: }
|