DomNode DomNamedNodeMap::set_named_item_ns(const DomNode &node) { if (!impl) return DomNode(); DomDocument_Impl *doc_impl = (DomDocument_Impl *) impl->owner_document.lock().get(); DomString namespace_uri = node.get_namespace_uri(); DomString local_name = node.get_local_name(); DomTreeNode *new_tree_node = (DomTreeNode *) node.impl->get_tree_node(); DomTreeNode *tree_node = impl->get_tree_node(); if (new_tree_node == tree_node) return node; unsigned int cur_index = tree_node->first_attribute; unsigned int last_index = cl_null_node_index; DomTreeNode *cur_attribute = tree_node->get_first_attribute(doc_impl); while (cur_attribute) { std::string lname = cur_attribute->get_node_name(); std::string::size_type lpos = lname.find_first_of(':'); if (lpos != std::string::npos) lname = lname.substr(lpos + 1); if (cur_attribute->get_namespace_uri() == namespace_uri && lname == local_name) { new_tree_node->parent = cur_attribute->parent; new_tree_node->previous_sibling = cur_attribute->previous_sibling; new_tree_node->next_sibling = cur_attribute->next_sibling; if (cur_attribute->previous_sibling == cl_null_node_index) tree_node->first_attribute = node.impl->node_index; else cur_attribute->get_previous_sibling(doc_impl)->next_sibling = node.impl->node_index; if (cur_attribute->next_sibling != cl_null_node_index) cur_attribute->get_next_sibling(doc_impl)->previous_sibling = node.impl->node_index; cur_attribute->parent = cl_null_node_index; cur_attribute->previous_sibling = cl_null_node_index; cur_attribute->next_sibling = cl_null_node_index; return node; } last_index = cur_index; cur_index = cur_attribute->next_sibling; cur_attribute = cur_attribute->get_next_sibling(doc_impl); } if (last_index == cl_null_node_index) { tree_node->first_attribute = node.impl->node_index; new_tree_node->parent = impl->node_index; new_tree_node->previous_sibling = cl_null_node_index; new_tree_node->next_sibling = cl_null_node_index; } else { new_tree_node->parent = impl->node_index; new_tree_node->previous_sibling = last_index; new_tree_node->next_sibling = cl_null_node_index; doc_impl->nodes[last_index]->next_sibling = node.impl->node_index; } return node; }
DomNode DomNode::named_item_ns( const DomString &namespace_uri, const DomString &local_name) const { DomNode node = get_first_child(); while (node.is_null() == false) { if (node.get_namespace_uri() == namespace_uri && node.get_local_name() == local_name) return node; node = node.get_next_sibling(); } return DomNode(); }
DomNode DomDocument::import_node(const DomNode &node, bool deep) { DomNode imported_node; switch (node.get_node_type()) { case NULL_NODE: return imported_node; case ELEMENT_NODE: imported_node = create_element_ns(node.get_namespace_uri(), node.get_node_name()); break; case ATTRIBUTE_NODE: imported_node = create_attribute_ns(node.get_namespace_uri(), node.get_node_name()); imported_node.set_node_value(node.get_node_value()); break; case TEXT_NODE: imported_node = create_text_node(node.get_node_value()); break; case CDATA_SECTION_NODE: imported_node = create_cdata_section(node.get_node_value()); break; case ENTITY_REFERENCE_NODE: imported_node = create_entity_reference(node.get_node_name()); break; case ENTITY_NODE: return imported_node; case PROCESSING_INSTRUCTION_NODE: imported_node = create_processing_instruction(node.to_processing_instruction().get_target(), node.to_processing_instruction().get_data()); break; case COMMENT_NODE: imported_node = create_comment(node.get_node_value()); break; case DOCUMENT_NODE: imported_node = create_document_fragment(); break; case DOCUMENT_TYPE_NODE: return imported_node; case DOCUMENT_FRAGMENT_NODE: imported_node = create_document_fragment(); break; case NOTATION_NODE: return imported_node; } if (node.is_element()) { DomElement import_element = imported_node.to_element(); DomNamedNodeMap node_attributes = node.get_attributes(); int size = node_attributes.get_length(); for (int index = 0; index < size; index++) { DomNode attr = node_attributes.item(index); import_element.set_attribute_node_ns(import_node(attr, deep).to_attr()); } } if (deep) { DomNode cur = node.get_first_child(); while (!cur.is_null()) { imported_node.append_child(import_node(cur, true)); cur = cur.get_next_sibling(); } } return imported_node; }
XMLResourceNode XMLResourceDocument::create_resource(const std::string &resource_id, const std::string &type) { if (resource_exists(resource_id)) throw Exception(string_format("Resource %1 already exists", resource_id)); std::vector<std::string> path_elements = PathHelp::split_basepath(resource_id); std::string name = PathHelp::get_filename(resource_id); // Walk tree as deep as we can get: DomNode parent = impl->document.get_document_element(); DomNode cur = parent.get_first_child(); auto path_it = path_elements.begin(); while (!cur.is_null() && path_it != path_elements.end()) { if (cur.is_element() && cur.get_namespace_uri() == impl->ns_resources && cur.get_local_name() == "section") { DomElement element = cur.to_element(); std::string name = element.get_attribute_ns(impl->ns_resources, "name"); if (name == *path_it) { ++path_it; parent = cur; cur = cur.get_first_child(); continue; } } cur = cur.get_next_sibling(); } // Create any missing parent nodes: std::string prefix = parent.get_prefix(); while (path_it != path_elements.end()) { DomElement section; if (prefix.empty()) { section = impl->document.create_element_ns( impl->ns_resources, (*path_it) ); } else { section = impl->document.create_element_ns( impl->ns_resources,(prefix + ":" + *path_it)); } parent.append_child(section); parent = section; ++path_it; } // Create node: DomElement resource_node; if (prefix.empty()) { resource_node = impl->document.create_element_ns( impl->ns_resources, (type)); } else { resource_node = impl->document.create_element_ns( impl->ns_resources,(prefix + ":" + type)); } resource_node.set_attribute_ns( impl->ns_resources, prefix.empty() ? "name" : (prefix + ":name"), name); parent.append_child(resource_node); // Create resource: impl->resources[resource_id] = XMLResourceNode(resource_node, *this); return impl->resources[resource_id]; }