QtDBusXML.h

Go to the documentation of this file.
00001 //# QtDBusXML.h: XML scheme to be used with CASA's Qt DBus communication.
00002 //# Copyright (C) 2009
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: $
00027 #ifndef QTDBUSXML_H_
00028 #define QTDBUSXML_H_
00029 
00030 #include <casa/Containers/Record.h>
00031 
00032 #include <QDomDocument>
00033 #include <QDBusArgument>
00034 #include <QMetaType>
00035 
00036 namespace casa {
00037 
00038 // Subclass of QDomDocument that represents an XML scheme that is to be used
00039 // with CASA's Qt dbus communication.
00040 // <casa-dbus>
00041 //   <time>[TIMESTAMP]</time>
00042 //   <from>[NAME]</from>
00043 //   <to>[NAME]</to>
00044 //   <method name="[METHOD]" async="[ISASYNC]">
00045 //     <param name="[NAME]" type="[TYPE]">[VALUE]</param>
00046 //     ...
00047 //   </method>
00048 //   <returned type="[TYPE]">[VALUE]</returned>
00049 // </casa-dbus>
00050 // Currently supported types for parameters and returned values:
00051 //   bool, int, uInt, double, String, Record, Array<Bool> and
00052 //   Array<Int>, and Records with these types.
00053 // For a discussion of what these fields mean, see the documentation for
00054 // QtDBusXmlApp.
00055 class QtDBusXML {
00056 public:
00057     // Static Methods //
00058     
00059     // Constructs and returns an XML message using the given parameters.  Only
00060     // uses the non-empty values.  Sets the time to the current.
00061     // <group>
00062     static QtDBusXML constructXML(const String& from = "",
00063             const String& to = "", const String& methodName = "",
00064             bool methodIsAsync = false, const Record& methodParams = Record(),
00065             const Record& retValue = Record()) {
00066         return constructXML(QString(from.c_str()), QString(to.c_str()),
00067                 QString(methodName.c_str()), methodIsAsync, methodParams,
00068                 retValue); }
00069     static QtDBusXML constructXML(const QString& from = "",
00070             const QString& to = "", const QString& methodName = "",
00071             bool methodIsAsync = false, const Record& methodParams = Record(),
00072             const Record& retValue = Record());
00073     // </group>
00074     
00075     // Constructs and returns an XML message from the given XML string.
00076     // <group>
00077     static QtDBusXML fromString(const String& xmlStr) {
00078         return fromString(QString(xmlStr.c_str())); }
00079     static QtDBusXML fromString(const QString& xmlStr);
00080     // </group>
00081     
00082     // Reads the values from the given XML message into the given parameters
00083     // which are not NULL.
00084     // <group>
00085     static void extractXML(const QtDBusXML& xml, String* time = NULL,
00086             String* from = NULL, String* to = NULL, String* methodName = NULL,
00087             Record* methodParams = NULL, Record* retValue = NULL) {
00088         QString* qtime = NULL, *qfrom = NULL, *qto = NULL, *qmethodName = NULL;
00089         if(time != NULL)       qtime       = new QString();
00090         if(from != NULL)       qfrom       = new QString();
00091         if(to != NULL)         qto         = new QString();
00092         if(methodName != NULL) qmethodName = new QString();
00093         extractXML(xml, qtime, qfrom, qto, qmethodName, methodParams,retValue);
00094         if(time != NULL)       { *time = qtime->toStdString(); delete qtime; }
00095         if(from != NULL)       { *from = qfrom->toStdString(); delete qfrom; }
00096         if(to != NULL)         { *to = qto->toStdString(); delete qto; }
00097         if(methodName != NULL) { *methodName = qmethodName->toStdString();
00098                                  delete qmethodName; }
00099     }
00100     static void extractXML(const QtDBusXML& xml, QString* time = NULL,
00101             QString* from = NULL, QString* to = NULL, QString* methodName=NULL,
00102             Record* methodParams = NULL, Record* retValue = NULL);
00103     // </group>
00104     
00105     
00106     // Non-Static Methods //
00107     
00108     // Constructor.
00109     QtDBusXML();
00110     
00111     // Copy constructor, see operator=().
00112     QtDBusXML(const QtDBusXML& copy);
00113     
00114     // Destructor.
00115     virtual ~QtDBusXML();
00116     
00117     
00118     // Gets the value of the time tag, or an empty string if there is none.
00119     // <group>
00120     String time() const { return qtime().toStdString(); }
00121     QString qtime() const;
00122     // </group>
00123     
00124     // Sets the time tag to the current time.
00125     void setTime();
00126     
00127     // Gets/Sets the from tag.
00128     // <group>
00129     String from() const { return qfrom().toStdString(); }
00130     QString qfrom() const;
00131     void setFrom(const String& value) { setFrom(QString(value.c_str())); }
00132     void setFrom(const QString& value);
00133     // </group>
00134     
00135     // Gets/Sets the to tag.
00136     // <group>
00137     String to() const { return qto().toStdString(); }
00138     QString qto() const;
00139     void setTo(const String& value) { setTo(QString(value.c_str())); }
00140     void setTo(const QString& value);
00141     // </group>
00142     
00143     // Gets/Sets the method name and whether the method call is asynchronous or
00144     // not (default is false).
00145     // <group>
00146     String methodName() const { return qmethodName().toStdString(); }
00147     QString qmethodName() const;
00148     bool methodIsAsync() const;
00149     void setMethodName(const String& value, bool isAsync = false) {
00150         setMethodName(QString(value.c_str()), isAsync); }
00151     void setMethodName(const QString& value, bool isAsync = false);
00152     void setMethodIsAsync(bool value);
00153     // </group>
00154     
00155     // Returns the type of the method parameter with the given name, or an
00156     // empty string if there is none.
00157     // <group>
00158     String methodParamType(const String& paramName) const {
00159         return qmethodParamType(QString(paramName.c_str())).toStdString(); }
00160     QString qmethodParamType(const QString& paramName) const;
00161     // </group>
00162     
00163     // Returns whether the method parameter with the given name is the
00164     // specified type or not.
00165     // <group>
00166     bool methodParamIsBool(const String& paramName) const;
00167     bool methodParamIsInt(const String& paramName) const;
00168     bool methodParamIsUInt(const String& paramName) const;
00169     bool methodParamIsDouble(const String& paramName) const;
00170     bool methodParamIsString(const String& paramName) const;
00171     bool methodParamIsRecord(const String& paramName) const;
00172     bool methodParamIsArrayBool(const String& paramName) const;
00173     bool methodParamIsArrayInt(const String& paramName) const;
00174     // </group>
00175     
00176     // Returns the value of the method parameter with the given name as the
00177     // specified type.  Is invalid if that parameter is not of the requested
00178     // type.  Note: the value can always be returned as a string
00179     // representation.
00180     // <group>
00181     bool methodParamBool(const String& paramName) const {
00182         return methodParamBool(QString(paramName.c_str())); }
00183     bool methodParamBool(const QString& paramName) const;
00184     int methodParamInt(const String& paramName) const {
00185         return methodParamInt(QString(paramName.c_str())); }
00186     int methodParamInt(const QString& paramName) const;
00187     uInt methodParamUInt(const String& paramName) const {
00188         return methodParamUInt(QString(paramName.c_str())); }
00189     uInt methodParamUInt(const QString& paramName) const;
00190     double methodParamDouble(const String& paramName) const {
00191         return methodParamDouble(QString(paramName.c_str())); }
00192     double methodParamDouble(const QString& paramName) const;
00193     String methodParamString(const String& paramName) const {
00194         return methodParamQString(QString(paramName.c_str())).toStdString(); }
00195     QString methodParamQString(const QString& paramName) const;
00196     Record methodParamRecord(const String& paramName) const {
00197         return methodParamRecord(QString(paramName.c_str())); }
00198     Record methodParamRecord(const QString& paramName) const;
00199     Array<Bool> methodParamArrayBool(const String& paramName) const {
00200         return methodParamArrayBool(QString(paramName.c_str())); }
00201     Array<Bool> methodParamArrayBool(const QString& paramName) const;
00202     Array<Int> methodParamArrayInt(const String& paramName) const {
00203         return methodParamArrayInt(QString(paramName.c_str())); }
00204     Array<Int> methodParamArrayInt(const QString& paramName) const;
00205     // </group>
00206     
00207     // Sets the parameter with the given name to the given value (and
00208     // associated type).
00209     // <group>
00210     void setMethodParam(const String& paramName, bool value) {
00211         setMethodParam(QString(paramName.c_str()), value); }
00212     void setMethodParam(const QString& paramName, bool value);
00213     void setMethodParam(const String& paramName, int value) {
00214         setMethodParam(QString(paramName.c_str()), value); }
00215     void setMethodParam(const QString& paramName, int value);
00216     void setMethodParam(const String& paramName, uInt value) {
00217         setMethodParam(QString(paramName.c_str()), value); }
00218     void setMethodParam(const QString& paramName, uInt value);
00219     void setMethodParam(const String& paramName, double value) {
00220         setMethodParam(QString(paramName.c_str()), value); }
00221     void setMethodParam(const QString& paramName, double value);
00222     void setMethodParam(const String& paramName, const String& value) {
00223         setMethodParam(QString(paramName.c_str()), QString(value.c_str())); }
00224     void setMethodParam(const QString& paramName, const QString& value);
00225     void setMethodParam(const String& paramName, const Record& value) {
00226         setMethodParam(QString(paramName.c_str()), value); }
00227     void setMethodParam(const QString& paramName, const Record& value);
00228     void setMethodParam(const String& paramName, const Array<bool>& value) {
00229         setMethodParam(QString(paramName.c_str()), value); }
00230     void setMethodParam(const QString& paramName, const Array<bool>& value);
00231     void setMethodParam(const String& paramName, const Array<int>& value) {
00232         setMethodParam(QString(paramName.c_str()), value); }
00233     void setMethodParam(const QString& paramName, const Array<int>& value);
00234     // </group>
00235     
00236     // Gets/Sets all method parameter values as a Record.
00237     // <group>
00238     Record methodParams() const;
00239     void setMethodParams(const Record& parameters);
00240     // </group>
00241     
00242     // Returns whether or not a returned value was set.
00243     bool returnedSet() const { return !qreturnedType().isEmpty(); }
00244     
00245     // Returns the type of the returned value, or empty string for none.
00246     // <group>
00247     String returnedType() const { return qreturnedType().toStdString(); }
00248     QString qreturnedType() const;
00249     // </group>
00250     
00251     // Returns whether the returned value is the specified type or not.
00252     // <group>
00253     bool returnedIsBool() const;
00254     bool returnedIsInt() const;
00255     bool returnedIsUInt() const;
00256     bool returnedIsDouble() const;
00257     bool returnedIsString() const;
00258     bool returnedIsRecord() const;
00259     bool returnedIsArrayBool() const;
00260     bool returnedIsArrayInt() const;
00261     // </group>
00262     
00263     // Returns the returned value as the specified type.  Is invalid if that
00264     // parameter is not of the requested type.  Note: the value can always be
00265     // returned as a string representation.
00266     // <group>
00267     bool returnedBool() const;
00268     int returnedInt() const;
00269     uInt returnedUInt() const;
00270     double returnedDouble() const;
00271     String returnedString() const { return returnedQString().toStdString(); }
00272     QString returnedQString() const;
00273     Record returnedRecord() const;
00274     Array<bool> returnedArrayBool() const;
00275     Array<int> returnedArrayInt() const;
00276     // </group>
00277     
00278     // Sets the returned value to the given value (and associated type).
00279     // <group>
00280     void setReturnedValue(bool value);
00281     void setReturnedValue(int value);
00282     void setReturnedValue(uInt value);
00283     void setReturnedValue(double value);
00284     void setReturnedValue(const String& value) {
00285         setReturnedValue(QString(value.c_str())); }
00286     void setReturnedValue(const QString& value);
00287     void setReturnedValue(const Record& value);
00288     void setReturnedValue(const Array<Bool>& value);
00289     void setReturnedValue(const Array<Int>& value);
00290     // </group>
00291     
00292     // Gets/Sets the returned value as a record.  ONLY the first field is used.
00293     // <group>
00294     Record returnedValue() const;
00295     void setReturnedValueRec(const Record& retValue);
00296     // </group>
00297     
00298     
00299     // Returns the whole XML as a string.
00300     // <group>
00301     String toXMLString() const { return toXMLQString().toStdString(); }
00302     QString toXMLQString() const;
00303     // </group>
00304     
00305     // Sets the whole XML as a string, and returns whether the operation
00307     // <group>
00308     bool fromXMLString(const String& value) {
00309         return fromXMLString(QString(value.c_str())); }
00310     bool fromXMLString(const QString& value);
00311     // </group>
00312     
00313     // Returns the underlying QDomDocument.
00314     // <group>
00315     QDomDocument& domDocument();
00316     const QDomDocument& domDocument() const;
00317     // </group>
00318     
00319     
00320     // Copy operator.
00321     QtDBusXML& operator=(const QtDBusXML& copy);
00322     
00323 private:
00324     // XML document.
00325     QDomDocument itsXML_;
00326     
00327     
00328     // Initialize object; meant to be called from constructor.
00329     void initialize();
00330     
00331     // Helper method for elemChildText().
00332     QString elemChildText(const QString& name) const {
00333         return elemChildText(itsXML_, name, false); }
00334     
00335     // Helper method for setElemChildText().
00336     void setElemChildText(const QString& name, const QString& value) {
00337         setElemChildText(itsXML_, name, value, true); }
00338     
00339     // Helper method that returns the element for the method parameter with the
00340     // given name, or a null element if it is not.  See elemChild().
00341     QDomElement methodParam(const QString& paramName,
00342             bool createIfAbsent = false) const;
00343     
00344     // Helper method for setting the method parameter values.
00345     void setMethodParam(const QString& name, const QString& type,
00346             const QString& value);
00347     
00348     // Helper method for setting the returned value.
00349     void setReturnedValue(const QString& type, const QString& value);
00350     
00351     
00352     // Static //
00353     
00354     // Converts between QStrings and bools.
00355     // <group>
00356     static bool qstringToBool(const QString& value);
00357     static QString qstringFromBool(bool value);
00358     // </group>
00359     
00360     // Returns the child of the given element with the given tag name.  If
00361     // createIfAbsent is true, then the element will be created and appended if
00362     // it is not present; otherwise, the returned element will be null if not
00363     // present.  If the given element is null, a null element is returned.
00364     // <group>
00365     static QDomElement elemChild(QDomDocument doc, const QString& name,
00366             bool createIfAbsent = false) {
00367         return elemChild(doc.documentElement(), name, createIfAbsent); }
00368     static QDomElement elemChild(QDomElement elem, const QString& name,
00369             bool createIfAbsent = false);
00370     // </group>
00371     
00372     // Returns the text value of the child of the given element with the given
00373     // tag name.  See elemChild().
00374     // <group>
00375     static QString elemChildText(QDomDocument doc, const QString& name,
00376             bool createIfAbsent = false) {
00377         return elemChildText(doc.documentElement(), name, createIfAbsent); }
00378     static QString elemChildText(QDomElement elem, const QString& name,
00379             bool createIfAbsent = false) {
00380         return elemChild(elem, name, createIfAbsent).text();
00381     }
00382     // </group>
00383     
00384     // Sets the text value of the child of the given element with the given tag
00385     // name to the given value.  See elemChild().
00386     // <group>
00387     static void setElemChildText(QDomDocument doc, const QString& name,
00388             const QString& value, bool createIfAbsent = false) {
00389         setElemChildText(doc.documentElement(), name, value, createIfAbsent); }
00390     static void setElemChildText(QDomElement elem, const QString& name,
00391             const QString& value, bool createIfAbsent = false) {
00392         setElemText(elemChild(elem, name, createIfAbsent), value); }
00393     // </group>
00394     
00395     // Sets the text value of the given element (if it is not null) to the
00396     // given text.
00397     static void setElemText(QDomElement elem, const QString& text);
00398     
00399     // Converts between a QDomElement and Record for values.
00400     // <group>
00401     static Record elemToRecord(QDomElement value);
00402     static void elemToRecord(Record& rec, QDomElement value);
00403     static void elemFromRecord(QDomElement elem, const Record& value);
00404     // </group>
00405     // Converts between a QDomElement and Bool Array for values.
00406     // <group>
00407     static Array<Bool> elemToArrayBool(QDomElement value);
00408     static void elemFromArrayBool(QDomElement elem, const Array<Bool>& value);
00409     // </group>
00410     // Converts between a QDomElement and Int Array for values.
00411     // <group>
00412     static Array<Int> elemToArrayInt(QDomElement value);
00413     static void elemFromArrayInt(QDomElement elem, const Array<Int>& value);
00414     // </group>
00415 };
00416 
00417 }
00418 
00419 #endif /* QTDBUSXML_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1