#!/usr/local/bin/ruby
#  encoding: utf-8
#  daybook_mod.rb

#  Created by midori on 2006-09-19.
#  Copyright (c) 2006. All rights reserved.
#  You can distribute/modify this program under the terms of the Ruby License.
#  Last change: 2006-10-25.

module CheckPath
  def m_checking_path_int(del_id, dir, ext)
    unless del_id =~ /\D/ 
      del_path = File.join( dir, del_id + "#{ext}")
      if FileTest.exist?(del_path)
        return del_id
      end
     end
    return ""
  end

end

module ReadWrite
  def read_file(path_file)
    str = ""
    begin
      open(path_file, "r") do |s|
        str = s.read
      end
    rescue SystemCallError
      $stderr.print "IO failes: " + $!
      raise
    end
    return str
  end
  def m_write_file(path_file, str)
    begin
      File.open(path_file, File::WRONLY | File::CREAT){|w|
      w.flock(File::LOCK_EX)
      w.print str
      }
    rescue SystemCallError
      $stderr.print "IO failes: " + $!
      raise
    end
  end
  def m_log_write_file(path_file, str)
    begin
      open(path_file, "a+") do |w|
      w.print str
      end
    rescue SystemCallError
      #print "Content-Type: text/html\n\n"
      $stderr.print "IO failes: " + $!
      raise
    end
  end

end

module MakeXML
  require "rexml/document"
  require "xml/xslt"
  include REXML
  def m_make_new_xml(docroot_element)
    doc = Document.new()
    doc << XMLDecl.new(version="1.0", encoding="UTF-8" )
    doc.add_text("\n")
    docroot = doc.add_element(docroot_element)
    doc.add_text("\n")
  return doc
  end
  def m_add_my_element(docroot, element, str)
    docroot.add_text("\n")
    newitem = Element.new( element )
    docroot.add_element(newitem)
    newitem.add_text(str)
    return newitem
  end
  def m_apply_xsl_path(str_xml, path_xsl)
     #xslt.xml = str_xml
     doc = Document.new()
     doc << XMLDecl.new(version="1.0", encoding="UTF-8" )
     doc.add_text("\n")
     sourcedoc = Document.new(str_xml)
     docroot = sourcedoc.root
     doc.add_element(docroot)

      xslt = XML::XSLT.new()
      xslt.xml = doc.to_s
      #xslt.xsl = Document.new(File.open(path_xsl))
      xslt.xslfile = path_xsl
      out = xslt.serve()
      xhtml = out.to_s
      return xhtml
  end
  def m_read_xml(path_xml)
    doc = Document.new()
    doc << XMLDecl.new(version="1.0", encoding="UTF-8" )
    doc.add_text("\n")
   
    sourcedoc = Document.new(File.new( path_xml ))
    docroot = sourcedoc.root
    doc.add_element(docroot)
     
    return  doc

  end
  def count_ele(doc, xpath_element)# count_ele(doc, "docroot/item")    
    return doc.get_elements(xpath_element).size
  end
  def delete_ele(doc, xpath_element)# delete_ele(doc, "docroot/item")  
    doc.delete_element(path_element)
    return doc
  end
  #-------------------
  def m_common_count_xmlfiles(path_log_xml)
    r = read_file(path_log_xml)
    unless r =~ /..*/
      return []
    end
    ary = Array.new
    r.each{
      |x|
      xf =  x.split(",")[1].chop
      if FileTest.exist?(xf)
        ary << xf
      end
    }
    
    new_list = Hash.new
    cnt = 0
    ary.each{|x| new_list[x] = cnt; cnt += 1}
    pathlist = new_list.keys
    
    
    return pathlist.sort.reverse
  end
  #-------------------
end
