2011年11月22日(Tue)
[JQuery] JQuery で全角文字半角文字を変換する。
$.fn.extend({ getHalfMap: function() { return { // 半濁音、濁音 "ガ":"ガ", "ギ":"ギ", "グ":"グ", "ゲ":"ゲ", "ゴ":"ゴ", "ザ":"ザ", "ジ":"ジ", "ズ":"ズ", "ゼ":"ゼ", "ゾ":"ゾ", "ダ":"ダ", "ヂ":"ヂ", "ヅ":"ヅ", "デ":"デ", "ド":"ド", "バ":"バ", "ビ":"ビ", "ブ":"ブ", "ベ":"ベ", "ボ":"ボ", "パ":"パ", "ピ":"ピ", "プ":"プ", "ペ":"ペ", "ポ":"ポ", // 無濁音 "ア":"ア", "イ":"イ", "ウ":"ウ", "エ":"エ", "オ":"オ", "カ":"カ", "キ":"キ", "ク":"ク", "ケ":"ケ", "コ":"コ", "サ":"サ", "シ":"シ", "ス":"ス", "セ":"セ", "ソ":"ソ", "タ":"タ", "チ":"チ", "ツ":"ツ", "テ":"テ", "ト":"ト", "ナ":"ナ", "ニ":"ニ", "ヌ":"ヌ", "ネ":"ネ", "ノ":"ノ", "ハ":"ハ", "ヒ":"ヒ", "フ":"フ", "ヘ":"ヘ", "ホ":"ホ", "マ":"マ", "ミ":"ミ", "ム":"ム", "メ":"メ", "モ":"モ", "ラ":"ラ", "リ":"リ", "ル":"ル", "レ":"レ", "ロ":"ロ", "ワ":"ワ", "ヲ":"ヲ", "ン":"ン" }; }, getAsciiMap: function() { var map = []; for (var i = 0; i < 127; i++) { var k = String.fromCharCode(i + 65248); var v = String.fromCharCode(i); map[k] = v; } return map; }, halfValue: function() { var val = $(this).val(); // カタカナ var map = $(this).getHalfMap(); for (var k in map) { var v = map[k]; var r = new RegExp(k, "g"); val = val.replace(r, v); } // ASCII val = val.replace(/([\uFF01-\uFF5E])/g, function($0){ alert(1); return String.fromCharCode($0.charCodeAt(0) - 65248); }); return val; }, fullValue: function() { var val = $(this).val(); // カタカナ var map = $(this).getHalfMap(); for (var k in map) { var v = map[k]; var r = new RegExp(v, "g"); val = val.replace(r, k); } // ASCII val = val.replace(/([\u0010-\u007e])/g, function($0){ return String.fromCharCode($0.charCodeAt(0) + 65248); }); return val; } });
コメント
トラックバック - http://d.hatena.ne.jp/alice_asahina/20111122
2011年11月21日(Mon)
JQuery で領域の開閉を行う。
JQuery |
/* * JQuery Accordion (Fixed Size) * * ex : 高 -> $('#accordion3').accordionH('100px', '200px', 'slow'); * 幅 -> $('#accordion3').accordionW('100px', '200px', 'slow'); * 両 -> $('#accordion3').accordion('100px', '200px', '100px', '200px', 'slow'); */ (function($){ $.fn.accordion = function(minH, maxH, minW, maxW, speed) { var overflow = $(this).css("overflow"); if (overflow == "hidden") { $(this).animate({ width : maxW, height : maxH }, speed, function() { $(this).css("overflow", "visible") }); } else { $(this).css("overflow", "hidden"); $(this).animate({ width : minW, height : minH }, speed); } } $.fn.accordionH = function(min, max, speed) { $(this).accordion(min, max, "100%", "100%", speed); } $.fn.accordionW = function(min, max, speed) { $(this).accordion("100%", "100%", min, max, speed); } })($);
トラックバック - http://d.hatena.ne.jp/alice_asahina/20111121
2011年11月20日(Sun)
Jquery で SSI #Include の代わりを
Jquery |
/* * JQuery Include * * ex : $("#footer").include("inc/footer.inc"); * */ (function($){ $.fn.include = function(url) { var rls = $.get(url, { targetId : this.get(0).id }, function(data){ var url = this.url; var src = url.split("?")[1]; var exp = "#" + src.replace(/.*targetId=([^&]+).*/ig, "$1"); $(exp).html(data); }); } })($);
トラックバック - http://d.hatena.ne.jp/alice_asahina/20111120
2011年11月16日(Wed)
カタカナか判別
java7 |
package com.sakuraweb.wizard.util; import java.lang.Character.UnicodeScript; public class KanaUtil { public static void main(String[] args) { System.out.println(UnicodeScript.KATAKANA == UnicodeScript.of(Character.codePointAt("カ", 0))); // true System.out.println(UnicodeScript.KATAKANA == UnicodeScript.of(Character.codePointAt("安", 0))); // false } }
となる。
トラックバック - http://d.hatena.ne.jp/alice_asahina/20111116
2011年11月15日(Tue)
ファイル入出力
java7 |
書込1
import java.io.File; import java.io.IOException; import java.nio.file.Files; public class JavaxNioTest { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { File f = new File("C:\\NIO.txt"); Files.write(f.toPath(), "出力テスト".getBytes()); } }
書込2
import java.io.BufferedWriter; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; public class JavaxNioTest2 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { File f = new File("C:\\NIO.txt"); Path p = f.toPath(); Charset c = StandardCharsets.UTF_8; try (// BufferedWriter bw = Files.newBufferedWriter(p, c); PrintWriter out = new PrintWriter(bw)) { out.println("Test"); } } }
読込み(全行)
[バグ]マルチバイト文字が含まれるファイルは読み込めない。
import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; public class JavaxNioTest3 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { File f = new File("C:/NIO.txt"); Path p = f.toPath(); Charset c = StandardCharsets.UTF_8; List<String> allLines = Files.readAllLines(p, c); System.out.println(allLines); } }
コピー
必要に応じて コピーオプションを設定。
デフォルトではコピー先にファイルが存在した場合エラーになる。
import java.io.File; import java.io.IOException; import java.nio.file.Files; public class JavaxNioTest4 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { File f1 = new File("C:/NIO.txt"); File f2 = new File("C:/NIO2.txt"); // ファイルが存在した場合、例外となるので事前に削除する。 // 削除時に例外が出る場合、exists などの条件付で削除する。 f2.delete(); Files.copy(f1.toPath(), f2.toPath()); } }
移動
import java.io.File; import java.io.IOException; import java.nio.file.Files; public class JavaxNioTest5 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { File f1 = new File("C:/NIO.txt"); File f2 = new File("C:/NIO2.txt"); f2.delete(); Files.move(f1.toPath(), f2.toPath()); } }
ファイルユーティリティを作ってみた。
package com.sakuraweb.wizard.io; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.Charset; import java.nio.file.CopyOption; import java.nio.file.Files; import java.nio.file.OpenOption; import java.nio.file.attribute.FileAttribute; import java.util.Collection; import java.util.List; public final class FileUtil { FileUtil() { /* nop */ } /** * ファイル複製。 * * @param src * 入力 * @param dest * 出力 * @param opts * オプション * @throws IOException * 入出力例外 */ public static void copy(File src, File dest, CopyOption... opts) throws IOException { /* ファイルコピー */ Files.copy(src.toPath(), dest.toPath(), opts); } /** * ファイル移動。 * * @param src * 入力 * @param dest * 出力 * @param opts * オプション * @throws IOException * 入出力例外 */ public static void move(File src, File dest, CopyOption... opts) throws IOException { /* ファイル移動 */ Files.move(src.toPath(), dest.toPath(), opts); } /** * ファイル出力。 * * @param src * 入力 * @param dest * 出力 * @param opts * オプション * @throws IOException * 入出力例外 */ public static void copy(InputStream src, File dest, CopyOption... opts) throws IOException { /* * コピー系では一番つかわれない。 */ Files.copy(src, dest.toPath(), opts); } /** * ストリーム出力。 * * @param src * 入力 * @param dest * 出力 * @throws IOException * 入出力例外 */ public static void copy(File src, OutputStream dest) throws IOException { /* JavaEE ダウンロード時に多様 */ Files.copy(src.toPath(), dest); } /** * 書込み。 * * @param dest * 出力 * @param output * データ * @param opts * オプション * @throws IOException * 入出力例外 */ public static void write(File dest, byte[] output, OpenOption... opts) throws IOException { Files.write(dest.toPath(), output, opts); } /** * 書込み(複数行) * * @param dest * 出力 * @param output * データ * @param cs * 文字コード * @param opts * オプション * @throws IOException * 入出力例外 */ public static void write(File dest, Collection<String> output, Charset cs, OpenOption... opts) throws IOException { Files.write(dest.toPath(), output, cs, opts); } /** * 全行読込み。 * * @param src * 入力 * @param cs * 文字コード * @return 内容 * @throws IOException * 入出力例外 */ public static List<String> readAll(File src, Charset cs) throws IOException { return Files.readAllLines(src.toPath(), cs); } }
トラックバック - http://d.hatena.ne.jp/alice_asahina/20111115