1 /*
2 * SimpleNode - simple XML node implementation
3 *
4 * Copyright 2003-2005 MenTaLguY <mental@rydia.net>
5 * Copyright 2003 Nathan Hurst
6 * Copyright 1999-2003 Lauris Kaplinski
7 * Copyright 2000-2002 Ximian Inc.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * See the file COPYING for details.
15 *
16 */
18 #include <glib/gstrfuncs.h>
19 #include "xml/simple-node.h"
20 #include "xml/node-event-vector.h"
21 #include "xml/node-fns.h"
22 #include "xml/repr.h"
23 #include "debug/event-tracker.h"
25 namespace Inkscape {
27 namespace XML {
29 namespace {
31 Util::SharedCStringPtr stringify_node(Node const &node) {
32 gchar *string;
33 switch (node.type()) {
34 case ELEMENT_NODE: {
35 char const *id=node.attribute("id");
36 if (id) {
37 string = g_strdup_printf("element(%p)=%s(#%s)", &node, node.name(), id);
38 } else {
39 string = g_strdup_printf("element(%p)=%s", &node, node.name());
40 }
41 } break;
42 case TEXT_NODE:
43 string = g_strdup_printf("text(%p)=%s", &node, node.content());
44 break;
45 case COMMENT_NODE:
46 string = g_strdup_printf("comment(%p)=<!--%s-->", &node, node.content());
47 break;
48 case DOCUMENT_NODE:
49 string = g_strdup_printf("document(%p)", &node);
50 break;
51 default:
52 string = g_strdup_printf("unknown(%p)", &node);
53 }
54 Util::SharedCStringPtr result=Util::SharedCStringPtr::copy(string);
55 g_free(string);
56 return result;
57 }
59 Util::SharedCStringPtr stringify_unsigned(unsigned n) {
60 gchar *string = g_strdup_printf("%u", n);
61 Util::SharedCStringPtr result=Util::SharedCStringPtr::copy(string);
62 g_free(string);
63 return result;
64 }
66 }
68 class DebugAddChild : public Debug::Event {
69 public:
70 DebugAddChild(Node const &node, Node const &child, Node const *prev)
71 : _parent(stringify_node(node)),
72 _child(stringify_node(child)),
73 _position(prev ? prev->position() + 1 : 0)
74 {}
76 static Category category() { return XML; }
78 Util::SharedCStringPtr name() const {
79 return Util::SharedCStringPtr::coerce("add-child");
80 }
81 unsigned propertyCount() const { return 3; }
82 PropertyPair property(unsigned i) const {
83 switch (i) {
84 case 0:
85 return PropertyPair("parent", _parent);
86 case 1:
87 return PropertyPair("child", _child);
88 case 2:
89 return PropertyPair("position", stringify_unsigned(_position));
90 default:
91 return PropertyPair();
92 }
93 }
94 private:
95 Util::SharedCStringPtr _parent;
96 Util::SharedCStringPtr _child;
97 unsigned _position;
98 };
100 class DebugRemoveChild : public Debug::Event {
101 public:
102 DebugRemoveChild(Node const &node, Node const &child, Node const *prev)
103 : _parent(stringify_node(node)),
104 _child(stringify_node(child))
105 {}
107 static Category category() { return XML; }
109 Util::SharedCStringPtr name() const {
110 return Util::SharedCStringPtr::coerce("remove-child");
111 }
112 unsigned propertyCount() const { return 2; }
113 PropertyPair property(unsigned i) const {
114 switch (i) {
115 case 0:
116 return PropertyPair("parent", _parent);
117 case 1:
118 return PropertyPair("child", _child);
119 default:
120 return PropertyPair();
121 }
122 }
123 private:
124 Util::SharedCStringPtr _parent;
125 Util::SharedCStringPtr _child;
126 };
128 class DebugSetChildPosition : public Debug::Event {
129 public:
130 DebugSetChildPosition(Node const &node, Node const &child, Node const *old_prev, Node const *new_prev)
131 : _parent(stringify_node(node)),
132 _child(stringify_node(child))
133 {
134 unsigned old_position = ( old_prev ? old_prev->position() : 0 );
135 _position = ( new_prev ? new_prev->position() : 0 );
136 if ( _position > old_position ) {
137 --_position;
138 }
139 }
141 static Category category() { return XML; }
143 Util::SharedCStringPtr name() const {
144 return Util::SharedCStringPtr::coerce("set-child-position");
145 }
146 unsigned propertyCount() const { return 3; }
147 PropertyPair property(unsigned i) const {
148 switch (i) {
149 case 0:
150 return PropertyPair("parent", _parent);
151 case 1:
152 return PropertyPair("child", _child);
153 case 2:
154 return PropertyPair("position", stringify_unsigned(_position));
155 default:
156 return PropertyPair();
157 }
158 }
159 private:
160 Util::SharedCStringPtr _parent;
161 Util::SharedCStringPtr _child;
162 unsigned _position;
163 };
165 class DebugSetContent : public Debug::Event {
166 public:
167 DebugSetContent(Node const &node,
168 Util::SharedCStringPtr old_content,
169 Util::SharedCStringPtr new_content)
170 : _node(stringify_node(node)), _content(new_content) {}
172 static Category category() { return XML; }
174 Util::SharedCStringPtr name() const {
175 if (_content) {
176 return Util::SharedCStringPtr::coerce("set-content");
177 } else {
178 return Util::SharedCStringPtr::coerce("clear-content");
179 }
180 }
181 unsigned propertyCount() const {
182 if (_content) {
183 return 2;
184 } else {
185 return 1;
186 }
187 }
188 PropertyPair property(unsigned i) const {
189 switch (i) {
190 case 0:
191 return PropertyPair("node", _node);
192 case 1:
193 return PropertyPair("content", _content);
194 default:
195 return PropertyPair();
196 }
197 }
198 private:
199 Util::SharedCStringPtr _node;
200 Util::SharedCStringPtr _content;
201 };
203 class DebugSetAttribute : public Debug::Event {
204 public:
205 DebugSetAttribute(Node const &node, GQuark name,
206 Util::SharedCStringPtr old_value,
207 Util::SharedCStringPtr new_value)
208 : _node(stringify_node(node)),
209 _name(Util::SharedCStringPtr::coerce(g_quark_to_string(name))),
210 _value(new_value) {}
212 static Category category() { return XML; }
214 Util::SharedCStringPtr name() const {
215 if (_value) {
216 return Util::SharedCStringPtr::coerce("set-attribute");
217 } else {
218 return Util::SharedCStringPtr::coerce("clear-attribute");
219 }
220 }
221 unsigned propertyCount() const {
222 if (_value) {
223 return 3;
224 } else {
225 return 2;
226 }
227 }
228 PropertyPair property(unsigned i) const {
229 switch (i) {
230 case 0:
231 return PropertyPair("node", _node);
232 case 1:
233 return PropertyPair("name", _name);
234 case 2:
235 return PropertyPair("value", _value);
236 default:
237 return PropertyPair();
238 }
239 }
241 private:
242 Util::SharedCStringPtr _node;
243 Util::SharedCStringPtr _name;
244 Util::SharedCStringPtr _value;
245 };
247 using Inkscape::Util::SharedCStringPtr;
248 using Inkscape::Util::List;
249 using Inkscape::Util::MutableList;
250 using Inkscape::Util::cons;
251 using Inkscape::Util::rest;
252 using Inkscape::Util::set_rest;
254 SimpleNode::SimpleNode(int code)
255 : Node(), _name(code), _attributes(), _child_count(0),
256 _cached_positions_valid(false)
257 {
258 this->_logger = NULL;
259 this->_document = NULL;
260 this->_parent = this->_next = NULL;
261 this->_first_child = this->_last_child = NULL;
262 }
264 SimpleNode::SimpleNode(SimpleNode const &node)
265 : Node(),
266 _cached_position(node._cached_position),
267 _name(node._name), _attributes(), _content(node._content),
268 _child_count(node._child_count),
269 _cached_positions_valid(node._cached_positions_valid)
270 {
271 _logger = NULL;
272 _document = NULL;
273 _parent = _next = NULL;
274 _first_child = _last_child = NULL;
276 for ( Node *child = node._first_child ;
277 child != NULL ; child = child->next() )
278 {
279 Node *child_copy=child->duplicate();
281 child_copy->_setParent(this);
282 if (_last_child) {
283 _last_child->_setNext(child_copy);
284 } else {
285 _first_child = child_copy;
286 }
287 _last_child = child_copy;
289 child_copy->release(); // release to avoid a leak
290 }
292 for ( List<AttributeRecord const> iter = node._attributes ;
293 iter ; ++iter )
294 {
295 _attributes = cons(*iter, _attributes);
296 }
297 }
299 gchar const *SimpleNode::name() const {
300 return g_quark_to_string(_name);
301 }
303 gchar const *SimpleNode::content() const {
304 return this->_content;
305 }
307 gchar const *SimpleNode::attribute(gchar const *name) const {
308 g_return_val_if_fail(name != NULL, NULL);
310 GQuark const key = g_quark_from_string(name);
312 for ( List<AttributeRecord const> iter = _attributes ;
313 iter ; ++iter )
314 {
315 if ( iter->key == key ) {
316 return iter->value;
317 }
318 }
320 return NULL;
321 }
323 unsigned SimpleNode::position() const {
324 g_return_val_if_fail(_parent != NULL, 0);
325 return _parent->_childPosition(*this);
326 }
328 unsigned SimpleNode::_childPosition(Node const &child) const {
329 if (!_cached_positions_valid) {
330 unsigned position=0;
331 for ( Node *sibling = _first_child ;
332 sibling ; sibling = sibling->next() )
333 {
334 sibling->_setCachedPosition(position);
335 position++;
336 }
337 _cached_positions_valid = true;
338 }
339 return child._cachedPosition();
340 }
342 Node *SimpleNode::nthChild(unsigned index) {
343 Node *child = _first_child;
344 for ( ; index > 0 && child ; child = child->next() ) {
345 index--;
346 }
347 return child;
348 }
350 bool SimpleNode::matchAttributeName(gchar const *partial_name) const {
351 g_return_val_if_fail(partial_name != NULL, false);
353 for ( List<AttributeRecord const> iter = _attributes ;
354 iter ; ++iter )
355 {
356 gchar const *name = g_quark_to_string(iter->key);
357 if (std::strstr(name, partial_name)) {
358 return true;
359 }
360 }
362 return false;
363 }
365 void SimpleNode::setContent(gchar const *content) {
366 SharedCStringPtr old_content=_content;
367 SharedCStringPtr new_content = ( content ? SharedCStringPtr::copy(content) : SharedCStringPtr() );
369 Debug::EventTracker<DebugSetContent> tracker(
370 *this, old_content, new_content
371 );
373 _content = new_content;
375 if ( _content != old_content ) {
376 if (_logger) {
377 _logger->notifyContentChanged(*this, old_content, _content);
378 }
380 _observers.notifyContentChanged(*this, old_content, _content);
381 }
382 }
384 void
385 SimpleNode::setAttribute(gchar const *name, gchar const *value, bool const is_interactive)
386 {
387 g_return_if_fail(name && *name);
389 GQuark const key = g_quark_from_string(name);
391 MutableList<AttributeRecord> ref;
392 MutableList<AttributeRecord> existing;
393 for ( existing = _attributes ; existing ; ++existing ) {
394 if ( existing->key == key ) {
395 break;
396 }
397 ref = existing;
398 }
400 Debug::EventTracker<> tracker;
402 SharedCStringPtr old_value=( existing ? existing->value : SharedCStringPtr() );
404 SharedCStringPtr new_value=SharedCStringPtr();
405 if (value) {
406 new_value = SharedCStringPtr::copy(value);
407 tracker.set<DebugSetAttribute>(*this, key, old_value, new_value);
408 if (!existing) {
409 if (ref) {
410 set_rest(ref, MutableList<AttributeRecord>(AttributeRecord(key, new_value)));
411 } else {
412 _attributes = MutableList<AttributeRecord>(AttributeRecord(key, new_value));
413 }
414 } else {
415 existing->value = new_value;
416 }
417 } else {
418 tracker.set<DebugSetAttribute>(*this, key, old_value, new_value);
419 if (existing) {
420 if (ref) {
421 set_rest(ref, rest(existing));
422 } else {
423 _attributes = rest(existing);
424 }
425 set_rest(existing, MutableList<AttributeRecord>());
426 }
427 }
429 if ( new_value != old_value ) {
430 if (_logger) {
431 _logger->notifyAttributeChanged(*this, key, old_value, new_value);
432 }
434 _observers.notifyAttributeChanged(*this, key, old_value, new_value);
435 }
436 }
438 void SimpleNode::addChild(Node *child, Node *ref) {
439 g_assert(child);
440 g_assert(!ref || ref->parent() == this);
441 g_assert(!child->parent());
443 Debug::EventTracker<DebugAddChild> tracker(*this, *child, ref);
445 Node *next;
446 if (ref) {
447 next = ref->next();
448 ref->_setNext(child);
449 } else {
450 next = _first_child;
451 _first_child = child;
452 }
453 if (!next) { // appending?
454 _last_child = child;
455 // set cached position if possible when appending
456 if (!ref) {
457 // if !next && !ref, child is sole child
458 child->_setCachedPosition(0);
459 _cached_positions_valid = true;
460 } else if (_cached_positions_valid) {
461 child->_setCachedPosition(ref->_cachedPosition() + 1);
462 }
463 } else {
464 // invalidate cached positions otherwise
465 _cached_positions_valid = false;
466 }
468 child->_setParent(this);
469 child->_setNext(next);
470 _child_count++;
472 if (_document) {
473 child->_bindDocument(*_document);
474 }
475 if (_logger) {
476 child->_bindLogger(*_logger);
477 _logger->notifyChildAdded(*this, *child, ref);
478 }
480 _observers.notifyChildAdded(*this, *child, ref);
481 }
483 void SimpleNode::_bindDocument(Document &document) {
484 g_assert(!_document || _document == &document);
486 if (!_document) {
487 _document = &document;
489 for ( Node *child = _first_child ; child != NULL ; child = child->next() ) {
490 child->_bindDocument(document);
491 }
492 }
493 }
495 void SimpleNode::_bindLogger(TransactionLogger &logger) {
496 g_assert(!_logger || _logger == &logger);
498 if (!_logger) {
499 _logger = &logger;
501 for ( Node *child = _first_child ; child != NULL ; child = child->next() ) {
502 child->_bindLogger(logger);
503 }
504 }
505 }
507 void SimpleNode::removeChild(Node *child) {
508 g_assert(child);
509 g_assert(child->parent() == this);
511 Node *ref = ( child != _first_child ? previous_node(child) : NULL );
513 Debug::EventTracker<DebugRemoveChild> tracker(*this, *child, ref);
515 Node *next = child->next();
516 if (ref) {
517 ref->_setNext(next);
518 } else {
519 _first_child = next;
520 }
521 if (!next) { // removing the last child?
522 _last_child = ref;
523 } else {
524 // removing any other child invalidates the cached positions
525 _cached_positions_valid = false;
526 }
528 child->_setNext(NULL);
529 child->_setParent(NULL);
530 _child_count--;
532 if (_logger) {
533 _logger->notifyChildRemoved(*this, *child, ref);
534 }
536 _observers.notifyChildRemoved(*this, *child, ref);
537 }
539 void SimpleNode::changeOrder(Node *child, Node *ref) {
540 g_return_if_fail(child);
541 g_return_if_fail(child->parent() == this);
542 g_return_if_fail(child != ref);
543 g_return_if_fail(!ref || ref->parent() == this);
545 Node *const prev = previous_node(child);
547 Debug::EventTracker<DebugSetChildPosition> tracker(*this, *child, prev, ref);
549 if (prev == ref) { return; }
551 Node *next;
553 /* Remove from old position. */
554 next=child->next();
555 if (prev) {
556 prev->_setNext(next);
557 } else {
558 _first_child = next;
559 }
560 if (!next) {
561 _last_child = prev;
562 }
564 /* Insert at new position. */
565 if (ref) {
566 next = ref->next();
567 ref->_setNext(child);
568 } else {
569 next = _first_child;
570 _first_child = child;
571 }
572 child->_setNext(next);
573 if (!next) {
574 _last_child = child;
575 }
577 _cached_positions_valid = false;
579 if (_logger) {
580 _logger->notifyChildOrderChanged(*this, *child, prev, ref);
581 }
583 _observers.notifyChildOrderChanged(*this, *child, prev, ref);
584 }
586 void SimpleNode::setPosition(int pos) {
587 g_return_if_fail(_parent != NULL);
589 // a position beyond the end of the list means the end of the list;
590 // a negative position is the same as an infinitely large position
592 Node *ref=NULL;
593 for ( Node *sibling = _parent->firstChild() ;
594 sibling && pos ; sibling = sibling->next() )
595 {
596 if ( sibling != this ) {
597 ref = sibling;
598 pos--;
599 }
600 }
602 _parent->changeOrder(this, ref);
603 }
605 namespace {
607 void child_added(Node *node, Node *child, Node *ref, void *data) {
608 reinterpret_cast<NodeObserver *>(data)->notifyChildAdded(*node, *child, ref);
609 }
611 void child_removed(Node *node, Node *child, Node *ref, void *data) {
612 reinterpret_cast<NodeObserver *>(data)->notifyChildRemoved(*node, *child, ref);
613 }
615 void content_changed(Node *node, gchar const *old_content, gchar const *new_content, void *data) {
616 reinterpret_cast<NodeObserver *>(data)->notifyContentChanged(*node, Util::SharedCStringPtr::coerce((const char *)old_content), Util::SharedCStringPtr::coerce((const char *)new_content));
617 }
619 void attr_changed(Node *node, gchar const *name, gchar const *old_value, gchar const *new_value, bool is_interactive, void *data) {
620 reinterpret_cast<NodeObserver *>(data)->notifyAttributeChanged(*node, g_quark_from_string(name), Util::SharedCStringPtr::coerce((const char *)old_value), Util::SharedCStringPtr::coerce((const char *)new_value));
621 }
623 void order_changed(Node *node, Node *child, Node *old_ref, Node *new_ref, void *data) {
624 reinterpret_cast<NodeObserver *>(data)->notifyChildOrderChanged(*node, *child, old_ref, new_ref);
625 }
627 const NodeEventVector OBSERVER_EVENT_VECTOR = {
628 &child_added,
629 &child_removed,
630 &attr_changed,
631 &content_changed,
632 &order_changed
633 };
635 };
637 void SimpleNode::synthesizeEvents(NodeEventVector const *vector, void *data) {
638 if (vector->attr_changed) {
639 for ( List<AttributeRecord const> iter = _attributes ;
640 iter ; ++iter )
641 {
642 vector->attr_changed(this, g_quark_to_string(iter->key), NULL, iter->value, false, data);
643 }
644 }
645 if (vector->child_added) {
646 Node *ref = NULL;
647 for ( Node *child = this->_first_child ;
648 child ; child = child->next() )
649 {
650 vector->child_added(this, child, ref, data);
651 ref = child;
652 }
653 }
654 if (vector->content_changed) {
655 vector->content_changed(this, NULL, this->_content, data);
656 }
657 }
659 void SimpleNode::synthesizeEvents(NodeObserver &observer) {
660 synthesizeEvents(&OBSERVER_EVENT_VECTOR, &observer);
661 }
663 Node *SimpleNode::root() {
664 Node *parent=this;
665 while (parent->parent()) {
666 parent = parent->parent();
667 }
669 if ( parent->type() == DOCUMENT_NODE ) {
670 for ( Node *child = _document->firstChild() ;
671 child ; child = child->next() )
672 {
673 if ( child->type() == ELEMENT_NODE ) {
674 return child;
675 }
676 }
677 return NULL;
678 } else if ( parent->type() == ELEMENT_NODE ) {
679 return parent;
680 } else {
681 return NULL;
682 }
683 }
685 void SimpleNode::mergeFrom(Node const *src, gchar const *key) {
686 g_return_if_fail(src != NULL);
687 g_return_if_fail(key != NULL);
688 g_assert(src != this);
690 setContent(src->content());
692 for ( Node const *child = src->firstChild() ; child != NULL ; child = child->next() )
693 {
694 gchar const *id = child->attribute(key);
695 if (id) {
696 Node *rch=sp_repr_lookup_child(this, key, id);
697 if (rch) {
698 rch->mergeFrom(child, key);
699 } else {
700 rch = child->duplicate();
701 appendChild(rch);
702 rch->release();
703 }
704 } else {
705 Node *rch=child->duplicate();
706 appendChild(rch);
707 rch->release();
708 }
709 }
711 for ( List<AttributeRecord const> iter = src->attributeList() ;
712 iter ; ++iter )
713 {
714 setAttribute(g_quark_to_string(iter->key), iter->value);
715 }
716 }
718 }
720 }
722 /*
723 Local Variables:
724 mode:c++
725 c-file-style:"stroustrup"
726 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
727 indent-tabs-mode:nil
728 fill-column:99
729 End:
730 */
731 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :