| 1 | #============================================================================= | ||
| 2 | # | ||
| 3 | # Copyright (c) 2010 Ars Aperta, Itaapy, Pierlis, Talend. | ||
| 4 | # Copyright (c) 2014 Jean-Marie Gouarné. | ||
| 5 | # Author: Jean-Marie Gouarné <jean.marie.gouarne@online.fr> | ||
| 6 | # | ||
| 7 | #============================================================================= | ||
| 8 | 2 | ▶ | use 5.010_001; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | #============================================================================= | ||
| 11 | # Base ODF element class and some derivatives | ||
| 12 | #============================================================================= | ||
| 13 | package ODF::lpOD::Element; | ||
| 14 | our $VERSION = '1.015'; | ||
| 15 | 2 | ▶ | use constant PACKAGE_DATE => '2014-04-30T08:27:41'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 2 | ▶ | use ODF::lpOD::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | #----------------------------------------------------------------------------- | ||
| 18 | 0 | ▶ | use XML::Twig 3.34; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 19 | use base 'XML::Twig::Elt'; | ||
| 20 | #=== element classes ========================================================= | ||
| 21 | |||
| 22 | our %CLASS = | ||
| 23 | ( | ||
| 24 | '#PCDATA' => odf_text_node, | ||
| 25 | 'text:p' => odf_paragraph, | ||
| 26 | 'text:h' => odf_heading, | ||
| 27 | 'text:span' => odf_text_element, | ||
| 28 | 'text:a' => odf_text_hyperlink, | ||
| 29 | 'text:bibliography-mark' => odf_bibliography_mark, | ||
| 30 | 'text:note' => odf_note, | ||
| 31 | 'office:annotation' => odf_annotation, | ||
| 32 | 'text:changed-region' => odf_changed_region, | ||
| 33 | 'text:section' => odf_section, | ||
| 34 | 'text:list' => odf_list, | ||
| 35 | 'table:table' => odf_table, | ||
| 36 | 'table:table-column-group' => odf_column_group, | ||
| 37 | 'table:table-header-columns' => odf_column_group, | ||
| 38 | 'table:table-row-group' => odf_row_group, | ||
| 39 | 'table:table-header-rows' => odf_row_group, | ||
| 40 | 'table:table-column' => odf_column, | ||
| 41 | 'table:table-row' => odf_row, | ||
| 42 | 'table:table-cell' => odf_cell, | ||
| 43 | 'table:covered-table-cell' => odf_cell, | ||
| 44 | 'text:variable-decl' => odf_simple_variable, | ||
| 45 | 'text:user-field-decl' => odf_user_variable, | ||
| 46 | 'draw:page' => odf_draw_page, | ||
| 47 | 'draw:rect' => odf_rectangle, | ||
| 48 | 'draw:ellipse' => odf_ellipse, | ||
| 49 | 'draw:line' => odf_line, | ||
| 50 | 'draw:connector' => odf_connector, | ||
| 51 | 'draw:frame' => odf_frame, | ||
| 52 | 'draw:image' => odf_image, | ||
| 53 | 'manifest:file-entry' => odf_file_entry, | ||
| 54 | 'style:font-face' => odf_font_declaration, | ||
| 55 | 'style:style' => odf_style, | ||
| 56 | 'style:default-style' => odf_style, | ||
| 57 | 'text:list-style' => odf_list_style, | ||
| 58 | 'text:list-level-style-number' => odf_list_level_style, | ||
| 59 | 'text:list-level-style-bullet' => odf_list_level_style, | ||
| 60 | 'text:list-level-style-image' => odf_list_level_style, | ||
| 61 | 'text:outline-level-style' => odf_list_level_style, | ||
| 62 | 'text:outline-style' => odf_outline_style, | ||
| 63 | 'style:master-page' => odf_master_page, | ||
| 64 | 'style:page-layout' => odf_page_layout, | ||
| 65 | 'draw:gradient' => odf_gradient, | ||
| 66 | 'style:presentation-page-layout' | ||
| 67 | => odf_presentation_page_layout, | ||
| 68 | 'style:header-style' => odf_page_end_style, | ||
| 69 | 'style:footer-style' => odf_page_end_style, | ||
| 70 | 'text:table-of-content' => odf_toc, | ||
| 71 | 'table:named-range' => odf_named_range | ||
| 72 | ); | ||
| 73 | |||
| 74 | sub get_class_map { %CLASS } | ||
| 75 | sub associate_tag | ||
| 76 | { | ||
| 77 | my $caller = shift; | ||
| 78 | my $class = ref($caller) || $caller; | ||
| 79 | $CLASS{$_} = $class for @_; | ||
| 80 | } | ||
| 81 | |||
| 82 | #=== aliases and initialization ============================================== | ||
| 83 | |||
| 84 | BEGIN | ||
| 85 | { | ||
| 86 | *create = *new; | ||
| 87 | *xe_new = *XML::Twig::Elt::new; | ||
| 88 | *get_tag = *XML::Twig::Elt::tag; | ||
| 89 | *get_tagname = *XML::Twig::Elt::tag; | ||
| 90 | *del_attributes = *XML::Twig::Elt::del_atts; | ||
| 91 | *get_children = *XML::Twig::Elt::children; | ||
| 92 | *get_descendants = *XML::Twig::Elt::descendants; | ||
| 93 | *get_parent = *XML::Twig::Elt::parent; | ||
| 94 | *get_ancestor = *XML::Twig::Elt::parent; | ||
| 95 | *previous_sibling = *XML::Twig::Elt::prev_sibling; | ||
| 96 | *ungroup = *XML::Twig::Elt::erase; | ||
| 97 | *get_root = *XML::Twig::Elt::root; | ||
| 98 | *is_element = *XML::Twig::Elt::is_elt; | ||
| 99 | *is_text_segment = *XML::Twig::Elt::is_text; | ||
| 100 | *_set_text = *XML::Twig::Elt::set_text; | ||
| 101 | *_get_text = *XML::Twig::Elt::text; | ||
| 102 | *_set_tag = *XML::Twig::Elt::set_tag; | ||
| 103 | *_set_first_child = *XML::Twig::Elt::set_first_child; | ||
| 104 | *_set_last_child = *XML::Twig::Elt::set_last_child; | ||
| 105 | *replace_element = *XML::Twig::Elt::replace; | ||
| 106 | *set_child = *set_first_child; | ||
| 107 | *get_element_list = *get_elements; | ||
| 108 | *get_bookmark_list = *get_bookmarks; | ||
| 109 | *get_index_mark_list = *get_index_marks; | ||
| 110 | *get_bibliography_mark_list = *get_bibliography_marks; | ||
| 111 | *get_table_list = *get_tables; | ||
| 112 | *get_draw_page_list = *get_draw_pages; | ||
| 113 | *get_part = *lpod_part; | ||
| 114 | *document_part = *lpod_part; | ||
| 115 | *get_document_part = *lpod_part; | ||
| 116 | *get_document = *document; | ||
| 117 | *get_document_type = *document_type; | ||
| 118 | *export = *serialize; | ||
| 119 | } | ||
| 120 | |||
| 121 | #=== exported constructor ==================================================== | ||
| 122 | |||
| 123 | sub _create { ODF::lpOD::Element->new(@_) } | ||
| 124 | |||
| 125 | #----------------------------------------------------------------------------- | ||
| 126 | |||
| 127 | our $INIT_CALLBACK = undef; | ||
| 128 | |||
| 129 | sub new | ||
| 130 | { | ||
| 131 | my $caller = shift; | ||
| 132 | my $class = ref($caller) || $caller; | ||
| 133 | my $data = shift or return undef; | ||
| 134 | my $element; | ||
| 135 | if (ref $data || $data =~ /\.xml$/i) # load from file | ||
| 136 | { | ||
| 137 | $data = load_file($data); | ||
| 138 | } | ||
| 139 | $data =~ s/^\s+//; | ||
| 140 | $data =~ s/\s+$//; | ||
| 141 | if ($data =~ /^</) # create from XML string | ||
| 142 | { | ||
| 143 | return ODF::lpOD::Element->parse_xml($data, @_); | ||
| 144 | } | ||
| 145 | # odf_element creation | ||
| 146 | return undef unless $data; | ||
| 147 | $element = $class->SUPER::new($data, @_); | ||
| 148 | # possible subclassing according to the tag | ||
| 149 | my $tag = $element->tag; | ||
| 150 | if ($CLASS{$tag}) | ||
| 151 | { | ||
| 152 | bless $element, $CLASS{$tag}; | ||
| 153 | } | ||
| 154 | elsif ($tag =~ /^number:.*-style$/) | ||
| 155 | { | ||
| 156 | bless $element, 'ODF::lpOD::DataStyle'; | ||
| 157 | } | ||
| 158 | # optional user-defined post-constructor function | ||
| 159 | if ($INIT_CALLBACK && (caller() eq 'XML::Twig')) | ||
| 160 | { | ||
| 161 | &$INIT_CALLBACK($element); | ||
| 162 | } | ||
| 163 | return $element; | ||
| 164 | } | ||
| 165 | |||
| 166 | #----------------------------------------------------------------------------- | ||
| 167 | |||
| 168 | sub parse_xml | ||
| 169 | { | ||
| 170 | state $twig; | ||
| 171 | unless ($twig) | ||
| 172 | { | ||
| 173 | $twig = XML::Twig->new | ||
| 174 | ( | ||
| 175 | elt_class => 'ODF::lpOD::Element', | ||
| 176 | output_encoding => TRUE, | ||
| 177 | id => $ODF::lpOD::Common::LPOD_ID | ||
| 178 | ); | ||
| 179 | $twig->set_output_encoding('UTF-8'); | ||
| 180 | } | ||
| 181 | my $class = shift; | ||
| 182 | $twig->safe_parse(@_) or return undef; | ||
| 183 | my $element = $twig->root; | ||
| 184 | $element->set_classes; | ||
| 185 | return $element; | ||
| 186 | } | ||
| 187 | |||
| 188 | sub clone | ||
| 189 | { | ||
| 190 | my $self = shift; | ||
| 191 | my $class = ref $self; | ||
| 192 | my $elt = $self->copy; | ||
| 193 | return bless $elt, $class; | ||
| 194 | } | ||
| 195 | |||
| 196 | #----------------------------------------------------------------------------- | ||
| 197 | |||
| 198 | sub convert { FALSE } | ||
| 199 | sub context_path {} | ||
| 200 | |||
| 201 | sub set_tag | ||
| 202 | { | ||
| 203 | my $self = shift; | ||
| 204 | my $tag = shift; | ||
| 205 | $self->_set_tag($tag); | ||
| 206 | bless $self, $CLASS{$tag} || 'ODF::lpOD::Element'; | ||
| 207 | $self->set_class; | ||
| 208 | return $tag; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub set_class | ||
| 212 | { | ||
| 213 | my $self = shift; | ||
| 214 | my $prefix = $self->ns_prefix or return $self; | ||
| 215 | if ($prefix eq 'text') | ||
| 216 | { | ||
| 217 | ODF::lpOD::TextField::classify($self); | ||
| 218 | } | ||
| 219 | return $self; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub set_classes | ||
| 223 | { | ||
| 224 | my $self = shift; | ||
| 225 | foreach my $e ($self->descendants_or_self) | ||
| 226 | { | ||
| 227 | my $class; | ||
| 228 | next if $e->isa('ODF::lpOD::TextNode'); | ||
| 229 | my $tag = $e->tag; | ||
| 230 | if ($tag =~ /^number:.*style$/) | ||
| 231 | { | ||
| 232 | $class = 'ODF::lpOD::Style'; | ||
| 233 | } | ||
| 234 | $class ||= $CLASS{$tag}; | ||
| 235 | $class ||= 'ODF::lpOD::Element'; | ||
| 236 | bless $e, $class; | ||
| 237 | $e->set_class; | ||
| 238 | } | ||
| 239 | return $self; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub check_tag | ||
| 243 | { | ||
| 244 | my $self = shift; | ||
| 245 | my $new_tag = shift; | ||
| 246 | my $old_tag = $self->tag; | ||
| 247 | return $old_tag unless $new_tag; | ||
| 248 | unless ($new_tag eq $old_tag) | ||
| 249 | { | ||
| 250 | $self->set_tag($new_tag); | ||
| 251 | } | ||
| 252 | return $self->tag; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub is | ||
| 256 | { | ||
| 257 | my $self = shift; | ||
| 258 | my $classname = shift; | ||
| 259 | unless (ref($classname)) | ||
| 260 | { | ||
| 261 | return ( | ||
| 262 | $self->isa($classname) || $classname eq $self->tag | ||
| 263 | ) ? TRUE : FALSE; | ||
| 264 | } | ||
| 265 | if (ref($classname) eq 'Regexp') | ||
| 266 | { | ||
| 267 | my $tag = $self->tag; | ||
| 268 | return ($tag =~ $classname) ? TRUE : FALSE; | ||
| 269 | } | ||
| 270 | else | ||
| 271 | { | ||
| 272 | alert("Wrong reference"); | ||
| 273 | return undef; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | sub set_id | ||
| 278 | { | ||
| 279 | my $self = shift; | ||
| 280 | return $self->set_attribute('id' => shift); | ||
| 281 | } | ||
| 282 | |||
| 283 | sub get_id | ||
| 284 | { | ||
| 285 | my $self = shift; | ||
| 286 | return $self->get_attribute('id'); | ||
| 287 | } | ||
| 288 | |||
| 289 | sub is_child | ||
| 290 | { | ||
| 291 | my $self = shift; | ||
| 292 | my $ref_elt = shift; | ||
| 293 | my $parent = $self->parent; | ||
| 294 | return ($parent && $parent == $ref_elt) ? TRUE : FALSE; | ||
| 295 | } | ||
| 296 | |||
| 297 | sub get_child | ||
| 298 | { | ||
| 299 | my $self = shift; | ||
| 300 | my $tag = $self->normalize_name(shift) or return undef; | ||
| 301 | return $self->first_child($tag); | ||
| 302 | } | ||
| 303 | |||
| 304 | sub set_first_child | ||
| 305 | { | ||
| 306 | my $self = shift; | ||
| 307 | return $self->_set_first_child(@_) if caller() eq 'XML::Twig::Elt'; | ||
| 308 | my $tag = $self->normalize_name(shift); | ||
| 309 | my $child = $self->first_child($tag) | ||
| 310 | // | ||
| 311 | $self->insert_element($tag); | ||
| 312 | $child->set_text(shift); | ||
| 313 | $child->set_attributes(@_); | ||
| 314 | return $child; | ||
| 315 | } | ||
| 316 | |||
| 317 | sub set_last_child | ||
| 318 | { | ||
| 319 | my $self = shift; | ||
| 320 | return $self->_set_last_child(@_) if caller() eq 'XML::Twig::Elt'; | ||
| 321 | my $tag = $self->normalize_name(shift); | ||
| 322 | my $child = $self->first_child($tag) | ||
| 323 | // | ||
| 324 | $self->append_element($tag); | ||
| 325 | $child->set_text(shift); | ||
| 326 | $child->set_attributes(@_); | ||
| 327 | return $child; | ||
| 328 | } | ||
| 329 | |||
| 330 | sub set_parent | ||
| 331 | { | ||
| 332 | my $self = shift; | ||
| 333 | my $tag = $self->normalize_name(shift); | ||
| 334 | my $parent = $self->parent; | ||
| 335 | |||
| 336 | if ($parent) | ||
| 337 | { | ||
| 338 | unless ($parent->is($tag)) | ||
| 339 | { | ||
| 340 | $parent = ODF::lpOD::Element->create($tag); | ||
| 341 | $parent->paste(before => $self); | ||
| 342 | $self->move(first_child => $parent); | ||
| 343 | } | ||
| 344 | } | ||
| 345 | else | ||
| 346 | { | ||
| 347 | $parent = ODF::lpOD::Element->create($tag); | ||
| 348 | $self->move(first_child => $parent); | ||
| 349 | } | ||
| 350 | |||
| 351 | $parent->set_text(shift); | ||
| 352 | $parent->set_attributes(@_); | ||
| 353 | return $parent; | ||
| 354 | } | ||
| 355 | |||
| 356 | sub delete_child | ||
| 357 | { | ||
| 358 | my $self = shift; | ||
| 359 | my $child = $self->get_child(shift) or return FALSE; | ||
| 360 | $child->delete; | ||
| 361 | return TRUE; | ||
| 362 | } | ||
| 363 | |||
| 364 | sub delete_children | ||
| 365 | { | ||
| 366 | my $self = shift; | ||
| 367 | my @children= $self->children(shift); | ||
| 368 | my $count = 0; | ||
| 369 | foreach my $e (@children) | ||
| 370 | { | ||
| 371 | $e->delete; $count++; | ||
| 372 | } | ||
| 373 | return $count; | ||
| 374 | } | ||
| 375 | |||
| 376 | sub import_children | ||
| 377 | { | ||
| 378 | my $self = shift; | ||
| 379 | my $source = shift or return FALSE; | ||
| 380 | my $count = 0; | ||
| 381 | foreach my $e ($source->children(shift)) | ||
| 382 | { | ||
| 383 | $e->clone->paste_last_child($self); $count++ | ||
| 384 | } | ||
| 385 | return $count; | ||
| 386 | } | ||
| 387 | |||
| 388 | sub substitute_children | ||
| 389 | { | ||
| 390 | my $self = shift; | ||
| 391 | my $source = shift or return FALSE; | ||
| 392 | $self->delete_children(@_); | ||
| 393 | return $self->import_children($source, @_); | ||
| 394 | } | ||
| 395 | |||
| 396 | sub replace_child | ||
| 397 | { | ||
| 398 | my $self = shift; | ||
| 399 | my $tag = $self->normalize_name(shift) or return undef; | ||
| 400 | $self->delete_child($tag); | ||
| 401 | my $child = $self->insert_element($tag); | ||
| 402 | $child->set_text(shift); | ||
| 403 | $child->set_attributes(@_); | ||
| 404 | return $child; | ||
| 405 | } | ||
| 406 | |||
| 407 | sub next | ||
| 408 | { | ||
| 409 | my $self = shift; | ||
| 410 | my $context = shift; | ||
| 411 | my $tag = $self->get_tag; | ||
| 412 | unless ($context) | ||
| 413 | { | ||
| 414 | return $self->next_sibling($tag); | ||
| 415 | } | ||
| 416 | return $self->next_elt($context, $tag); | ||
| 417 | } | ||
| 418 | |||
| 419 | sub previous | ||
| 420 | { | ||
| 421 | my $self = shift; | ||
| 422 | my $context = shift; | ||
| 423 | my $tag = $self->get_tag; | ||
| 424 | unless ($context) | ||
| 425 | { | ||
| 426 | return $self->previous_sibling($tag); | ||
| 427 | } | ||
| 428 | return $self->prev_elt($self, $tag); | ||
| 429 | } | ||
| 430 | |||
| 431 | sub get_class | ||
| 432 | { | ||
| 433 | my $self = shift; | ||
| 434 | return Scalar::Util::blessed($self); | ||
| 435 | } | ||
| 436 | |||
| 437 | sub get_children_elements | ||
| 438 | { | ||
| 439 | my $self = shift; | ||
| 440 | return $self->children(qr'^[^#]'); | ||
| 441 | } | ||
| 442 | |||
| 443 | sub get_descendant_elements | ||
| 444 | { | ||
| 445 | my $self = shift; | ||
| 446 | return $self->descendants(qr'^[^#]'); | ||
| 447 | } | ||
| 448 | |||
| 449 | sub group | ||
| 450 | { | ||
| 451 | my $self = shift; | ||
| 452 | my @elts = @_; | ||
| 453 | $_->move(last_child => $self) for @elts; | ||
| 454 | } | ||
| 455 | |||
| 456 | sub node_info | ||
| 457 | { | ||
| 458 | my $self = shift; | ||
| 459 | my %i = (); | ||
| 460 | $i{text} = $self->_get_text; | ||
| 461 | $i{size} = length($i{text}); | ||
| 462 | $i{tag} = $self->tag; | ||
| 463 | $i{class} = $self->get_class; | ||
| 464 | $i{attributes} = $self->get_attributes; | ||
| 465 | return %i; | ||
| 466 | } | ||
| 467 | |||
| 468 | sub has_text | ||
| 469 | { | ||
| 470 | my $self = shift; | ||
| 471 | return $self->has_child(TEXT_SEGMENT) ? TRUE : FALSE; | ||
| 472 | } | ||
| 473 | |||
| 474 | sub is_text_container | ||
| 475 | { | ||
| 476 | my $self = shift; | ||
| 477 | my $name = $self->tag; | ||
| 478 | return $name =~ /^text:(p|h|span)$/ ? TRUE : FALSE; | ||
| 479 | } | ||
| 480 | |||
| 481 | sub normalize_name | ||
| 482 | { | ||
| 483 | my $self = shift; | ||
| 484 | my $name = shift // return undef; | ||
| 485 | $name =~ s/^\s+//; | ||
| 486 | $name =~ s/\s+$//; | ||
| 487 | return $name if $name =~ /^</; | ||
| 488 | $name .= ' name' if $name eq 'style'; | ||
| 489 | if ($name && ! ref $name) | ||
| 490 | { | ||
| 491 | unless ($name =~ /[:#]/) | ||
| 492 | { | ||
| 493 | my $prefix = $self->ns_prefix; | ||
| 494 | $name = $prefix . ':' . $name if $prefix; | ||
| 495 | } | ||
| 496 | $name =~ s/[ _]/-/g; | ||
| 497 | } | ||
| 498 | return $name; | ||
| 499 | } | ||
| 500 | |||
| 501 | sub repeat | ||
| 502 | { | ||
| 503 | my $self = shift; | ||
| 504 | unless ($self->parent) | ||
| 505 | { | ||
| 506 | alert "Repeat not allowed for root elements"; | ||
| 507 | return FALSE; | ||
| 508 | } | ||
| 509 | my $r = shift; | ||
| 510 | return undef unless defined $r; | ||
| 511 | |||
| 512 | my $count = 0; | ||
| 513 | while ($r > 1) | ||
| 514 | { | ||
| 515 | my $elt = $self->clone; | ||
| 516 | $elt->paste_after($self); | ||
| 517 | $count++; $r--; | ||
| 518 | } | ||
| 519 | return $count; | ||
| 520 | } | ||
| 521 | |||
| 522 | sub set_lpod_mark | ||
| 523 | { | ||
| 524 | state $count = 0; | ||
| 525 | my $self = shift; | ||
| 526 | my %opt = @_; | ||
| 527 | |||
| 528 | my $id; | ||
| 529 | if (defined $opt{id}) | ||
| 530 | { | ||
| 531 | $id = $opt{id}; delete $opt{id}; | ||
| 532 | } | ||
| 533 | else | ||
| 534 | { | ||
| 535 | $id = lpod_common->new_id; | ||
| 536 | } | ||
| 537 | |||
| 538 | my $e = $self->insert_element($ODF::lpOD::Common::LPOD_MARK, %opt); | ||
| 539 | $e->set_attribute($ODF::lpOD::Common::LPOD_ID, $id); | ||
| 540 | return $id; | ||
| 541 | } | ||
| 542 | |||
| 543 | sub ro | ||
| 544 | { | ||
| 545 | my $self = shift; | ||
| 546 | my $ro = shift; | ||
| 547 | unless (defined $ro) | ||
| 548 | { | ||
| 549 | return $self->att('#lpod:ro') // FALSE; | ||
| 550 | } | ||
| 551 | elsif (is_true($ro)) | ||
| 552 | { | ||
| 553 | $self->set_att('#lpod:ro', TRUE); | ||
| 554 | } | ||
| 555 | else | ||
| 556 | { | ||
| 557 | $self->del_att('#lpod:ro') if $self->att('#lpod:ro'); | ||
| 558 | return undef; | ||
| 559 | } | ||
| 560 | } | ||
| 561 | |||
| 562 | sub rw | ||
| 563 | { | ||
| 564 | my $self = shift; | ||
| 565 | my $rw = shift; | ||
| 566 | unless (defined $rw) | ||
| 567 | { | ||
| 568 | return is_false($self->att('#lpod:ro')); | ||
| 569 | } | ||
| 570 | elsif (is_true($rw)) | ||
| 571 | { | ||
| 572 | $self->del_att('#lpod:ro') if $self->att('#lpod:ro'); | ||
| 573 | return TRUE; | ||
| 574 | } | ||
| 575 | elsif (is_false($rw)) | ||
| 576 | { | ||
| 577 | $self->set_att('#lpod:ro', TRUE); | ||
| 578 | return FALSE; | ||
| 579 | } | ||
| 580 | } | ||
| 581 | |||
| 582 | sub get_lpod_mark | ||
| 583 | { | ||
| 584 | my $self = shift; | ||
| 585 | my $id = shift; | ||
| 586 | return $self->get_element | ||
| 587 | ( | ||
| 588 | $ODF::lpOD::Common::LPOD_MARK, | ||
| 589 | attribute => $ODF::lpOD::Common::LPOD_ID, | ||
| 590 | value => $id | ||
| 591 | ); | ||
| 592 | } | ||
| 593 | |||
| 594 | sub remove_lpod_mark | ||
| 595 | { | ||
| 596 | my $self = shift; | ||
| 597 | my $mark = $self->get_lpod_mark(shift); | ||
| 598 | if ($mark) | ||
| 599 | { | ||
| 600 | $mark->delete; return TRUE; | ||
| 601 | } | ||
| 602 | return FALSE; | ||
| 603 | } | ||
| 604 | |||
| 605 | sub remove_lpod_marks | ||
| 606 | { | ||
| 607 | my $self = shift; | ||
| 608 | $_->delete() | ||
| 609 | for $self->get_elements($ODF::lpOD::Common::LPOD_MARK); | ||
| 610 | } | ||
| 611 | |||
| 612 | sub set_lpod_id | ||
| 613 | { | ||
| 614 | my $self = shift; | ||
| 615 | return $self->set_att($ODF::lpOD::Common::LPOD_ID, shift); | ||
| 616 | } | ||
| 617 | |||
| 618 | sub remove_lpod_id | ||
| 619 | { | ||
| 620 | my $self = shift; | ||
| 621 | return $self->del_att($ODF::lpOD::Common::LPOD_ID); | ||
| 622 | } | ||
| 623 | |||
| 624 | sub strip_lpod_id | ||
| 625 | { | ||
| 626 | my $self = shift; | ||
| 627 | return $self->strip_att($ODF::lpOD::Common::LPOD_ID); | ||
| 628 | } | ||
| 629 | |||
| 630 | sub lpod_part | ||
| 631 | { | ||
| 632 | my $self = shift; | ||
| 633 | my $part = shift; | ||
| 634 | if ($part) | ||
| 635 | { | ||
| 636 | return $self->set_att($ODF::lpOD::Common::LPOD_PART, $part); | ||
| 637 | } | ||
| 638 | else | ||
| 639 | { | ||
| 640 | return | ||
| 641 | $self->att($ODF::lpOD::Common::LPOD_PART) || | ||
| 642 | $self->root->att($ODF::lpOD::Common::LPOD_PART); | ||
| 643 | } | ||
| 644 | } | ||
| 645 | |||
| 646 | sub document | ||
| 647 | { | ||
| 648 | my $self = shift; | ||
| 649 | my $part = $self->lpod_part or return undef; | ||
| 650 | return $part->document; | ||
| 651 | } | ||
| 652 | |||
| 653 | sub document_type | ||
| 654 | { | ||
| 655 | my $self = shift; | ||
| 656 | my $doc = $self->document or return undef; | ||
| 657 | return $doc->get_type; | ||
| 658 | } | ||
| 659 | |||
| 660 | #----------------------------------------------------------------------------- | ||
| 661 | |||
| 662 | sub text_segments | ||
| 663 | { | ||
| 664 | my $self = shift; | ||
| 665 | my %opt = | ||
| 666 | ( | ||
| 667 | deep => FALSE, | ||
| 668 | @_ | ||
| 669 | ); | ||
| 670 | return (is_true($opt{deep})) ? | ||
| 671 | $self->descendants(TEXT_SEGMENT) : | ||
| 672 | $self->children(TEXT_SEGMENT); | ||
| 673 | } | ||
| 674 | |||
| 675 | sub search_in_text_segment | ||
| 676 | { | ||
| 677 | my $self = shift; | ||
| 678 | unless ($self->is_text) | ||
| 679 | { | ||
| 680 | alert("Not in text segment"); | ||
| 681 | return undef; | ||
| 682 | } | ||
| 683 | return search_string($self->get_text, @_); | ||
| 684 | } | ||
| 685 | |||
| 686 | sub replace_in_text_segment | ||
| 687 | { | ||
| 688 | my $self = shift; | ||
| 689 | my $expr = shift; | ||
| 690 | my $repl = shift; | ||
| 691 | |||
| 692 | my ($content, $change_count) = search_string | ||
| 693 | ($self->get_text, $expr, replace => $repl, @_); | ||
| 694 | $self->set_text($content) if $change_count; | ||
| 695 | return $change_count; | ||
| 696 | } | ||
| 697 | |||
| 698 | #--- generic element retrieval method ---------------------------------------- | ||
| 699 | |||
| 700 | sub _get_elements | ||
| 701 | { | ||
| 702 | my $self = shift; | ||
| 703 | my $tag = shift; | ||
| 704 | if (ref $tag) | ||
| 705 | { | ||
| 706 | return $self->descendants($tag); | ||
| 707 | } | ||
| 708 | my %opt = | ||
| 709 | ( | ||
| 710 | content => undef, | ||
| 711 | attribute => undef, | ||
| 712 | position => undef, | ||
| 713 | @_ | ||
| 714 | ); | ||
| 715 | $tag = $self->normalize_name($tag); | ||
| 716 | my $xpath = './/' . ($tag // ""); | ||
| 717 | |||
| 718 | if (defined $opt{attribute}) | ||
| 719 | { | ||
| 720 | my $a = $opt{attribute}; | ||
| 721 | my $v = input_conversion($opt{value}); | ||
| 722 | $a =~ s/[ _]/-/g; | ||
| 723 | unless ($a =~ /:/) | ||
| 724 | { | ||
| 725 | $tag =~ /^(.*):/; $a = $1 . ':' . $a; | ||
| 726 | } | ||
| 727 | $xpath .= '[@' . $a . '="' . $v . '"]'; | ||
| 728 | } | ||
| 729 | |||
| 730 | my $pos = $opt{position}; | ||
| 731 | my $expr = $opt{content}; | ||
| 732 | unless (defined $opt{content}) | ||
| 733 | { | ||
| 734 | return defined $pos ? | ||
| 735 | $self->get_xpath($xpath, $pos) : | ||
| 736 | $self->get_xpath($xpath); | ||
| 737 | } | ||
| 738 | else | ||
| 739 | { | ||
| 740 | my $elt; | ||
| 741 | my @elts = (); | ||
| 742 | my $count = 0; | ||
| 743 | unless (defined $pos) | ||
| 744 | { | ||
| 745 | foreach $elt ($self->get_xpath($xpath)) | ||
| 746 | { | ||
| 747 | push @elts, $elt if $elt->count_matches($expr); | ||
| 748 | } | ||
| 749 | return @elts; | ||
| 750 | } | ||
| 751 | elsif ($pos >= 0) | ||
| 752 | { | ||
| 753 | foreach $elt ($self->get_xpath($xpath)) | ||
| 754 | { | ||
| 755 | if ($elt->count_matches($expr)) | ||
| 756 | { | ||
| 757 | $count++; | ||
| 758 | return $elt if $count > $pos; | ||
| 759 | } | ||
| 760 | } | ||
| 761 | return undef; | ||
| 762 | } | ||
| 763 | else | ||
| 764 | { | ||
| 765 | foreach $elt ($self->get_xpath($xpath)) | ||
| 766 | { | ||
| 767 | push @elts, $elt if $elt->count_matches($expr); | ||
| 768 | } | ||
| 769 | my $size = scalar @elts; | ||
| 770 | return ($size >= abs($pos)) ? $elts[$pos] : undef; | ||
| 771 | } | ||
| 772 | } | ||
| 773 | } | ||
| 774 | |||
| 775 | sub get_element | ||
| 776 | { | ||
| 777 | my $self = shift; | ||
| 778 | my $tag = shift; | ||
| 779 | my %opt = @_; | ||
| 780 | $opt{position} //= 0; | ||
| 781 | if ($opt{bookmark}) | ||
| 782 | { | ||
| 783 | return $self->get_element_by_bookmark | ||
| 784 | ($opt{bookmark}, tag => $tag); | ||
| 785 | } | ||
| 786 | return $self->_get_elements($tag, %opt); | ||
| 787 | } | ||
| 788 | |||
| 789 | sub get_element_by_id | ||
| 790 | { | ||
| 791 | my $self = shift; | ||
| 792 | my $tag = shift; | ||
| 793 | return $self->get_element($tag, attribute => 'id', value => shift); | ||
| 794 | } | ||
| 795 | |||
| 796 | sub get_element_by_name | ||
| 797 | { | ||
| 798 | my $self = shift; | ||
| 799 | my $name = shift; | ||
| 800 | unless ($name) | ||
| 801 | { | ||
| 802 | alert "Missing object name"; return undef; | ||
| 803 | } | ||
| 804 | return $self->get_element($name, attribute => 'name', value => shift); | ||
| 805 | } | ||
| 806 | |||
| 807 | sub get_elements | ||
| 808 | { | ||
| 809 | my $self = shift; | ||
| 810 | my $tag = shift; | ||
| 811 | my %opt = @_; | ||
| 812 | delete $opt{position}; | ||
| 813 | return $self->_get_elements($tag, %opt); | ||
| 814 | } | ||
| 815 | |||
| 816 | #--- specific unnamed element retrieval methods ------------------------------ | ||
| 817 | |||
| 818 | sub get_text_element | ||
| 819 | { | ||
| 820 | my $self = shift; | ||
| 821 | my %opt = @_; | ||
| 822 | my $type = $opt{type} // 'p'; | ||
| 823 | delete $opt{type}; | ||
| 824 | $type = 'text:' . $type unless $type =~ /:/; | ||
| 825 | |||
| 826 | if ($opt{bookmark}) | ||
| 827 | { | ||
| 828 | return $self->get_element_by_bookmark | ||
| 829 | ($opt{bookmark}, tag => $type); | ||
| 830 | } | ||
| 831 | unless (defined $opt{style}) | ||
| 832 | { | ||
| 833 | return $self->get_element($type, %opt); | ||
| 834 | } | ||
| 835 | else | ||
| 836 | { | ||
| 837 | return $self->get_element | ||
| 838 | ( | ||
| 839 | $type, | ||
| 840 | attribute => 'style name', | ||
| 841 | value => $opt{style}, | ||
| 842 | position => $opt{position}, | ||
| 843 | content => $opt{content} | ||
| 844 | ); | ||
| 845 | } | ||
| 846 | } | ||
| 847 | |||
| 848 | sub get_paragraph | ||
| 849 | { | ||
| 850 | my $self = shift; | ||
| 851 | return $self->get_text_element(type => 'p', @_); | ||
| 852 | } | ||
| 853 | |||
| 854 | sub get_text_span | ||
| 855 | { | ||
| 856 | my $self = shift; | ||
| 857 | return $self->get_text_element(type => 'span', @_); | ||
| 858 | } | ||
| 859 | |||
| 860 | sub get_parent_paragraph | ||
| 861 | { | ||
| 862 | my $self = shift; | ||
| 863 | return $self->parent(qr'text:(p|h)'); | ||
| 864 | } | ||
| 865 | |||
| 866 | sub get_text_elements | ||
| 867 | { | ||
| 868 | my $self = shift; | ||
| 869 | my %opt = @_; | ||
| 870 | my $type = $opt{type} // 'p'; | ||
| 871 | delete $opt{type}; | ||
| 872 | $type = 'text:' . $type unless $type =~ /:/; | ||
| 873 | |||
| 874 | if ($opt{style}) | ||
| 875 | { | ||
| 876 | $opt{attribute} = 'style name'; | ||
| 877 | $opt{value} = $opt{style}; | ||
| 878 | delete $opt{style}; | ||
| 879 | } | ||
| 880 | return $self->get_elements($type, %opt); | ||
| 881 | } | ||
| 882 | |||
| 883 | sub get_paragraphs | ||
| 884 | { | ||
| 885 | my $self = shift; | ||
| 886 | return $self->get_text_elements(type => 'p', @_); | ||
| 887 | } | ||
| 888 | |||
| 889 | sub get_text_spans | ||
| 890 | { | ||
| 891 | my $self = shift; | ||
| 892 | return $self->get_text_elements(type => 'span', @_); | ||
| 893 | } | ||
| 894 | |||
| 895 | sub get_heading | ||
| 896 | { | ||
| 897 | my $self = shift; | ||
| 898 | my %opt = @_; | ||
| 899 | if ($opt{bookmark}) | ||
| 900 | { | ||
| 901 | return $self->get_element_by_bookmark | ||
| 902 | ($opt{bookmark}, tag => 'text:h'); | ||
| 903 | } | ||
| 904 | if (defined $opt{level}) | ||
| 905 | { | ||
| 906 | $opt{attribute} = 'outline level'; | ||
| 907 | $opt{value} = $opt{level}; | ||
| 908 | delete $opt{level}; | ||
| 909 | } | ||
| 910 | return $self->get_element('text:h', %opt); | ||
| 911 | } | ||
| 912 | |||
| 913 | sub get_headings | ||
| 914 | { | ||
| 915 | my $self = shift; | ||
| 916 | my %opt = @_; | ||
| 917 | unless (is_true($opt{all})) | ||
| 918 | { | ||
| 919 | if (defined $opt{level}) | ||
| 920 | { | ||
| 921 | $opt{attribute} = 'outline level'; | ||
| 922 | $opt{value} = $opt{level}; | ||
| 923 | delete $opt{level}; | ||
| 924 | } | ||
| 925 | return $self->get_elements('text:h', %opt); | ||
| 926 | } | ||
| 927 | else | ||
| 928 | { | ||
| 929 | unless (defined $opt{level}) | ||
| 930 | { | ||
| 931 | return $self->get_elements('text:h'); | ||
| 932 | } | ||
| 933 | my @headings = (); | ||
| 934 | my $h = $self->first_child('text:h'); | ||
| 935 | while ($h) | ||
| 936 | { | ||
| 937 | my $l = $h->get_level; | ||
| 938 | push @headings, $h if $l > 0 and $l <= $opt{level}; | ||
| 939 | $h = $h->next_sibling('text:h'); | ||
| 940 | } | ||
| 941 | return @headings; | ||
| 942 | } | ||
| 943 | } | ||
| 944 | |||
| 945 | sub get_hyperlinks | ||
| 946 | { | ||
| 947 | my $self = shift; | ||
| 948 | my %opt = @_; | ||
| 949 | my $type = $opt{type}; | ||
| 950 | delete $opt{type}; | ||
| 951 | unless ($type) | ||
| 952 | { | ||
| 953 | return ( | ||
| 954 | $self->get_hyperlinks(type => 'text', %opt), | ||
| 955 | $self->get_hyperlinks(type => 'draw', %opt) | ||
| 956 | ); | ||
| 957 | } | ||
| 958 | if (defined $opt{url}) | ||
| 959 | { | ||
| 960 | $opt{attribute} = 'xlink:href'; | ||
| 961 | $opt{value} = $opt{url}; | ||
| 962 | delete $opt{url}; | ||
| 963 | } | ||
| 964 | return $self->get_elements("$type:a", %opt); | ||
| 965 | } | ||
| 966 | |||
| 967 | sub get_list | ||
| 968 | { | ||
| 969 | my $self = shift; | ||
| 970 | return $self->get_element('text:list', @_); | ||
| 971 | } | ||
| 972 | |||
| 973 | sub get_list_by_id | ||
| 974 | { | ||
| 975 | my $self = shift; | ||
| 976 | return $self->get_list(attribute => 'xml:id', value => shift); | ||
| 977 | } | ||
| 978 | |||
| 979 | sub get_lists | ||
| 980 | { | ||
| 981 | my $self = shift; | ||
| 982 | return $self->get_elements('text:list', @_); | ||
| 983 | } | ||
| 984 | |||
| 985 | sub get_fields | ||
| 986 | { | ||
| 987 | my $self = shift; | ||
| 988 | my $type = shift; | ||
| 989 | unless ($type) | ||
| 990 | { | ||
| 991 | my @elts; | ||
| 992 | for (ODF::lpOD::TextField->types) | ||
| 993 | { | ||
| 994 | push @elts, $self->get_fields($_); | ||
| 995 | } | ||
| 996 | return @elts; | ||
| 997 | } | ||
| 998 | return $self->get_elements('text:' . $type); | ||
| 999 | } | ||
| 1000 | |||
| 1001 | #--- table retrieval --------------------------------------------------------- | ||
| 1002 | |||
| 1003 | sub get_table | ||
| 1004 | { | ||
| 1005 | my $self = shift; | ||
| 1006 | my $arg = shift // 0; | ||
| 1007 | return is_numeric($arg) ? | ||
| 1008 | $self->get_table_by_position($arg, @_) : | ||
| 1009 | $self->get_table_by_name($arg, @_); | ||
| 1010 | } | ||
| 1011 | |||
| 1012 | sub get_parent_table | ||
| 1013 | { | ||
| 1014 | my $self = shift; | ||
| 1015 | return $self->parent('table:table'); | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | sub get_parent_cell | ||
| 1019 | { | ||
| 1020 | my $self = shift; | ||
| 1021 | return $self->parent('table:table-cell'); | ||
| 1022 | } | ||
| 1023 | |||
| 1024 | sub get_tables | ||
| 1025 | { | ||
| 1026 | my $self = shift; | ||
| 1027 | return $self->get_elements('table:table', @_); | ||
| 1028 | } | ||
| 1029 | |||
| 1030 | sub get_table_by_name | ||
| 1031 | { | ||
| 1032 | my $self = shift; | ||
| 1033 | my $name = shift; | ||
| 1034 | return $self->get_element_by_name('table:table', $name); | ||
| 1035 | } | ||
| 1036 | |||
| 1037 | sub get_table_by_position | ||
| 1038 | { | ||
| 1039 | my $self = shift; | ||
| 1040 | my $position = shift || 0; | ||
| 1041 | return $self->get_element('table:table', position => $position); | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | sub get_table_by_content | ||
| 1045 | { | ||
| 1046 | my $self = shift; | ||
| 1047 | my $expr = shift; | ||
| 1048 | unless (defined $expr) | ||
| 1049 | { | ||
| 1050 | alert "Missing search expression"; | ||
| 1051 | return FALSE; | ||
| 1052 | } | ||
| 1053 | foreach my $t ($self->get_tables(@_)) | ||
| 1054 | { | ||
| 1055 | foreach my $n ($t->descendants(TEXT_SEGMENT)) | ||
| 1056 | { | ||
| 1057 | my $text = $n->get_text() or next; | ||
| 1058 | return $t; | ||
| 1059 | } | ||
| 1060 | } | ||
| 1061 | return FALSE; | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | #--- check & retrieval tools for bookmarks, index marks ---------------------- | ||
| 1065 | |||
| 1066 | sub get_position_mark | ||
| 1067 | { | ||
| 1068 | my $self = shift; | ||
| 1069 | my $tag = $self->normalize_name(shift); | ||
| 1070 | my $name = shift; | ||
| 1071 | my $role = shift; | ||
| 1072 | unless ($name) | ||
| 1073 | { | ||
| 1074 | alert ("Name is mandatory for position mark retrieval"); | ||
| 1075 | return FALSE; | ||
| 1076 | } | ||
| 1077 | my $attr = $tag =~ /bookmark|reference-mark/ ? | ||
| 1078 | 'text:name' : 'text:id'; | ||
| 1079 | my %opt = (attribute => $attr, value => $name); | ||
| 1080 | if (!defined $role) | ||
| 1081 | { | ||
| 1082 | my $single = $self->get_element($tag, %opt); | ||
| 1083 | unless ($single) | ||
| 1084 | { | ||
| 1085 | my $start = $self->get_element | ||
| 1086 | ($tag . '-start', %opt); | ||
| 1087 | my $end = $self->get_element | ||
| 1088 | ($tag . '-end', %opt); | ||
| 1089 | return wantarray ? ($start, $end) : $start; | ||
| 1090 | } | ||
| 1091 | return $single; | ||
| 1092 | } | ||
| 1093 | elsif ($role =~ /^(start|end)$/) | ||
| 1094 | { | ||
| 1095 | return $self->get_element($tag . '-' . $_, %opt); | ||
| 1096 | } | ||
| 1097 | else | ||
| 1098 | { | ||
| 1099 | alert "Wrong role $role"; | ||
| 1100 | return FALSE; | ||
| 1101 | } | ||
| 1102 | } | ||
| 1103 | |||
| 1104 | sub check_position_mark | ||
| 1105 | { | ||
| 1106 | my $self = shift; | ||
| 1107 | my $tag = shift; | ||
| 1108 | my $name = shift; | ||
| 1109 | |||
| 1110 | my %opt = (attribute => 'text:name', value => $name); | ||
| 1111 | |||
| 1112 | return TRUE if $self->get_element($tag, %opt); | ||
| 1113 | |||
| 1114 | my $start = $self->get_position_mark($tag, $name, 'start') | ||
| 1115 | or return FALSE; | ||
| 1116 | my $end = $self->get_position_mark($tag, $name, 'end') | ||
| 1117 | or return FALSE; | ||
| 1118 | return $start->before($end) ? TRUE : FALSE; | ||
| 1119 | } | ||
| 1120 | |||
| 1121 | sub remove_position_mark | ||
| 1122 | { | ||
| 1123 | my $self = shift; | ||
| 1124 | my $tag = shift; | ||
| 1125 | my $name = shift; | ||
| 1126 | |||
| 1127 | my %opt = (attribute => 'text:name', value => $name); | ||
| 1128 | |||
| 1129 | my $single = $self->get_element($tag, %opt); | ||
| 1130 | if ($single) | ||
| 1131 | { | ||
| 1132 | $single->delete; | ||
| 1133 | return TRUE; | ||
| 1134 | } | ||
| 1135 | |||
| 1136 | my $start = $self->get_position_mark($tag, $name, 'start') | ||
| 1137 | or return FALSE; | ||
| 1138 | my $end = $self->get_position_mark($tag, $name, 'end') | ||
| 1139 | or return FALSE; | ||
| 1140 | $start->delete; | ||
| 1141 | $end->delete; | ||
| 1142 | return TRUE; | ||
| 1143 | } | ||
| 1144 | |||
| 1145 | #--- text mark retrieval stuff ----------------------------------------------- | ||
| 1146 | |||
| 1147 | sub get_bookmark | ||
| 1148 | { | ||
| 1149 | my $self = shift; | ||
| 1150 | return $self->get_position_mark('text:bookmark', shift); | ||
| 1151 | } | ||
| 1152 | |||
| 1153 | sub get_bookmarks | ||
| 1154 | { | ||
| 1155 | my $self = shift; | ||
| 1156 | return $self->get_elements(qr'bookmark$|bookmark-start$'); | ||
| 1157 | } | ||
| 1158 | |||
| 1159 | sub get_reference_mark | ||
| 1160 | { | ||
| 1161 | my $self = shift; | ||
| 1162 | return $self->get_position_mark('text:reference-mark', shift); | ||
| 1163 | } | ||
| 1164 | |||
| 1165 | sub get_reference_marks | ||
| 1166 | { | ||
| 1167 | my $self = shift; | ||
| 1168 | return $self->get_elements(qr'reference-mark$|reference-mark-start$'); | ||
| 1169 | } | ||
| 1170 | |||
| 1171 | sub get_index_marks | ||
| 1172 | { | ||
| 1173 | my $self = shift; | ||
| 1174 | my $type = shift; | ||
| 1175 | |||
| 1176 | my $filter; | ||
| 1177 | if (!defined $type) | ||
| 1178 | { | ||
| 1179 | alert "Missing index mark type"; | ||
| 1180 | } | ||
| 1181 | elsif ($type eq "lexical" || $type eq "alphabetical") | ||
| 1182 | { | ||
| 1183 | $filter = 'alphabetical-index-mark'; | ||
| 1184 | } | ||
| 1185 | elsif ($type eq "toc") | ||
| 1186 | { | ||
| 1187 | $filter = 'toc-mark'; | ||
| 1188 | } | ||
| 1189 | elsif ($type eq "user") | ||
| 1190 | { | ||
| 1191 | $filter = 'user-index-mark'; | ||
| 1192 | } | ||
| 1193 | else | ||
| 1194 | { | ||
| 1195 | alert "Wrong index mark type"; | ||
| 1196 | } | ||
| 1197 | return FALSE unless $filter; | ||
| 1198 | $filter = $filter . '$|' . $filter . '-start$'; | ||
| 1199 | return $self->get_elements(qr($filter)); | ||
| 1200 | } | ||
| 1201 | |||
| 1202 | sub clean_marks | ||
| 1203 | { | ||
| 1204 | my $self = shift; | ||
| 1205 | my $count = 0; | ||
| 1206 | my ($tag, $start, $end, $att, $id); | ||
| 1207 | foreach $start ($self->get_elements(qr'mark-start$')) | ||
| 1208 | { | ||
| 1209 | $tag = $start->get_tag; | ||
| 1210 | $att = $tag =~ /bookmark/ ? 'text:name' : 'text:id'; | ||
| 1211 | $id = $start->get_attribute($att); | ||
| 1212 | unless ($id) | ||
| 1213 | { | ||
| 1214 | $start->delete; $count++; | ||
| 1215 | next; | ||
| 1216 | } | ||
| 1217 | $tag =~ s/start$/end/; | ||
| 1218 | $end = $self->get_element | ||
| 1219 | ($tag, attribute => $att, value => $id); | ||
| 1220 | unless ($end) | ||
| 1221 | { | ||
| 1222 | $start->delete; $count++; | ||
| 1223 | next; | ||
| 1224 | } | ||
| 1225 | unless ($start->before($end)) | ||
| 1226 | { | ||
| 1227 | $start->delete; $end->delete; $count += 2; | ||
| 1228 | } | ||
| 1229 | } | ||
| 1230 | foreach $end ($self->get_elements(qr'mark-end$')) | ||
| 1231 | { | ||
| 1232 | $tag = $end->get_tag; | ||
| 1233 | $att = $tag =~ /bookmark/ ? 'text:name' : 'text:id'; | ||
| 1234 | $id = $end->get_attribute($att); | ||
| 1235 | unless ($id) | ||
| 1236 | { | ||
| 1237 | $end->delete; $count++; | ||
| 1238 | next; | ||
| 1239 | } | ||
| 1240 | $tag =~ s/end$/start/; | ||
| 1241 | $start = $self->get_element | ||
| 1242 | ($tag, attribute => $att, value => $id); | ||
| 1243 | unless ($start) | ||
| 1244 | { | ||
| 1245 | $end->delete; $count++; | ||
| 1246 | next; | ||
| 1247 | } | ||
| 1248 | unless ($end->after($start)) | ||
| 1249 | { | ||
| 1250 | $start->delete; $end->delete; $count += 2; | ||
| 1251 | } | ||
| 1252 | } | ||
| 1253 | return $count; | ||
| 1254 | } | ||
| 1255 | |||
| 1256 | sub remove_bookmark | ||
| 1257 | { | ||
| 1258 | my $self = shift; | ||
| 1259 | return $self->remove_position_mark('text:bookmark', shift); | ||
| 1260 | } | ||
| 1261 | |||
| 1262 | sub check_bookmark | ||
| 1263 | { | ||
| 1264 | my $self = shift; | ||
| 1265 | return $self->check_position_mark('text:bookmark', shift); | ||
| 1266 | } | ||
| 1267 | |||
| 1268 | sub get_element_by_bookmark | ||
| 1269 | { | ||
| 1270 | my $self = shift; | ||
| 1271 | my $name = shift; | ||
| 1272 | my %opt = @_; | ||
| 1273 | |||
| 1274 | my $bookmark = $self->get_position_mark | ||
| 1275 | ('text:bookmark', $name, $opt{role}); | ||
| 1276 | unless ($bookmark) | ||
| 1277 | { | ||
| 1278 | alert("Bookmark not found"); return FALSE; | ||
| 1279 | } | ||
| 1280 | if ($opt{tag}) | ||
| 1281 | { | ||
| 1282 | return $bookmark->get_ancestor($opt{tag}); | ||
| 1283 | } | ||
| 1284 | return $bookmark->parent; | ||
| 1285 | } | ||
| 1286 | |||
| 1287 | sub get_paragraph_by_bookmark | ||
| 1288 | { | ||
| 1289 | my $self = shift; | ||
| 1290 | my $name = shift; | ||
| 1291 | my %opt = @_; | ||
| 1292 | $opt{tag} = qr'text:(p|h)'; | ||
| 1293 | return $self->get_element_by_bookmark($name, %opt); | ||
| 1294 | } | ||
| 1295 | |||
| 1296 | sub get_bookmark_text | ||
| 1297 | { | ||
| 1298 | my $self = shift; | ||
| 1299 | my ($start, $end) = $self->get_bookmark(shift); | ||
| 1300 | unless ($start && $end && $start->before($end)) | ||
| 1301 | { | ||
| 1302 | alert "The required bookmark in not defined in the context"; | ||
| 1303 | return undef; | ||
| 1304 | } | ||
| 1305 | my $text = ""; | ||
| 1306 | my $n = $start->next_elt($self, TEXT_SEGMENT); | ||
| 1307 | while ($n && $n->before($end)) | ||
| 1308 | { | ||
| 1309 | $text .= $n->get_text; | ||
| 1310 | $n = $n->next_elt($self, TEXT_SEGMENT); | ||
| 1311 | } | ||
| 1312 | return $text; | ||
| 1313 | } | ||
| 1314 | |||
| 1315 | sub remove_reference_mark | ||
| 1316 | { | ||
| 1317 | my $self = shift; | ||
| 1318 | return $self->remove_position_mark('text:reference-mark', shift); | ||
| 1319 | } | ||
| 1320 | |||
| 1321 | sub check_reference_mark | ||
| 1322 | { | ||
| 1323 | my $self = shift; | ||
| 1324 | return $self->check_position_mark('text:reference-mark', shift); | ||
| 1325 | } | ||
| 1326 | |||
| 1327 | sub get_bibliography_marks | ||
| 1328 | { | ||
| 1329 | my $self = shift; | ||
| 1330 | my $text = shift; | ||
| 1331 | return defined $text ? | ||
| 1332 | $self->get_elements | ||
| 1333 | ( | ||
| 1334 | 'text:bibliography-mark', | ||
| 1335 | attribute => 'identifier', | ||
| 1336 | value => $text | ||
| 1337 | ) | ||
| 1338 | : | ||
| 1339 | $self->get_elements('text:bibliography-mark'); | ||
| 1340 | } | ||
| 1341 | |||
| 1342 | #--- note retrieval ---------------------------------------------------------- | ||
| 1343 | |||
| 1344 | sub get_note | ||
| 1345 | { | ||
| 1346 | my $self = shift; | ||
| 1347 | my $id = shift; | ||
| 1348 | unless ($id) | ||
| 1349 | { | ||
| 1350 | alert "Missing note identifier"; return FALSE; | ||
| 1351 | } | ||
| 1352 | return $self->get_element( | ||
| 1353 | 'text:note', | ||
| 1354 | attribute => 'id', | ||
| 1355 | value => $id | ||
| 1356 | ); | ||
| 1357 | } | ||
| 1358 | |||
| 1359 | sub get_notes | ||
| 1360 | { | ||
| 1361 | my $self = shift; | ||
| 1362 | my %opt = process_options(@_); | ||
| 1363 | my $class = $opt{class} || $opt{note_class}; | ||
| 1364 | my $label = $opt{label}; | ||
| 1365 | my $citation = $opt{citation}; | ||
| 1366 | |||
| 1367 | my $xp = './/text:note'; | ||
| 1368 | $xp .= '[@text:note-class="' . $class . '"]' if defined $class; | ||
| 1369 | if (defined $label || defined $citation) | ||
| 1370 | { | ||
| 1371 | $xp .= '/text:note-citation'; | ||
| 1372 | $xp .= '[@text:label="' . $label . '"]' | ||
| 1373 | if defined $label; | ||
| 1374 | $xp .= '[string()="' . $citation . '"]' | ||
| 1375 | if defined $citation; | ||
| 1376 | my @result = (); | ||
| 1377 | foreach my $n ($self->get_xpath($xp)) | ||
| 1378 | { | ||
| 1379 | push @result, $n->parent; | ||
| 1380 | } | ||
| 1381 | return @result; | ||
| 1382 | } | ||
| 1383 | return $self->get_xpath($xp); | ||
| 1384 | } | ||
| 1385 | |||
| 1386 | sub get_annotations | ||
| 1387 | { | ||
| 1388 | my $self = shift; | ||
| 1389 | my %opt = @_; | ||
| 1390 | my $date = $opt{date}; | ||
| 1391 | my $author = $opt{author}; | ||
| 1392 | |||
| 1393 | my $xp = './/office:annotation'; | ||
| 1394 | $xp .= '[@dc:date="' . $date . '"]' if $date; | ||
| 1395 | $xp .= '[@dc:creator="' . $author . '"]' if $author; | ||
| 1396 | |||
| 1397 | return $self->get_xpath($xp); | ||
| 1398 | } | ||
| 1399 | |||
| 1400 | #--- tracked change retrieval ------------------------------------------------ | ||
| 1401 | |||
| 1402 | sub get_changes | ||
| 1403 | { | ||
| 1404 | my $self = shift; | ||
| 1405 | my %opt = @_; | ||
| 1406 | my $context = $self; | ||
| 1407 | |||
| 1408 | unless ($opt{date} || $opt{author}) | ||
| 1409 | { | ||
| 1410 | return $context->get_elements('text:changed-region'); | ||
| 1411 | } | ||
| 1412 | |||
| 1413 | my @r = (); | ||
| 1414 | foreach my $ci ($context->descendants('text:changed-region')) | ||
| 1415 | { | ||
| 1416 | my ($elt, $text); | ||
| 1417 | if ($opt{date}) | ||
| 1418 | { | ||
| 1419 | $elt = $ci->first_descendant('dc:date') or next; | ||
| 1420 | $text = $elt->get_text or next; | ||
| 1421 | if (ref $opt{date}) | ||
| 1422 | { | ||
| 1423 | my $start = ${opt{date}}[0]; | ||
| 1424 | my $end = ${opt{date}}[1]; | ||
| 1425 | next if $start && ($text lt $start); | ||
| 1426 | next if $end && ($text gt $end); | ||
| 1427 | } | ||
| 1428 | else | ||
| 1429 | { | ||
| 1430 | next unless $text eq $opt{date}; | ||
| 1431 | } | ||
| 1432 | } | ||
| 1433 | if ($opt{author}) | ||
| 1434 | { | ||
| 1435 | $elt = $ci->first_descendant('dc:creator') or next; | ||
| 1436 | $text = $elt->get_text; | ||
| 1437 | next unless $text eq $opt{author}; | ||
| 1438 | } | ||
| 1439 | push @r, $ci; | ||
| 1440 | } | ||
| 1441 | return @r; | ||
| 1442 | } | ||
| 1443 | |||
| 1444 | sub get_change | ||
| 1445 | { | ||
| 1446 | my $self = shift; | ||
| 1447 | return $self->get_element( | ||
| 1448 | 'text:changed-region', | ||
| 1449 | attribute => 'id', | ||
| 1450 | value => shift | ||
| 1451 | ); | ||
| 1452 | } | ||
| 1453 | |||
| 1454 | #--- section retrieval ------------------------------------------------------- | ||
| 1455 | |||
| 1456 | sub get_section | ||
| 1457 | { | ||
| 1458 | my $self = shift; | ||
| 1459 | return $self->get_element | ||
| 1460 | ('text:section', attribute => 'text:name', value => shift); | ||
| 1461 | } | ||
| 1462 | |||
| 1463 | sub get_sections | ||
| 1464 | { | ||
| 1465 | my $self = shift; | ||
| 1466 | return $self->get_elements('text:section', @_); | ||
| 1467 | } | ||
| 1468 | |||
| 1469 | sub get_parent_section | ||
| 1470 | { | ||
| 1471 | my $self = shift; | ||
| 1472 | return $self->parent('text:section'); | ||
| 1473 | } | ||
| 1474 | |||
| 1475 | #--- frame & draw page retrieval --------------------------------------------- | ||
| 1476 | |||
| 1477 | sub get_shape | ||
| 1478 | { | ||
| 1479 | my $self = shift; | ||
| 1480 | my $type = shift; | ||
| 1481 | $type = 'draw:' . $type unless $type =~ /:/; | ||
| 1482 | return $self->get_element( | ||
| 1483 | $type, attribute => 'draw:name', value => shift | ||
| 1484 | ); | ||
| 1485 | } | ||
| 1486 | |||
| 1487 | sub get_rectangle | ||
| 1488 | { | ||
| 1489 | my $self = shift; return $self->get_shape('rect', @_); | ||
| 1490 | } | ||
| 1491 | |||
| 1492 | sub get_rectangles | ||
| 1493 | { | ||
| 1494 | my $self = shift; return $self->get_elements('draw:rect', @_); | ||
| 1495 | } | ||
| 1496 | |||
| 1497 | sub get_ellipse | ||
| 1498 | { | ||
| 1499 | my $self = shift; return $self->get_shape('ellipse', @_); | ||
| 1500 | } | ||
| 1501 | |||
| 1502 | sub get_ellipses | ||
| 1503 | { | ||
| 1504 | my $self = shift; return $self->get_elements('draw:ellipse', @_); | ||
| 1505 | } | ||
| 1506 | |||
| 1507 | sub get_line | ||
| 1508 | { | ||
| 1509 | my $self = shift; return $self->get_shape('line', @_); | ||
| 1510 | } | ||
| 1511 | |||
| 1512 | sub get_lines | ||
| 1513 | { | ||
| 1514 | my $self = shift; return $self->get_elements('draw:line', @_); | ||
| 1515 | } | ||
| 1516 | |||
| 1517 | sub get_connector | ||
| 1518 | { | ||
| 1519 | my $self = shift; return $self->get_shape('connector', @_); | ||
| 1520 | } | ||
| 1521 | |||
| 1522 | sub get_connectors | ||
| 1523 | { | ||
| 1524 | my $self = shift; return $self->get_elements('draw:connector', @_); | ||
| 1525 | } | ||
| 1526 | |||
| 1527 | sub get_frame | ||
| 1528 | { | ||
| 1529 | my $self = shift; return $self->get_shape('frame', @_); | ||
| 1530 | } | ||
| 1531 | |||
| 1532 | sub get_parent_frame | ||
| 1533 | { | ||
| 1534 | my $self = shift; | ||
| 1535 | return $self->parent('draw:frame'); | ||
| 1536 | } | ||
| 1537 | |||
| 1538 | sub get_frames | ||
| 1539 | { | ||
| 1540 | my $self = shift; return $self->get_elements('draw:frame', @_); | ||
| 1541 | } | ||
| 1542 | |||
| 1543 | sub get_draw_page_by_position | ||
| 1544 | { | ||
| 1545 | my $self = shift; | ||
| 1546 | return $self->get_element('draw:page', position => shift); | ||
| 1547 | } | ||
| 1548 | |||
| 1549 | sub get_draw_page_by_name | ||
| 1550 | { | ||
| 1551 | my $self = shift; | ||
| 1552 | return $self->get_element( | ||
| 1553 | 'draw:page', attribute => 'name', value => shift | ||
| 1554 | ); | ||
| 1555 | } | ||
| 1556 | |||
| 1557 | sub get_draw_page | ||
| 1558 | { | ||
| 1559 | my $self = shift; | ||
| 1560 | my $arg = shift; | ||
| 1561 | return $self->get_element( | ||
| 1562 | 'draw:page', attribute => 'id', value => $arg | ||
| 1563 | ) || | ||
| 1564 | $self->get_draw_page_by_name($arg); | ||
| 1565 | } | ||
| 1566 | |||
| 1567 | sub get_draw_pages | ||
| 1568 | { | ||
| 1569 | my $self = shift; return $self->get_elements('draw:page', @_); | ||
| 1570 | } | ||
| 1571 | |||
| 1572 | #----------------------------------------------------------------------------- | ||
| 1573 | |||
| 1574 | sub get_attribute | ||
| 1575 | { | ||
| 1576 | my $self = shift; | ||
| 1577 | my $name = $self->normalize_name(shift) or return undef; | ||
| 1578 | my $value = $self->att($name); | ||
| 1579 | return output_conversion($value); | ||
| 1580 | } | ||
| 1581 | |||
| 1582 | sub get_attributes | ||
| 1583 | { | ||
| 1584 | my $self = shift; | ||
| 1585 | return undef unless $self->is_element; | ||
| 1586 | my $atts = $self->atts or return undef; | ||
| 1587 | my %attr = %{$atts}; | ||
| 1588 | my %result = (); | ||
| 1589 | $result{$_} = output_conversion($attr{$_}) for keys %attr; | ||
| 1590 | |||
| 1591 | return wantarray ? %result : { %result }; | ||
| 1592 | } | ||
| 1593 | |||
| 1594 | sub set_attribute | ||
| 1595 | { | ||
| 1596 | my $self = shift; | ||
| 1597 | my $name = $self->normalize_name(shift) or return undef; | ||
| 1598 | my $value = input_conversion(shift); | ||
| 1599 | if ($name =~ /color$/) | ||
| 1600 | { | ||
| 1601 | $value = color_code($value); | ||
| 1602 | } | ||
| 1603 | return defined $value ? | ||
| 1604 | $self->set_att($name, $value) : $self->del_attribute($name); | ||
| 1605 | } | ||
| 1606 | |||
| 1607 | sub set_boolean_attribute | ||
| 1608 | { | ||
| 1609 | my $self = shift; | ||
| 1610 | my ($name, $value) = @_; | ||
| 1611 | $value = odf_boolean($value); | ||
| 1612 | return $self->set_attribute($name, $value); | ||
| 1613 | } | ||
| 1614 | |||
| 1615 | sub get_boolean_attribute | ||
| 1616 | { | ||
| 1617 | my $self = shift; | ||
| 1618 | my $value = $self->get_attribute(shift); | ||
| 1619 | if (!defined $value) | ||
| 1620 | { | ||
| 1621 | return undef; | ||
| 1622 | } | ||
| 1623 | elsif ($value eq 'true') | ||
| 1624 | { | ||
| 1625 | return TRUE; | ||
| 1626 | } | ||
| 1627 | elsif ($value eq 'false') | ||
| 1628 | { | ||
| 1629 | return FALSE; | ||
| 1630 | } | ||
| 1631 | else | ||
| 1632 | { | ||
| 1633 | alert("Improper ODF boolean"); | ||
| 1634 | return undef; | ||
| 1635 | } | ||
| 1636 | } | ||
| 1637 | |||
| 1638 | sub input_convert_attributes | ||
| 1639 | { | ||
| 1640 | my $self = shift; | ||
| 1641 | my $in = shift; | ||
| 1642 | my %out = (); | ||
| 1643 | my $prefix = $self->ns_prefix; | ||
| 1644 | foreach my $kin (keys %{$in}) | ||
| 1645 | { | ||
| 1646 | my $kout = $kin; | ||
| 1647 | unless ($kout =~ /:/) | ||
| 1648 | { | ||
| 1649 | $kout = $prefix . ':' . $kout; | ||
| 1650 | } | ||
| 1651 | $kout =~ s/ /-/g; | ||
| 1652 | $out{$kout} = input_conversion($in->{$kin}); | ||
| 1653 | } | ||
| 1654 | return wantarray ? %out : { %out }; | ||
| 1655 | } | ||
| 1656 | |||
| 1657 | sub set_attributes | ||
| 1658 | { | ||
| 1659 | my $self = shift; | ||
| 1660 | my $attr = shift or return undef; | ||
| 1661 | my %attr = ref $attr ? %{$attr} : ($attr, @_); | ||
| 1662 | |||
| 1663 | foreach my $k (keys %attr) | ||
| 1664 | { | ||
| 1665 | $self->set_attribute($k, $attr{$k}); | ||
| 1666 | } | ||
| 1667 | return $self->get_attributes; | ||
| 1668 | } | ||
| 1669 | |||
| 1670 | sub del_attribute | ||
| 1671 | { | ||
| 1672 | my $self = shift; | ||
| 1673 | my $name = $self->normalize_name(shift); | ||
| 1674 | return $self->att($name) ? $self->del_att($name) : FALSE; | ||
| 1675 | } | ||
| 1676 | |||
| 1677 | sub clear | ||
| 1678 | { | ||
| 1679 | my $self = shift; | ||
| 1680 | return $self->_set_text(''); | ||
| 1681 | } | ||
| 1682 | |||
| 1683 | sub get_text | ||
| 1684 | { | ||
| 1685 | my $self = shift; | ||
| 1686 | my %opt = (recursive => FALSE, @_); | ||
| 1687 | my $text = undef; | ||
| 1688 | unless ($self->is_element) | ||
| 1689 | { | ||
| 1690 | $text = $self->text; | ||
| 1691 | } | ||
| 1692 | elsif (is_true($opt{recursive})) | ||
| 1693 | { | ||
| 1694 | foreach my $t ($self->descendants(TEXT_SEGMENT)) | ||
| 1695 | { | ||
| 1696 | $text .= $t->text; | ||
| 1697 | } | ||
| 1698 | } | ||
| 1699 | else | ||
| 1700 | { | ||
| 1701 | $text = $self->text_only; | ||
| 1702 | } | ||
| 1703 | return output_conversion($text); | ||
| 1704 | } | ||
| 1705 | |||
| 1706 | sub set_text | ||
| 1707 | { | ||
| 1708 | my $self = shift; | ||
| 1709 | my $input = shift; | ||
| 1710 | return undef unless defined $input; | ||
| 1711 | |||
| 1712 | my $text = caller() ne 'XML::Twig::Elt' ? | ||
| 1713 | input_conversion($input) : $input; | ||
| 1714 | my $r = $self->_set_text($text); | ||
| 1715 | bless $_, 'ODF::lpOD::TextNode' for $self->children(TEXT_SEGMENT); | ||
| 1716 | return $r; | ||
| 1717 | } | ||
| 1718 | |||
| 1719 | sub get_text_content | ||
| 1720 | { | ||
| 1721 | my $self = shift; | ||
| 1722 | my $t = ""; | ||
| 1723 | foreach my $p ($self->descendants('text:p')) | ||
| 1724 | { | ||
| 1725 | $t .= ($p->get_text(@_) // ""); | ||
| 1726 | } | ||
| 1727 | return $t; | ||
| 1728 | } | ||
| 1729 | |||
| 1730 | sub set_text_content | ||
| 1731 | { | ||
| 1732 | my $self = shift; | ||
| 1733 | my $text = shift; | ||
| 1734 | my %opt = @_; | ||
| 1735 | |||
| 1736 | my @paragraphs = $self->descendants('text:p'); | ||
| 1737 | my $p = shift @paragraphs; | ||
| 1738 | unless (defined $p) | ||
| 1739 | { | ||
| 1740 | $p = ODF::lpOD::Element->create('text:p'); | ||
| 1741 | $p->paste_first_child($self); | ||
| 1742 | } | ||
| 1743 | else | ||
| 1744 | { | ||
| 1745 | $_->delete() for @paragraphs; | ||
| 1746 | } | ||
| 1747 | $p->set_style($opt{style}) if $opt{style}; | ||
| 1748 | return $p->set_text($text); | ||
| 1749 | } | ||
| 1750 | |||
| 1751 | sub get_family {} | ||
| 1752 | |||
| 1753 | sub get_name | ||
| 1754 | { | ||
| 1755 | my $self = shift; | ||
| 1756 | return $self->get_attribute('name'); | ||
| 1757 | } | ||
| 1758 | |||
| 1759 | sub set_name | ||
| 1760 | { | ||
| 1761 | my $self = shift; | ||
| 1762 | my $name = shift; | ||
| 1763 | return undef unless defined $name; | ||
| 1764 | return caller() eq 'XML::Twig::Elt' ? | ||
| 1765 | $self->set_tag($name) : | ||
| 1766 | $self->set_attribute('name' => $name); | ||
| 1767 | } | ||
| 1768 | |||
| 1769 | sub get_size | ||
| 1770 | { | ||
| 1771 | my $self = shift; | ||
| 1772 | my $sep = shift // ', '; | ||
| 1773 | my $w = $self->get_attribute('svg:width'); | ||
| 1774 | my $h = $self->get_attribute('svg:height'); | ||
| 1775 | return undef unless (defined $w && defined $h); | ||
| 1776 | return wantarray ? ($w, $h) : join $sep, $w, $h; | ||
| 1777 | } | ||
| 1778 | |||
| 1779 | sub set_size | ||
| 1780 | { | ||
| 1781 | my $self = shift; | ||
| 1782 | my ($w, $h) = input_2d_value(@_); | ||
| 1783 | $self->set_attribute('svg:width' => $w); | ||
| 1784 | $self->set_attribute('svg:height' => $h); | ||
| 1785 | return $self->get_size; | ||
| 1786 | } | ||
| 1787 | |||
| 1788 | sub get_display | ||
| 1789 | { | ||
| 1790 | my $self = shift; | ||
| 1791 | return is_true($self->get_attribute('display')); | ||
| 1792 | } | ||
| 1793 | |||
| 1794 | sub set_display | ||
| 1795 | { | ||
| 1796 | my $self = shift; | ||
| 1797 | return $self->set_attribute('display' => odf_boolean(shift)); | ||
| 1798 | } | ||
| 1799 | |||
| 1800 | sub get_position | ||
| 1801 | { | ||
| 1802 | my $self = shift; | ||
| 1803 | my $sep = shift // ', '; | ||
| 1804 | my $x = $self->get_attribute('svg:x'); | ||
| 1805 | my $y = $self->get_attribute('svg:y'); | ||
| 1806 | return undef unless (defined $x && defined $y); | ||
| 1807 | if (wantarray) | ||
| 1808 | { | ||
| 1809 | return ($x, $y); | ||
| 1810 | } | ||
| 1811 | else { | ||
| 1812 | my $r; | ||
| 1813 | $r = join $sep, $x, $y if (defined $x && defined $y); | ||
| 1814 | return $r; | ||
| 1815 | } | ||
| 1816 | } | ||
| 1817 | |||
| 1818 | sub set_position | ||
| 1819 | { | ||
| 1820 | my $self = shift; | ||
| 1821 | my ($x, $y) = input_2d_value(@_); | ||
| 1822 | $self->set_attribute('svg:x' => $x); | ||
| 1823 | $self->set_attribute('svg:y' => $y); | ||
| 1824 | return $self->get_position; | ||
| 1825 | } | ||
| 1826 | |||
| 1827 | sub get_url | ||
| 1828 | { | ||
| 1829 | my $self = shift; | ||
| 1830 | return $self->get_attribute('xlink:href'); | ||
| 1831 | } | ||
| 1832 | |||
| 1833 | sub set_url | ||
| 1834 | { | ||
| 1835 | my $self = shift; | ||
| 1836 | return $self->set_attribute('xlink:href' => shift); | ||
| 1837 | } | ||
| 1838 | |||
| 1839 | sub get_style | ||
| 1840 | { | ||
| 1841 | my $self = shift; | ||
| 1842 | return $self->get_attribute('style name'); | ||
| 1843 | } | ||
| 1844 | |||
| 1845 | sub set_style | ||
| 1846 | { | ||
| 1847 | my $self = shift; | ||
| 1848 | my $style = shift; | ||
| 1849 | my $name; | ||
| 1850 | if (ref $style) | ||
| 1851 | { | ||
| 1852 | if ($style->isa('ODF::lpOD::Style')) | ||
| 1853 | { | ||
| 1854 | $name = $style->get_name; | ||
| 1855 | } | ||
| 1856 | else | ||
| 1857 | { | ||
| 1858 | alert "Wrong style"; return undef; | ||
| 1859 | } | ||
| 1860 | } | ||
| 1861 | else | ||
| 1862 | { | ||
| 1863 | $name = $style; | ||
| 1864 | } | ||
| 1865 | return $self->set_attribute('style name' => $name); | ||
| 1866 | } | ||
| 1867 | |||
| 1868 | sub insert_element | ||
| 1869 | { | ||
| 1870 | my $self = shift; | ||
| 1871 | my $tag = $self->normalize_name(shift) or return undef; | ||
| 1872 | my %opt = | ||
| 1873 | ( | ||
| 1874 | position => 'FIRST_CHILD', | ||
| 1875 | @_ | ||
| 1876 | ); | ||
| 1877 | my $position = uc $opt{position}; | ||
| 1878 | $position =~ s/ /_/g; | ||
| 1879 | my $new_elt; | ||
| 1880 | if (ref $tag) | ||
| 1881 | { | ||
| 1882 | if ($tag->parent && $position ne 'PARENT') | ||
| 1883 | { | ||
| 1884 | alert "Element already belonging to a tree"; | ||
| 1885 | return FALSE; | ||
| 1886 | } | ||
| 1887 | $new_elt = $tag; | ||
| 1888 | } | ||
| 1889 | else | ||
| 1890 | { | ||
| 1891 | $new_elt = ODF::lpOD::Element->new($tag); | ||
| 1892 | } | ||
| 1893 | if (defined $opt{after}) | ||
| 1894 | { | ||
| 1895 | $new_elt->paste_after($opt{after}); return $new_elt; | ||
| 1896 | } | ||
| 1897 | elsif (defined $opt{before}) | ||
| 1898 | { | ||
| 1899 | $new_elt->paste_before($opt{before}); return $new_elt; | ||
| 1900 | } | ||
| 1901 | |||
| 1902 | if ($position =~ /^(FIRST_CHILD|LAST_CHILD)$/) | ||
| 1903 | { | ||
| 1904 | $new_elt->paste((lc $position) => $self); | ||
| 1905 | } | ||
| 1906 | elsif ($position eq 'NEXT_SIBLING') | ||
| 1907 | { | ||
| 1908 | $new_elt->paste_after($self); | ||
| 1909 | } | ||
| 1910 | elsif ($position eq 'PREV_SIBLING') | ||
| 1911 | { | ||
| 1912 | $new_elt->paste_before($self); | ||
| 1913 | } | ||
| 1914 | elsif ($position eq 'WITHIN') | ||
| 1915 | { | ||
| 1916 | if ($opt{offset}) | ||
| 1917 | { | ||
| 1918 | $new_elt->paste_within($self, $opt{offset}); | ||
| 1919 | } | ||
| 1920 | else | ||
| 1921 | { | ||
| 1922 | $new_elt->paste_first_child($self); | ||
| 1923 | } | ||
| 1924 | } | ||
| 1925 | elsif ($position eq 'PARENT') | ||
| 1926 | { | ||
| 1927 | if ($self->parent) | ||
| 1928 | { | ||
| 1929 | $new_elt->paste_before($self); | ||
| 1930 | $self->move(last_child => $new_elt); | ||
| 1931 | } | ||
| 1932 | else | ||
| 1933 | { | ||
| 1934 | $self->paste_last_child($new_elt); | ||
| 1935 | } | ||
| 1936 | } | ||
| 1937 | else | ||
| 1938 | { | ||
| 1939 | alert("Wrong insertion option"); | ||
| 1940 | return FALSE; | ||
| 1941 | } | ||
| 1942 | return $new_elt; | ||
| 1943 | } | ||
| 1944 | |||
| 1945 | sub append_element | ||
| 1946 | { | ||
| 1947 | my $self = shift; | ||
| 1948 | return $self->insert_element(shift, position => 'LAST_CHILD'); | ||
| 1949 | } | ||
| 1950 | |||
| 1951 | sub insert | ||
| 1952 | { | ||
| 1953 | my $self = shift; | ||
| 1954 | my $target = shift or return undef; | ||
| 1955 | return $target->insert_element($self, @_); | ||
| 1956 | } | ||
| 1957 | |||
| 1958 | sub append | ||
| 1959 | { | ||
| 1960 | my $self = shift; | ||
| 1961 | my $target = shift or return undef; | ||
| 1962 | return $target->append_element($self); | ||
| 1963 | } | ||
| 1964 | |||
| 1965 | sub set_comment | ||
| 1966 | { | ||
| 1967 | my $self = shift; | ||
| 1968 | unless ($self->parent) | ||
| 1969 | { | ||
| 1970 | alert "Not allowed in free element"; return undef; | ||
| 1971 | } | ||
| 1972 | my $text = input_conversion(shift); | ||
| 1973 | my $cmt = ODF::lpOD::Element->create('#COMMENT' => $text); | ||
| 1974 | $cmt->paste_before($self); | ||
| 1975 | return $cmt; | ||
| 1976 | } | ||
| 1977 | |||
| 1978 | sub set_annotation | ||
| 1979 | { | ||
| 1980 | my $self = shift; | ||
| 1981 | my $a = ODF::lpOD::Annotation->create(@_); | ||
| 1982 | $a->paste_first_child($self); | ||
| 1983 | return $a; | ||
| 1984 | } | ||
| 1985 | |||
| 1986 | sub serialize | ||
| 1987 | { | ||
| 1988 | my $self = shift; | ||
| 1989 | my %opt = process_options | ||
| 1990 | ( | ||
| 1991 | empty_tags => EMPTY_TAGS, | ||
| 1992 | @_ | ||
| 1993 | ); | ||
| 1994 | |||
| 1995 | $opt{pretty} //= ($opt{indent} // lpod->debug); | ||
| 1996 | $self->set_pretty_print(PRETTY_PRINT) if is_true($opt{pretty}); | ||
| 1997 | $self->set_empty_tag_style($opt{empty_tags}); | ||
| 1998 | delete @opt{qw(pretty indent empty_tags)}; | ||
| 1999 | return $self->sprint(%opt); | ||
| 2000 | } | ||
| 2001 | |||
| 2002 | #============================================================================= | ||
| 2003 | |||
| 2004 | sub _search_forward | ||
| 2005 | { | ||
| 2006 | my $self = shift; | ||
| 2007 | my $expr = shift; | ||
| 2008 | my %opt = (@_); | ||
| 2009 | |||
| 2010 | my $offset = $opt{offset}; | ||
| 2011 | |||
| 2012 | my ($target_node, $n, $start_pos, $end_pos, $match); | ||
| 2013 | if ($self->is_text) | ||
| 2014 | { | ||
| 2015 | $n = $self; | ||
| 2016 | } | ||
| 2017 | elsif ($opt{start_mark}) | ||
| 2018 | { | ||
| 2019 | if ($opt{start_mark}->is_text) | ||
| 2020 | { | ||
| 2021 | $n = $opt{start_mark}; | ||
| 2022 | } | ||
| 2023 | else | ||
| 2024 | { | ||
| 2025 | $n = $opt{start_mark} | ||
| 2026 | ->last_descendant | ||
| 2027 | ->next_elt($self, TEXT_SEGMENT); | ||
| 2028 | } | ||
| 2029 | } | ||
| 2030 | else | ||
| 2031 | { | ||
| 2032 | $n = $self->first_descendant(TEXT_SEGMENT); | ||
| 2033 | } | ||
| 2034 | my %info = $n->node_info() if $n; | ||
| 2035 | if (defined $offset) | ||
| 2036 | { | ||
| 2037 | while ($n && $offset >= $info{size}) | ||
| 2038 | { | ||
| 2039 | if ($opt{end_mark} && ! $n->before($opt{end_mark})) | ||
| 2040 | { | ||
| 2041 | $n = undef; last; | ||
| 2042 | } | ||
| 2043 | $offset -= $info{size}; | ||
| 2044 | $n = $n->next_elt($self, TEXT_SEGMENT); | ||
| 2045 | %info = $n->node_info() if $n; | ||
| 2046 | } | ||
| 2047 | } | ||
| 2048 | while ($n && !defined $start_pos) | ||
| 2049 | { | ||
| 2050 | if ($opt{end_mark} && ! $n->before($opt{end_mark})) | ||
| 2051 | { | ||
| 2052 | $n = undef; last; | ||
| 2053 | } | ||
| 2054 | unless (defined $expr) | ||
| 2055 | { | ||
| 2056 | $start_pos = $offset; | ||
| 2057 | $match = defined $opt{range} ? | ||
| 2058 | substr($info{text}, $start_pos, $opt{range}) : | ||
| 2059 | substr($info{text}, $start_pos); | ||
| 2060 | $end_pos = $start_pos + length($match); | ||
| 2061 | } | ||
| 2062 | else | ||
| 2063 | { | ||
| 2064 | ($start_pos, $end_pos, $match) = | ||
| 2065 | search_string | ||
| 2066 | ( | ||
| 2067 | $info{text}, | ||
| 2068 | $expr, | ||
| 2069 | offset => $offset, | ||
| 2070 | range => $opt{range} | ||
| 2071 | ); | ||
| 2072 | } | ||
| 2073 | if (defined $start_pos) | ||
| 2074 | { | ||
| 2075 | $target_node = $n; | ||
| 2076 | } | ||
| 2077 | else | ||
| 2078 | { | ||
| 2079 | $n = $n->next_elt($self, TEXT_SEGMENT); | ||
| 2080 | %info = $n->node_info() if $n; | ||
| 2081 | $offset = 0; | ||
| 2082 | } | ||
| 2083 | } | ||
| 2084 | return wantarray ? | ||
| 2085 | ($target_node, $start_pos, $match, $end_pos) : | ||
| 2086 | $start_pos; | ||
| 2087 | } | ||
| 2088 | |||
| 2089 | sub _search_backward | ||
| 2090 | { | ||
| 2091 | my $self = shift; | ||
| 2092 | my $expr = shift; | ||
| 2093 | my %opt = (@_); | ||
| 2094 | |||
| 2095 | my $offset = $opt{offset}; | ||
| 2096 | if (defined $offset && $offset > 0) | ||
| 2097 | { | ||
| 2098 | $offset = -abs($offset); | ||
| 2099 | } | ||
| 2100 | my ($target_node, $n, $start_pos, $end_pos, $match); | ||
| 2101 | |||
| 2102 | if ($self->is_text) | ||
| 2103 | { | ||
| 2104 | $n = $self; | ||
| 2105 | } | ||
| 2106 | elsif ($opt{start_mark}) | ||
| 2107 | { | ||
| 2108 | unless ($opt{start_mark}->is_text) | ||
| 2109 | { | ||
| 2110 | $n = $opt{start_mark}->prev_elt($self, TEXT_SEGMENT); | ||
| 2111 | } | ||
| 2112 | else | ||
| 2113 | { | ||
| 2114 | $n = $opt{start_mark}; | ||
| 2115 | } | ||
| 2116 | } | ||
| 2117 | else | ||
| 2118 | { | ||
| 2119 | $n = $self->last_descendant(TEXT_SEGMENT); | ||
| 2120 | } | ||
| 2121 | my %info = $n->node_info() if $n; | ||
| 2122 | if (defined $offset) | ||
| 2123 | { | ||
| 2124 | while ($n && abs($offset) >= $info{size}) | ||
| 2125 | { | ||
| 2126 | if ($opt{end_mark} && ! $n->after($opt{end_mark})) | ||
| 2127 | { | ||
| 2128 | $n = undef; last; | ||
| 2129 | } | ||
| 2130 | $offset += $info{size}; | ||
| 2131 | $n = $n->prev_elt($self, TEXT_SEGMENT); | ||
| 2132 | %info = $n->node_info() if $n; | ||
| 2133 | } | ||
| 2134 | } | ||
| 2135 | while ($n && !defined $start_pos) | ||
| 2136 | { | ||
| 2137 | if ($opt{end_mark} && ! $n->before($opt{end_mark})) | ||
| 2138 | { | ||
| 2139 | $n = undef; last; | ||
| 2140 | } | ||
| 2141 | unless (defined $expr) | ||
| 2142 | { | ||
| 2143 | $start_pos = $offset; | ||
| 2144 | $match = defined $opt{range} ? | ||
| 2145 | substr($info{text}, $start_pos, $opt{range}) : | ||
| 2146 | substr($info{text}, $start_pos); | ||
| 2147 | $end_pos = $start_pos + length($match); | ||
| 2148 | } | ||
| 2149 | else | ||
| 2150 | { | ||
| 2151 | ($start_pos, $end_pos, $match) = | ||
| 2152 | search_string | ||
| 2153 | ( | ||
| 2154 | $info{text}, | ||
| 2155 | $expr, | ||
| 2156 | offset => $offset, | ||
| 2157 | range => $opt{range} | ||
| 2158 | ); | ||
| 2159 | } | ||
| 2160 | if (defined $start_pos) | ||
| 2161 | { | ||
| 2162 | $target_node = $n; | ||
| 2163 | } | ||
| 2164 | else | ||
| 2165 | { | ||
| 2166 | $n = $n->next_elt($self, TEXT_SEGMENT); | ||
| 2167 | %info = $n->node_info() if $n; | ||
| 2168 | $offset = 0; | ||
| 2169 | } | ||
| 2170 | } | ||
| 2171 | return wantarray ? | ||
| 2172 | ($target_node, $start_pos, $match, $end_pos) : | ||
| 2173 | $start_pos; | ||
| 2174 | } | ||
| 2175 | |||
| 2176 | sub search | ||
| 2177 | { | ||
| 2178 | my $self = shift; | ||
| 2179 | my $expr = input_conversion(shift); | ||
| 2180 | my %opt = process_options | ||
| 2181 | ( | ||
| 2182 | backward => FALSE, | ||
| 2183 | start_mark => undef, | ||
| 2184 | end_mark => undef, | ||
| 2185 | offset => undef, | ||
| 2186 | range => undef, | ||
| 2187 | @_ | ||
| 2188 | ); | ||
| 2189 | unless (defined $expr || defined $opt{offset}) | ||
| 2190 | { | ||
| 2191 | alert("Missing search argument"); | ||
| 2192 | return undef; | ||
| 2193 | } | ||
| 2194 | my $backward = $opt{backward}; delete $opt{backward}; | ||
| 2195 | if (defined $opt{offset} && $opt{offset} < 0) | ||
| 2196 | { | ||
| 2197 | $backward = TRUE; | ||
| 2198 | } | ||
| 2199 | my %r = (); | ||
| 2200 | my $match = undef; | ||
| 2201 | if(is_false($backward)) | ||
| 2202 | { | ||
| 2203 | ($r{segment}, $r{offset}, $match, $r{end}) = | ||
| 2204 | $self->_search_forward($expr, %opt); | ||
| 2205 | } | ||
| 2206 | else | ||
| 2207 | { | ||
| 2208 | ($r{segment}, $r{offset}, $match, $r{end}) = | ||
| 2209 | $self->_search_backward($expr, %opt); | ||
| 2210 | } | ||
| 2211 | $r{match} = output_conversion($match); | ||
| 2212 | return wantarray ? %r : { %r }; | ||
| 2213 | } | ||
| 2214 | |||
| 2215 | sub replace | ||
| 2216 | { | ||
| 2217 | my $self = shift; | ||
| 2218 | return $self->replace_element(@_) if caller() eq 'XML::Twig::Elt'; | ||
| 2219 | my $expr = shift; | ||
| 2220 | my $repl = shift; | ||
| 2221 | return $self->count_matches($expr, @_) unless defined $repl; | ||
| 2222 | my %opt = | ||
| 2223 | ( | ||
| 2224 | deep => TRUE, | ||
| 2225 | @_ | ||
| 2226 | ); | ||
| 2227 | my $deep = $opt{deep}; delete $opt{deep}; | ||
| 2228 | my $count = 0; | ||
| 2229 | foreach my $segment ($self->text_segments(deep => $deep)) | ||
| 2230 | { | ||
| 2231 | $count += $segment->replace_in_text_segment | ||
| 2232 | ($expr, $repl, %opt); | ||
| 2233 | } | ||
| 2234 | return $count; | ||
| 2235 | } | ||
| 2236 | |||
| 2237 | sub count_matches | ||
| 2238 | { | ||
| 2239 | my $self = shift; | ||
| 2240 | my $expr = shift; | ||
| 2241 | my %opt = | ||
| 2242 | ( | ||
| 2243 | deep => TRUE, | ||
| 2244 | @_ | ||
| 2245 | ); | ||
| 2246 | my $count = 0; | ||
| 2247 | foreach my $segment ($self->text_segments(deep => $opt{deep})) | ||
| 2248 | { | ||
| 2249 | my $t = $segment->get_text; | ||
| 2250 | $count += count_substrings($t, $expr); | ||
| 2251 | } | ||
| 2252 | return $count; | ||
| 2253 | } | ||
| 2254 | |||
| 2255 | #============================================================================= | ||
| 2256 | |||
| 2257 | our $AUTOLOAD; | ||
| 2258 | |||
| 2259 | sub AUTOLOAD | ||
| 2260 | { | ||
| 2261 | $AUTOLOAD =~ /(.*:)(.*)/; | ||
| 2262 | my $package = $1; | ||
| 2263 | my $method = $2; | ||
| 2264 | my $element = shift; | ||
| 2265 | |||
| 2266 | $method =~ /^([gs]et)_(.*)/; | ||
| 2267 | my $action = $1; | ||
| 2268 | |||
| 2269 | no strict; | ||
| 2270 | my $target = ${$package . "ATTRIBUTE"}{$2}; | ||
| 2271 | use strict; | ||
| 2272 | unless ($action && $target) | ||
| 2273 | { | ||
| 2274 | alert "Unknown method $method @_"; | ||
| 2275 | return undef; | ||
| 2276 | } | ||
| 2277 | my $name = $target->{attribute}; | ||
| 2278 | my $type = $target->{type}; | ||
| 2279 | |||
| 2280 | my $value = undef; | ||
| 2281 | if ($action eq 'get') | ||
| 2282 | { | ||
| 2283 | $value = $element->get_attribute($name, @_); | ||
| 2284 | return ($type and ($type eq 'boolean')) ? | ||
| 2285 | is_true($value) : $value; | ||
| 2286 | } | ||
| 2287 | elsif ($action eq 'set') | ||
| 2288 | { | ||
| 2289 | $value = input_conversion(shift); | ||
| 2290 | if ($type) | ||
| 2291 | { | ||
| 2292 | $value = check_odf_value($value, $type); | ||
| 2293 | } | ||
| 2294 | return defined $value ? | ||
| 2295 | $element->set_att($name => $value) : | ||
| 2296 | $element->del_attribute($name); | ||
| 2297 | } | ||
| 2298 | else | ||
| 2299 | { | ||
| 2300 | alert "Unknown method $method @_"; | ||
| 2301 | } | ||
| 2302 | |||
| 2303 | return undef; | ||
| 2304 | } | ||
| 2305 | |||
| 2306 | sub not_allowed | ||
| 2307 | { | ||
| 2308 | my $self = shift; | ||
| 2309 | my $tag = $self->get_tag; | ||
| 2310 | my $class = ref $self; | ||
| 2311 | alert "Not allowed for this $tag ($class) element"; | ||
| 2312 | return undef; | ||
| 2313 | } | ||
| 2314 | |||
| 2315 | # Avoid "Unknown method DESTROY" warnings from AUTOLOAD, which gets called | ||
| 2316 | # when objects are being deleted. | ||
| 2317 | sub DESTROY {} # https://rt.cpan.org/Public/Bug/Display.html?id=97977 | ||
| 2318 | |||
| 2319 | #============================================================================= | ||
| 2320 | package ODF::lpOD::TextNode; | ||
| 2321 | use base 'ODF::lpOD::Element'; | ||
| 2322 | our $VERSION = '1.000'; | ||
| 2323 | use constant PACKAGE_DATE => '2011-02-27T00:44:46'; | ||
| 2324 | use ODF::lpOD::Common; | ||
| 2325 | #----------------------------------------------------------------------------- | ||
| 2326 | |||
| 2327 | BEGIN | ||
| 2328 | { | ||
| 2329 | *create = *XML::Twig::Elt::new; | ||
| 2330 | *get_tag = *XML::Twig::Elt::tag; | ||
| 2331 | *set_tag = *ODF::lpOD::Element::set_tag; | ||
| 2332 | *set_text = *ODF::lpOD::Element::set_text; | ||
| 2333 | *get_parent = *XML::Twig::Elt::parent; | ||
| 2334 | *get_ancestor = *XML::Twig::Elt::parent; | ||
| 2335 | *previous_sibling = *XML::Twig::Elt::prev_sibling; | ||
| 2336 | *get_root = *XML::Twig::Elt::root; | ||
| 2337 | *is_element = *XML::Twig::Elt::is_elt; | ||
| 2338 | *is_text_segment = *XML::Twig::Elt::is_text; | ||
| 2339 | *_set_text = *XML::Twig::Elt::set_text; | ||
| 2340 | *_get_text = *XML::Twig::Elt::text; | ||
| 2341 | *_set_tag = *XML::Twig::Elt::set_tag; | ||
| 2342 | *replace_element = *XML::Twig::Elt::replace; | ||
| 2343 | } | ||
| 2344 | |||
| 2345 | #----------------------------------------------------------------------------- | ||
| 2346 | |||
| 2347 | sub node_info | ||
| 2348 | { | ||
| 2349 | my $self = shift; | ||
| 2350 | my %i = (); | ||
| 2351 | $i{text} = $self->_get_text; | ||
| 2352 | $i{size} = length($i{text}); | ||
| 2353 | $i{tag} = TEXT_SEGMENT; | ||
| 2354 | $i{class} = __PACKAGE__; | ||
| 2355 | $i{attributes} = undef; | ||
| 2356 | return %i; | ||
| 2357 | } | ||
| 2358 | |||
| 2359 | sub get_text | ||
| 2360 | { | ||
| 2361 | my $self = shift; | ||
| 2362 | return output_conversion($self->text); | ||
| 2363 | } | ||
| 2364 | |||
| 2365 | #============================================================================= | ||
| 2366 | package ODF::lpOD::BibliographyMark; | ||
| 2367 | use base 'ODF::lpOD::Element'; | ||
| 2368 | our $VERSION = '1.000'; | ||
| 2369 | use constant PACKAGE_DATE => '2010-12-24T13:37:35'; | ||
| 2370 | #============================================================================= | ||
| 2371 | package ODF::lpOD::Note; | ||
| 2372 | use base 'ODF::lpOD::Element'; | ||
| 2373 | our $VERSION = '1.002'; | ||
| 2374 | use constant PACKAGE_DATE => '2011-02-22T00:16:40'; | ||
| 2375 | use ODF::lpOD::Common; | ||
| 2376 | #----------------------------------------------------------------------------- | ||
| 2377 | |||
| 2378 | BEGIN { | ||
| 2379 | *set_text = *set_body; | ||
| 2380 | } | ||
| 2381 | |||
| 2382 | #----------------------------------------------------------------------------- | ||
| 2383 | |||
| 2384 | sub _create { ODF::lpOD::Note->create(@_) } | ||
| 2385 | |||
| 2386 | #----------------------------------------------------------------------------- | ||
| 2387 | |||
| 2388 | sub create | ||
| 2389 | { | ||
| 2390 | my $caller = shift; | ||
| 2391 | my $class = ref($caller) || $caller; | ||
| 2392 | my $id = shift; | ||
| 2393 | unless ($id) | ||
| 2394 | { | ||
| 2395 | alert "Missing mandatory note identifier"; | ||
| 2396 | return FALSE; | ||
| 2397 | } | ||
| 2398 | my %opt = process_options | ||
| 2399 | ( | ||
| 2400 | class => 'footnote', | ||
| 2401 | @_ | ||
| 2402 | ); | ||
| 2403 | my $note = ODF::lpOD::Element->create('text:note'); | ||
| 2404 | $note->set_id($id); | ||
| 2405 | $note->set_citation($opt{citation}, $opt{label}); | ||
| 2406 | $note->{style} = $opt{style}; | ||
| 2407 | if ($opt{body}) | ||
| 2408 | { | ||
| 2409 | $note->set_body(@{$opt{body}}); | ||
| 2410 | } | ||
| 2411 | else | ||
| 2412 | { | ||
| 2413 | $note->set_body($opt{text}); | ||
| 2414 | } | ||
| 2415 | |||
| 2416 | return $note; | ||
| 2417 | } | ||
| 2418 | |||
| 2419 | #----------------------------------------------------------------------------- | ||
| 2420 | |||
| 2421 | sub get_citation | ||
| 2422 | { | ||
| 2423 | my $self = shift; | ||
| 2424 | my $c = $self->first_child('text:note-citation') | ||
| 2425 | or return undef; | ||
| 2426 | return $c->get_text; | ||
| 2427 | } | ||
| 2428 | |||
| 2429 | sub set_citation | ||
| 2430 | { | ||
| 2431 | my $self = shift; | ||
| 2432 | my $text = shift; | ||
| 2433 | my $label = shift; | ||
| 2434 | my $c = $self->set_child('text:note-citation'); | ||
| 2435 | $c->set_attribute('label' => $label) if defined $label; | ||
| 2436 | $c->set_text($text); | ||
| 2437 | return $c; | ||
| 2438 | } | ||
| 2439 | |||
| 2440 | sub set_label | ||
| 2441 | { | ||
| 2442 | my $self = shift; | ||
| 2443 | my $label = shift; | ||
| 2444 | my $c = $self->set_child('text:note-citation'); | ||
| 2445 | $c->set_attribute('label' => $label) if defined $label; | ||
| 2446 | return $c; | ||
| 2447 | } | ||
| 2448 | |||
| 2449 | sub get_label | ||
| 2450 | { | ||
| 2451 | my $self = shift; | ||
| 2452 | my $c = $self->first_child('text:note-citation') | ||
| 2453 | or return undef; | ||
| 2454 | return $c->get_attribute('label'); | ||
| 2455 | } | ||
| 2456 | |||
| 2457 | sub get_body | ||
| 2458 | { | ||
| 2459 | my $self = shift; | ||
| 2460 | return $self->first_child('text:note-body'); | ||
| 2461 | } | ||
| 2462 | |||
| 2463 | sub set_body | ||
| 2464 | { | ||
| 2465 | my $self = shift; | ||
| 2466 | my $body = $self->get_body(); | ||
| 2467 | if ($body) | ||
| 2468 | { | ||
| 2469 | $body->cut_children; | ||
| 2470 | } | ||
| 2471 | else | ||
| 2472 | { | ||
| 2473 | $body = $self->append_element('text:note-body'); | ||
| 2474 | } | ||
| 2475 | foreach my $arg (@_) | ||
| 2476 | { | ||
| 2477 | if (ref $arg) | ||
| 2478 | { | ||
| 2479 | $arg->paste_last_child($body); | ||
| 2480 | } | ||
| 2481 | else | ||
| 2482 | { | ||
| 2483 | my $p = ODF::lpOD::Paragraph->create( | ||
| 2484 | text => $arg, style => $self->{style} | ||
| 2485 | ); | ||
| 2486 | $p->paste_last_child($body); | ||
| 2487 | } | ||
| 2488 | } | ||
| 2489 | return $body; | ||
| 2490 | } | ||
| 2491 | |||
| 2492 | #============================================================================= | ||
| 2493 | package ODF::lpOD::Annotation; | ||
| 2494 | use base 'ODF::lpOD::Element'; | ||
| 2495 | our $VERSION = '1.002'; | ||
| 2496 | use constant PACKAGE_DATE => '2011-02-15T11:16:59'; | ||
| 2497 | use ODF::lpOD::Common; | ||
| 2498 | #----------------------------------------------------------------------------- | ||
| 2499 | |||
| 2500 | BEGIN { | ||
| 2501 | *set_creator = *set_author; | ||
| 2502 | *get_creator = *get_author; | ||
| 2503 | } | ||
| 2504 | |||
| 2505 | #----------------------------------------------------------------------------- | ||
| 2506 | |||
| 2507 | sub _create { ODF::lpOD::Annotation->create(@_) } | ||
| 2508 | |||
| 2509 | #----------------------------------------------------------------------------- | ||
| 2510 | |||
| 2511 | sub create | ||
| 2512 | { | ||
| 2513 | my $caller = shift; | ||
| 2514 | my %opt = @_; | ||
| 2515 | my $a = ODF::lpOD::Element->create('office:annotation'); | ||
| 2516 | $a->set_date($opt{date}); | ||
| 2517 | $a->set_author($opt{author}); | ||
| 2518 | $a->set_style($opt{style}); | ||
| 2519 | $a->set_size($opt{size}) if defined $opt{size}; | ||
| 2520 | $a->set_position($opt{position}) if defined $opt{position}; | ||
| 2521 | $a->set_display($opt{display}); | ||
| 2522 | my $content = $opt{content}; | ||
| 2523 | unshift @$content, $opt{text} if defined $opt{text}; | ||
| 2524 | $a->set_content(@$content) if $content; | ||
| 2525 | return $a; | ||
| 2526 | } | ||
| 2527 | |||
| 2528 | #----------------------------------------------------------------------------- | ||
| 2529 | |||
| 2530 | sub set_date | ||
| 2531 | { | ||
| 2532 | my $self = shift; | ||
| 2533 | my $date = shift; | ||
| 2534 | my $elt = $self->set_child('dc:date'); | ||
| 2535 | unless ($date) | ||
| 2536 | { | ||
| 2537 | return $elt->set_text(iso_date); | ||
| 2538 | } | ||
| 2539 | else | ||
| 2540 | { | ||
| 2541 | my $d = check_odf_value($date, 'date'); | ||
| 2542 | unless ($d) | ||
| 2543 | { | ||
| 2544 | alert "Wrong date"; return undef; | ||
| 2545 | } | ||
| 2546 | return $elt->set_text($d); | ||
| 2547 | } | ||
| 2548 | } | ||
| 2549 | |||
| 2550 | sub get_date | ||
| 2551 | { | ||
| 2552 | my $self = shift; | ||
| 2553 | my $elt = $self->first_child('dc:date') or return undef; | ||
| 2554 | return $elt->get_text; | ||
| 2555 | } | ||
| 2556 | |||
| 2557 | sub set_author | ||
| 2558 | { | ||
| 2559 | my $self = shift; | ||
| 2560 | my $elt = $self->set_child('dc:creator'); | ||
| 2561 | return $elt->set_text | ||
| 2562 | ( | ||
| 2563 | shift | ||
| 2564 | // | ||
| 2565 | (scalar getlogin()) | ||
| 2566 | // | ||
| 2567 | (scalar getpwuid($<)) | ||
| 2568 | // | ||
| 2569 | $< | ||
| 2570 | ); | ||
| 2571 | } | ||
| 2572 | |||
| 2573 | sub get_author | ||
| 2574 | { | ||
| 2575 | my $self = shift; | ||
| 2576 | my $elt = $self->first_child('dc:creator') or return undef; | ||
| 2577 | return $elt->get_text; | ||
| 2578 | } | ||
| 2579 | |||
| 2580 | sub get_content | ||
| 2581 | { | ||
| 2582 | my $self = shift; | ||
| 2583 | return $self->children; | ||
| 2584 | } | ||
| 2585 | |||
| 2586 | sub set_content | ||
| 2587 | { | ||
| 2588 | my $self = shift; | ||
| 2589 | $self->cut_children(qr'^text'); | ||
| 2590 | foreach my $arg (@_) | ||
| 2591 | { | ||
| 2592 | if (ref $arg) | ||
| 2593 | { | ||
| 2594 | $arg->paste_last_child($self); | ||
| 2595 | } | ||
| 2596 | else | ||
| 2597 | { | ||
| 2598 | my $p = ODF::lpOD::Paragraph->create( | ||
| 2599 | text => $arg, style => $self->{style} | ||
| 2600 | ); | ||
| 2601 | $p->paste_last_child($self); | ||
| 2602 | } | ||
| 2603 | } | ||
| 2604 | return $self->get_content; | ||
| 2605 | } | ||
| 2606 | |||
| 2607 | sub set_style | ||
| 2608 | { | ||
| 2609 | my $self = shift; | ||
| 2610 | return $self->{style} = shift; | ||
| 2611 | } | ||
| 2612 | |||
| 2613 | sub get_style | ||
| 2614 | { | ||
| 2615 | my $self = shift; | ||
| 2616 | return $self->{style}; | ||
| 2617 | } | ||
| 2618 | |||
| 2619 | sub set_text | ||
| 2620 | { | ||
| 2621 | my $self = shift; | ||
| 2622 | return $self->set_content(@_); | ||
| 2623 | } | ||
| 2624 | |||
| 2625 | sub get_text | ||
| 2626 | { | ||
| 2627 | my $self = shift; | ||
| 2628 | return $self->get_text_content(@_); | ||
| 2629 | } | ||
| 2630 | |||
| 2631 | #============================================================================= | ||
| 2632 | package ODF::lpOD::ChangedRegion; | ||
| 2633 | use base 'ODF::lpOD::Element'; | ||
| 2634 | our $VERSION = '1.000'; | ||
| 2635 | use constant PACKAGE_DATE => '2010-12-24T13:39:17'; | ||
| 2636 | use ODF::lpOD::Common; | ||
| 2637 | #----------------------------------------------------------------------------- | ||
| 2638 | |||
| 2639 | sub get_changed_context | ||
| 2640 | { | ||
| 2641 | my $self = shift; | ||
| 2642 | my $tcr = $self->parent('text:tracked-changes'); | ||
| 2643 | my $context = $tcr ? $tcr->parent() : undef; | ||
| 2644 | unless ($context) | ||
| 2645 | { | ||
| 2646 | alert "Unknown tracked change context"; | ||
| 2647 | } | ||
| 2648 | return $context; | ||
| 2649 | } | ||
| 2650 | |||
| 2651 | sub get_info | ||
| 2652 | { | ||
| 2653 | my $self = shift; | ||
| 2654 | my $tag = shift; | ||
| 2655 | $tag = 'dc:' . $tag unless $tag =~ /:/; | ||
| 2656 | my $info = $self->first_descendant($tag) or return undef; | ||
| 2657 | return $info->get_text; | ||
| 2658 | } | ||
| 2659 | |||
| 2660 | sub get_date | ||
| 2661 | { | ||
| 2662 | my $self = shift; | ||
| 2663 | return $self->get_info('date'); | ||
| 2664 | } | ||
| 2665 | |||
| 2666 | sub get_author | ||
| 2667 | { | ||
| 2668 | my $self = shift; | ||
| 2669 | return $self->get_info('creator'); | ||
| 2670 | } | ||
| 2671 | |||
| 2672 | sub get_type | ||
| 2673 | { | ||
| 2674 | my $self = shift; | ||
| 2675 | my $t = $self->first_child or return undef; | ||
| 2676 | my $type = $t->get_tag; $type =~ s/^text://; | ||
| 2677 | return $type; | ||
| 2678 | } | ||
| 2679 | |||
| 2680 | sub get_deleted_content | ||
| 2681 | { | ||
| 2682 | my $self = shift; | ||
| 2683 | my $deleted = $self->first_child('text:deletion') or return undef; | ||
| 2684 | my @content = (); | ||
| 2685 | foreach my $e ($deleted->children) | ||
| 2686 | { | ||
| 2687 | my $tag = $e->get_tag; | ||
| 2688 | push @content, $e unless $tag eq 'office:change-info'; | ||
| 2689 | } | ||
| 2690 | return wantarray ? @content : [ @content ]; | ||
| 2691 | } | ||
| 2692 | |||
| 2693 | sub get_change_mark | ||
| 2694 | { | ||
| 2695 | my $self = shift; | ||
| 2696 | my $id = $self->get_id; | ||
| 2697 | my $context = $self->get_changed_context or return undef; | ||
| 2698 | my $type = $self->get_type(); | ||
| 2699 | unless ($type) | ||
| 2700 | { | ||
| 2701 | alert "Unknown change type"; return undef; | ||
| 2702 | } | ||
| 2703 | my $tag = ($type eq 'deletion') ? 'text:change' : 'text:change-start'; | ||
| 2704 | return $context->get_element( | ||
| 2705 | $tag, | ||
| 2706 | attribute => 'change id', | ||
| 2707 | value => $id | ||
| 2708 | ); | ||
| 2709 | } | ||
| 2710 | |||
| 2711 | sub get_insertion_marks | ||
| 2712 | { | ||
| 2713 | my $self = shift; | ||
| 2714 | my $id = $self->get_id; | ||
| 2715 | my $context = $self->get_changed_context or return undef; | ||
| 2716 | my $start = $context->get_element( | ||
| 2717 | 'text:change-start', | ||
| 2718 | attribute => 'change id', | ||
| 2719 | value => $id | ||
| 2720 | ); | ||
| 2721 | my $end = $context->get_element( | ||
| 2722 | 'text:change-end', | ||
| 2723 | attribute => 'change id', | ||
| 2724 | value => $id | ||
| 2725 | ); | ||
| 2726 | return wantarray ? ($start, $end) : [ $start, $end ]; | ||
| 2727 | } | ||
| 2728 | |||
| 2729 | #============================================================================= | ||
| 2730 | package ODF::lpOD::FileEntry; | ||
| 2731 | use base 'ODF::lpOD::Element'; | ||
| 2732 | our $VERSION = '1.000'; | ||
| 2733 | use constant PACKAGE_DATE => '2010-12-24T13:39:36'; | ||
| 2734 | use ODF::lpOD::Common; | ||
| 2735 | #----------------------------------------------------------------------------- | ||
| 2736 | |||
| 2737 | BEGIN { | ||
| 2738 | *set_text = *not_allowed; | ||
| 2739 | *insert_element = *not_allowed; | ||
| 2740 | *append_element = *not_allowed; | ||
| 2741 | } | ||
| 2742 | |||
| 2743 | #----------------------------------------------------------------------------- | ||
| 2744 | |||
| 2745 | our @ALLOWED_ATTRIBUTES = ('manifest:media-type', 'manifest:full-path'); | ||
| 2746 | |||
| 2747 | sub set_attribute | ||
| 2748 | { | ||
| 2749 | my $self = shift; | ||
| 2750 | my $name = $self->normalize_name(shift); | ||
| 2751 | unless (grep $_ eq $name, @ALLOWED_ATTRIBUTES) | ||
| 2752 | { | ||
| 2753 | alert "Attribute $name is not allowed"; | ||
| 2754 | return FALSE; | ||
| 2755 | } | ||
| 2756 | return $self->SUPER::set_attribute($name, @_); | ||
| 2757 | } | ||
| 2758 | |||
| 2759 | sub get_path | ||
| 2760 | { | ||
| 2761 | my $self = shift; | ||
| 2762 | return $self->get_attribute('full path'); | ||
| 2763 | } | ||
| 2764 | |||
| 2765 | sub set_path | ||
| 2766 | { | ||
| 2767 | my $self = shift; | ||
| 2768 | my $path = shift; | ||
| 2769 | unless ($path) | ||
| 2770 | { | ||
| 2771 | alert "Missing or wrong path"; return FALSE; | ||
| 2772 | } | ||
| 2773 | my $old_path = $self->get_path; | ||
| 2774 | my $lpod_part = $self->lpod_part; | ||
| 2775 | my $other = $lpod_part ? $lpod_part->get_entry($path) : undef; | ||
| 2776 | if ($other) | ||
| 2777 | { | ||
| 2778 | if ($other == $self) | ||
| 2779 | { | ||
| 2780 | return TRUE; | ||
| 2781 | } | ||
| 2782 | else | ||
| 2783 | { | ||
| 2784 | alert "Non unique entry path $path"; | ||
| 2785 | return FALSE; | ||
| 2786 | } | ||
| 2787 | } | ||
| 2788 | $self->set_attribute('full path' => $path); | ||
| 2789 | if ($path =~ /.\/$/) | ||
| 2790 | { | ||
| 2791 | $self->set_attribute('media type' => ""); | ||
| 2792 | } | ||
| 2793 | return TRUE; | ||
| 2794 | } | ||
| 2795 | |||
| 2796 | sub get_type | ||
| 2797 | { | ||
| 2798 | my $self = shift; | ||
| 2799 | return $self->get_attribute('media type'); | ||
| 2800 | } | ||
| 2801 | |||
| 2802 | sub set_type | ||
| 2803 | { | ||
| 2804 | my $self = shift; | ||
| 2805 | my $type = shift; | ||
| 2806 | return $self->set_attribute('media type' => $type); | ||
| 2807 | } | ||
| 2808 | |||
| 2809 | #============================================================================= | ||
| 2810 | 1; | ||