001: /*
002: * ====================================================================
003: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
004: *
005: * This software is licensed as described in the file COPYING, which
006: * you should have received as part of this distribution. The terms
007: * are also available at http://svnkit.com/license.html
008: * If newer versions of this license are posted there, you may use a
009: * newer version instead, at your option.
010: * ====================================================================
011: */
012: package org.tmatesoft.svn.core.internal.wc;
013:
014: import java.io.File;
015:
016: /**
017: * @version 1.1.1
018: * @author TMate Software Ltd.
019: */
020: public class SVNFileListUtil {
021:
022: /**
023: * This method is a replacement for file.list(), which composes decomposed file names (e.g. umlauts in file names on the Mac).
024: */
025: public static String[] list(File directory) {
026: if (!SVNFileUtil.isOSX) {
027: return directory.list();
028: }
029: final String[] fileNames = directory.list();
030: if (fileNames == null) {
031: return null;
032: }
033:
034: final String[] composedFileNames = new String[fileNames.length];
035: for (int i = 0; i < composedFileNames.length; i++) {
036: composedFileNames[i] = compose(fileNames[i]);
037: }
038: return composedFileNames;
039: }
040:
041: /**
042: * This method is a replacement for file.listFiles(), which composes decomposed file names (e.g. umlauts in file names on the Mac).
043: */
044: public static File[] listFiles(File directory) {
045: if (SVNFileUtil.isOSX) {
046: final String[] fileNames = list(directory);
047: if (fileNames == null) {
048: return null;
049: }
050:
051: File[] files = new File[fileNames.length];
052: for (int i = 0; i < files.length; i++) {
053: files[i] = new File(directory.getPath(), fileNames[i]);
054: }
055: return files;
056: } else if (SVNFileUtil.isOpenVMS) {
057: File[] files = directory.listFiles();
058: if (files == null || files.length == 0) {
059: return files;
060: }
061: File[] processed = new File[files.length];
062: for (int i = 0; i < files.length; i++) {
063: File file = files[i];
064: String name = file.getName();
065: if (file.isFile() && name.endsWith(".")) {
066: // chances there is a file without extension and '.' added by openVMS.
067: name = name.substring(0, name.lastIndexOf('.'));
068: file = new File(directory, name);
069: if (file.exists() && file.isFile()) {
070: processed[i] = file;
071: continue;
072: }
073: }
074: processed[i] = file;
075: }
076: return processed;
077: }
078: return directory.listFiles();
079: }
080:
081: private static String compose(String decomposedString) {
082: if (decomposedString == null) {
083: return null;
084: }
085:
086: StringBuffer buffer = null;
087: for (int i = 1, length = decomposedString.length(); i < length; i++) {
088: final char chr = decomposedString.charAt(i);
089: if (chr == '\u0300') { // grave `
090: buffer = compose(
091: i,
092: "AaEeIiOoUu",
093: "\u00C0\u00E0\u00C8\u00E8\u00CC\u00EC\u00D2\u00F2\u00D9\u00F9",
094: decomposedString, buffer);
095: } else if (chr == '\u0301') { // acute '
096: buffer = compose(
097: i,
098: "AaEeIiOoUuYy",
099: "\u00C1\u00E1\u00C9\u00E9\u00CD\u00ED\u00D3\u00F3\u00DA\u00FA\u00DD\u00FD",
100: decomposedString, buffer);
101: } else if (chr == '\u0302') { // circumflex ^
102: buffer = compose(
103: i,
104: "AaEeIiOoUuYy",
105: "\u00C2\u00E2\u00CA\u00EA\u00CE\u00EE\u00D4\u00F4\u00DB\u00FB\u0176\u0177",
106: decomposedString, buffer);
107: } else if (chr == '\u0303') { // tilde ~
108: buffer = compose(
109: i,
110: "AaNnOoUu",
111: "\u00C3\u00E3\u00D1\u00F1\u00D5\u00F5\u0168\u0169",
112: decomposedString, buffer);
113: } else if (chr == '\u0308') { // umlaut/dieresis (two dots above)
114: buffer = compose(
115: i,
116: "AaEeIiOoUuYy",
117: "\u00C4\u00E4\u00CB\u00EB\u00CF\u00EF\u00D6\u00F6\u00DC\u00FC\u0178\u00FF",
118: decomposedString, buffer);
119: } else if (chr == '\u030A') { // ring above (as in Angstrom)
120: buffer = compose(i, "Aa", "\u00C5\u00E5",
121: decomposedString, buffer);
122: } else if (chr == '\u0327') { // cedilla ,
123: buffer = compose(i, "Cc", "\u00C7\u00E7",
124: decomposedString, buffer);
125: } else if (buffer != null) {
126: buffer.append(chr);
127: }
128: }
129:
130: if (buffer == null) {
131: return decomposedString;
132: }
133:
134: return buffer.toString();
135: }
136:
137: // Utils ==================================================================
138:
139: private static StringBuffer compose(int i, String decomposedChars,
140: String composedChars, String decomposedString,
141: StringBuffer buffer) {
142: final char previousChar = decomposedString.charAt(i - 1);
143: final int decomposedIndex = decomposedChars
144: .indexOf(previousChar);
145: if (decomposedIndex >= 0) {
146: if (buffer == null) {
147: buffer = new StringBuffer(decomposedString.length() + 2);
148: buffer.append(decomposedString.substring(0, i - 1));
149: } else {
150: buffer.delete(buffer.length() - 1, buffer.length());
151: }
152:
153: buffer.append(composedChars.charAt(decomposedIndex));
154: } else {
155: if (buffer == null) {
156: buffer = new StringBuffer(decomposedString.length() + 2);
157: buffer.append(decomposedString.substring(0, i));
158: }
159: }
160: return buffer;
161: }
162: }
|