File: gtkxpath.rb

package info (click to toggle)
libxml-parser-ruby 0.6.8-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 912 kB
  • ctags: 1,523
  • sloc: ruby: 11,080; ansic: 1,958; xml: 467; makefile: 59
file content (259 lines) | stat: -rwxr-xr-x 5,913 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#! /usr/local/bin/ruby

## XPath demo for Gtk
## 1999 by yoshidam
##

require 'gtk'
require 'xml/dom2/core'
require 'xml/dom2/dombuilder'
require 'xml/dom2/xpath'
#require 'uconv'

GC.disable

## TREE_MODE = 0: expand entity references, and not create DOCUMENT_TYPE_NODE
## TREE_MODE = 1: not expand entity references
TREE_MODE = 0
## enpand tree at the beginning
EXPAND_TREE = false
## trim extra white spaces
TRIM = true
## concatenate folding lines
UNFOLD = false
## parse external entity
PARSE_EXT = true

## Gtk resources
#Gtk::RC::parse_string <<EOS
#style "default" {
#  fontset = "-*-helvetica-medium-r-normal--12-*-*-*-*-*-*-*,\ 
#             -*-fixed-medium-r-normal--14-*-*-*-*-*-jisx0208.1983-0,*"
#}
#widget_class "*" style "default"
#EOS

def unfold(str)
  str.
    gsub(/([-])\n\s*([-])/, '\1\2').
    gsub(/([a-z])-\n\s+([a-z])/, '\1\2').
    gsub(/\s+/, ' ')
end

module Gtk
  class TreeItem
    attr :xml_node
    alias initialize0 initialize
    def initialize(*arg)
      initialize0(arg[0])
      @xml_node = arg[1]
    end
  end
end

## Extend the Node class to manipulate the Gtk::Tree
module XML
module DOM
class Node
  ##  a node to Gtk::Tree
  def appendNodeToTree(node)
    if @treeitem.nil?
      raise "Cannot append tree"
    end
    if @tree.nil?
      @tree = Gtk::Tree::new()
      @treeitem.set_subtree(@tree)
      if EXPAND_TREE
        @tree.show
        @treeitem.expand
      end
    end
    @tree.append(node)
  end

  ## create Gtk::Tree from XML::Node tree
  def newTreeItem(parent = nil)
    if !@treeitem.nil?
      raise "tree item already exist"
    end

    case self.nodeType
    when TEXT_NODE
      attr = self.parentNode.attributes
      if attr && attr['xml:space'] != 'preserve'
        self.nodeValue = unfold(self.nodeValue) if UNFOLD
      end
#      str = "\"" + Uconv::u8toeuc(self.nodeValue) + "\""
      str = "\"" + self.nodeValue + "\""
    when CDATA_SECTION_NODE
#      str = "<![CDATA[" + Uconv::u8toeuc(self.nodeValue) + "]]>"
      str = "<![CDATA[" + self.nodeValue + "]]>"
    when PROCESSING_INSTRUCTION_NODE
#      str = "?" + Uconv::u8toeuc(self.nodeValue)
      str = "?" + self.nodeValue
    when ELEMENT_NODE
      attr = ''
      @attr.each do |a|  ## self.attributes do |a|
        attr += a.to_s + ", "
      end if @attr
      attr.chop!
      attr.chop!
#      str = Uconv::u8toeuc(nodeName)
      str = nodeName
      if (attr != '');
#        str += "  (" + Uconv::u8toeuc(attr) + ")"
        str += "  (" + attr + ")"
      end
    when COMMENT_NODE
#      str = "<!--" + Uconv::u8toeuc(self.nodeValue) + "-->"
      str = "<!--" + self.nodeValue + "-->"
    when DOCUMENT_TYPE_NODE
#      str = "#doctype: " + Uconv::u8toeuc(self.nodeName)
      str = "#doctype: " + self.nodeName
    when ENTITY_REFERENCE_NODE
#      str = "&" + Uconv::u8toeuc(self.nodeName) + ";"
      str = "&" + self.nodeName + ";"
    else
#      str = Uconv::u8toeuc(self.nodeName)
      str = self.nodeName
    end
    str.gsub!(/\n/, "\\\\n")
    @treeitem = Gtk::TreeItem::new(str, self)
    @selecting = false
    @treeitem.signal_connect("select") do |w|
#      $text.set_text(Uconv::u8toeuc(w.xml_node.makeXPath)) unless @selecting
      $text.set_text(w.xml_node.makeXPath) unless @selecting
    end
    if (parent.nil? && !self.parentNode.nil?)
      self.parentNode.appendNodeToTree(@treeitem)
    else
      parent.append(@treeitem)
    end
    @treeitem.show
    self.childNodes do |c|
      c.newTreeItem
    end
  end

  def selectNode
    @selecting = true
    parentNode.showTree if parentNode
    if @treeitem
      @treeitem.activate
    else
      print "Unseen node on the tree view\n"
    end
    @selecting = false
  end

  def showTree
    if parentNode
      parentNode.showTree
    end
    @tree.show if @tree
  end

  def deselect
    @treeitem.deselect
    childNodes do |node|
      node.deselect
    end
  end
end
end
end

## create XML tree
builder = XML::DOM::DOMBuilder.new
builder.setBase("./")
begin
  xmltree = builder.parse($<.read, PARSE_EXT)
rescue XML::Parser::Error
  line = builder.line
  print "#{$0}: #{$!} (in line #{line})\n"
  exit 1
end
print "Parsing end\n"
GC.start

## unify sequential Text nodes
xmltree.documentElement.normalize
xmltree.trim if TRIM
print "Normalization end\n"
GC.start

## create Gtk window
window = Gtk::Window::new(Gtk::WINDOW_TOPLEVEL)
window.signal_connect("delete_event") { exit }
window.signal_connect("destroy_event") { exit }

window.border_width(10)
window.set_title($<.filename)

box1 = Gtk::VBox::new(FALSE, 5)
box1_h = Gtk::HBox::new(FALSE, 5)
window.add(box1)
box1.show
box1.add(box1_h)
box1_h.show

tree = nil
$text = Gtk::Entry.new()
box1_h.pack_start($text, TRUE, TRUE, 0)
$text.show
button = Gtk::Button::new("Find")
button.signal_connect("clicked") do
  text = $text.get_text
  xmltree.deselect
  context = XPath::DOM::Context.new(xmltree)
  begin
    tree.set_selection_mode(Gtk::SELECTION_MULTIPLE)
#    proc = XPath.compile(Uconv.euctou8(text))
    proc = XPath.compile(text)
    result = proc.call(context)
    result.each do |node|
      node.selectNode
    end
  rescue
    ## XPath Error
    print "\a#{$!}: #{text}\n"
  ensure
    tree.set_selection_mode(Gtk::SELECTION_SINGLE)
  end
  1
end
box1_h.pack_start(button, FALSE, FALSE, 0)
button.show

scroll = Gtk::ScrolledWindow.new
scroll.show
scroll.set_usize(600,400)
box1.add(scroll)


tree = Gtk::Tree::new()
##tree.set_selection_mode(Gtk::SELECTION_MULTIPLE)
scroll.add_with_viewport(tree) ## gtk+-1.2
##scroll.add(tree) ## gtk+-1.0
tree.show

## construct Gtk tree
xmltree.newTreeItem(tree)
print "Tree construction end\n"

box2 = Gtk::VBox::new(FALSE, 10)
box2.border_width(10)
box1.pack_start(box2, FALSE, TRUE, 0)
box2.show

button = Gtk::Button::new("Quit")
button.signal_connect("clicked") do
  exit
end
box2.pack_start(button, TRUE, TRUE, 0)
button.show


window.show
GC.start
Gtk::main()