properties.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: properties.h
  5. * created: September
  6. * author: Martin Heinrich
  7. *
  8. *
  9. * Copyright 2007 Martin Heinrich
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ******************************************************************************/
  24. #ifndef LOG4QT_PROPERTIES_H
  25. #define LOG4QT_PROPERTIES_H
  26. /******************************************************************************
  27. * Dependencies
  28. ******************************************************************************/
  29. #include <QtCore/QHash>
  30. #include <QtCore/QStringList>
  31. /******************************************************************************
  32. * Declarations
  33. ******************************************************************************/
  34. class QIODevice;
  35. class QSettings;
  36. namespace Log4Qt
  37. {
  38. /*!
  39. * \brief The class Properties implements a JAVA property hash.
  40. */
  41. class Properties : public QHash<QString, QString>
  42. {
  43. public:
  44. Properties(Properties *pDefaultProperties = 0);
  45. // virtual ~Properties(); // Use compiler default
  46. // Properties(const Properties &rOther); // Use compiler default
  47. // Properties &operator=(const Properties &rOther); // Not implemented
  48. public:
  49. Properties *defaultProperties() const;
  50. QString property(const QString &rKey) const;
  51. QString property(const QString &rKey,
  52. const QString &rDefaultValue) const;
  53. void setDefaultProperties(Properties *pDefault);
  54. void setProperty(const QString &rKey,
  55. const QString &rValue);
  56. // JAVA: void list(QTextStream &rTextStream);
  57. void load(QIODevice *pDevice);
  58. /*!
  59. * Reads all child keys from the QSettings object \a rSettings and
  60. * inserts them into this object. The value is created using
  61. * QVariant::toString(). Types that do not support toString() are
  62. * resulting in an empty string.
  63. *
  64. * \code
  65. * QSettings settings;
  66. * settings.setValue("Package", "Full");
  67. * settings.setValue("Background", Qt::white);
  68. * settings.setValue("Support", true);
  69. * settings.setValue("Help/Language", "en_UK");
  70. *
  71. * Properties properties
  72. * properties.load(&settings)
  73. *
  74. * // properties (("Package", "Full"), ("Background", ""), ("Support", "true"))
  75. * \endcode
  76. */
  77. void load(const QSettings &rSettings);
  78. QStringList propertyNames() const;
  79. // JAVA: void save(QIODevice *pDevice) const;
  80. private:
  81. void parseProperty(const QString &rProperty,
  82. int line);
  83. static int hexDigitValue(const QChar &rDigit);
  84. static QString trimLeft(const QString &rString);
  85. private:
  86. Properties *mpDefaultProperties;
  87. static const char msEscapeChar;
  88. static const char *msValueEscapeCodes;
  89. static const char *msValueEscapeChars;
  90. static const char *msKeyEscapeCodes;
  91. static const char *msKeyEscapeChars;
  92. };
  93. /**************************************************************************
  94. * Operators, Helper
  95. **************************************************************************/
  96. #ifndef QT_NO_DEBUG_STREAM
  97. /*!
  98. * \relates Properties
  99. *
  100. * Writes all object member variables to the given debug stream \a rDebug and
  101. * returns the stream.
  102. *
  103. * <tt>
  104. * %Properties(default:0x0 properties:QHash(("log4j.appender.testAppender.layout", "org.apache.log4j.PatternLayout ")
  105. * ("log4j.appender.testAppender.layout.ConversionPattern", "[%t] %-5p %l: %m%n")
  106. * ("log4j.appender.testAppender.Append", "false ")
  107. * ("log4j.appender.testAppender.File", "output/temp ")
  108. * ("log4j.rootCategory", "TRACE, testAppender")
  109. * ("log4j.appender.testAppender", "org.apache.log4j.FileAppender")) )
  110. * </tt>
  111. * \sa QDebug
  112. */
  113. QDebug operator<<(QDebug debug,
  114. const Properties &rProperties);
  115. #endif // QT_NO_DEBUG_STREAM
  116. /**************************************************************************
  117. * Inline
  118. **************************************************************************/
  119. inline Properties::Properties(Properties *pDefaultProperties) :
  120. mpDefaultProperties(pDefaultProperties)
  121. {}
  122. inline Properties *Properties::defaultProperties() const
  123. { return mpDefaultProperties; }
  124. inline void Properties::setDefaultProperties(Properties *pDefaultProperties)
  125. { mpDefaultProperties = pDefaultProperties; }
  126. inline void Properties::setProperty(const QString &rKey,
  127. const QString &rValue)
  128. { insert(rKey, rValue); }
  129. } // namespace Log4Qt
  130. Q_DECLARE_TYPEINFO(Log4Qt::Properties, Q_MOVABLE_TYPE);
  131. #endif // LOG4QT_PROPERTIES_H