From 9f3a482a83b94f11ad522d97b4f6d26f60f46905 Mon Sep 17 00:00:00 2001 From: "vincent@cubedesigners.com" Date: Tue, 16 Aug 2011 15:38:09 +0000 Subject: [PATCH] --- .../tools/fwstk/src/cube/files/FileIO.java | 136 ++++++++++++++++++ .../tools/fwstk/src/cube/util/Array.java | 34 +++++ .../tools/fwstk/src/cube/util/AsciiUtils.java | 47 ++++++ .../tools/fwstk/src/cube/util/StringUtil.java | 88 ++++++++++++ 4 files changed, 305 insertions(+) create mode 100644 fluidbook/tools/fwstk/src/cube/files/FileIO.java create mode 100644 fluidbook/tools/fwstk/src/cube/util/Array.java create mode 100644 fluidbook/tools/fwstk/src/cube/util/AsciiUtils.java create mode 100644 fluidbook/tools/fwstk/src/cube/util/StringUtil.java diff --git a/fluidbook/tools/fwstk/src/cube/files/FileIO.java b/fluidbook/tools/fwstk/src/cube/files/FileIO.java new file mode 100644 index 000000000..63d4f4c96 --- /dev/null +++ b/fluidbook/tools/fwstk/src/cube/files/FileIO.java @@ -0,0 +1,136 @@ +package cube.files; + +import java.beans.XMLDecoder; +import java.beans.XMLEncoder; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +// Classe utilitaire permettant de simplifier la manipulation +// des fichiers notamment en lecture et écriture +public class FileIO extends File { + /** + * + */ + private static final long serialVersionUID = -2704136793943547396L; + protected String mode; + protected Boolean serialize = false; + protected Boolean serializex = false; + protected Boolean gzip = false; + protected FileInputStream fis; + protected BufferedInputStream bis; + public DataInputStream input; + public ObjectInputStream object_input; + public XMLDecoder xml_input; + public GZIPInputStream zinput; + protected FileOutputStream fos; + protected BufferedOutputStream bos; + public DataOutputStream output; + public ObjectOutputStream object_output; + public XMLEncoder xml_output; + public GZIPOutputStream zoutput; + + public FileIO(String path) { + super(path); + } + + public FileIO(File file) { + super(file.getAbsolutePath()); + } + + public void open(String openMode) throws IOException { + this.mode = openMode.substring(0, 1); + this.serialize = (openMode.length() > 1 && openMode.substring(1, 2) + .compareTo("o") == 0); + this.serializex = (openMode.length() > 1 && openMode.substring(1, 2) + .compareTo("x") == 0); + this.gzip = (openMode.length() > 1 && openMode.substring(1, 2) + .compareTo("z") == 0); + + if (this.mode.compareTo("r") == 0) { + + if (!this.canRead()) { + throw new IOException("The file " + this.toURI() + + " is not readable"); + } + fis = new FileInputStream(this); + if (serialize) { + object_input = new ObjectInputStream(fis); + } else if (serializex) { + xml_input = new XMLDecoder(fis); + } else if (gzip) { + bis = new BufferedInputStream(fis); + zinput = new GZIPInputStream(bis); + } else { + bis = new BufferedInputStream(fis); + input = new DataInputStream(bis); + } + } else if (this.mode.compareTo("w") == 0 + || this.mode.compareTo("a") == 0) { + if (!this.exists()) { + File parent = this.getAbsoluteFile().getParentFile(); + if (!parent.exists()) { + parent.mkdirs(); + } + this.createNewFile(); + } + if (!this.canWrite()) { + throw new IOException("The file " + this.toURI() + + " is not writable"); + } + fos = new FileOutputStream(this, (this.mode.compareTo("a") == 0)); + + + if (this.serialize == true) { + object_output = new ObjectOutputStream(fos); + } else if (this.serializex == true) { + xml_output = new XMLEncoder(fos); + } else if (this.gzip) { + bos = new BufferedOutputStream(fos); + zoutput = new GZIPOutputStream(bos); + } else { + bos = new BufferedOutputStream(fos); + output = new DataOutputStream(bos); + } + } + } + + public void close() throws IOException { + if (mode.compareTo("r") == 0) { + if (serialize) { + object_input.close(); + } else if (serializex) { + xml_input.close(); + } else if (gzip) { + zinput.close(); + bis.close(); + } else { + input.close(); + bis.close(); + } + fis.close(); + } else if (mode.compareTo("w") == 0 || mode.compareTo("a") == 0) { + if (serialize) { + object_output.close(); + } else if (serializex) { + xml_output.close(); + } else if (gzip) { + zoutput.close(); + bos.close(); + } else { + output.close(); + bos.close(); + } + fos.close(); + } + } +} diff --git a/fluidbook/tools/fwstk/src/cube/util/Array.java b/fluidbook/tools/fwstk/src/cube/util/Array.java new file mode 100644 index 000000000..86eeffb29 --- /dev/null +++ b/fluidbook/tools/fwstk/src/cube/util/Array.java @@ -0,0 +1,34 @@ +package cube.util; + +import java.util.TreeMap; + +public class Array { + public static Integer[] parseRange(String str) { + TreeMap res = new TreeMap(); + String[] ranges = str.split("[;,]{1}"); + int e0; + int e1; + for (int i = 0; i < ranges.length; i++) { + String[] e = ranges[i].split("-"); + e0 = Integer.parseInt(e[0]); + if (e.length > 1) { + e1 = Integer.parseInt(e[1]); + } else { + e1 = 0; + } + if (e.length == 1) { + res.put(e0, true); + } else if (e0 > e1) { + for (int j = e1; j <= e0; j++) { + res.put(j, true); + } + } else if (e0 < e1) { + for (int j = e0; j <= e1; j++) { + res.put(j, true); + } + } + } + return res.keySet().toArray(new Integer[0]); + + } +} diff --git a/fluidbook/tools/fwstk/src/cube/util/AsciiUtils.java b/fluidbook/tools/fwstk/src/cube/util/AsciiUtils.java new file mode 100644 index 000000000..84bf8b75d --- /dev/null +++ b/fluidbook/tools/fwstk/src/cube/util/AsciiUtils.java @@ -0,0 +1,47 @@ +package cube.util; + +public class AsciiUtils { + private static final String PLAIN_ASCII = + "AaEeIiOoUu" // grave + + "AaEeIiOoUuYy" // acute + + "AaEeIiOoUuYy" // circumflex + + "AaOoNn" // tilde + + "AaEeIiOoUuYy" // umlaut + + "Aa" // ring + + "Cc" // cedilla + + "OoUu" // double acute + ; + + private static final String UNICODE = + "\u00C0\u00E0\u00C8\u00E8\u00CC\u00EC\u00D2\u00F2\u00D9\u00F9" + + "\u00C1\u00E1\u00C9\u00E9\u00CD\u00ED\u00D3\u00F3\u00DA\u00FA\u00DD\u00FD" + + "\u00C2\u00E2\u00CA\u00EA\u00CE\u00EE\u00D4\u00F4\u00DB\u00FB\u0176\u0177" + + "\u00C3\u00E3\u00D5\u00F5\u00D1\u00F1" + + "\u00C4\u00E4\u00CB\u00EB\u00CF\u00EF\u00D6\u00F6\u00DC\u00FC\u0178\u00FF" + + "\u00C5\u00E5" + + "\u00C7\u00E7" + + "\u0150\u0151\u0170\u0171" + ; + + // private constructor, can't be instantiated! + private AsciiUtils() { } + + // remove accentued from a string and replace with ascii equivalent + public static String convertNonAscii(String s) { + if (s == null) return null; + StringBuilder sb = new StringBuilder(); + int n = s.length(); + for (int i = 0; i < n; i++) { + char c = s.charAt(i); + int pos = UNICODE.indexOf(c); + if (pos > -1){ + sb.append(PLAIN_ASCII.charAt(pos)); + } + else { + sb.append(c); + } + } + return sb.toString(); + } + +} diff --git a/fluidbook/tools/fwstk/src/cube/util/StringUtil.java b/fluidbook/tools/fwstk/src/cube/util/StringUtil.java new file mode 100644 index 000000000..ebe9528fb --- /dev/null +++ b/fluidbook/tools/fwstk/src/cube/util/StringUtil.java @@ -0,0 +1,88 @@ +package cube.util; + +public class StringUtil { + + public static String removeAccents(String in) { + return AsciiUtils.convertNonAscii(in); + } + + public static String condenseWhite(String in) { + return in.replaceAll("\\p{Space}++", " "); + } + + public static String removePoints(String in) { + return in.replaceAll( + "[\\x21-\\x2f\\x3a-\\x3f\\x5b-\\x5f\\x7b-\\xa0\\xaa-\\xbf]", + " "); + } + + public static String removeControl(String in) { + return in.replaceAll("\\p{Cntrl}", " "); + } + + public static String removeTags(String in) { + in = in.replaceAll("<.+>", " "); + return condenseWhite(in); + } + + public static String trim(String str, String[] chars) { + + Boolean suite = false; + for (String c : chars) { + if (str.contains(c)) { + suite = true; + break; + } + } + if (!suite) { + return str; + } + + str = ltrim(str, chars); + str = rtrim(str, chars); + + return str; + } + + public static String rtrim(String str, String[] charsToTrim) { + int length = str.length() - 1; + + rightloop: for (; length >= 0; length--) { + for (String c : charsToTrim) { + if (str.indexOf(c) == length) { + continue rightloop; + } + } + break; + } + + str = str.substring(0, length + 1); + return str; + } + + public static String[] splitStr(String str) { + + byte[] chars = str.getBytes(); + String[] res = new String[chars.length]; + for (int i = 0; i < chars.length; i++) { + res[i] = String.valueOf((char) chars[i]); + } + + return res; + } + + public static String ltrim(String str, String[] charsToTrim) { + int startIndex = 0; + leftloop: for (startIndex = 0; startIndex <= str.length(); startIndex++) { + for (String c : charsToTrim) { + if (str.indexOf(c) == startIndex) { + continue leftloop; + } + } + break; + } + str = str.substring(startIndex); + return str; + + } +} -- 2.39.5