CL_DomString CL_DomNode::find_prefix(const CL_DomString &namespace_uri) const { CL_DomElement cur = to_element(); while (!cur.is_null()) { CL_DomNamedNodeMap attributes = cur.get_attributes(); int size = attributes.get_length(); for (int index = 0; index < size; index++) { CL_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 CL_DomString(); }
CL_Resource CL_ResourceManager::create_resource(const CL_String &resource_id, const CL_String &type) { if (resource_exists(resource_id)) throw CL_Exception(cl_format("Resource %1 already exists", resource_id)); std::vector<CL_String> path_elements = CL_PathHelp::split_basepath(resource_id); CL_String name = CL_PathHelp::get_filename(resource_id); // Walk tree as deep as we can get: CL_DomNode parent = impl->document.get_document_element(); CL_DomNode cur = parent.get_first_child(); std::vector<CL_String>::iterator 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") { CL_DomElement element = cur.to_element(); CL_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: CL_String prefix = parent.get_prefix(); while (path_it != path_elements.end()) { CL_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: CL_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] = CL_Resource(resource_node, *this); return impl->resources[resource_id]; }