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"
24 #include "debug/simple-event.h"
25 #include "util/share.h"
26 #include "util/format.h"
28 namespace Inkscape {
30 namespace XML {
32 namespace {
34 Util::ptr_shared<char> stringify_node(Node const &node) {
35 gchar *string;
36 switch (node.type()) {
37 case ELEMENT_NODE: {
38 char const *id=node.attribute("id");
39 if (id) {
40 string = g_strdup_printf("element(%p)=%s(#%s)", &node, node.name(), id);
41 } else {
42 string = g_strdup_printf("element(%p)=%s", &node, node.name());
43 }
44 } break;
45 case TEXT_NODE:
46 string = g_strdup_printf("text(%p)=%s", &node, node.content());
47 break;
48 case COMMENT_NODE:
49 string = g_strdup_printf("comment(%p)=<!--%s-->", &node, node.content());
50 break;
51 case DOCUMENT_NODE:
52 string = g_strdup_printf("document(%p)", &node);
53 break;
54 default:
55 string = g_strdup_printf("unknown(%p)", &node);
56 }
57 Util::ptr_shared<char> result=Util::share_string(string);
58 g_free(string);
59 return result;
60 }
62 typedef Debug::SimpleEvent<Debug::Event::XML> DebugXML;
64 class DebugXMLNode : public DebugXML {
65 public:
66 DebugXMLNode(Node const &node, Util::ptr_shared<char> name)
67 : DebugXML(name)
68 {
69 _addProperty("node", stringify_node(node));
70 }
71 };
73 class DebugAddChild : public DebugXMLNode {
74 public:
75 DebugAddChild(Node const &node, Node const &child, Node const *prev)
76 : DebugXMLNode(node, Util::share_static_string("add-child"))
77 {
78 _addProperty("child", stringify_node(child));
79 _addProperty("position", Util::format("%d", ( prev ? prev->position() + 1 : 0 )));
80 }
81 };
83 class DebugRemoveChild : public DebugXMLNode {
84 public:
85 DebugRemoveChild(Node const &node, Node const &child)
86 : DebugXMLNode(node, Util::share_static_string("remove-child"))
87 {
88 _addProperty("child", stringify_node(child));
89 }
90 };
92 class DebugSetChildPosition : public DebugXMLNode {
93 public:
94 DebugSetChildPosition(Node const &node, Node const &child,
95 Node const *old_prev, Node const *new_prev)
96 : DebugXMLNode(node, Util::share_static_string("set-child-position"))
97 {
98 _addProperty("child", stringify_node(child));
100 unsigned old_position = ( old_prev ? old_prev->position() : 0 );
101 unsigned position = ( new_prev ? new_prev->position() : 0 );
102 if ( position > old_position ) {
103 --position;
104 }
106 _addProperty("position", Util::format("%d", position));
107 }
108 };
110 class DebugSetContent : public DebugXMLNode {
111 public:
112 DebugSetContent(Node const &node,
113 Util::ptr_shared<char> content)
114 : DebugXMLNode(node, Util::share_static_string("set-content"))
115 {
116 _addProperty("content", content);
117 }
118 };
120 class DebugClearContent : public DebugXMLNode {
121 public:
122 DebugClearContent(Node const &node)
123 : DebugXMLNode(node, Util::share_static_string("clear-content"))
124 {}
125 };
127 class DebugSetAttribute : public DebugXMLNode {
128 public:
129 DebugSetAttribute(Node const &node,
130 GQuark name,
131 Util::ptr_shared<char> value)
132 : DebugXMLNode(node, Util::share_static_string("set-attribute"))
133 {
134 _addProperty("name", Util::share_static_string(g_quark_to_string(name)));
135 _addProperty("value", value);
136 }
137 };
139 class DebugClearAttribute : public DebugXMLNode {
140 public:
141 DebugClearAttribute(Node const &node, GQuark name)
142 : DebugXMLNode(node, Util::share_static_string("clear-attribute"))
143 {
144 _addProperty("name", Util::share_static_string(g_quark_to_string(name)));
145 }
146 };
148 }
150 using Util::ptr_shared;
151 using Util::share_string;
152 using Util::share_unsafe;
153 using Util::share_static_string;
154 using Util::List;
155 using Util::MutableList;
156 using Util::cons;
157 using Util::rest;
158 using Util::set_rest;
160 SimpleNode::SimpleNode(int code)
161 : Node(), _name(code), _attributes(), _child_count(0),
162 _cached_positions_valid(false)
163 {
164 this->_document = NULL;
165 this->_document = NULL;
166 this->_parent = this->_next = NULL;
167 this->_first_child = this->_last_child = NULL;
168 }
170 SimpleNode::SimpleNode(SimpleNode const &node)
171 : Node(),
172 _cached_position(node._cached_position),
173 _name(node._name), _attributes(), _content(node._content),
174 _child_count(node._child_count),
175 _cached_positions_valid(node._cached_positions_valid)
176 {
177 _document = NULL;
178 _parent = _next = NULL;
179 _first_child = _last_child = NULL;
181 for ( Node *child = node._first_child ;
182 child != NULL ; child = child->next() )
183 {
184 Node *child_copy=child->duplicate(NULL); // FIXME
186 child_copy->_setParent(this);
187 if (_last_child) {
188 _last_child->_setNext(child_copy);
189 } else {
190 _first_child = child_copy;
191 }
192 _last_child = child_copy;
194 child_copy->release(); // release to avoid a leak
195 }
197 for ( List<AttributeRecord const> iter = node._attributes ;
198 iter ; ++iter )
199 {
200 _attributes = cons(*iter, _attributes);
201 }
202 }
204 gchar const *SimpleNode::name() const {
205 return g_quark_to_string(_name);
206 }
208 gchar const *SimpleNode::content() const {
209 return this->_content;
210 }
212 gchar const *SimpleNode::attribute(gchar const *name) const {
213 g_return_val_if_fail(name != NULL, NULL);
215 GQuark const key = g_quark_from_string(name);
217 for ( List<AttributeRecord const> iter = _attributes ;
218 iter ; ++iter )
219 {
220 if ( iter->key == key ) {
221 return iter->value;
222 }
223 }
225 return NULL;
226 }
228 unsigned SimpleNode::position() const {
229 g_return_val_if_fail(_parent != NULL, 0);
230 return _parent->_childPosition(*this);
231 }
233 unsigned SimpleNode::_childPosition(Node const &child) const {
234 if (!_cached_positions_valid) {
235 unsigned position=0;
236 for ( Node *sibling = _first_child ;
237 sibling ; sibling = sibling->next() )
238 {
239 sibling->_setCachedPosition(position);
240 position++;
241 }
242 _cached_positions_valid = true;
243 }
244 return child._cachedPosition();
245 }
247 Node *SimpleNode::nthChild(unsigned index) {
248 Node *child = _first_child;
249 for ( ; index > 0 && child ; child = child->next() ) {
250 index--;
251 }
252 return child;
253 }
255 bool SimpleNode::matchAttributeName(gchar const *partial_name) const {
256 g_return_val_if_fail(partial_name != NULL, false);
258 for ( List<AttributeRecord const> iter = _attributes ;
259 iter ; ++iter )
260 {
261 gchar const *name = g_quark_to_string(iter->key);
262 if (std::strstr(name, partial_name)) {
263 return true;
264 }
265 }
267 return false;
268 }
270 void SimpleNode::setContent(gchar const *content) {
271 ptr_shared<char> old_content=_content;
272 ptr_shared<char> new_content = ( content ? share_string(content) : ptr_shared<char>() );
274 Debug::EventTracker<> tracker;
275 if (new_content) {
276 tracker.set<DebugSetContent>(*this, new_content);
277 } else {
278 tracker.set<DebugClearContent>(*this);
279 }
281 _content = new_content;
283 if ( _content != old_content ) {
284 if (_document) {
285 _document->logger()->notifyContentChanged(*this, old_content, _content);
286 }
288 _observers.notifyContentChanged(*this, old_content, _content);
289 }
290 }
292 void
293 SimpleNode::setAttribute(gchar const *name, gchar const *value, bool const is_interactive)
294 {
295 g_return_if_fail(name && *name);
297 GQuark const key = g_quark_from_string(name);
299 MutableList<AttributeRecord> ref;
300 MutableList<AttributeRecord> existing;
301 for ( existing = _attributes ; existing ; ++existing ) {
302 if ( existing->key == key ) {
303 break;
304 }
305 ref = existing;
306 }
308 Debug::EventTracker<> tracker;
310 ptr_shared<char> old_value=( existing ? existing->value : ptr_shared<char>() );
312 ptr_shared<char> new_value=ptr_shared<char>();
313 if (value) {
314 new_value = share_string(value);
315 tracker.set<DebugSetAttribute>(*this, key, new_value);
316 if (!existing) {
317 if (ref) {
318 set_rest(ref, MutableList<AttributeRecord>(AttributeRecord(key, new_value)));
319 } else {
320 _attributes = MutableList<AttributeRecord>(AttributeRecord(key, new_value));
321 }
322 } else {
323 existing->value = new_value;
324 }
325 } else {
326 tracker.set<DebugClearAttribute>(*this, key);
327 if (existing) {
328 if (ref) {
329 set_rest(ref, rest(existing));
330 } else {
331 _attributes = rest(existing);
332 }
333 set_rest(existing, MutableList<AttributeRecord>());
334 }
335 }
337 if ( new_value != old_value && (!old_value || !new_value || strcmp(old_value, new_value))) {
338 if (_document) {
339 _document->logger()->notifyAttributeChanged(*this, key, old_value, new_value);
340 }
342 _observers.notifyAttributeChanged(*this, key, old_value, new_value);
343 }
344 }
346 void SimpleNode::addChild(Node *child, Node *ref) {
347 g_assert(child);
348 g_assert(!ref || ref->parent() == this);
349 g_assert(!child->parent());
351 Debug::EventTracker<DebugAddChild> tracker(*this, *child, ref);
353 Node *next;
354 if (ref) {
355 next = ref->next();
356 ref->_setNext(child);
357 } else {
358 next = _first_child;
359 _first_child = child;
360 }
361 if (!next) { // appending?
362 _last_child = child;
363 // set cached position if possible when appending
364 if (!ref) {
365 // if !next && !ref, child is sole child
366 child->_setCachedPosition(0);
367 _cached_positions_valid = true;
368 } else if (_cached_positions_valid) {
369 child->_setCachedPosition(ref->_cachedPosition() + 1);
370 }
371 } else {
372 // invalidate cached positions otherwise
373 _cached_positions_valid = false;
374 }
376 child->_setParent(this);
377 child->_setNext(next);
378 _child_count++;
380 if (_document) {
381 child->_bindDocument(*_document);
382 _document->logger()->notifyChildAdded(*this, *child, ref);
383 }
385 _observers.notifyChildAdded(*this, *child, ref);
386 }
388 void SimpleNode::_bindDocument(Document &document) {
389 g_assert(!_document || _document == &document);
391 if (!_document) {
392 _document = &document;
394 for ( Node *child = _first_child ; child != NULL ; child = child->next() ) {
395 child->_bindDocument(document);
396 }
397 }
398 }
400 void SimpleNode::removeChild(Node *child) {
401 g_assert(child);
402 g_assert(child->parent() == this);
404 Node *ref = ( child != _first_child ? previous_node(child) : NULL );
406 Debug::EventTracker<DebugRemoveChild> tracker(*this, *child);
408 Node *next = child->next();
409 if (ref) {
410 ref->_setNext(next);
411 } else {
412 _first_child = next;
413 }
414 if (!next) { // removing the last child?
415 _last_child = ref;
416 } else {
417 // removing any other child invalidates the cached positions
418 _cached_positions_valid = false;
419 }
421 child->_setNext(NULL);
422 child->_setParent(NULL);
423 _child_count--;
425 if (_document) {
426 _document->logger()->notifyChildRemoved(*this, *child, ref);
427 }
429 _observers.notifyChildRemoved(*this, *child, ref);
430 }
432 void SimpleNode::changeOrder(Node *child, Node *ref) {
433 g_return_if_fail(child);
434 g_return_if_fail(child->parent() == this);
435 g_return_if_fail(child != ref);
436 g_return_if_fail(!ref || ref->parent() == this);
438 Node *const prev = previous_node(child);
440 Debug::EventTracker<DebugSetChildPosition> tracker(*this, *child, prev, ref);
442 if (prev == ref) { return; }
444 Node *next;
446 /* Remove from old position. */
447 next=child->next();
448 if (prev) {
449 prev->_setNext(next);
450 } else {
451 _first_child = next;
452 }
453 if (!next) {
454 _last_child = prev;
455 }
457 /* Insert at new position. */
458 if (ref) {
459 next = ref->next();
460 ref->_setNext(child);
461 } else {
462 next = _first_child;
463 _first_child = child;
464 }
465 child->_setNext(next);
466 if (!next) {
467 _last_child = child;
468 }
470 _cached_positions_valid = false;
472 if (_document) {
473 _document->logger()->notifyChildOrderChanged(*this, *child, prev, ref);
474 }
476 _observers.notifyChildOrderChanged(*this, *child, prev, ref);
477 }
479 void SimpleNode::setPosition(int pos) {
480 g_return_if_fail(_parent != NULL);
482 // a position beyond the end of the list means the end of the list;
483 // a negative position is the same as an infinitely large position
485 Node *ref=NULL;
486 for ( Node *sibling = _parent->firstChild() ;
487 sibling && pos ; sibling = sibling->next() )
488 {
489 if ( sibling != this ) {
490 ref = sibling;
491 pos--;
492 }
493 }
495 _parent->changeOrder(this, ref);
496 }
498 namespace {
500 void child_added(Node *node, Node *child, Node *ref, void *data) {
501 reinterpret_cast<NodeObserver *>(data)->notifyChildAdded(*node, *child, ref);
502 }
504 void child_removed(Node *node, Node *child, Node *ref, void *data) {
505 reinterpret_cast<NodeObserver *>(data)->notifyChildRemoved(*node, *child, ref);
506 }
508 void content_changed(Node *node, gchar const *old_content, gchar const *new_content, void *data) {
509 reinterpret_cast<NodeObserver *>(data)->notifyContentChanged(*node, Util::share_unsafe((const char *)old_content), Util::share_unsafe((const char *)new_content));
510 }
512 void attr_changed(Node *node, gchar const *name, gchar const *old_value, gchar const *new_value, bool is_interactive, void *data) {
513 reinterpret_cast<NodeObserver *>(data)->notifyAttributeChanged(*node, g_quark_from_string(name), Util::share_unsafe((const char *)old_value), Util::share_unsafe((const char *)new_value));
514 }
516 void order_changed(Node *node, Node *child, Node *old_ref, Node *new_ref, void *data) {
517 reinterpret_cast<NodeObserver *>(data)->notifyChildOrderChanged(*node, *child, old_ref, new_ref);
518 }
520 const NodeEventVector OBSERVER_EVENT_VECTOR = {
521 &child_added,
522 &child_removed,
523 &attr_changed,
524 &content_changed,
525 &order_changed
526 };
528 };
530 void SimpleNode::synthesizeEvents(NodeEventVector const *vector, void *data) {
531 if (vector->attr_changed) {
532 for ( List<AttributeRecord const> iter = _attributes ;
533 iter ; ++iter )
534 {
535 vector->attr_changed(this, g_quark_to_string(iter->key), NULL, iter->value, false, data);
536 }
537 }
538 if (vector->child_added) {
539 Node *ref = NULL;
540 for ( Node *child = this->_first_child ;
541 child ; child = child->next() )
542 {
543 vector->child_added(this, child, ref, data);
544 ref = child;
545 }
546 }
547 if (vector->content_changed) {
548 vector->content_changed(this, NULL, this->_content, data);
549 }
550 }
552 void SimpleNode::synthesizeEvents(NodeObserver &observer) {
553 synthesizeEvents(&OBSERVER_EVENT_VECTOR, &observer);
554 }
556 Node *SimpleNode::root() {
557 Node *parent=this;
558 while (parent->parent()) {
559 parent = parent->parent();
560 }
562 if ( parent->type() == DOCUMENT_NODE ) {
563 for ( Node *child = _document->firstChild() ;
564 child ; child = child->next() )
565 {
566 if ( child->type() == ELEMENT_NODE ) {
567 return child;
568 }
569 }
570 return NULL;
571 } else if ( parent->type() == ELEMENT_NODE ) {
572 return parent;
573 } else {
574 return NULL;
575 }
576 }
578 void SimpleNode::mergeFrom(Node const *src, gchar const *key) {
579 g_return_if_fail(src != NULL);
580 g_return_if_fail(key != NULL);
581 g_assert(src != this);
583 setContent(src->content());
585 for ( Node const *child = src->firstChild() ; child != NULL ; child = child->next() )
586 {
587 gchar const *id = child->attribute(key);
588 if (id) {
589 Node *rch=sp_repr_lookup_child(this, key, id);
590 if (rch) {
591 rch->mergeFrom(child, key);
592 } else {
593 rch = child->duplicate(_document);
594 appendChild(rch);
595 rch->release();
596 }
597 } else {
598 Node *rch=child->duplicate(_document);
599 appendChild(rch);
600 rch->release();
601 }
602 }
604 for ( List<AttributeRecord const> iter = src->attributeList() ;
605 iter ; ++iter )
606 {
607 setAttribute(g_quark_to_string(iter->key), iter->value);
608 }
609 }
611 }
613 }
615 /*
616 Local Variables:
617 mode:c++
618 c-file-style:"stroustrup"
619 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
620 indent-tabs-mode:nil
621 fill-column:99
622 End:
623 */
624 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :