bool DomNode::operator ==(const DomNode &other) const { if (is_null() || other.is_null()) return is_null() == other.is_null(); else return impl->node_index == other.impl->node_index; }
DomElement DomElement::get_next_sibling_element() const { DomNode node = get_next_sibling(); while (!node.is_null() && !node.is_element()) node = node.get_next_sibling(); return node.to_element(); }
void TexturePacker::process_resource(Canvas &canvas, Resource &item_resource, std::vector<Subtexture> &packed_sub_textures, std::map<Texture, std::string> &generated_texture_filenames, int &generated_texture_index, const std::string &image_pathname ) { // Found a sprite resource, lets modify its content! Resource resource = item_resource; // Iterate through all nodes, and remove all previous image tags DomElement &element = resource.get_element(); DomNode cur = element.get_first_child(); while (!cur.is_null()) { DomNode next = cur.get_next_sibling(); DomNode::NodeType nodeType = (DomNode::NodeType)cur.get_node_type(); // Only remove the <image> tag, as we want to keep the other sprite attributes if (cur.get_node_name() == "image") element.remove_child(cur); cur = next; } // Add new image tag to resource DOM std::vector<Subtexture>::size_type index, size; size = packed_sub_textures.size(); for(index = 0; index < size; ++index) { Subtexture subtexture = packed_sub_textures[index]; // Try to find out if we already have created a texture-on-disk for this subtexture std::string texture_filename; Texture2D texture = subtexture.get_texture(); std::map<Texture, std::string>::iterator it; it = generated_texture_filenames.find(texture); if(it == generated_texture_filenames.end()) { // Texture not found, generate a filename and dump texture to disk texture_filename = string_format("texture%1.png", ++generated_texture_index); PNGProvider::save(texture.get_pixeldata(canvas), image_pathname + texture_filename); generated_texture_filenames[texture] = texture_filename; } else { // Previously dumped textures, lets reuse the filename texture_filename = (*it).second; } // Add <grid> DOM element DomElement new_grid_element = element.get_owner_document().create_element("grid"); new_grid_element.set_attribute("pos", string_format("%1,%2", subtexture.get_geometry().left + last_border_size, subtexture.get_geometry().top + last_border_size)); new_grid_element.set_attribute("size", string_format("%1,%2", subtexture.get_geometry().get_width()- last_border_size*2, subtexture.get_geometry().get_height()- last_border_size*2)); // Add <image> DOM element DomElement new_image_element = element.get_owner_document().create_element("image"); new_image_element.set_attribute("file", texture_filename); new_image_element.append_child(new_grid_element); // Add <image> element under <sprite> element element.append_child(new_image_element); } }
std::string DomNode::select_string(const DomString &xpath_expression) const { DomNode node = select_node(xpath_expression); if (node.is_element()) return node.to_element().get_text(); else return node.get_node_value(); }
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(const DomString &name) const { DomNode node = get_first_child(); while (node.is_null() == false) { if (node.get_node_name() == name) return node; node = node.get_next_sibling(); } return DomNode(); }
DomNodeList DomNode::get_child_nodes() const { DomNodeList list; DomNode node = get_first_child(); while (!node.is_null()) { list.add_item(node); node = node.get_next_sibling(); } return list; }
bool DomElement::has_attribute_ns( const DomString &namespace_uri, const DomString &local_name) const { if (impl) { DomNode attribute = get_attributes().get_named_item_ns(namespace_uri, local_name); return attribute.is_attr(); } return false; }
DomElement DomDocument::get_document_element() { DomNode cur = get_first_child(); while (!cur.is_null()) { if (cur.is_element()) return cur.to_element(); cur = cur.get_next_sibling(); } return DomElement(); }
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(); }
DomNodeList::DomNodeList( DomNode &node, const DomString &namespace_uri, const DomString &name, bool local_name) { DomNode current_child = node.get_first_child(); while (!current_child.is_null()) { if (local_name) { if (current_child.get_namespace_uri() == namespace_uri && current_child.get_local_name() == name) { add_item(current_child); } } else { if (current_child.get_namespace_uri() == namespace_uri && current_child.get_node_name() == name) { add_item(current_child); } } current_child = current_child.get_next_sibling(); } }
int LuaDomElement2::getChildren(lua_State *L) { DomElement* obj = DomElementValue::check2( L, 1 ); DomNode* n = obj->getFirst(); lua_newtable( L ); int t = lua_gettop( L ); int j = 1; while( n ) { if( pushNode( L, n ) ) // Wir wollen nur Text und Element { lua_rawseti( L, t, j ); j++; } n = n->getNext(); } return 1; }
ProgramObject ProgramObject::load( GraphicContext &gc, const std::string &resource_id, const XMLResourceDocument &resources) { ProgramObject program_object(gc); XMLResourceNode resource = resources.get_resource(resource_id); DomNode node = resource.get_element().get_first_child(); while (!node.is_null()) { if (node.is_element()) { DomElement element = node.to_element(); if (element.get_tag_name() == "shader") { ShaderObject shader = ShaderObject::load(gc, element.get_attribute("name"), resources); program_object.attach(shader); } else if (element.get_tag_name() == "bind-attribute") { program_object.bind_attribute_location( StringHelp::text_to_int(element.get_attribute("index")), element.get_attribute("name")); } } node = node.get_next_sibling(); } if (!resource.get_element().get_attribute("shader").empty()) { ShaderObject shader = ShaderObject::load(gc, resource.get_element().get_attribute("shader"), resources); program_object.attach(shader); } if (resource.get_element().get_attribute("link", "true") == "true") if(!program_object.link()) throw Exception(string_format("Unable to link program object: %1", program_object.get_info_log())); return program_object; }
DomString DomNode::find_prefix(const DomString &namespace_uri) const { DomElement cur = to_element(); while (!cur.is_null()) { DomNamedNodeMap attributes = cur.get_attributes(); int size = attributes.get_length(); for (int index = 0; index < size; index++) { DomNode attribute = attributes.item(index); if (attribute.get_prefix() == "xmlns" && attribute.get_node_value() == namespace_uri) { return attribute.get_local_name(); } } cur = cur.get_parent_node().to_element(); } return DomString(); }
DomNodeList::DomNodeList(DomNode &node, const DomString &tag_name) { DomNode current_child = node.get_first_child(); while (!current_child.is_null()) { if (current_child.get_node_name() == tag_name) { add_item(current_child); } current_child = current_child.get_next_sibling(); } }
ResourceItem *TexturePacker::load_resource(Canvas &canvas, std::string &resource_id, Resource &resource, ResourceManager &resources) { ResourceItem *item = 0; try { std::string type = resource.get_type(); if(type == "sprite") { item = load_sprite(canvas, resource_id, resource, resources); } else if(type == "image") { item = load_image(canvas, resource_id, resource, resources); } else { throw Exception(string_format("Resourcetype %1 is not supported", type)); } } catch(Exception ex) { item = new NotSupportedResourceItem(resource, ex.message); } // Find resource path by traversing parents DomElement &dom_element = resource.get_element(); DomNode parent = dom_element.get_parent_node(); while (!parent.is_null()) { DomElement parent_element = parent.to_element(); std::string parent_name = parent_element.get_attribute("name"); if(parent_name.length() > 0) item->resource_path = string_format("%1/%2", parent_name, item->resource_path); parent = parent.get_parent_node(); } return item; }
DomNode DomNamedNodeMap::set_named_item(const DomNode &node) { if (!impl) return DomNode(); DomDocument_Impl *doc_impl = (DomDocument_Impl *) impl->owner_document.lock().get(); DomString name = node.get_node_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) { if (cur_attribute->get_node_name() == 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; }
std::string DomElement::get_text() const { std::string str; if (has_child_nodes() == false) return str; DomNode cur = get_first_child(); while (!cur.is_null()) { if (cur.is_text() || cur.is_cdata_section()) str.append(cur.get_node_value()); if (cur.is_element()) str.append(cur.to_element().get_text()); cur = cur.get_next_sibling(); } return str; }
void XMLResourceDocument::destroy_resource(const std::string &resource_id) { std::map<std::string, XMLResourceNode>::iterator it; it = impl->resources.find(resource_id); if (it == impl->resources.end()) return; DomNode cur = it->second.get_element(); impl->resources.erase(it); DomNode parent = cur.get_parent_node(); while (!parent.is_null()) { parent.remove_child(cur); if (parent.has_child_nodes()) break; cur = parent; parent = parent.get_parent_node(); } }
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; }
/** * Appends a child node. * @param child the node to append to this document node */ void DomNode::appendNode( const DomNode &child ) { const QString childStr ( child.toString() ); closeTag( (childStr.length() >= 2 && (childStr[0] == '<' || childStr[1] == '<')) ); str += childStr; }
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]; }
std::vector<DomNode> DomDocument::load( IODevice &input, bool eat_whitespace, DomNode insert_point) { clear_all(); XMLTokenizer tokenizer(input); tokenizer.set_eat_whitespace(eat_whitespace); if (insert_point.is_element() == false) insert_point = *this; std::vector<DomNode> node_stack; node_stack.push_back(insert_point); std::vector<DomNode> result; try { XMLToken cur_token; tokenizer.next(&cur_token); while (cur_token.type != XMLToken::NULL_TOKEN) { switch (cur_token.type) { case XMLToken::TEXT_TOKEN: node_stack.back().append_child(create_text_node(cur_token.value)); if (node_stack.back() == insert_point) result.push_back(node_stack.back().get_last_child()); break; case XMLToken::CDATA_SECTION_TOKEN: node_stack.back().append_child(create_cdata_section(cur_token.value)); if (node_stack.back() == insert_point) result.push_back(node_stack.back().get_last_child()); break; case XMLToken::ELEMENT_TOKEN: if (cur_token.variant != XMLToken::END) { DomString namespace_uri = DomDocument_Impl::find_namespace_uri(cur_token.name, cur_token, node_stack.back()); DomElement element = create_element_ns(namespace_uri, cur_token.name); node_stack.back().append_child(element); if (node_stack.back() == insert_point) result.push_back(node_stack.back().get_last_child()); int size = (int)cur_token.attributes.size(); for (int i = 0; i < size; i++) { XMLToken::Attribute &attribute = cur_token.attributes[i]; DomString attribute_namespace_uri = DomDocument_Impl::find_namespace_uri(attribute.first, cur_token, node_stack.back()); element.set_attribute_ns( attribute_namespace_uri, attribute.first, attribute.second); } if (cur_token.variant == XMLToken::BEGIN) node_stack.push_back(element); } else { node_stack.pop_back(); if (node_stack.empty()) throw Exception("Malformed XML tree!"); } break; case XMLToken::NULL_TOKEN: break; case XMLToken::ENTITY_REFERENCE_TOKEN: break; case XMLToken::ENTITY_TOKEN: break; case XMLToken::COMMENT_TOKEN: node_stack.back().append_child(create_comment(cur_token.value)); if (node_stack.back() == insert_point) result.push_back(node_stack.back().get_last_child()); break; case XMLToken::DOCUMENT_TYPE_TOKEN: break; case XMLToken::NOTATION_TOKEN: break; case XMLToken::PROCESSING_INSTRUCTION_TOKEN: node_stack.back().append_child(create_processing_instruction(cur_token.name, cur_token.value)); if (node_stack.back() == insert_point) result.push_back(node_stack.back().get_last_child()); break; } tokenizer.next(&cur_token); } } catch (const Exception& e) { for (auto & elem : result) { insert_point.remove_child(elem); } throw; } return result; }