| 1 |
// Copyright (c) 1997 James Clark |
| 2 |
// See the file copying.txt for copying permission. |
| 3 |
|
| 4 |
#ifndef Pattern_INCLUDED |
| 5 |
#define Pattern_INCLUDED 1 |
| 6 |
|
| 7 |
#include <OpenSP/Boolean.h> |
| 8 |
#include "Node.h" |
| 9 |
#include <OpenSP/Link.h> |
| 10 |
#include <OpenSP/IList.h> |
| 11 |
#include <OpenSP/IListIter.h> |
| 12 |
#include <OpenSP/StringC.h> |
| 13 |
#include <OpenSP/Vector.h> |
| 14 |
#include <OpenSP/Ptr.h> |
| 15 |
#include <OpenSP/Resource.h> |
| 16 |
|
| 17 |
#ifdef DSSSL_NAMESPACE |
| 18 |
namespace DSSSL_NAMESPACE { |
| 19 |
#endif |
| 20 |
|
| 21 |
class MatchBase { |
| 22 |
public: |
| 23 |
class MatchContext : public SdataMapper { |
| 24 |
public: |
| 25 |
const Vector<StringC> &classAttributeNames() const; |
| 26 |
const Vector<StringC> &idAttributeNames() const; |
| 27 |
protected: |
| 28 |
Vector<StringC> classAttributeNames_; |
| 29 |
Vector<StringC> idAttributeNames_; |
| 30 |
}; |
| 31 |
MatchBase() : trivial_(0) {} |
| 32 |
MatchBase(bool trivial) : trivial_(trivial) {} |
| 33 |
bool trivial() const { return trivial_; } |
| 34 |
virtual bool matches(const NodePtr &, MatchContext &) const = 0; |
| 35 |
void swap(MatchBase &mb) { |
| 36 |
bool tem = trivial_; |
| 37 |
trivial_ = mb.trivial_; |
| 38 |
mb.trivial_ = tem; |
| 39 |
} |
| 40 |
private: |
| 41 |
bool trivial_; |
| 42 |
}; |
| 43 |
|
| 44 |
class Pattern : virtual public MatchBase { |
| 45 |
public: |
| 46 |
class Qualifier : public Link { |
| 47 |
public: |
| 48 |
virtual ~Qualifier(); |
| 49 |
virtual bool satisfies(const NodePtr &, MatchContext &) const = 0; |
| 50 |
virtual void contributeSpecificity(int *) const = 0; |
| 51 |
virtual bool vacuous() const; |
| 52 |
protected: |
| 53 |
static bool matchAttribute(const StringC &name, |
| 54 |
const StringC &value, |
| 55 |
const NodePtr &nd, |
| 56 |
MatchContext &context); |
| 57 |
}; |
| 58 |
class IdQualifier : public Qualifier { |
| 59 |
public: |
| 60 |
IdQualifier(const StringC &); |
| 61 |
bool satisfies(const NodePtr &, MatchContext &) const; |
| 62 |
void contributeSpecificity(int *) const; |
| 63 |
private: |
| 64 |
StringC id_; |
| 65 |
}; |
| 66 |
class ClassQualifier : public Qualifier { |
| 67 |
public: |
| 68 |
ClassQualifier(const StringC &); |
| 69 |
bool satisfies(const NodePtr &, MatchContext &) const; |
| 70 |
void contributeSpecificity(int *) const; |
| 71 |
private: |
| 72 |
StringC class_; |
| 73 |
}; |
| 74 |
class AttributeHasValueQualifier : public Qualifier { |
| 75 |
public: |
| 76 |
AttributeHasValueQualifier(const StringC &); |
| 77 |
bool satisfies(const NodePtr &, MatchContext &) const; |
| 78 |
void contributeSpecificity(int *) const; |
| 79 |
private: |
| 80 |
StringC name_; |
| 81 |
}; |
| 82 |
class AttributeMissingValueQualifier : public Qualifier { |
| 83 |
public: |
| 84 |
AttributeMissingValueQualifier(const StringC &); |
| 85 |
bool satisfies(const NodePtr &, MatchContext &) const; |
| 86 |
void contributeSpecificity(int *) const; |
| 87 |
private: |
| 88 |
StringC name_; |
| 89 |
}; |
| 90 |
class AttributeQualifier : public Qualifier { |
| 91 |
public: |
| 92 |
AttributeQualifier(const StringC &, const StringC &); |
| 93 |
bool satisfies(const NodePtr &, MatchContext &) const; |
| 94 |
void contributeSpecificity(int *) const; |
| 95 |
private: |
| 96 |
StringC name_; |
| 97 |
StringC value_; |
| 98 |
}; |
| 99 |
class PositionQualifier : public Qualifier { |
| 100 |
public: |
| 101 |
void contributeSpecificity(int *) const; |
| 102 |
}; |
| 103 |
class FirstOfTypeQualifier : public PositionQualifier { |
| 104 |
public: |
| 105 |
bool satisfies(const NodePtr &, MatchContext &) const; |
| 106 |
}; |
| 107 |
class LastOfTypeQualifier : public PositionQualifier { |
| 108 |
public: |
| 109 |
bool satisfies(const NodePtr &, MatchContext &) const; |
| 110 |
}; |
| 111 |
class FirstOfAnyQualifier : public PositionQualifier { |
| 112 |
public: |
| 113 |
bool satisfies(const NodePtr &, MatchContext &) const; |
| 114 |
}; |
| 115 |
class LastOfAnyQualifier : public PositionQualifier { |
| 116 |
public: |
| 117 |
bool satisfies(const NodePtr &, MatchContext &) const; |
| 118 |
}; |
| 119 |
class OnlyQualifier : public Qualifier { |
| 120 |
public: |
| 121 |
void contributeSpecificity(int *) const; |
| 122 |
}; |
| 123 |
class OnlyOfTypeQualifier : public OnlyQualifier { |
| 124 |
public: |
| 125 |
bool satisfies(const NodePtr &, MatchContext &) const; |
| 126 |
}; |
| 127 |
class OnlyOfAnyQualifier : public OnlyQualifier { |
| 128 |
public: |
| 129 |
bool satisfies(const NodePtr &, MatchContext &) const; |
| 130 |
}; |
| 131 |
class VacuousQualifier : public Qualifier { |
| 132 |
public: |
| 133 |
bool vacuous() const; |
| 134 |
}; |
| 135 |
class PriorityQualifier : public VacuousQualifier { |
| 136 |
public: |
| 137 |
PriorityQualifier(long); |
| 138 |
void contributeSpecificity(int *) const; |
| 139 |
bool satisfies(const NodePtr &, MatchContext &) const; |
| 140 |
private: |
| 141 |
long n_; |
| 142 |
}; |
| 143 |
class ImportanceQualifier : public VacuousQualifier { |
| 144 |
public: |
| 145 |
ImportanceQualifier(long); |
| 146 |
void contributeSpecificity(int *) const; |
| 147 |
bool satisfies(const NodePtr &, MatchContext &) const; |
| 148 |
private: |
| 149 |
long n_; |
| 150 |
}; |
| 151 |
typedef unsigned Repeat; |
| 152 |
class Element : public Link { |
| 153 |
public: |
| 154 |
Element(const StringC &); |
| 155 |
bool matches(const NodePtr &, MatchContext &) const; |
| 156 |
void contributeSpecificity(int *) const; |
| 157 |
void addQualifier(Qualifier *); |
| 158 |
void setRepeat(Repeat minRepeat, Repeat maxRepeat); |
| 159 |
Repeat minRepeat() const; |
| 160 |
Repeat maxRepeat() const; |
| 161 |
bool mustHaveGi(StringC &) const; |
| 162 |
bool trivial() const; |
| 163 |
private: |
| 164 |
StringC gi_; |
| 165 |
Repeat minRepeat_; |
| 166 |
Repeat maxRepeat_; |
| 167 |
IList<Qualifier> qualifiers_; |
| 168 |
}; |
| 169 |
class ChildrenQualifier : public Qualifier { |
| 170 |
public: |
| 171 |
ChildrenQualifier(IList<Element> &); |
| 172 |
bool satisfies(const NodePtr &, MatchContext &) const; |
| 173 |
void contributeSpecificity(int *) const; |
| 174 |
private: |
| 175 |
IList<Element> children_; |
| 176 |
}; |
| 177 |
Pattern(); |
| 178 |
Pattern(IList<Element> &); |
| 179 |
void swap(Pattern &); |
| 180 |
bool matches(const NodePtr &, MatchContext &) const; |
| 181 |
// If mustHaveGi returns true, then any element that matches the pattern |
| 182 |
// must have the string returned in str. |
| 183 |
bool mustHaveGi(StringC &str) const; |
| 184 |
// A pattern is trivial if any element that has the gi returned my mustHaveGi |
| 185 |
// matches the pattern, or, if mustHaveGi returns false, if any element matches |
| 186 |
// the pattern. |
| 187 |
static int compareSpecificity(const Pattern &, const Pattern &); |
| 188 |
enum { |
| 189 |
importanceSpecificity, |
| 190 |
idSpecificity, |
| 191 |
classSpecificity, |
| 192 |
giSpecificity, |
| 193 |
repeatSpecificity, |
| 194 |
prioritySpecificity, |
| 195 |
onlySpecificity, |
| 196 |
positionSpecificity, |
| 197 |
attributeSpecificity, |
| 198 |
nSpecificity |
| 199 |
}; |
| 200 |
private: |
| 201 |
Pattern(const Pattern &); // undefined |
| 202 |
void operator=(const Pattern &); // undefined |
| 203 |
void computeSpecificity(int *) const; |
| 204 |
static bool computeTrivial(const IList<Element> &); |
| 205 |
|
| 206 |
static bool matchAncestors(const IListIter<Element> &, |
| 207 |
const NodePtr &, |
| 208 |
MatchContext &); |
| 209 |
static bool matchAncestors1(const IListIter<Element> &, |
| 210 |
const NodePtr &, |
| 211 |
MatchContext &); |
| 212 |
|
| 213 |
IList<Element> ancestors_; // first is self, second is parent ... |
| 214 |
}; |
| 215 |
|
| 216 |
inline |
| 217 |
void Pattern::Element::setRepeat(Repeat minRepeat, Repeat maxRepeat) |
| 218 |
{ |
| 219 |
minRepeat_ = minRepeat; |
| 220 |
maxRepeat_ = maxRepeat; |
| 221 |
} |
| 222 |
|
| 223 |
inline |
| 224 |
Pattern::Repeat Pattern::Element::minRepeat() const |
| 225 |
{ |
| 226 |
return minRepeat_; |
| 227 |
} |
| 228 |
|
| 229 |
inline |
| 230 |
Pattern::Repeat Pattern::Element::maxRepeat() const |
| 231 |
{ |
| 232 |
return maxRepeat_; |
| 233 |
} |
| 234 |
|
| 235 |
inline |
| 236 |
void Pattern::Element::addQualifier(Qualifier *q) |
| 237 |
{ |
| 238 |
qualifiers_.insert(q); |
| 239 |
} |
| 240 |
|
| 241 |
inline |
| 242 |
bool Pattern::matchAncestors(const IListIter<Element> &ancestors, |
| 243 |
const NodePtr &node, |
| 244 |
MatchContext &context) |
| 245 |
{ |
| 246 |
return ancestors.done() || matchAncestors1(ancestors, node, context); |
| 247 |
} |
| 248 |
|
| 249 |
inline |
| 250 |
bool Pattern::matches(const NodePtr &nd, MatchContext &context) const |
| 251 |
{ |
| 252 |
return matchAncestors(IListIter<Element>(ancestors_), |
| 253 |
nd, |
| 254 |
context); |
| 255 |
} |
| 256 |
|
| 257 |
inline |
| 258 |
void Pattern::swap(Pattern &pattern) |
| 259 |
{ |
| 260 |
MatchBase::swap(pattern); |
| 261 |
ancestors_.swap(pattern.ancestors_); |
| 262 |
} |
| 263 |
|
| 264 |
inline |
| 265 |
bool Pattern::Element::mustHaveGi(StringC &gi) const |
| 266 |
{ |
| 267 |
if (minRepeat_ == 0) |
| 268 |
return 0; |
| 269 |
else if (gi_.size()) { |
| 270 |
gi = gi_; |
| 271 |
return 1; |
| 272 |
} |
| 273 |
else |
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
inline |
| 278 |
bool Pattern::mustHaveGi(StringC &gi) const |
| 279 |
{ |
| 280 |
return !ancestors_.empty() && ancestors_.head()->mustHaveGi(gi); |
| 281 |
} |
| 282 |
|
| 283 |
inline |
| 284 |
const Vector<StringC> &Pattern::MatchContext::classAttributeNames() const |
| 285 |
{ |
| 286 |
return classAttributeNames_; |
| 287 |
} |
| 288 |
|
| 289 |
inline |
| 290 |
const Vector<StringC> &Pattern::MatchContext::idAttributeNames() const |
| 291 |
{ |
| 292 |
return idAttributeNames_; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
#ifdef DSSSL_NAMESPACE |
| 297 |
} |
| 298 |
#endif |
| 299 |
|
| 300 |
#endif /* not Pattern_INCLUDED */ |