map<string, CanVariable> CanIdTranslator::translateData(int commandId, string moduleName, string dataHex)
{
	vector<XmlNode> commandNodes = xmlNode.findChild("commands").getChildren();

	for (int n = 0; n < commandNodes.size(); n++)
	{
		map<string, string> attributes = commandNodes[n].getAttributes();

		bool correct = false;

		if (commandId >= 128)
		{
			if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)
			{
				correct = true;
			}
		}
		else if (stoi(attributes["id"]) == commandId)
		{
			correct = true;
		}

		if (correct)
		{
			XmlNode varablesNode = commandNodes[n].findChild("variables");

			return translateData(varablesNode.getChildren(), dataHex);
		}
	}

	map<string, CanVariable> variables;
	return variables;
}
int CanIdTranslator::resolveCommandId(string commandName, string moduleName)
{
	XmlNode commandsNode = xmlNode.findChild("commands");
	vector<XmlNode> commandNodes = commandsNode.getChildren();

	for (int n = 0; n < commandNodes.size(); n++)
	{
		map<string, string> attributes = commandNodes[n].getAttributes();

		if (attributes["name"].compare(commandName) == 0)
		{
			int commandId = stoi(attributes["id"]);

			if (commandId >= 128)
			{
				if (attributes["module"] == moduleName)
				{
					return commandId;
				}
			}
			else
			{
				return commandId;
			}
		}
	}

	return -1;
}
int CanIdTranslator::resolveClassId(string className)
{
	XmlNode classesNode = xmlNode.findChild("classes");
	vector<XmlNode> classNodes = classesNode.getChildren();

	for (int n = 0; n < classNodes.size(); n++)
	{
		map<string, string> attributes = classNodes[n].getAttributes();

		if (attributes["name"].compare(className) == 0)
		{
			return stoi(attributes["id"]);
		}
	}

	return -1;
}
string CanIdTranslator::lookupClassName(int classId)
{
	XmlNode classesNode = xmlNode.findChild("classes");
	vector<XmlNode> classNodes = classesNode.getChildren();

	for (int n = 0; n < classNodes.size(); n++)
	{
		map<string, string> attributes = classNodes[n].getAttributes();

		if (stoi(attributes["id"]) == classId)
		{
			return attributes["name"];
		}
	}

	return "";
}
int CanIdTranslator::resolveModuleId(string moduleName)
{
	XmlNode modulesNode = xmlNode.findChild("modules");
	vector<XmlNode> moduleNodes = modulesNode.getChildren();

	for (int n = 0; n < moduleNodes.size(); n++)
	{
		map<string, string> attributes = moduleNodes[n].getAttributes();

		if (attributes["name"].compare(moduleName) == 0)
		{
			return stoi(attributes["id"]);
		}
	}

	return -1;
}
string CanIdTranslator::lookupModuleName(int moduleId)
{
	XmlNode modulesNode = xmlNode.findChild("modules");
	vector<XmlNode> moduleNodes = modulesNode.getChildren();

	for (int n = 0; n < moduleNodes.size(); n++)
	{
		map<string, string> attributes = moduleNodes[n].getAttributes();

		if (stoi(attributes["id"]) == moduleId)
		{
			return attributes["name"];
		}
	}

	return "";
}
int CanIdTranslator::resolveDirectionFlag(string directionName)
{
	XmlNode definesNode = xmlNode.findChild("defines");
	vector<XmlNode> defineNodes = definesNode.getChildren();

	for (int n = 0; n < defineNodes.size(); n++)
	{
		map<string, string> attributes = defineNodes[n].getAttributes();

		if (attributes["group"].compare("DirectionFlag") == 0 && attributes["name"].compare(directionName) == 0)
		{
			return stoi(attributes["id"]);
		}
	}

	return -1;
}
string CanIdTranslator::lookupDirectionFlag(int directionFlag)
{
	XmlNode definesNode = xmlNode.findChild("defines");
	vector<XmlNode> defineNodes = definesNode.getChildren();

	for (int n = 0; n < defineNodes.size(); n++)
	{
		map<string, string> attributes = defineNodes[n].getAttributes();

		if (attributes["group"].compare("DirectionFlag") == 0 && stoi(attributes["id"]) == directionFlag)
		{
			return attributes["name"];
		}
	}

	return "";
}
string CanIdTranslator::getDefineId(string name, string group)
{
	XmlNode definesNode = xmlNode.findChild("defines");
	vector<XmlNode> defineNodes = definesNode.getChildren();

	for (int n = 0; n < defineNodes.size(); n++)
	{
		map<string, string> attributes = defineNodes[n].getAttributes();

		if (attributes["group"] == group && attributes["name"] == name)
		{
			return attributes["id"];
		}
	}

	return "";
}
int CanIdTranslator::resolveNMTCommandId(string commandName)
{
	XmlNode commandsNode = xmlNode.findChild("nmt_messages");
	vector<XmlNode> commandNodes = commandsNode.getChildren();

	for (int n = 0; n < commandNodes.size(); n++)
	{
		map<string, string> attributes = commandNodes[n].getAttributes();

		if (attributes["name"].compare(commandName) == 0)
		{
			return stoi(attributes["id"]);
		}
	}

	return -1;
}
string CanIdTranslator::lookupNMTCommandName(int commandId)
{
	XmlNode commandsNode = xmlNode.findChild("nmt_messages");
	vector<XmlNode> commandNodes = commandsNode.getChildren();

	for (int n = 0; n < commandNodes.size(); n++)
	{
		map<string, string> attributes = commandNodes[n].getAttributes();

		if (stoi(attributes["id"]) == commandId)
		{
			return attributes["name"];
		}
	}

	return "";
}
void CanIdTranslator::makeNMTDataValid(string commandName, map<string, CanVariable> &data)
{
	vector<XmlNode> commandNodes = xmlNode.findChild("nmt_messages").getChildren();

	for (int n = 0; n < commandNodes.size(); n++)
	{
		map<string, string> attributes = commandNodes[n].getAttributes();

		if (attributes["name"] == commandName)
		{
			XmlNode varablesNode = commandNodes[n].findChild("variables");

			makeDataValid(varablesNode.getChildren(), data);

			return;
		}
	}
}
map<string, CanVariable> CanIdTranslator::translateNMTData(int commandId, string dataHex)
{
	vector<XmlNode> commandNodes = xmlNode.findChild("nmt_messages").getChildren();

	for (int n = 0; n < commandNodes.size(); n++)
	{
		map<string, string> attributes = commandNodes[n].getAttributes();

		if (stoi(attributes["id"]) == commandId)
		{
			XmlNode varablesNode = commandNodes[n].findChild("variables");

			return translateData(varablesNode.getChildren(), dataHex);
		}
	}

	map<string, CanVariable> variables;
	return variables;
}
string CanIdTranslator::lookupCommandName(int commandId, string moduleName)
{
	XmlNode commandsNode = xmlNode.findChild("commands");
	vector<XmlNode> commandNodes = commandsNode.getChildren();

	for (int n = 0; n < commandNodes.size(); n++)
	{
		map<string, string> attributes = commandNodes[n].getAttributes();

		if (commandId >= 128)
		{
			if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)
			{
				return attributes["name"];
			}
		}
		else if (stoi(attributes["id"]) == commandId)
		{
			return attributes["name"];
		}
	}

	return "";
}
void plainconf::handleSpecialCase(XmlNode *pNode)
{
    const XmlNodeList *pUnknownList = pNode->getChildren(UNKNOWN_KEYWORDS);

    if (!pUnknownList)
        return ;

    int bModuleNode = (strcasecmp(pNode->getName(), "module") == 0);
    XmlNodeList::const_iterator iter;

    for (iter = pUnknownList->begin(); iter != pUnknownList->end(); ++iter)
    {
        const char *value = (*iter)->getValue();

        if (bModuleNode)
            appendModuleParam(pNode, value);
        else
        {
            const char *p = strstr(value, "::");

            //Only hanlde has :: case
            if (p)
            {
                /**
                 * CASE such as cache::enablecache 1, will be treated as
                 * module chace {
                 * param enablecache 1
                 * }
                 */
                char newname[1024] = {0};
                char newvalue[4096] = {0};
                strncpy(newname, value, p - value);
                strcpy(newvalue, p + 2);

                XmlNode *pRootNode = pNode;

                while (pRootNode->getParent())
                    pRootNode = pRootNode->getParent();

                const XmlNodeList *pModuleList = pRootNode->getChildren("module");

                if (pModuleList)
                {
                    XmlNodeList::const_iterator iter2;

                    for (iter2 = pModuleList->begin(); iter2 != pModuleList->end(); ++iter2)
                    {
                        if (strcasecmp((*iter2)->getValue(), newname) == 0)
                        {
                            addModuleWithParam(pNode, newname, newvalue);
                            break;
                        }
                    }

                    if (iter2 == pModuleList->end())
                        logToMem(LOG_LEVEL_ERR,
                                 "Module[%s] not defined in server leve while checking [%s].", newname,
                                 value);

                }
                else
                    logToMem(LOG_LEVEL_ERR,
                             "No module defined in server leve while checking [%s].", value);
            }
        }
    }
}