From: siena@... (Siena.) Date: 2002-05-27T22:50:24+09:00 Subject: [ruby-list:35275] Re: ANNOUNCE: REXML のドキュメント Siena. です。 ▼ [ruby-list:35269] < 須藤功平 さん 》たので、[ruby-list:35193]のよしださんの案(と 》http://www.yoshidam.net/diary/rexml.diff)を紹介しました。 よしださんの最近の分の日記を拝見しました。 こつこつと進めてらしたのですね。素晴らしいです。 差分は大分良さそうに見えます。 # その間、あたしはというと、HTML ファイル to REXML # オブジェクトなモジュールを書いて遊んでました ^^; Source と Output でのプラットフォーム間の差異はどうしましょうか。 いずれ、Iconv などを使わねばならなくなりますよね。 プラットフォーム別の変換モジュールを使うように変更します? このままで、Iconv かそのラッパで吸収するようにします? あと、出力関係を改善できそうだったので、ちょっとだけいじってみました。 REXML 2.3.3 + http://www.yoshidam.net/diary/rexml.diff をベースに、 次の点を変更したつもり ^^; なのですが、いかがでしょうか。 * Output#encoding から文字エンコーディングを取得できるようにした * XMLDecl#write で出力の文字エンコーディングが反映されるようにした * Document#write に出力の文字エンコーディングを指定できるようにした (おまけ) --- Siena. diff -ru ../yoshidam/document.rb ./document.rb --- ../yoshidam/document.rb Mon May 27 21:11:52 2002 +++ ./document.rb Mon May 27 22:20:56 2002 @@ -126,8 +126,11 @@ # document will be written # @param indent (optional) if given, the starting indent for the lines # in the document. - def write( output, indent=-1 ) - output = Output.new( output, xml_decl.encoding ) if xml_decl.encoding != "UTF-8" + def write( output, encoding = xml_decl.encoding, indent=-1 ) + if( ( encoding.is_a? Numeric ) && ( indent == -1 ) ) + encoding, indent = nil, encoding # for old-style arguments + end + output = Output.new( output, encoding ) if encoding.downcase != "utf-8" @children.each { |node| node.write( output, indent ) output << "\n" unless indent<0 or node == @children[-1] diff -ru ../yoshidam/output.rb ./output.rb --- ../yoshidam/output.rb Mon May 27 21:11:52 2002 +++ ./output.rb Mon May 27 21:26:22 2002 @@ -30,8 +30,10 @@ end end - def initialize real_IO, encoding="iso-8859-1" + attr_reader :encoding # don't allow users to change this while writing + def initialize real_IO, enc="iso-8859-1" @output = real_IO + @encoding = enc.downcase case encoding when /^iso-8859-1$/i instance_eval "alias :encode :iso_8859_1" diff -ru ../yoshidam/xmldecl.rb ./xmldecl.rb --- ../yoshidam/xmldecl.rb Mon May 27 21:11:52 2002 +++ ./xmldecl.rb Mon May 27 21:08:20 2002 @@ -44,7 +44,7 @@ def write writer, indent=-1 indent( writer, indent ) writer << START.sub(/\\/u, '') - writer << " #{content}" + writer << content( writer ) writer << STOP.sub(/\\/u, '') end @@ -87,9 +87,10 @@ alias :stand_alone? :standalone private - def content - rv = "version='#@version'" - rv << " encoding='#{encoding}'" if @encoding + def content( writer ) + rv = " version='#@version'" + enc = ( writer.encoding || encoding || '' ).downcase + rv << " encoding='#{enc}'" if enc != '' && enc != 'utf-8' rv << " standalone='#@standalone'" if @standalone rv end