]> _ Git - cubeextranet.git/commitdiff
(no commit message)
authorvincent@cubedesigners.com <vincent@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Tue, 16 Aug 2011 15:38:09 +0000 (15:38 +0000)
committervincent@cubedesigners.com <vincent@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Tue, 16 Aug 2011 15:38:09 +0000 (15:38 +0000)
fluidbook/tools/fwstk/src/cube/files/FileIO.java [new file with mode: 0644]
fluidbook/tools/fwstk/src/cube/util/Array.java [new file with mode: 0644]
fluidbook/tools/fwstk/src/cube/util/AsciiUtils.java [new file with mode: 0644]
fluidbook/tools/fwstk/src/cube/util/StringUtil.java [new file with mode: 0644]

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