"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2b/pubmedXml.cpp" (12 Feb 2021, 4591 Bytes) of package /linux/privat/cb2bib-2.0.1.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 "pubmedXml.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0.0_vs_2.0.1.

    1 /***************************************************************************
    2  *   Copyright (C) 2004-2021 by Pere Constans
    3  *   constans@molspaces.com
    4  *   cb2Bib version 2.0.1. Licensed under the GNU GPL version 3.
    5  *   See the LICENSE file that comes with this distribution.
    6  ***************************************************************************/
    7 #include "pubmedXml.h"
    8 
    9 #include "cb2bib_utilities.h"
   10 
   11 
   12 pubmedXml::pubmedXml(const QString& xml) : QXmlStreamReader()
   13 {
   14     _reference.clearReference();
   15     addData(xml);
   16     readReference();
   17 }
   18 
   19 
   20 void pubmedXml::readReference()
   21 {
   22     if (hasError())
   23         return;
   24 
   25     while (reading("PubmedArticle"))
   26         if (name() == "Journal")
   27             readJournal();
   28         else if (name() == "AuthorList")
   29             readAuthors();
   30         else if (name() == "MeshHeadingList")
   31             readKeywords();
   32         else if (name() == "ArticleTitle")
   33             readField("title");
   34         else if (name() == "AbstractText")
   35             readField("abstract");
   36         else if (name() == "MedlinePgn")
   37         {
   38             readNext();
   39             _reference["pages"] = text().toString().remove(QRegExp("\\,[\\s\\d]+")); // Cases "11-5, 25"
   40         }
   41         else if (name() == "PMID")
   42         {
   43             readNext();
   44             _reference["url"] = c2bUtils::pubmedUrl.arg(text().toString());
   45         }
   46         else if (name() == "ArticleId")
   47         {
   48             if (attributes().count() > 0)
   49                 if (attributes().at(0).value().toString() == "doi")
   50                     readField("doi");
   51         }
   52 }
   53 
   54 void pubmedXml::readField(const QString& field)
   55 {
   56     _reference[field] = readElementText(QXmlStreamReader::IncludeChildElements);
   57 }
   58 
   59 void pubmedXml::readAuthors()
   60 {
   61     QStringList authors;
   62     while (reading("AuthorList"))
   63     {
   64         if (name() == "Author" && attributes().value("ValidYN") != "N")
   65         {
   66             QString a, i;
   67             bool abbreviated(true);
   68             while (reading("Author"))
   69                 if (name() == "LastName")
   70                 {
   71                     readNext();
   72                     a = a + ' ' + text().toString();
   73                 }
   74                 else if (name() == "ForeName" || name() == "FirstName")
   75                 {
   76                     readNext();
   77                     a = text().toString() + ' ' + a;
   78                     abbreviated = false;
   79                 }
   80                 else if (name() == "Suffix")
   81                 {
   82                     readNext();
   83                     if (text() == "2nd")
   84                         a = a + " II";
   85                     else if (text() == "3rd")
   86                         a = a + " III";
   87                     else
   88                         a = a + ' ' + text().toString();
   89                 }
   90                 else if (name() == "Initials")
   91                 {
   92                     readNext();
   93                     i = text().toString();
   94                 }
   95             if (abbreviated)
   96                 authors.append(i + ' ' + a);
   97             else
   98                 authors.append(a);
   99         }
  100     }
  101     _reference["author"] = authors.join(", ");
  102 }
  103 
  104 void pubmedXml::readJournal()
  105 {
  106     QString ja, jf, mldate;
  107     while (reading("Journal"))
  108         if (name() == "ISOAbbreviation")
  109         {
  110             readNext();
  111             ja = text().toString();
  112         }
  113         else if (name() == "Title")
  114         {
  115             readNext();
  116             jf = text().toString();
  117         }
  118         else if (name() == "Volume")
  119             readField("volume");
  120         else if (name() == "Issue")
  121             readField("number");
  122         else if (name() == "Year")
  123             readField("year");
  124         else if (name() == "MedlineDate")
  125         {
  126             readNext();
  127             mldate = text().toString();
  128         }
  129     if (jf.isEmpty())
  130         _reference["journal"] = ja;
  131     else
  132         _reference["journal"] = jf;
  133     if (_reference.value("year").isEmpty())
  134     {
  135         mldate.remove(QRegExp("[^\\d\\s-]"));
  136         mldate.remove(QRegExp("\\b\\d{1,3}\\b"));
  137         _reference["year"] = mldate;
  138     }
  139 }
  140 
  141 void pubmedXml::readKeywords()
  142 {
  143     QStringList k;
  144     while (reading("MeshHeadingList"))
  145         if (name() == "DescriptorName")
  146         {
  147             readNext();
  148             k.append(text().toString());
  149         }
  150     _reference["keywords"] = k.join(", ");
  151 }
  152 
  153 bool pubmedXml::reading(const QString& tag)
  154 {
  155     while (!atEnd())
  156     {
  157         TokenType t = readNext();
  158         if (t == StartElement)
  159             return true;
  160         else if (t == EndElement)
  161             if (name() == tag)
  162                 return false;
  163     }
  164     return false;
  165 }