OSDN Git Service

Adds validation of inputs
authornangxiang <nangxiang@users.sourceforge.jp>
Sat, 17 Sep 2011 15:17:25 +0000 (00:17 +0900)
committernangxiang <nangxiang@users.sourceforge.jp>
Sat, 17 Sep 2011 15:17:25 +0000 (00:17 +0900)
hanMorph/app/controllers/hanmorph_controller.rb
hanMorph/app/views/hanmorph/_daxu.html.erb
hanMorph/app/views/hanmorph/_searchmenu.html.erb
hanMorph/app/views/shuowen/index.html.erb

index 5955ffd..b479c39 100644 (file)
@@ -5,19 +5,35 @@ class HanmorphController < ApplicationController
   layout "main"
   caches_page :xiesheng, :rime_groups
   caches_action :xiesheng, :rime_groups
-
+  
   def index
-    c = params[:char]
-    @extext = params[:extext]
-    if c then
-      (@ucs, @char) = process_char(c)
-      process_unihan
-      process_sbgy
-      process_dianzhu
-      process_daxu
+    if request.post? then
+      @errors = []
+      (@ucs, @char, @extext) = check_targets
+#      raise Exception
+      if @errors.empty? then
+        begin
+          main_procedure_at_home
+        rescue StandardError => bang
+          @message = bang.message
+        end
+      end
+    else
+      @char = params[:char]
+      main_procedure_at_home
     end
+  end
+  def main_procedure_at_home
+    process_unihan
+    process_sbgy
+    process_dianzhu
+    process_daxu
     if @extext then
-      search_daxu
+      begin
+        search_daxu
+      rescue TooManyCandidatesError => bang
+        @errors << bang
+      end
     end
   end
   def rime_groups
@@ -62,6 +78,23 @@ class HanmorphController < ApplicationController
   end
   
   private
+  # Validation Input Parameters
+  def check_targets
+    c = params[:char]
+    extext = params[:extext]
+    # TODO: 不正な文字のチェック
+    if extext and extext.size == 0 then
+      @errors << "検索する説解が入力されていません"
+    elsif ! extext and c and c.size == 0 then
+      @errors << "検索する文字が入力されていません"
+    elsif c =~/[0-9A-Fa-f]+/ and not c =~ /[0-9A-Fa-f]{4,5}/ then
+      @errors << "#{c}: 正しいコードポイントではありません"
+    else
+      (ucs, char) = process_char(c)
+      return [ucs, char, extext]
+    end
+  end
+
   # Main Procesures
   def process_unihan
     cp = ucs2codepoint(@ucs)
@@ -112,6 +145,10 @@ class HanmorphController < ApplicationController
     @pattern = sprintf("%%%s%%", @extext)
     wordheads = DxWordhead.find_by_sql(["select * from dx_wordheads " +
                                        "where exp like ?", @pattern])
+    searched = wordheads.size
+    if searched > 50 then
+      raise TooManyCandidatesError, "#{searched}件、見付かりました。検索条件を絞り込んでください。"
+    end
     wordheads.each do |word|
       wordid = word.wordid
       rad = DxRadical.find(:first, :conditions => ["id = ?", word.radical_id])
@@ -149,3 +186,6 @@ class HanmorphController < ApplicationController
     return exp.sub(/#{pattern}/u, replaced)
   end
 end
+
+class TooManyCandidatesError < RuntimeError
+end
index 7b4cb44..551cf7b 100644 (file)
@@ -1,4 +1,4 @@
-  <% if @char and @char.size > 0 and @dx_words then %>
+  <% if @char and @char.size > 0 and @dx_words and @dx_words.size > 0 then %>
   <h2>
     説文解字(大徐本)
     <%= link_to_function "▲▼", "new Effect.toggle('sw', 'blind')" %>
index 8e20178..a69119a 100644 (file)
       <% end %>
       </li>
     </ul>
+
+    <% if @errors && !@errors.empty? then %>
+    <ul style="color: red">
+      <% if @message then %>
+        <li><%= @message %></li>
+      <% end %>
+      <% for error in @errors %>
+        <li><%= error %></li>
+      <% end %>
+    </ul>
+    <% end %>
   </div>
index d535ac9..992de34 100644 (file)
@@ -6,6 +6,7 @@
     <li><%= link_to("大徐本", :controller => "daxu", :action => "index") %></li>
     <li><span>段注</span></li>
     <li><%= link_to("廣韻", :controller => "sbgy", :action => "index") %></li>
+    <li><%= link_to("漢語大字典", :controller => "unihan", :action => "hdz") %></li>
     <li><%= link_to("十七部表", :controller => "hanmorph", :action => "rime_groups") %></li>
     <li><%= link_to("諧聲表", :controller => "hanmorph", :action => "xiesheng") %></li>
     <li><%= link_to("説明", :controller => "hanmorph", :action => "explanation") %></li>