This file is indexed.

/usr/lib/perl5/Xacobeo/UI/SourceView.pm is in xacobeo 0.13-2build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package Xacobeo::UI::SourceView;

=head1 NAME

Xacobeo::UI::SourceView - Text editor that displays XML.

=head1 SYNOPSIS

	use Xacobeo::Document;
	use Xacobeo::UI::SourceView;
	
	my $view = Xacobeo::UI::SourceView->new();
	$view->set_show_line_numbers(TRUE);
	$window->add($view);
	
	# Load a document
	my $document = Xacobeo::Document->new_from_file($file, $type);
	$view->set_document($document);
	$view->load_node($document->documentNode);

=head1 DESCRIPTION

The text editor widget that's used for displaying XML. This widget is a
L<Gtk2::SourceView2::View>.

=head1 PROPERTIES

=head2 document

The document being displayed.

=head2 namespaces

The namespaces registered in the document.

=head1 METHODS

The following methods are available:

=head2 new

Creates a new instance. This is simply the parent's constructor.

=cut

use strict;
use warnings;

use Data::Dumper;

use Glib qw(TRUE FALSE);
use Gtk2;
use Gtk2::Pango qw(PANGO_WEIGHT_LIGHT PANGO_WEIGHT_BOLD);
use Gtk2::SourceView2;

use Xacobeo::Utils qw(
	isa_dom_nodelist
	isa_dom_boolean
	isa_dom_number
	isa_dom_literal
	isa_dom_namespace
	escape_xml_attribute
);
use Xacobeo::XS;
use Xacobeo::I18n;
use Xacobeo::Document;
use Xacobeo::GObject;


Xacobeo::GObject->register_package('Gtk2::SourceView2::View' =>
	properties => [
		Glib::ParamSpec->object(
			'document',
			"Document",
			"The main document being displayed.",
			'Xacobeo::Document',
			['readable', 'writable'],
		),

		# FIXME this property is redundant as we can use $self->document->namespaces
		Glib::ParamSpec->scalar(
			'namespaces',
			"Namespaces",
			"The namespaces in the main document.",
			['readable', 'writable'],
		),
	],
);


# The tag table shared by all editors
my $TAG_TABLE = _create_tag_table(Gtk2::TextTagTable->new());


sub INIT_INSTANCE {
	my $self = shift;
	my $buffer = _create_buffer();
	$self->set_buffer($buffer);
	$self->set_editable(FALSE);
}


=head2 set_document

Sets a new document. This method only registers the document with the view, to
display the document use the method load_node and pass the root node.

Parameters:

=over

=item * $document

The main document; an instance of L<Xacobeo::Document>.

=back

=cut

sub set_document {
	my $self = shift;
	my ($document) = @_;

	$self->document($document);
	$self->namespaces(
		$self->document ? $self->document->namespaces : undef
	);
	$self->clear();
}


=head2 show_node

Scrolls the text so that the given node is displayed.

Parameters:

=over

=item * $node

The node to show; an instance of L<XML::LibXML::Node>.

=back

=cut

sub show_node {
	my $self = shift;
	my ($node) = @_;

	my $mark_name = Xacobeo::XS::->get_node_mark($node);
	my $buffer = $self->get_buffer;

	# Clear any previous selection
	if (my $marks = delete $self->{selected}) {
		my @iters = map { $buffer->get_iter_at_mark($_) } @{ $marks };
		$buffer->remove_tag_by_name('selected', @iters);
	}

	# Scroll to the right place in the source view
	my $mark_start = $buffer->get_mark("$mark_name|start");
	if ($mark_start) {
		my $iter_start = $buffer->get_iter_at_mark($mark_start);
		$buffer->place_cursor($iter_start);

		my $mark_end = $buffer->get_mark("$mark_name|end");
		my $iter_end = $buffer->get_iter_at_mark($mark_end);

		$buffer->apply_tag_by_name('selected', $iter_start, $iter_end);
		$self->scroll_to_mark($mark_start, 0.25, FALSE, 0.0, 0.5);

		$self->{selected} = [$mark_start, $mark_end];
	}
	else {
		print "Got no mark at $mark_name!\n";
	}
}


=head2 load_node

Sets the editor's text according to the text representation of the given node.
This is the method that will actually add text to the widget.

Parameters:

=over

=item * $node

The node to be loaded into the editor; an instance of L<XML::LibXML::Node>.

=back

=cut

sub load_node {
	my $self = shift;
	my ($node) = @_;

	# It's faster to disconnect the buffer from the view and to reconnect it back
	my $buffer = $self->get_buffer;
	$self->set_buffer(Gtk2::SourceView2::Buffer->new(undef));
	$buffer->delete($buffer->get_start_iter, $buffer->get_end_iter);


	# A NodeList
	if (! defined $node) {
		_buffer_add($buffer, error => __("Node is undef"));
	}
	elsif ($node->isa('Xacobeo::Error')) {
		_buffer_add($buffer, error => $node->message);
	}
	elsif (isa_dom_nodelist($node)) {
		my @children = $node->get_nodelist;
		my $count = scalar @children;

		# Formatting using to indicate which result is being displayed
		my $i = 0;
		my $format = sprintf ' %%%dd. ', length $count;

		foreach my $child (@children) {
			# Add the result count
			my $result = sprintf $format, ++$i;
			_buffer_add($buffer, result_count => $result);

			if (isa_dom_namespace($child)) {
				# The namespaces nodes are an invention of XML::LibXML and they don't
				# work with the XS code, we deal with them manually.
				_buffer_add($buffer, syntax => ' ');
				_buffer_add($buffer, namespace_name => $child->nodeName);
				_buffer_add($buffer, syntax => '="');

				my $uri = escape_xml_attribute($child->getData);
				_buffer_add($buffer, namespace_uri => $uri);

				_buffer_add($buffer, syntax => '"');
			}
			else {
				Xacobeo::XS->load_text_buffer($buffer, $child, $self->namespaces);
			}

			_buffer_add($buffer, syntax => "\n") if --$count;
		}
	}

	# A Boolean value ex: true() or false()
	elsif (isa_dom_boolean($node)) {
		_buffer_add($buffer, boolean => $node->to_literal);
	}

	# A Number ex: 2 + 5
	elsif (isa_dom_number($node)) {
		_buffer_add($buffer, number => $node->to_literal);
	}

	# A Literal (a single text string) ex: "hello"
	elsif (isa_dom_literal($node)) {
		_buffer_add($buffer, literal => $node->to_literal);
	}

	else {
		# Any kind of XML node
		Xacobeo::XS->load_text_buffer($buffer, $node, $self->namespaces);
	}


	# Add the buffer back into into the text view
	$self->set_buffer($buffer);

	# Scroll to the beginning
	$self->scroll_to_iter($buffer->get_start_iter, 0.0, FALSE, 0.0, 0.0);
}


sub clear {
	my $self = shift;
	$self->get_buffer->set_text('');
}


#
# Adds the given text at the end of the buffer. The text is added with a tag
# which can be used for performing syntax highlighting.
#
sub _buffer_add {
	my ($buffer, $tag, $string) = @_;
	$buffer->insert_with_tags_by_name($buffer->get_end_iter, $string, $tag);
	return;
}


sub _create_buffer {
	my $buffer = Gtk2::SourceView2::Buffer->new($TAG_TABLE);
	$buffer->set_highlight_syntax(undef);

	# This will disable the undo/redo forever
	$buffer->begin_not_undoable_action();
	
	return $buffer;
}


#
# Creates the text tag table shared by all source views.
#
sub _create_tag_table {
	my $tag_table = Gtk2::TextTagTable->new();

	_add_tag($tag_table, result_count =>
		family     => 'Courier 10 Pitch',
		background => '#EDE9E3',
		foreground => 'black',
		style      => 'italic',
		weight     => PANGO_WEIGHT_LIGHT
	);

	# Make the boolean and number look a like
	foreach my $name qw(boolean number) {
		_add_tag($tag_table, $name =>
			family     => 'Courier 10 Pitch',
			foreground => 'black',
			weight     => PANGO_WEIGHT_BOLD
		);
	}

	_add_tag($tag_table, attribute_name =>
		foreground => 'red',
	);

	_add_tag($tag_table, attribute_value =>
		foreground => 'blue',
	);

	_add_tag($tag_table, comment =>
		foreground => '#008000',
		style      => 'italic',
		weight     => PANGO_WEIGHT_LIGHT,
	);

	_add_tag($tag_table, dtd =>
		foreground => '#558CBA',
		style      => 'italic',
	);

	_add_tag($tag_table, element =>
		foreground => '#800080',
		weight     => PANGO_WEIGHT_BOLD,
	);

	_add_tag($tag_table, pi =>
		foreground => '#558CBA',
		style      => 'italic',
	);

	_add_tag($tag_table, pi_data =>
		foreground => 'red',
		style      => 'italic',
	);

	_add_tag($tag_table, syntax =>
		foreground => 'black',
		weight     => PANGO_WEIGHT_BOLD,
	);

	_add_tag($tag_table, literal =>
		foreground => 'black',
	);

	_add_tag($tag_table, cdata =>
		foreground => 'red',
		weight     => PANGO_WEIGHT_BOLD
	);

	_add_tag($tag_table, cdata_content =>
		foreground => 'purple',
		weight     => PANGO_WEIGHT_LIGHT,
		style      => 'italic',
	);

	_add_tag($tag_table, namespace_name =>
		foreground => 'red',
		style      => 'italic',
		weight     => PANGO_WEIGHT_LIGHT,
	);

	_add_tag($tag_table, namespace_uri =>
		foreground => 'blue',
		style      => 'italic',
		weight     => PANGO_WEIGHT_LIGHT,
	);

	_add_tag($tag_table, entity_ref =>
		foreground => 'red',
		style      => 'italic',
		weight     => PANGO_WEIGHT_BOLD,
	);

	_add_tag($tag_table, error =>
		foreground => 'red',
	);

	_add_tag($tag_table, selected =>
		background => 'yellow',
	);

	return $tag_table;
}


#
# Creates a text tag (Gtk2::TextTag) with the given name and properties and adds
# it to the given tag table.
#
sub _add_tag {
	my ($tag_table, $name, @properties) = @_;
	my $tag = Gtk2::TextTag->new($name);
	$tag->set(@properties);
	$tag_table->add($tag);
}

# A true value
1;


=head1 AUTHORS

Emmanuel Rodriguez E<lt>potyl@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2008,2009 by Emmanuel Rodriguez.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.

=cut