From 85496126f5139ba1b15861e7fd8bbbc23b835fd9 Mon Sep 17 00:00:00 2001 From: sbh Date: Wed, 9 Jul 2014 17:28:42 +0400 Subject: [PATCH] Processing of "Validators" in XML --- src/Config/CMakeLists.txt | 2 + src/Config/Config_Common.cpp | 28 +++++++++++ src/Config/Config_Common.h | 8 +++ src/Config/Config_FeatureMessage.cpp | 5 ++ src/Config/Config_FeatureMessage.h | 1 + src/Config/Config_FeatureReader.cpp | 43 ++++++++-------- src/Config/Config_Keywords.h | 16 +++--- src/Config/Config_ValidatorMessage.cpp | 70 ++++++++++++++++++++++++++ src/Config/Config_ValidatorMessage.h | 45 +++++++++++++++++ src/Config/Config_WidgetAPI.cpp | 14 +++--- src/Config/Config_WidgetReader.cpp | 5 +- src/Config/Config_XMLReader.cpp | 35 +++++++++++-- src/Config/Config_XMLReader.h | 1 + 13 files changed, 234 insertions(+), 39 deletions(-) create mode 100644 src/Config/Config_ValidatorMessage.cpp create mode 100644 src/Config/Config_ValidatorMessage.h diff --git a/src/Config/CMakeLists.txt b/src/Config/CMakeLists.txt index 2e826fc24..aa7ea19e4 100644 --- a/src/Config/CMakeLists.txt +++ b/src/Config/CMakeLists.txt @@ -14,6 +14,7 @@ SET(PROJECT_HEADERS Config_WidgetReader.h Config_PointerMessage.h Config_Common.h + Config_ValidatorMessage.h ) SET(PROJECT_SOURCES @@ -25,6 +26,7 @@ SET(PROJECT_SOURCES Config_WidgetReader.cpp Config_PointerMessage.cpp Config_Common.cpp + Config_ValidatorMessage.cpp ) SET(XML_RESOURCES diff --git a/src/Config/Config_Common.cpp b/src/Config/Config_Common.cpp index 97a2a6d67..84e6328a2 100644 --- a/src/Config/Config_Common.cpp +++ b/src/Config/Config_Common.cpp @@ -6,10 +6,13 @@ */ #include "Config_Common.h" +#include #include #include +#include //for stringstream + bool isElementNode(xmlNodePtr theNode) { return theNode->type == XML_ELEMENT_NODE; @@ -51,6 +54,31 @@ bool hasChild(xmlNodePtr theNode) return false; } +bool getValidatorInfo(xmlNodePtr theNode, + std::string& outValidatorId, + std::list& outValidatorParameters) +{ + //Validator id: + char* anIdProp = (char*) xmlGetProp(theNode, BAD_CAST _ID); + if (!anIdProp || anIdProp[0] == 0) { + return false; + } + outValidatorId = std::string(anIdProp); + + //Validator parameters: + char* aParamProp = (char*) xmlGetProp(theNode, BAD_CAST VALIDATOR_PARAMETERS); + if (aParamProp && aParamProp[0] != 0) { + std::string aPropString = std::string(aParamProp); + std::stringstream aPropStringStream(aPropString); + char COMMA_DELIM = ','; + std::string aValidatorParameter; + while (std::getline(aPropStringStream, aValidatorParameter, ',')) { + outValidatorParameters.push_back(aValidatorParameter); + } + } + return true; +} + std::string library(const std::string& theLibName) { std::string aLibName = theLibName; diff --git a/src/Config/Config_Common.h b/src/Config/Config_Common.h index 2a579b91e..4a204f59b 100644 --- a/src/Config/Config_Common.h +++ b/src/Config/Config_Common.h @@ -11,6 +11,7 @@ #include "Config_def.h" #include +#include #include //>> Forward declaration of xmlNodePtr. @@ -49,6 +50,13 @@ CONFIG_EXPORT bool isNode(xmlNodePtr theNode, const char* theNodeName, ...); */ CONFIG_EXPORT bool hasChild(xmlNodePtr theNode); +/* + * + */ +CONFIG_EXPORT bool getValidatorInfo(xmlNodePtr theNode, + std::string& outValidatorId, + std::list& outValidatorParameters); + /*! \brief Convert the given parameter to the platform-specific library name. diff --git a/src/Config/Config_FeatureMessage.cpp b/src/Config/Config_FeatureMessage.cpp index 7b0f60337..041405eb3 100644 --- a/src/Config/Config_FeatureMessage.cpp +++ b/src/Config/Config_FeatureMessage.cpp @@ -21,6 +21,11 @@ Config_FeatureMessage::Config_FeatureMessage(const Events_ID theId, const void* myNestedFeatures = ""; } +Config_FeatureMessage::~Config_FeatureMessage() +{ + +} + const std::string& Config_FeatureMessage::icon() const { return myIcon; diff --git a/src/Config/Config_FeatureMessage.h b/src/Config/Config_FeatureMessage.h index 2613c9926..752169fb4 100644 --- a/src/Config/Config_FeatureMessage.h +++ b/src/Config/Config_FeatureMessage.h @@ -33,6 +33,7 @@ class Config_FeatureMessage: public Events_Message public: //const Events_ID theID, const void* theSender = 0 CONFIG_EXPORT Config_FeatureMessage(const Events_ID theId, const void* theParent = 0); + CONFIG_EXPORT virtual ~Config_FeatureMessage(); //Auto-generated getters/setters CONFIG_EXPORT const std::string& icon() const; diff --git a/src/Config/Config_FeatureReader.cpp b/src/Config/Config_FeatureReader.cpp index 6e6bbb85f..bdd6e0801 100644 --- a/src/Config/Config_FeatureReader.cpp +++ b/src/Config/Config_FeatureReader.cpp @@ -18,6 +18,7 @@ #include #include +#include #ifdef _DEBUG #include @@ -53,45 +54,45 @@ void Config_FeatureReader::processNode(xmlNodePtr theNode) //If a feature has xml definition for it's widget: aMessage.setUseInput(hasChild(theNode)); aEvLoop->send(aMessage); - } //The m_last* variables always defined before fillFeature() call. XML is a tree. - if (isNode(theNode, NODE_GROUP, NULL)) { + } else if (isNode(theNode, NODE_GROUP, NULL)) { myLastGroup = getProperty(theNode, _ID); - } - if (isNode(theNode, NODE_WORKBENCH, NULL)) { + } else if (isNode(theNode, NODE_WORKBENCH, NULL)) { myLastWorkbench = getProperty(theNode, _ID); + //Process SOURCE, VALIDATOR nodes. } - //Process SOURCE nodes. Config_XMLReader::processNode(theNode); } bool Config_FeatureReader::processChildren(xmlNodePtr theNode) { - return isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NULL); + return isNode(theNode, NODE_WORKBENCH, NODE_GROUP, NODE_FEATURE, NULL); } -void Config_FeatureReader::fillFeature(xmlNodePtr theRoot, Config_FeatureMessage& outFtMessage) +void Config_FeatureReader::fillFeature(xmlNodePtr theNode, + Config_FeatureMessage& outFeatureMessage) { - outFtMessage.setId(getProperty(theRoot, _ID)); - outFtMessage.setPluginLibrary(myLibraryName); - outFtMessage.setNestedFeatures(getProperty(theRoot, FEATURE_NESTED)); - bool isFtInternal = isInternalFeature(theRoot); - outFtMessage.setInternal(isFtInternal); - if(isFtInternal) { + outFeatureMessage.setId(getProperty(theNode, _ID)); + outFeatureMessage.setPluginLibrary(myLibraryName); + outFeatureMessage.setNestedFeatures(getProperty(theNode, FEATURE_NESTED)); + + bool isInternal = isInternalFeature(theNode); + outFeatureMessage.setInternal(isInternal); + if(isInternal) { //Internal feature has no visual representation. return; } - outFtMessage.setText(getProperty(theRoot, FEATURE_TEXT)); - outFtMessage.setTooltip(getProperty(theRoot, FEATURE_TOOLTIP)); - outFtMessage.setIcon(getProperty(theRoot, FEATURE_ICON)); - outFtMessage.setKeysequence(getProperty(theRoot, FEATURE_KEYSEQUENCE)); - outFtMessage.setGroupId(myLastGroup); - outFtMessage.setWorkbenchId(myLastWorkbench); + outFeatureMessage.setText(getProperty(theNode, FEATURE_TEXT)); + outFeatureMessage.setTooltip(getProperty(theNode, FEATURE_TOOLTIP)); + outFeatureMessage.setIcon(getProperty(theNode, FEATURE_ICON)); + outFeatureMessage.setKeysequence(getProperty(theNode, FEATURE_KEYSEQUENCE)); + outFeatureMessage.setGroupId(myLastGroup); + outFeatureMessage.setWorkbenchId(myLastWorkbench); } -bool Config_FeatureReader::isInternalFeature(xmlNodePtr theRoot) +bool Config_FeatureReader::isInternalFeature(xmlNodePtr theNode) { - std::string prop = getProperty(theRoot, FEATURE_INTERNAL); + std::string prop = getProperty(theNode, FEATURE_INTERNAL); std::transform(prop.begin(), prop.end(), prop.begin(), ::tolower); if(prop.empty() || prop == "false" || prop == "0") { return false; diff --git a/src/Config/Config_Keywords.h b/src/Config/Config_Keywords.h index 37f9194de..f8f4f222f 100644 --- a/src/Config/Config_Keywords.h +++ b/src/Config/Config_Keywords.h @@ -15,6 +15,7 @@ const static char* NODE_WORKBENCH = "workbench"; const static char* NODE_GROUP = "group"; const static char* NODE_FEATURE = "feature"; const static char* NODE_SOURCE = "source"; +const static char* NODE_VALIDATOR = "validator"; //Widgets const static char* WDG_DOUBLEVALUE = "doublevalue"; @@ -37,19 +38,22 @@ const static char* WDG_FEATURE_SELECTOR = "feature_selector"; const static char* WDG_FEATURE_OR_ATTRIBUTE_SELECTOR = "feature_or_attribute_selector"; const static char* WDG_DOUBLEVALUE_EDITOR = "doublevalue_editor"; +//Common Widget's or Feature's Properties const static char* _ID = "id"; -//const static char* WORKBENCH_ID = "id"; -//const static char* GROUP_ID = "id"; -//const static char* FEATURE_ID = "id"; -const static char* FEATURE_TEXT = "title"; const static char* FEATURE_TOOLTIP = "tooltip"; const static char* FEATURE_ICON = "icon"; +const static char* FEATURE_TEXT = "title"; const static char* FEATURE_KEYSEQUENCE = "keysequence"; const static char* FEATURE_NESTED = "nested"; const static char* FEATURE_INTERNAL = "internal"; -const static char* SOURCE_FILE = "path"; - +// TODO: Rename const static char* PREVIOUS_FEATURE_PARAM = "previous_feature_param"; +const static char* ANY_WDG_TOOLTIP = FEATURE_TOOLTIP; +const static char* ANY_WDG_ICON = FEATURE_ICON; +const static char* ANY_WDG_LABEL = "label"; + +const static char* SOURCE_FILE = "path"; +const static char* VALIDATOR_PARAMETERS = "parameters"; // doublevalue properties: const static char* INFO_WDG_TEXT = FEATURE_TEXT; diff --git a/src/Config/Config_ValidatorMessage.cpp b/src/Config/Config_ValidatorMessage.cpp new file mode 100644 index 000000000..a5ac9699b --- /dev/null +++ b/src/Config/Config_ValidatorMessage.cpp @@ -0,0 +1,70 @@ +/* + * Config_ValidatorMessage.cpp + * + * Created on: 08 èþëÿ 2014 ã. + * Author: sbh + */ + +#include + +Config_ValidatorMessage::Config_ValidatorMessage(const Events_ID theId, const void* theParent) +: Events_Message(theId, theParent) +{ + myValidatorId = ""; + myFeatureId = ""; + myAttributeId = ""; +} + +Config_ValidatorMessage::~Config_ValidatorMessage() +{ +} + +//static const const char* Config_ValidatorMessage::UID() const +//{ +// return "ValidatorMessage"; +//} + +const std::string& Config_ValidatorMessage::validatorId() const +{ + return myValidatorId; +} + +const std::string& Config_ValidatorMessage::featureId() const +{ + return myFeatureId; +} + +const std::string& Config_ValidatorMessage::attributeId() const +{ + return myAttributeId; +} + +const std::list& Config_ValidatorMessage::parameters() const +{ + return myVaidatorParameters; +} + +bool Config_ValidatorMessage::isValid() const +{ + return !myValidatorId.empty(); +} + +void Config_ValidatorMessage::setValidatorId(const std::string& validatorId) +{ + myValidatorId = validatorId; +} + +void Config_ValidatorMessage::setValidatorParameters(const std::list& parameters) +{ + myVaidatorParameters = parameters; +} + +void Config_ValidatorMessage::setFeatureId(const std::string& theId) +{ + myFeatureId = theId; +} + +void Config_ValidatorMessage::setAttributeId(const std::string& theId) +{ + myAttributeId = theId; +} diff --git a/src/Config/Config_ValidatorMessage.h b/src/Config/Config_ValidatorMessage.h new file mode 100644 index 000000000..21bbf2475 --- /dev/null +++ b/src/Config/Config_ValidatorMessage.h @@ -0,0 +1,45 @@ +/* + * Config_ValidatorMessage.h + * + * Created on: 08 èþëÿ 2014 ã. + * Author: sbh + */ + +#ifndef Config_ValidatorMessage_H_ +#define Config_ValidatorMessage_H_ + +#include +#include + +#include +#include + +/// Event ID that feature is loaded (comes with Config_FeatureMessage) +static const char * EVENT_VALIDATOR_LOADED = "ValidatorLoaded"; + +class Config_ValidatorMessage : public Events_Message +{ + std::string myValidatorId; + std::string myFeatureId; + std::string myAttributeId; + std::list myVaidatorParameters; + +public: + CONFIG_EXPORT Config_ValidatorMessage(const Events_ID theId, const void* theParent = 0); + CONFIG_EXPORT virtual ~Config_ValidatorMessage(); + + //CONFIG_EXPORT static const char* UID() const; + + CONFIG_EXPORT const std::string& validatorId() const; + const std::string& featureId() const; + const std::string& attributeId() const; + CONFIG_EXPORT const std::list& parameters() const; + CONFIG_EXPORT bool isValid() const; + + CONFIG_EXPORT void setValidatorId(const std::string& theId); + CONFIG_EXPORT void setFeatureId(const std::string& theId); + CONFIG_EXPORT void setAttributeId(const std::string& theId); + CONFIG_EXPORT void setValidatorParameters(const std::list& parameters); +}; + +#endif /* Config_ValidatorMessage_H_ */ diff --git a/src/Config/Config_WidgetAPI.cpp b/src/Config/Config_WidgetAPI.cpp index 2fdad6735..8d295ce05 100644 --- a/src/Config/Config_WidgetAPI.cpp +++ b/src/Config/Config_WidgetAPI.cpp @@ -94,20 +94,20 @@ std::string Config_WidgetAPI::getProperty(const char* thePropName) const std::string Config_WidgetAPI::widgetId() const { - return getProperty("id"); + return getProperty(_ID); } -std::string Config_WidgetAPI::widgetTooltip() const +std::string Config_WidgetAPI::widgetIcon() const { - return getProperty("tooltip"); + return getProperty(ANY_WDG_ICON); } -std::string Config_WidgetAPI::widgetIcon() const +std::string Config_WidgetAPI::widgetLabel() const { - return getProperty("icon"); + return getProperty(ANY_WDG_LABEL); } -std::string Config_WidgetAPI::widgetLabel() const +std::string Config_WidgetAPI::widgetTooltip() const { - return getProperty("label"); + return getProperty(ANY_WDG_TOOLTIP); } diff --git a/src/Config/Config_WidgetReader.cpp b/src/Config/Config_WidgetReader.cpp index 7ba92f996..7ce67a2e7 100644 --- a/src/Config/Config_WidgetReader.cpp +++ b/src/Config/Config_WidgetReader.cpp @@ -44,7 +44,7 @@ void Config_WidgetReader::processNode(xmlNodePtr theNode) { if (isNode(theNode, NODE_FEATURE, NULL)) { std::string aNodeName = getProperty(theNode, _ID); - myWidgetCache[aNodeName] = dumpNode(theNode);; + myWidgetCache[aNodeName] = dumpNode(theNode); myDescriptionCache[aNodeName] = getProperty(theNode, FEATURE_TEXT); } //Process SOURCE nodes. @@ -67,6 +67,9 @@ std::string Config_WidgetReader::dumpNode(xmlNodePtr theNode) if (isNode(aChildrenNode, NODE_SOURCE, NULL)) { Config_XMLReader aSourceReader = Config_XMLReader(getProperty(aChildrenNode, SOURCE_FILE)); + //Register all validators in the sourced xml + aSourceReader.readAll(); + //Dump! xmlNodePtr aSourceRoot = aSourceReader.findRoot(); int size = xmlNodeDump(buffer, aSourceRoot->doc, aSourceRoot, 0, 1); } else { diff --git a/src/Config/Config_XMLReader.cpp b/src/Config/Config_XMLReader.cpp index e86579ede..a9bf87dc6 100644 --- a/src/Config/Config_XMLReader.cpp +++ b/src/Config/Config_XMLReader.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -72,6 +73,8 @@ void Config_XMLReader::processNode(xmlNodePtr theNode) #ifdef _DEBUG std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl; #endif + } else if (isNode(theNode, NODE_VALIDATOR, NULL)) { + processValidator(theNode); } } @@ -89,7 +92,9 @@ bool Config_XMLReader::processChildren(xmlNodePtr aNode) */ xmlNodePtr Config_XMLReader::findRoot() { - myXmlDoc = xmlParseFile(myDocumentPath.c_str()); + if (myXmlDoc == NULL) { + myXmlDoc = xmlParseFile(myDocumentPath.c_str()); + } if (myXmlDoc == NULL) { #ifdef _DEBUG std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath @@ -117,7 +122,10 @@ void Config_XMLReader::readRecursively(xmlNodePtr theParent) return; xmlNodePtr aNode = theParent->xmlChildrenNode; for(; aNode; aNode = aNode->next) { - processNode(aNode); + //Still no text processing in features... + if(isElementNode(aNode)) { + processNode(aNode); + } if (processChildren(aNode)) { readRecursively(aNode); } @@ -135,13 +143,32 @@ xmlNodePtr Config_XMLReader::node(void* theNode) /* * Returns named property for a given node as std::string. */ -std::string Config_XMLReader::getProperty(xmlNodePtr theNode, const char* name) +std::string Config_XMLReader::getProperty(xmlNodePtr theNode, const char* theName) { std::string result = ""; - char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST name); + char* aPropChars = (char*) xmlGetProp(theNode, BAD_CAST theName); if (!aPropChars || aPropChars[0] == 0) return result; result = std::string(aPropChars); return result; } +void Config_XMLReader::processValidator(xmlNodePtr theNode) +{ + Events_ID aValidatoEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED); + Events_Loop* aEvLoop = Events_Loop::loop(); + Config_ValidatorMessage aMessage(aValidatoEvent, this); + std::string aValidatorId; + std::list aValidatorParameters; + getValidatorInfo(theNode, aValidatorId, aValidatorParameters); + aMessage.setValidatorId(aValidatorId); + aMessage.setValidatorParameters(aValidatorParameters); + if(isNode(theNode->parent, NODE_FEATURE, NULL)) { + aMessage.setFeatureId(getProperty(theNode->parent, _ID)); + } else { + xmlNodePtr aWdgNode = theNode->parent; + aMessage.setAttributeId(getProperty(aWdgNode, _ID)); + aMessage.setFeatureId(getProperty(aWdgNode->parent, _ID)); + } + aEvLoop->send(aMessage); +} diff --git a/src/Config/Config_XMLReader.h b/src/Config/Config_XMLReader.h index 8915147e9..47fc97b7b 100644 --- a/src/Config/Config_XMLReader.h +++ b/src/Config/Config_XMLReader.h @@ -49,6 +49,7 @@ protected: xmlNodePtr node(void* theNode); std::string getProperty(xmlNodePtr theNode, const char* property); + void processValidator(xmlNodePtr theNode); protected: std::string myDocumentPath; -- 2.39.2