#!ruby -Ku
# -*- coding: utf-8 -*-
#text = IO.read("ホームページ.txt")
# ----- 括弧のクラス ---------------------------------------------------
# Stapleクラスは自分の出現位置と対応する括弧のタイプを知っている
# ----------------------------------------------------------------------
class Staple
attr_reader :type
def initialize( type )
@type = type # 自身の括弧のタイプ
end
# 対応する括弧のタイプ
def pare
case @type
when '{'
str = '}'
when '}'
str = '{'
end
return str
end
end
class JavaDocParse
def initialize(text)
@text = text
@begin_count = 0
@in_process = false
@is_end = false
end
def parse()
text_array = Array.new
@text.reverse.each do |c|
countup_begin(c)
text_array << "#{c}"
break if @is_end
end
ret_text = ""
text_array.reverse.each do |line|
ret_text << line
end
ret_text
end
def countup_begin(context)
# /** から */までのJavaDocを抜き出す
# 前提条件:テキスト反転(下から上の走査)
# */ で開始
@begin_count +=1 if /^.*\*\// =~ context
# /* で終了
@begin_count -=1 if /^.*\/\*/ =~ context
unless @in_process
@in_process = 0 < @begin_count
p "開始" if @in_process
else
if 0 == @begin_count
p "完了"
@is_end = true
end
end
end
end
START_PATTERN = /¥{/
END_PATTERN = /¥}/
class ServletParse
def initialize(text)
@text = text
@begin_count = 0
@in_process = false
@is_in_comment = false
@is_end = false
end
def parse()
ret_text = ""
@text.each do |c|
countup_begin(c)
ret_text << "#{c}"
break if @is_end
end
ret_text
end
def countup_begin(context)
# コメントアウト行【複数行】開始(/*)
if /^.*\/\*/ =~ context
@is_in_comment = true
end
# コメントアウト行【複数行】終了(*/)
if /^.*\*\// =~ context
@is_in_comment = false
end
# コメントアウト行(/* */)【複数行】は、無視する
unless @is_in_comment
# コメントアウト行(//)は、無視する
unless /^[\s]*\/\// =~ context
stack = Array.new() # スタックを配列だけで書いてみる
# 一文字ずつ読み込んで、括弧の開始であれば、スタックに追加。
# 括弧の終了であれば、スタックの最後と比較して一致していれば
# スタックから取り出し、不一致であれば、スタックに追加。
dq_cnt = 0
context.scan(/./).each do |char|
#ダブルクオート内の{}は無視する
dq_cnt += 1 if char.match( /"/ )
next if (dq_cnt % 2 == 1)
if char.match( /[{]/ )
staple = Staple.new( char )
stack.push( staple )
elsif char.match( /[}]/ )
if stack.last != nil
if char == stack.last.pare
stack.pop
else
staple = Staple.new( char )
stack.push( staple )
end
else
staple = Staple.new( char )
stack.push( staple )
end
end
end
# 最終的にスタックに残っているものが括弧の対応が取れていない
stack.each do |e|
p e.type
@begin_count +=1 if /\{/ =~ e.type
@begin_count -=1 if /\}/ =~ e.type
end
end
end
unless @in_process
@in_process = 0 < @begin_count
p "開始" if @in_process
end
if @in_process
if 0 == @begin_count
p "完了"
@is_end = true
end
end
end
end
text2 = ""
#。詳しくは「IO のエンコーディングとエンコーディングの変換」を
#エンコーディングは Encoding.default_external に
Encoding.default_external="Shift_JIS"
Encoding.default_internal="UTF-8"
a_text = IO.readlines("SampleServlet.java")
#open("ホームページ.txt") {|file|
# print file.readlines[3]
#}
#a.slice(0,2)
start_no = 22 - 1
end_no = a_text.size #47 - 17 + 1
java_doc = JavaDocParse.new(a_text.slice(0, start_no))
servlet_parse = ServletParse.new(a_text.slice(start_no, end_no))
text = java_doc.parse()
text << servlet_parse.parse()
#.each { |c| text << "#{c}" }
#a_text.each { |c| text << "#{c}\n" }
open("log.txt", "w:utf-8") { |f| f.write(text)}
最終更新:2012年08月08日 07:06