"Fossies" - the Fresh Open Source Software Archive

Member "fimex-1.4.2/src/metgm/MetGmCommentAttributeParser.cc" (6 Jan 2020, 4090 Bytes) of package /linux/privat/fimex-1.4.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "MetGmCommentAttributeParser.cc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.4.1_vs_1.4.2.

    1 /*
    2  * Fimex
    3  *
    4  * (C) Copyright 2011-2019, met.no
    5  *
    6  * Project Info:  https://wiki.met.no/fimex/start
    7  *
    8  * This library is free software; you can redistribute it and/or modify it
    9  * under the terms of the GNU Lesser General Public License as published by
   10  * the Free Software Foundation; either version 2.1 of the License, or
   11  * (at your option) any later version.
   12  *
   13  * This library is distributed in the hope that it will be useful, but
   14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   15  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
   16  * License for more details.
   17  *
   18  * You should have received a copy of the GNU Lesser General Public
   19  * License along with this library; if not, write to the Free Software
   20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
   21  * USA.
   22  */
   23 
   24 // internals
   25 //
   26 #include "MetGmCommentAttributeParser.h"
   27 
   28 // fimex
   29 //
   30 #include "fimex/CDM.h"
   31 #include "fimex/CDMAttribute.h"
   32 #include "fimex/CDMException.h"
   33 #include "fimex/CDMReader.h"
   34 #include "fimex/Logger.h"
   35 #include "fimex/XMLDoc.h"
   36 
   37 // libxml2
   38 //
   39 #include <libxml/tree.h>
   40 #include <libxml/xpath.h>
   41 
   42 namespace MetNoFimex {
   43 
   44 static Logger_p logger = getLogger("fimex.MetGmCDMWriter.MetGmCommentAttribute");
   45 
   46 const std::string FREE_TEXT = "metgm_free_text";
   47 const std::string VERSION = "metgm_version";
   48 const std::string ANALYSIS_DATE_TIME = "metgm_analysis_date_time";
   49 const std::string START_DATE_TIME = "metgm_start_date_time";
   50 const std::string DATA_TYPE = "metgm_data_type";
   51 const std::string MODEL_TYPE = "metgm_model_type";
   52 const std::string PRODUCTION_NATION = "metgm_production_nation";
   53 
   54 std::shared_ptr<MetGmCommentAttributeParser> MetGmCommentAttributeParser::createMetGmCommentAttributeParser(const CDMReader_p& pCdmReader)
   55 {
   56     std::shared_ptr<MetGmCommentAttributeParser> parser = std::shared_ptr<MetGmCommentAttributeParser>(new MetGmCommentAttributeParser);
   57 
   58     CDMAttribute metgmMetaData; // encoded within comment
   59     if(pCdmReader->getCDM().getAttribute(pCdmReader->getCDM().globalAttributeNS(), "comment", metgmMetaData)) {
   60         try {
   61             std::string commentStr = metgmMetaData.getStringValue();
   62             if (! (commentStr.size() > 0 && commentStr.substr(0,1) == "<")) throw CDMException("comment not xml");
   63             XMLDoc_p doc = XMLDoc::fromString(metgmMetaData.getStringValue());
   64 
   65             if(doc.get() != 0) {
   66                 xmlXPathObject_p xpathObj = doc->getXPathObject("/meta_data/attribute");
   67                 xmlNodeSetPtr nodes = xpathObj->nodesetval;
   68                 size_t size = (nodes) ? nodes->nodeNr : 0;
   69                 for (size_t i = 0; i < size; ++i) {
   70                     xmlNodePtr node = nodes->nodeTab[i];
   71                     std::string attributeName = getXmlProp(node, "name");
   72                     if (attributeName == FREE_TEXT) {
   73                         parser->freeText_ = getXmlProp(node, "value");
   74                     } else if (attributeName == VERSION) {
   75                         parser->version_ = getXmlProp(node, "value");
   76                     } else if (attributeName == DATA_TYPE) {
   77                         parser->dataType_ = getXmlProp(node, "value");
   78                     } else if (attributeName == MODEL_TYPE) {
   79                         parser->modelType_ = getXmlProp(node, "value");
   80                     } else if (attributeName == PRODUCTION_NATION) {
   81                         parser->productNation_ = getXmlProp(node, "value");
   82                     } else if (attributeName == ANALYSIS_DATE_TIME) {
   83                         parser->analysisDateTime_ = getXmlProp(node, "value");
   84                     } else if (attributeName == START_DATE_TIME) {
   85                         parser->startDateTime_ = getXmlProp(node, "value");
   86                     }
   87                 }
   88             }
   89         } catch (CDMException& exception) {
   90             // ignore
   91             LOG4FIMEX(logger, Logger::DEBUG, "failed parsing global attribute 'comment' as xml: '" << metgmMetaData.getStringValue() << "'");
   92         }
   93     }
   94 
   95     return parser;
   96 }
   97 
   98 } // namespace MetNoFimex