/* * PlotConfigurationLoader.cc * * Created on: 8 nov. 2013 * Author: guillaume */ #include "PlotConfigurationLoader.hh" #include "DefaultPlotConfiguration.hh" #include "PanelPlotNodeRegistry.hh" #include "ColormapManager.hh" #include "Constant.hh" #include "CommonNode.hh" #include "Properties.hh" #include "AxesNode.hh" #include #include namespace plot { static const std::string DEFAULT_NODE_NAME = "default"; static const std::string COLORMAP_NODE_NAME = "colormap"; PlotConfigurationLoader::PlotConfigurationLoader() { _nodeMapLoader[typeid(DefaultPlotConfiguration).name()] = NodeLoader( DEFAULT_NODE_NAME, NodeCfgSPtr(new DefaultNode())); _nodeMapLoader[typeid(ColormapManager).name()] = NodeLoader( COLORMAP_NODE_NAME, NodeCfgSPtr(new ColormapNode())); } PlotConfigurationLoader::~PlotConfigurationLoader() { } void PlotConfigurationLoader::initParser( const AMDA::Parameters::CfgContext& pCtx) { AMDA::helpers::Properties lProperties("app.properties"); std::string configFile = lProperties["app.plot.configfile"]; if (configFile.empty()) { LOG4CXX_WARN(gLogger, "Unset property app.plot.configfile in app.properties."); return; } xmlDocPtr lDoc = NULL; xmlNodePtr lRoot = NULL; LOG4CXX_DEBUG(gLogger, "File to parse: " << configFile) try { if (access(configFile.c_str(), F_OK) != 0) { LOG4CXX_WARN(gLogger, "Unknown file " + configFile); return; } if (!(lDoc = xmlParseFile(configFile.c_str()))) { LOG4CXX_ERROR(gLogger, "Invalid file " + configFile); return; } if (!(lRoot = xmlDocGetRootElement(lDoc))) { LOG4CXX_ERROR(gLogger, "Empty file " + configFile); return; } AMDA::XMLConfigurator::NodeGrpCfg rootNodeParser = RootNode( _nodeLoader); rootNodeParser.proceed(lRoot, pCtx); } catch (...) { if (lDoc) { xmlFreeDoc(lDoc); } throw; } if (lDoc) { xmlFreeDoc(lDoc); } } /** * ************************** RootNode ******************************* */ RootNode::RootNode(const NodeLoader& pNodeLoader) : NodeGrpCfg() { // Push node loader. getChildList()[std::get < 0 > (pNodeLoader)] = std::get < 1 > (pNodeLoader); } RootNode::~RootNode() { } void RootNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pCtx) { AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, pCtx); } /** * ************************** DefaultNode ******************************* */ DefaultNode::DefaultNode() : NodeGrpCfg() { getChildList()["page"] = AMDA::XMLConfigurator::NodeCfgSPtr( new DefaultPageNode()); getChildList()["panel"] = AMDA::XMLConfigurator::NodeCfgSPtr( new DefaultPanelNode()); } DefaultNode::~DefaultNode() { } void DefaultNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pCtx) { AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, pCtx); } /** * ************************** DefaultPageNode ******************************* */ DefaultPageNode::DefaultPageNode() : NodeGrpCfg() { getChildList()["font"] = AMDA::XMLConfigurator::NodeCfgSPtr( new FontNode()); getChildList()["margin"] = AMDA::XMLConfigurator::NodeCfgSPtr( new MarginNode()); } DefaultPageNode::~DefaultPageNode() { } void DefaultPageNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) { LOG4CXX_DEBUG(gLogger, "PageNode::proceed"); DefaultPlotConfiguration* defaultConfiguration = pContext.get< DefaultPlotConfiguration*>(); xmlChar* value = NULL; // -- dimension if ((value = xmlGetProp(pNode, (const xmlChar *) "dimension"))) { defaultConfiguration->_defaultPage.setDimension((const char*) value); xmlFree(value); } // -- orientation if ((value = xmlGetProp(pNode, (const xmlChar *) "orientation"))) { defaultConfiguration->_defaultPage.setOrientation((const char*) value); xmlFree(value); } // -- mode if ((value = xmlGetProp(pNode, (const xmlChar *) "mode"))) { defaultConfiguration->_defaultPage.setMode((const char*) value); xmlFree(value); } // -- dpi if ((value = xmlGetProp(pNode, (const xmlChar *) "dpi"))) { defaultConfiguration->_defaultPage._dpi = atoi((const char*) value); xmlFree(value); } AMDA::Parameters::CfgContext context; context.push(&defaultConfiguration->_defaultPage); AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, context); } /** * ************************** DefaultPanelNode ******************************* */ DefaultPanelNode::DefaultPanelNode() : NodeGrpCfg() { getChildList()["font"] = AMDA::XMLConfigurator::NodeCfgSPtr( new FontNode()); getChildList()["title"] = AMDA::XMLConfigurator::NodeCfgSPtr( new TitleNode()); getChildList()["timeAxis"] = AMDA::XMLConfigurator::NodeCfgSPtr( new DefaultTimeAxisNode()); getChildList()["epochAxis"] = AMDA::XMLConfigurator::NodeCfgSPtr( new DefaultEpochAxisNode()); getChildList()["colorAxis"] = AMDA::XMLConfigurator::NodeCfgSPtr( new DefaultColorAxisNode()); getChildList()["xAxis"] = AMDA::XMLConfigurator::NodeCfgSPtr( new DefaultDigitalAxisNode()); getChildList()["yAxis"] = AMDA::XMLConfigurator::NodeCfgSPtr( new DefaultDigitalAxisNode()); // retrieve registered plot properties node : for( auto child : PanelPlotNodeRegistry::getInstance().getConfigurationNodes() ){ std::ostringstream msg; msg << "adding child : " << child.first << std::endl; LOG4CXX_DEBUG(gLogger, msg.str()); getChildList()[child.first] = child.second; } } DefaultPanelNode::~DefaultPanelNode() { } void DefaultPanelNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) { LOG4CXX_DEBUG(gLogger, "DefaultPanelNode::proceed"); xmlChar* value = NULL; DefaultPlotConfiguration* defaultConfiguration = pContext.get< DefaultPlotConfiguration*>(); // -- max serie resolution if ((value = xmlGetProp(pNode, (const xmlChar *) "resolution"))) { defaultConfiguration->_defaultPanel._resolution = atoi((const char*) value); xmlFree(value); } // -- color value = xmlGetProp(pNode, (const xmlChar *) "backgroundColor"); if (value) { try { std::string strValue((const char*) value); createColor(defaultConfiguration->_defaultPanel._backgroundColor, strValue); } catch (std::logic_error& e) { LOG4CXX_WARN(gLogger, "Plot Default Configuration Panel BackgroundColor : " << e.what()); defaultConfiguration->_defaultPanel._backgroundColor._colorIndex = 0; defaultConfiguration->_defaultPanel._backgroundColor._colorMapIndex = 0; } xmlFree(value); } else { LOG4CXX_WARN(gLogger, "No default background color is defined for plot panels"); defaultConfiguration->_defaultPanel._backgroundColor._colorIndex = 0; defaultConfiguration->_defaultPanel._backgroundColor._colorMapIndex = 0; } // -- color map index value = xmlGetProp(pNode, (const xmlChar *) "colorMapIndex"); if (value) { defaultConfiguration->_defaultPanel._backgroundColor._colorMapIndex = atoi((const char*) value); xmlFree(value); } else if (defaultConfiguration->_defaultPanel._backgroundColor._colorIndex != -1) { LOG4CXX_WARN(gLogger, "No default color map is defined, color map 0 will be used"); defaultConfiguration->_defaultPanel._backgroundColor._colorMapIndex = 0; } AMDA::Parameters::CfgContext context; context.push(&defaultConfiguration->_defaultPanel); context.push(defaultConfiguration); AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, context); } /** * ************************** Default*AxisNode ******************************* */ void DefaultTimeAxisNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pCtx) { LOG4CXX_DEBUG(gLogger, "DefaultTimeAxisNode::proceed"); Panel* panel = pCtx.get(); TimeAxis* axis = new TimeAxis(); // read dedicated attributes xmlChar* value = NULL; // -- axis id //axis->_id = DefaultPlotConfiguration::TIME_DEFAULT_ID; // -- scale type value = xmlGetProp(pNode, (const xmlChar *) "id"); if (value) { axis->_id = (const char*) value; xmlFree(value); } panel->addAxis(axis->_id, boost::shared_ptr(axis)); // -- scale type value = xmlGetProp(pNode, (const xmlChar *) "format"); if (value) { axis->_timeFormat = (const char*) value; xmlFree(value); } AMDA::Parameters::CfgContext context; context.push(axis); context.push(panel); AnyAxisNode::proceed(pNode, context); } void DefaultEpochAxisNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pCtx) { LOG4CXX_DEBUG(gLogger, "DefaultEpochAxisNode::proceed"); Panel* panel = pCtx.get(); EpochAxis* axis = new EpochAxis(); // read dedicated attributes xmlChar* value = NULL; value = xmlGetProp(pNode, (const xmlChar *) "id"); if (value) { axis->_id = (const char*) value; xmlFree(value); } panel->addAxis(axis->_id, boost::shared_ptr(axis)); AMDA::Parameters::CfgContext context; context.push(axis); context.push(panel); AnyAxisNode::proceed(pNode, context); } void DefaultColorAxisNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pCtx) { LOG4CXX_DEBUG(gLogger, "DefaultColorAxisNode::proceed"); Panel* panel = pCtx.get(); ColorAxis* axis = new ColorAxis(); // read dedicated attributes xmlChar* value = NULL; // -- axis id //axis->_id = DefaultPlotConfiguration::TIME_DEFAULT_ID; // -- scale type value = xmlGetProp(pNode, (const xmlChar *) "id"); if (value) { axis->_id = (const char*) value; xmlFree(value); } panel->addAxis(axis->_id, boost::shared_ptr(axis)); AMDA::Parameters::CfgContext context; context.push(axis); context.push(panel); AnyAxisNode::proceed(pNode, context); } void DefaultDigitalAxisNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pCtx) { LOG4CXX_DEBUG(gLogger, "DefaultDigitalAxisNode::proceed"); Panel* panel = pCtx.get(); DigitalAxis* axis = new DigitalAxis; // read dedicated attributes xmlChar* value = NULL; // -- axis id axis->_id = (const char*) pNode->name; panel->addAxis(axis->_id, boost::shared_ptr(axis)); // -- scale type value = xmlGetProp(pNode, (const xmlChar *) "scale"); if (value) { axis->setScale((const char*) value); xmlFree(value); } AMDA::Parameters::CfgContext context; context.push(axis); context.push(panel); AnyAxisNode::proceed(pNode, context); } /** * ************************** ColormapNode ******************************* */ ColormapNode::ColormapNode() : NodeGrpCfg() { getChildList()["grayscale"] = AMDA::XMLConfigurator::NodeCfgSPtr( new GrayscaleNode()); getChildList()["color"] = AMDA::XMLConfigurator::NodeCfgSPtr( new ColorscaleNode()); getChildList()["coloraxis"] = AMDA::XMLConfigurator::NodeCfgSPtr( new ColormapAxisNode()); } ColormapNode::~ColormapNode() { } void ColormapNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) { LOG4CXX_DEBUG(gLogger, "ColormapNode::proceed"); // Get colormap folder xmlChar* lXmlFolder = xmlGetProp(pNode, (const xmlChar *) "path"); if (lXmlFolder) { AMDA::helpers::Properties lProperties("app.properties"); // Config file path boost::filesystem::path configFilePath(lProperties["app.plot.configfile"]); boost::filesystem::path colorPath(std::string((const char*) lXmlFolder)); colorPath = boost::filesystem::absolute(colorPath, configFilePath.parent_path()); pContext.get()->_path = colorPath.string(); xmlFree(lXmlFolder); } AMDA::Parameters::CfgContext context; context.push(pContext.get()); AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, pContext); } /** * ************************** GrayscaleNode ******************************* */ GrayscaleNode::GrayscaleNode() : NodeGrpCfg() { getChildList()["file"] = AMDA::XMLConfigurator::NodeCfgSPtr( new ColorFileNode()); } GrayscaleNode::~GrayscaleNode() { } void GrayscaleNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) { LOG4CXX_DEBUG(gLogger, "GrayscaleNode::proceed"); ColormapManager* lColormapManager = pContext.get(); AMDA::Parameters::CfgContext context; std::shared_ptr lColorFileMap(new ColorFileMap()); context.push(lColorFileMap.get()); context.push(lColormapManager); AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, context); xmlChar* value = NULL; // -- default color map index value = xmlGetProp(pNode, (const xmlChar *) "default"); if (value) { lColormapManager->_defaultColormap[PlotCommon::Mode::GRAYSCALE] = atoi((const char*) value); xmlFree(value); } if (!value || lColorFileMap.get()->find( lColormapManager->_defaultColormap[PlotCommon::Mode::GRAYSCALE]) == lColorFileMap->end()) { lColormapManager->_defaultColormap[PlotCommon::Mode::GRAYSCALE] = 0; if (lColorFileMap->size() == 0) { LOG4CXX_WARN(gLogger, "No default color map (grayscale mode) is set, default color map will be 0"); } } lColormapManager->_colormap[PlotCommon::Mode::GRAYSCALE] = *(lColorFileMap.get()); } /** * ************************** ColorscaleNode ******************************* */ ColorscaleNode::ColorscaleNode() : NodeGrpCfg() { getChildList()["file"] = AMDA::XMLConfigurator::NodeCfgSPtr( new ColorFileNode()); } ColorscaleNode::~ColorscaleNode() { } void ColorscaleNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) { LOG4CXX_DEBUG(gLogger, "ColorscaleNode::proceed"); ColormapManager* lColormapManager = pContext.get(); AMDA::Parameters::CfgContext context; std::shared_ptr lColorFileMap(new ColorFileMap()); context.push(lColorFileMap.get()); context.push(lColormapManager); AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, context); // read dedicated attributes xmlChar* value = NULL; // -- default color map index value = xmlGetProp(pNode, (const xmlChar *) "default"); if (value) { lColormapManager->_defaultColormap[PlotCommon::Mode::COLOR] = atoi((const char*) value); xmlFree(value); } if (!value || lColorFileMap.get()->find( lColormapManager->_defaultColormap[PlotCommon::Mode::COLOR]) == lColorFileMap->end()) { lColormapManager->_defaultColormap[PlotCommon::Mode::COLOR] = 0; if (lColorFileMap->size() == 0) { LOG4CXX_WARN(gLogger, "No default color map (color mode) is set, default color map will be 0"); } } lColormapManager->_colormap[PlotCommon::Mode::COLOR] = *(lColorFileMap.get()); } /** * ************************** ColorAxisNode ******************************* */ ColormapAxisNode::ColormapAxisNode() : NodeGrpCfg() { getChildList()["file"] = AMDA::XMLConfigurator::NodeCfgSPtr( new ColorFileNode()); } ColormapAxisNode::~ColormapAxisNode() { } void ColormapAxisNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) { LOG4CXX_DEBUG(gLogger, "ColormapAxisNode::proceed"); ColormapManager* lColormapManager = pContext.get(); AMDA::Parameters::CfgContext context; std::shared_ptr lColorFileMap(new ColorFileMap()); context.push(lColorFileMap.get()); context.push(lColormapManager); AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, context); xmlChar* value = NULL; // -- default color map index value = xmlGetProp(pNode, (const xmlChar *) "default"); if (value) { lColormapManager->_defaultColorAxis = atoi((const char*) value); xmlFree(value); } if (!value || lColorFileMap.get()->find( lColormapManager->_defaultColorAxis) == lColorFileMap->end()) { lColormapManager->_defaultColorAxis = 0; if (lColorFileMap->size() == 0) { LOG4CXX_WARN(gLogger, "No default color axis is set, default color map will be 0"); } } lColormapManager->_coloraxis = *(lColorFileMap.get()); } /** * ************************** ColorFileNode ******************************* */ ColorFileNode::ColorFileNode() { } ColorFileNode::~ColorFileNode() { } void ColorFileNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pCtx) { LOG4CXX_DEBUG(gLogger, "ColorFileNode::proceed "); // Get list ColorFileMap* lColorFileList = pCtx.get(); // Get filename. std::string lFilePath = std::string( ((const char*) pNode->children->content)); // Get default color map index xmlChar* value = NULL; int index = lColorFileList->size(); value = xmlGetProp(pNode, (const xmlChar *) "index"); if (value) { index = atoi((const char*) value); xmlFree(value); } else { LOG4CXX_WARN(gLogger, "No index is set for " << lFilePath << " color map file, force to " << index); } (*lColorFileList)[index] = lFilePath; } } //plot