ctkPluginManifest.cpp

Go to the documentation of this file.
00001 /*=============================================================================
00002 
00003   Library: CTK
00004 
00005   Copyright (c) 2010 German Cancer Research Center,
00006     Division of Medical and Biological Informatics
00007 
00008   Licensed under the Apache License, Version 2.0 (the "License");
00009   you may not use this file except in compliance with the License.
00010   You may obtain a copy of the License at
00011 
00012     http://www.apache.org/licenses/LICENSE-2.0
00013 
00014   Unless required by applicable law or agreed to in writing, software
00015   distributed under the License is distributed on an "AS IS" BASIS,
00016   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017   See the License for the specific language governing permissions and
00018   limitations under the License.
00019 
00020 =============================================================================*/
00021 
00022 #include "ctkPluginManifest_p.h"
00023 
00024 #include <QStringList>
00025 #include <QIODevice>
00026 #include <QDebug>
00027 
00028 #include <stdexcept>
00029 
00030 
00031   ctkPluginManifest::ctkPluginManifest()
00032   {
00033 
00034   }
00035 
00036   ctkPluginManifest::ctkPluginManifest(const QByteArray& in)
00037   {
00038     read(in);
00039   }
00040 
00041   void ctkPluginManifest::read(const QByteArray& in)
00042   {
00043     mainAttributes.clear();
00044     sections.clear();
00045 
00046     QString key;
00047     QString value;
00048     QString currSection;
00049 
00050     QList<QByteArray> lines = in.split('\n');
00051 
00052     foreach (const QString& line, lines)
00053     {
00054       // skip empty lines and comments
00055       if (line.trimmed().isEmpty() | line.startsWith('#')) continue;
00056 
00057       // a valid new key/value pair starts with no white-space and contains
00058       // either a ':' or a '='
00059       if (!(line.startsWith(' ') || line.startsWith('\t')) && (line.contains(':') || line.contains('=')))
00060       {
00061 
00062         // see if we found a new section header
00063         if (line.startsWith('['))
00064         {
00065           currSection = line.mid(1, line.indexOf(']')-1);
00066         }
00067         else
00068         {
00069           // we found a new key/value pair, save the old one
00070           if (!key.isEmpty())
00071           {
00072             if (currSection.isEmpty())
00073             {
00074               mainAttributes.insert(key, value);
00075             }
00076             else
00077             {
00078               sections[currSection].insert(key, value);
00079             }
00080           }
00081 
00082           int colonIndex = line.indexOf(':');
00083           int equalIndex = line.indexOf('=');
00084 
00085           int sepIndex = -1;
00086           if (colonIndex < 0) sepIndex = equalIndex;
00087           else if (equalIndex < 0) sepIndex = colonIndex;
00088           else sepIndex = colonIndex < equalIndex ? colonIndex : equalIndex;
00089 
00090           key = line.left(sepIndex);
00091           value = line.right(line.size()-(sepIndex+1)).trimmed();
00092         }
00093       }
00094       else
00095       {
00096         // add the line to the value
00097         value += line;
00098       }
00099     }
00100 
00101     // save the last key/value pair
00102     if (!key.isEmpty())
00103     {
00104       if (currSection.isEmpty())
00105       {
00106         mainAttributes.insert(key, value);
00107       }
00108       else
00109       {
00110         sections[currSection].insert(key, value);
00111       }
00112     }
00113 
00114   }
00115 
00116   ctkPluginManifest::Attributes ctkPluginManifest::getMainAttributes() const
00117   {
00118     return mainAttributes;
00119   }
00120 
00121   QString ctkPluginManifest::getAttribute(const QString& key) const
00122   {
00123     if (!mainAttributes.contains(key)) return QString();
00124     return mainAttributes[key];
00125   }
00126 
00127   ctkPluginManifest::Attributes ctkPluginManifest::getAttributes(const QString& section) const
00128   {
00129     if (!sections.contains(section))
00130     {
00131       throw std::invalid_argument(std::string("Manifest section invalid: ") + qPrintable(section));
00132     }
00133 
00134     return sections[section];
00135   }
00136 
00137   QStringList ctkPluginManifest::getSections() const
00138   {
00139     return sections.keys();
00140 
00141 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines