Class: Xyml::Document

Inherits:
Array
  • Object
show all
Defined in:
lib/ixyml/xyml.rb

Overview

Xyml::Document class implements the online data loaded from a XYML file. Because XYML is a subset of XML from the viewpoint of data structure, the data in this class is a tree composed of elements, attributes and texts. I call this tree “XYML element tree.” The tree data in this class is composed of alternate hashes and arrays, in the same way of XYML files. See “Xyml module” for the mapping between XYML and XML.

Xyml::Documentクラスは、XYMLファイルを読みだしたオンライン上のデータを実装するクラスである。 XYMLファイルはデータ構造としてはXMLのサブセットであるため、Xyml::Documentクラスの保持する データは、エレメント・属性・テキストからなるツリーである。これを、XYMLエレメントツリーと呼ぶこととする。 XYMLファイルフォーマットと同様に、このクラスによるツリーも、ハッシュと配列との交互の組み合わせ で実現されている。XYMLとXMLとの対応付けについては、“XYMLモジュール”を参照のこと。

+-------------------------+  load_XYML +------------------+
|                         |<-----------|                  |
|  Xyml::Document         |  out_XYML  |    XYML file     |
|  instance               |----------->|(YAML subset file)|
|  +-------------+        |            +------------------+ 
|  |  Raw Object |        |
|  +-------------+        |  out_JSON  +------------------+ 
|                         |----------->| JSON subset file |
+-------------------------+            +------------------+
  |                 |  ^
  |  to_domobj      |  |
  V                 |  | 
+-------------------|--|--+
|                   |  |  |  load_XML +------------------+
|                   |  +---<----------|                  |
|  REXML::Document  |     |  out_XML  | XML subset file  |
|  instance         +---------------->|                  |
|                         |           +------------------+ 
+-------------------------+

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*argv) ⇒ Document

create an instance of Xyml::Document.

Xyml::Documentのインスタンスを生成する。

Args

if first argument in *argv is designated:

*argvの第一要素が指定されている場合:

  • case of a symbol

  • シンボルの場合

    • create an instance composed of only a root element such that the name of the root elemnemt is the first argument

    • ルート要素のみからなるインスタンスを生成。ルート要素の名前が、argvの第一要素となる。

      xyml_tree=Xyml::Document.new(:a)
      #-> [{a:[]}]
      
  • case of an IO instance

  • IOインスタンスの場合

    • create an instance corresponding to the XYML file loaded through the IO. note that only XYML file can be loaded, not XML.(use load_XML method to load an XML file.)

    • IOを通してロードしたXYMLファイルに対応したインスタンスを生成する。XYMLファイルのみが指定可能であり、XMLは不可であることに注意。(XMLファイルをロードする場合は、load_XMLメソッドを使用)

      # aaa.xyml
      #  - a:
      #    -b: ccc
      #    -d:
      #      - eee 
      xyml_tree=Xyml::Document.new(File.open("aaa.xyml"))
      #-> [{a: [{b: "ccc"},{d: ["eee"]}]}]
      
  • case of a tree composed of alternate hashes and arrays.

  • 交互になったハッシュと配列とにより構成したツリーの場合

    • create an instance reflecting a input tree.

    • 入力引数のツリーを反映したインスタンスを生成。

      xyml_tree=Xyml::Document.new({a: [{b: "ccc"},{d: ["eee"]}]})
      #-> [{a: [{b: "ccc"},{d: ["eee"]}]}]
      xyml_tree.out_XYML(File.open("aaa.xyml","w"))
      #-> aaa.xyml
      #  - a:
      #    -b: ccc
      #    -d:
      #      - eee
      


379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/ixyml/xyml.rb', line 379

def initialize *argv
  if argv.size==1
    if argv[0].is_a?(Symbol)
      @root=Xyml::Element.new argv[0]
      self.push @root
      @root._sp(:_iamroot)
    elsif argv[0].is_a?(IO)
      raw_yaml=YAML.load(argv[0])
      @root=Xyml.rawobj2element raw_yaml[0]
      self.clear.push @root
      @root._sp(:_iamroot)
    elsif argv[0].is_a?(Hash)
      @root=Xyml.rawobj2element argv[0]
      self.clear.push @root
      @root._sp(:_iamroot)
    end
  elsif argv.size>1
    raise "tried to create Xyml::Document with more than one parameters."
  end
end

Instance Attribute Details

#rootObject (readonly)

the root of the XYML element tree. This root element is a hash extended by “Xyml_element module,” as all other XYML elements in the tree are also such hashes. The idential element to “@root” is stored in the begining of the array that this class inherites. Threrefore “@root” and “self.at(0)” stand for the same object. @root is provided for accessibility.

XYMLエレメントツリーのルート。ツリー中の他のエレメントと同じく、このルートエレメントは “xyml_elementモジュール”により拡張されたハッシュである。@rootと同一のエレメントは、このクラスが 継承する配列の先頭にも格納されている。つまり、“@root”と“self.at(0)”とは同じオブジェクトを指す。 “@root”は、アクセスしやすいように設けたものである。



338
339
340
# File 'lib/ixyml/xyml.rb', line 338

def root
  @root
end

Instance Method Details

#load_XML(io) ⇒ Object

load an XML file through the designated IO and set the tree data in the file to the self.

XMLファイルをIOよりロードして、そのツリーデータを自身に設定する。

# aaa.xml
# <a b="ccc">
#   <d>eee</d>
# </a>
xyml_tree=Xyml::Document.new
xyml_tree.load_XML(File.open("aaa.xml"))
#-> [{a: [{b: "ccc"},{d: ["eee"]}]}]


410
411
412
413
414
415
416
# File 'lib/ixyml/xyml.rb', line 410

def load_XML io
  xml=REXML::Document.new(io)
  @root=Xyml.domobj2element xml.root
  self.clear.push @root
  @root._sp(:_iamroot)
  io.close
end

#load_XYML(io) ⇒ Object

load an XYML file through the designated IO and set the tree data in the file to the self.

XYMLファイルをIOよりロードして、そのツリーデータを自身に設定する。

# aaa.xyml
#  - a:
#    -b: ccc
#    -d:
#      - eee 
xyml_tree=Xyml::Document.new
xyml_tree.load_XYML(File.open("aaa.xyml"))
#-> [{a: [{b: "ccc"},{d: ["eee"]}]}]


495
496
497
498
499
500
# File 'lib/ixyml/xyml.rb', line 495

def load_XYML io
  raw_yaml=YAML.load(io)
  @root=Xyml.rawobj2element raw_yaml[0]
  self.clear.push @root
  io.close
end

#out_JSON(io) ⇒ Object

save a JSON file corresponding to the tree data in the self through the designated IO. Note that a JSON file can be loaded by load_XYML method because JSON is a part of YAML.

自身のツリーデータを、指定されたIOを通して、JSONファイルに保存する。JSONファイルのロードは、 load_XYMLメソッドで実施できることに注意(JSONはYAML仕様の一部分となっているため)。

xyml_tree=Xyml::Document.new({a: [{b: "ccc"},{d: ["eee"]}]})
#-> [{a: [{b: "ccc"},{d: ["eee"]}]}]
xyml_tree.out_JSON(File.open("aaa.json","w"))
#-> aaa.jdon
#  [{"a":[{"b":"ccc"},{"d":["eee"]}]}]


512
513
514
515
516
# File 'lib/ixyml/xyml.rb', line 512

def out_JSON io
  serialized=JSON.generate(Xyml.remove_parent_rcsv(self)) 
  io.print serialized
  io.close
end

#out_XML(io, indent = nil) ⇒ Object

save an XML file corresponding to the tree data in the self through the designated IO.

自身のツリーデータを、指定されたIOを通して、XMLファイルに保存する。

Args

indent(if not nil)

a saved XML file is formatted with the designaged indent.

xyml_tree=Xyml::Document.new({a: [{b: "ccc"},{d: ["eee"]}]})
#-> [{a: [{b: "ccc"},{d: ["eee"]}]}]
xyml_tree.out_XML(File.open("aaa.xml","w"))
#-> aaa.xml
# <a b="ccc">
#   <d>eee</d>
# </a>


430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/ixyml/xyml.rb', line 430

def out_XML io,indent=nil

  if indent
    Xyml.rawobj2domobj(@root).write(io,indent.to_i)
  else
    sio=StringIO.new
    Xyml.rawobj2domobj(@root).write(sio)
    sio.rewind
    io.print sio.read,"\n"
  end
  io.close
end

#out_XYML(io) ⇒ Object

save a XYML file corresponding to the tree data in the self through the designated IO.

自身のツリーデータを、指定されたIOを通して、XYMLファイルに保存する。

xyml_tree=Xyml::Document.new({a: [{b: "ccc"},{d: ["eee","fff"]}]})
#-> [{a: [{b: "ccc"},{d: ["eee","fff"]}]}]
xyml_tree.out_XYML(File.open("aaa.xyml","w"))
#-> aaa.xyml
#  - a:
#    -b: ccc
#    -d:
#      - eee
#      - fff


456
457
458
459
# File 'lib/ixyml/xyml.rb', line 456

def out_XYML io
  Xyml.doc2file(self,io)
  io.close
end

#out_XYML_standard(io) ⇒ Object

save a XYML file corresponding to the tree data in the self through the designated IO, in the way that a saved XYML file is in the “standard syle.” For example, a XYML file has no redandant partitions in texts in the “standard style.” Two XYML files can be compared presicely if they are in the standard style.

自身のツリーデータを、指定されたIOを通して、“標準スタイル”で、XYMLファイルに保存する。 例えば、“標準スタイル”ではXYMLファイル内で冗長なテキストの分かち書きを行わない。 標準スタイルであれば、2つのファイルを正確に比較することが可能となる。

xyml_tree=Xyml::Document.new({a: [{b: "ccc"},{d: ["eee","fff"]}]})
#-> [{a: [{b: "ccc"},{d: ["eee","fff"]}]}]
xyml_tree.out_XYML(File.open("aaa.xyml","w"))
#-> aaa.xyml
#  - a:
#    -b: ccc
#    -d:
#      - eeefff


477
478
479
480
481
# File 'lib/ixyml/xyml.rb', line 477

def out_XYML_standard io
  io.print "---\n"
  Xyml.out_xyml_rcsv_std(self,0,io)
  io.close
end

#to_domobjObject

convert the tree data in the self into a REXML::Document instance.

自身のツリーデータを、REXML::Documentインスタンスに変換する。

Return

a REXML::Document instance.

xyml_tree=Xyml::Document.new({a: [{b: "ccc"},{d: ["eee"]}]})
REXML::Document rexml_tree=xyml_tree.to_domobj


525
526
527
# File 'lib/ixyml/xyml.rb', line 525

def to_domobj
  Xyml.rawobj2domobj(@root)
end