ctkLDAPExpr.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkPluginConstants.h"
  16. #include "ctkPluginFramework_global.h"
  17. #include <exception>
  18. #include <QString>
  19. #include <QHash>
  20. #include <QSharedDataPointer>
  21. #include <stdexcept>
  22. class ctkLDAPExprData;
  23. /**
  24. \brief LDAP Expression
  25. \date 19 May 2010
  26. \author Xavi Planes
  27. \ingroup ctkPluginFramework
  28. */
  29. class CTK_PLUGINFW_EXPORT ctkLDAPExpr {
  30. public:
  31. const static int AND = 0;
  32. const static int OR = 1;
  33. const static int NOT = 2;
  34. const static int EQ = 4;
  35. const static int LE = 8;
  36. const static int GE = 16;
  37. const static int APPROX = 32;
  38. const static int COMPLEX = AND | OR | NOT;
  39. const static int SIMPLE = EQ | LE | GE | APPROX;
  40. typedef char Byte;
  41. public:
  42. //!
  43. ctkLDAPExpr(const QString &filter) throw ( std::invalid_argument );
  44. //!
  45. ctkLDAPExpr(const ctkLDAPExpr& other);
  46. /**
  47. * Get object class set matched by this LDAP expression. This will not work
  48. * with wildcards and NOT expressions. If a set can not be determined return null.
  49. *
  50. * @return A set of classes matched, otherwise an empty set.
  51. */
  52. QSet<QString> getMatchedObjectClasses() const;
  53. /**
  54. * Checks if this LDAP expression is "simple". The definition of
  55. * a simple filter is:
  56. * <ul>
  57. * <li><code>(<it>name</it>=<it>value</it>)</code> is simple if
  58. * <it>name</it> is a member of the provided <code>keywords</code>,
  59. * and <it>value</it> does not contain a wildcard character;</li>
  60. * <li><code>(| EXPR+ )</code> is simple if all <code>EXPR</code>
  61. * expressions are simple;</li>
  62. * <li>No other expressions are simple.</li>
  63. * </ul>
  64. * If the filter is found to be simple, the <code>cache</code> is
  65. * filled with mappings from the provided keywords to lists
  66. * of attribute values. The keyword-value-pairs are the ones that
  67. * satisfy this expression, for the given keywords.
  68. *
  69. * @param keywords The keywords to look for.
  70. * @param cache An array (indexed by the keyword indexes) of lists to
  71. * fill in with values saturating this expression.
  72. * @return <code>true</code> if this expression is simple,
  73. * <code>false</code> otherwise.
  74. */
  75. bool isSimple(
  76. const QList<QString> &keywords,
  77. QHash<int, QList<QString> > &cache,
  78. bool matchCase) const;
  79. //!
  80. static bool query(const QString &filter, const ctkDictionary &pd)
  81. throw (std::invalid_argument);
  82. //! Evaluate this LDAP filter.
  83. bool evaluate(const ctkDictionary &p, bool matchCase) const;
  84. //!
  85. const QString toQString() const;
  86. private:
  87. //!
  88. bool compare(const QVariant &obj, int op, const QString &s) const;
  89. //!
  90. static bool compareQString(const QString &s1, int op, const QString &s2);
  91. //!
  92. const static QString fixupQString(const QString &s);
  93. //!
  94. static bool patSubstr(const QString &s, const QString &pat);
  95. //!
  96. static bool patSubstr(const QString &s, int si, const QString &pat, int pi);
  97. //! Contains the current parser position and parsing utility methods.
  98. class ParseState {
  99. int m_pos;
  100. QString m_str;
  101. public:
  102. ParseState(const QString &str) throw (std::invalid_argument);
  103. //! Move m_pos to remove the prefix \a pre
  104. bool prefix(const QString &pre);
  105. /** Peek a char at m_pos
  106. \note If index out of bounds, throw exception
  107. */
  108. QChar peek();
  109. //! Increment m_pos by n
  110. void skip(int n);
  111. //! return string from m_pos until the end
  112. const QString rest();
  113. //! Move m_pos until there's no spaces
  114. void skipWhite();
  115. //! Get string until special chars. Move m_pos
  116. const QString getAttributeName();
  117. //! Get string and convert * to WILDCARD
  118. const QString getAttributeValue();
  119. //! Throw InvalidSyntaxException exception
  120. void error(const QString &m) throw (std::invalid_argument);
  121. };
  122. //!
  123. static ctkLDAPExpr parseExpr(ParseState &ps)
  124. throw (std::invalid_argument);
  125. //!
  126. static ctkLDAPExpr parseSimple(ParseState &ps)
  127. throw (std::invalid_argument);
  128. private:
  129. //!
  130. ctkLDAPExpr(int op, const QList<ctkLDAPExpr> &args);
  131. //!
  132. ctkLDAPExpr(int op, const QString &attrName, const QString &attrValue);
  133. private:
  134. const static QChar WILDCARD; // = 65535;
  135. const static QString WILDCARD_QString;// = QString( WILDCARD );
  136. const static QString NULLQ;// = "Null query";
  137. const static QString GARBAGE;// = "Trailing garbage";
  138. const static QString EOS;// = "Unexpected end of query";
  139. const static QString MALFORMED;// = "Malformed query";
  140. const static QString OPERATOR;// = "Undefined m_operator";
  141. private:
  142. //! Shared pointer
  143. QSharedDataPointer<ctkLDAPExprData> d;
  144. };
  145. /**
  146. \brief LDAP Expression Data
  147. \date 19 May 2010
  148. \author Xavi Planes
  149. \ingroup ctkPluginFramework
  150. */
  151. class ctkLDAPExprData : public QSharedData
  152. {
  153. public:
  154. ctkLDAPExprData( int op, QList<ctkLDAPExpr> args )
  155. {
  156. m_operator = op;
  157. m_args = args;
  158. m_attrName = QString::Null( );
  159. m_attrValue = QString::Null( );
  160. }
  161. ctkLDAPExprData( int op, QString attrName, QString attrValue )
  162. {
  163. m_operator = op;
  164. m_args.clear();
  165. m_attrName = attrName;
  166. m_attrValue = attrValue;
  167. }
  168. ctkLDAPExprData( const ctkLDAPExprData& other ) : QSharedData(other)
  169. {
  170. m_operator = other.m_operator;
  171. m_args = other.m_args;
  172. m_attrName = other.m_attrName;
  173. m_attrValue = other.m_attrValue;
  174. }
  175. //!
  176. int m_operator;
  177. //!
  178. QList<ctkLDAPExpr> m_args;
  179. //!
  180. QString m_attrName;
  181. //!
  182. QString m_attrValue;
  183. };