ctkLDAPExpr_p.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 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. #ifndef CTKLDAPEXPR_P_H
  16. #define CTKLDAPEXPR_P_H
  17. #include "ctkPluginConstants.h"
  18. #include "ctkPluginFramework_global.h"
  19. #include <QString>
  20. #include <QHash>
  21. #include <QSharedDataPointer>
  22. #include <QVector>
  23. #include <QStringList>
  24. #include <stdexcept>
  25. class ctkLDAPExprData;
  26. /**
  27. \brief LDAP Expression
  28. \date 19 May 2010
  29. \author Xavi Planes
  30. \ingroup ctkPluginFramework
  31. */
  32. class ctkLDAPExpr {
  33. public:
  34. const static int AND; // = 0;
  35. const static int OR; // = 1;
  36. const static int NOT; // = 2;
  37. const static int EQ; // = 4;
  38. const static int LE; // = 8;
  39. const static int GE; // = 16;
  40. const static int APPROX; // = 32;
  41. const static int COMPLEX; // = AND | OR | NOT;
  42. const static int SIMPLE; // = EQ | LE | GE | APPROX;
  43. typedef char Byte;
  44. typedef QVector<QStringList> LocalCache;
  45. /**
  46. * Creates an invalid ctkLDAPExpr object. Use with care.
  47. *
  48. * @see isNull()
  49. */
  50. ctkLDAPExpr();
  51. //!
  52. ctkLDAPExpr(const QString &filter) throw ( std::invalid_argument );
  53. //!
  54. ctkLDAPExpr(const ctkLDAPExpr& other);
  55. ctkLDAPExpr& operator=(const ctkLDAPExpr& other);
  56. ~ctkLDAPExpr();
  57. /**
  58. * Get object class set matched by this LDAP expression. This will not work
  59. * with wildcards and NOT expressions. If a set can not be determined return null.
  60. *
  61. * @return A set of classes matched, otherwise an empty set.
  62. */
  63. QSet<QString> getMatchedObjectClasses() const;
  64. /**
  65. * Checks if this LDAP expression is "simple". The definition of
  66. * a simple filter is:
  67. * <ul>
  68. * <li><code>(<it>name</it>=<it>value</it>)</code> is simple if
  69. * <it>name</it> is a member of the provided <code>keywords</code>,
  70. * and <it>value</it> does not contain a wildcard character;</li>
  71. * <li><code>(| EXPR+ )</code> is simple if all <code>EXPR</code>
  72. * expressions are simple;</li>
  73. * <li>No other expressions are simple.</li>
  74. * </ul>
  75. * If the filter is found to be simple, the <code>cache</code> is
  76. * filled with mappings from the provided keywords to lists
  77. * of attribute values. The keyword-value-pairs are the ones that
  78. * satisfy this expression, for the given keywords.
  79. *
  80. * @param keywords The keywords to look for.
  81. * @param cache An array (indexed by the keyword indexes) of lists to
  82. * fill in with values saturating this expression.
  83. * @return <code>true</code> if this expression is simple,
  84. * <code>false</code> otherwise.
  85. */
  86. bool isSimple(
  87. const QStringList& keywords,
  88. LocalCache& cache,
  89. bool matchCase) const;
  90. /**
  91. * Returns <code>true</code> if this instance is invalid, i.e. it was
  92. * constructed using ctkLDAPExpr().
  93. *
  94. * @return <code>true</code> if the expression is invalid,
  95. * <code>false</code> otherwise.
  96. */
  97. bool isNull() const;
  98. //!
  99. static bool query(const QString &filter, const ctkDictionary &pd)
  100. throw (std::invalid_argument);
  101. //! Evaluate this LDAP filter.
  102. bool evaluate(const ctkDictionary &p, bool matchCase) const;
  103. //!
  104. const QString toString() const;
  105. private:
  106. class ParseState;
  107. //!
  108. ctkLDAPExpr(int op, const QList<ctkLDAPExpr> &args);
  109. //!
  110. ctkLDAPExpr(int op, const QString &attrName, const QString &attrValue);
  111. //!
  112. static ctkLDAPExpr parseExpr(ParseState &ps)
  113. throw (std::invalid_argument);
  114. //!
  115. static ctkLDAPExpr parseSimple(ParseState &ps)
  116. throw (std::invalid_argument);
  117. //!
  118. bool compare(const QVariant &obj, int op, const QString &s) const;
  119. //!
  120. static bool compareString(const QString &s1, int op, const QString &s2);
  121. //!
  122. static QString fixupString(const QString &s);
  123. //!
  124. static bool patSubstr(const QString &s, const QString &pat);
  125. //!
  126. static bool patSubstr(const QString &s, int si, const QString &pat, int pi);
  127. const static QChar WILDCARD; // = 65535;
  128. const static QString WILDCARD_QString;// = QString( WILDCARD );
  129. const static QString NULLQ;// = "Null query";
  130. const static QString GARBAGE;// = "Trailing garbage";
  131. const static QString EOS;// = "Unexpected end of query";
  132. const static QString MALFORMED;// = "Malformed query";
  133. const static QString OPERATOR;// = "Undefined m_operator";
  134. //! Shared pointer
  135. QSharedDataPointer<ctkLDAPExprData> d;
  136. };
  137. #endif // CTKLDAPEXPR_P_H