| 1 |
// Copyright (c) 1997 James Clark |
| 2 |
// See the file copying.txt for copying permission. |
| 3 |
|
| 4 |
#include "stylelib.h" |
| 5 |
#include "Pattern.h" |
| 6 |
#include <OpenSP/macros.h> |
| 7 |
#include <OpenSP/Vector.h> |
| 8 |
#include "Interpreter.h" |
| 9 |
|
| 10 |
#ifdef DSSSL_NAMESPACE |
| 11 |
namespace DSSSL_NAMESPACE { |
| 12 |
#endif |
| 13 |
|
| 14 |
Pattern::Pattern() |
| 15 |
{ |
| 16 |
} |
| 17 |
|
| 18 |
Pattern::Pattern(IList<Element> &ancestors) |
| 19 |
: MatchBase(computeTrivial(ancestors)) |
| 20 |
{ |
| 21 |
ancestors_.swap(ancestors); |
| 22 |
} |
| 23 |
|
| 24 |
bool Pattern::computeTrivial(const IList<Element> &ancestors) |
| 25 |
{ |
| 26 |
IListIter<Element> iter(ancestors); |
| 27 |
if (iter.done()) |
| 28 |
return 1; |
| 29 |
if (!iter.cur()->trivial()) |
| 30 |
return 0; |
| 31 |
iter.next(); |
| 32 |
if (!iter.done()) |
| 33 |
return 0; |
| 34 |
return 1; |
| 35 |
} |
| 36 |
|
| 37 |
Pattern::Element::Element(const StringC &gi) |
| 38 |
: gi_(gi), minRepeat_(1), maxRepeat_(1) |
| 39 |
{ |
| 40 |
} |
| 41 |
|
| 42 |
bool Pattern::Element::matches(const NodePtr &nd, MatchContext &context) const |
| 43 |
{ |
| 44 |
if (gi_.size()) { |
| 45 |
if (!nd->hasGi(GroveString(gi_.data(), gi_.size()))) |
| 46 |
return 0; |
| 47 |
} |
| 48 |
else { |
| 49 |
GroveString tem; |
| 50 |
if (nd->getGi(tem) != accessOK) |
| 51 |
return 0; |
| 52 |
} |
| 53 |
for (IListIter<Qualifier> iter(qualifiers_); !iter.done(); iter.next()) |
| 54 |
if (!iter.cur()->satisfies(nd, context)) |
| 55 |
return 0; |
| 56 |
return 1; |
| 57 |
} |
| 58 |
|
| 59 |
bool Pattern::Element::trivial() const |
| 60 |
{ |
| 61 |
if (minRepeat_ > 1) |
| 62 |
return 0; |
| 63 |
for (IListIter<Qualifier> iter(qualifiers_); !iter.done(); iter.next()) |
| 64 |
if (!iter.cur()->vacuous()) |
| 65 |
return 0; |
| 66 |
return 1; |
| 67 |
} |
| 68 |
|
| 69 |
void Pattern::Element::contributeSpecificity(int *s) const |
| 70 |
{ |
| 71 |
if (gi_.size()) |
| 72 |
s[giSpecificity] += minRepeat_; |
| 73 |
for (IListIter<Qualifier> iter(qualifiers_); !iter.done(); iter.next()) |
| 74 |
iter.cur()->contributeSpecificity(s); |
| 75 |
if (minRepeat_ != maxRepeat_) |
| 76 |
s[repeatSpecificity] -= 1; |
| 77 |
} |
| 78 |
|
| 79 |
Pattern::Qualifier::~Qualifier() |
| 80 |
{ |
| 81 |
} |
| 82 |
|
| 83 |
bool Pattern::Qualifier::vacuous() const |
| 84 |
{ |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
bool Pattern::Qualifier::matchAttribute(const StringC &name, |
| 89 |
const StringC &value, |
| 90 |
const NodePtr &nd, |
| 91 |
MatchContext &context) |
| 92 |
{ |
| 93 |
NamedNodeListPtr atts; |
| 94 |
if (nd->getAttributes(atts) != accessOK) |
| 95 |
return 0; |
| 96 |
NodePtr att; |
| 97 |
if (atts->namedNode(GroveString(name.data(), name.size()), att) != accessOK) |
| 98 |
return 0; |
| 99 |
bool implied; |
| 100 |
if (att->getImplied(implied) == accessOK && implied) |
| 101 |
return 0; |
| 102 |
GroveString tokens; |
| 103 |
if (att->tokens(tokens) == accessOK) { |
| 104 |
if (tokens.size() != value.size()) |
| 105 |
return 0; |
| 106 |
NodePtr node; |
| 107 |
NamedNodeListPtr normalizer; |
| 108 |
if (att->firstChild(node) != accessOK |
| 109 |
|| node->getEntity(node) != accessOK |
| 110 |
|| node->getGroveRoot(node) != accessOK |
| 111 |
|| node->getEntities(normalizer) != accessOK) |
| 112 |
normalizer = atts; |
| 113 |
StringC tem(value); |
| 114 |
tem.resize(normalizer->normalize(&tem[0], tem.size())); |
| 115 |
if (tokens != GroveString(tem.data(), tem.size())) |
| 116 |
return 0; |
| 117 |
} |
| 118 |
else { |
| 119 |
NodePtr tem; |
| 120 |
StringC s; |
| 121 |
if (att->firstChild(tem) == accessOK) { |
| 122 |
do { |
| 123 |
GroveString chunk; |
| 124 |
if (tem->charChunk(context, chunk) == accessOK) |
| 125 |
s.append(chunk.data(), chunk.size()); |
| 126 |
} while (tem.assignNextChunkSibling() == accessOK); |
| 127 |
} |
| 128 |
if (s != value) |
| 129 |
return 0; |
| 130 |
} |
| 131 |
return 1; |
| 132 |
} |
| 133 |
|
| 134 |
Pattern::ChildrenQualifier::ChildrenQualifier(IList<Element> &children) |
| 135 |
{ |
| 136 |
children.swap(children_); |
| 137 |
} |
| 138 |
|
| 139 |
bool Pattern::ChildrenQualifier::satisfies(const NodePtr &nd, |
| 140 |
MatchContext &context) const |
| 141 |
{ |
| 142 |
ASSERT(!children_.empty()); |
| 143 |
NodePtr child; |
| 144 |
if (nd->firstChild(child) != accessOK) |
| 145 |
return 0; |
| 146 |
Vector<const Element *> toMatch; |
| 147 |
for (IListIter<Element> iter(children_); !iter.done(); iter.next()) |
| 148 |
toMatch.push_back(iter.cur()); |
| 149 |
do { |
| 150 |
size_t j = 0; |
| 151 |
for (size_t i = 0; i < toMatch.size(); i++) { |
| 152 |
if (!toMatch[i]->matches(child, context)) { |
| 153 |
if (j != i) |
| 154 |
toMatch[j] = toMatch[i]; |
| 155 |
j++; |
| 156 |
} |
| 157 |
} |
| 158 |
if (j == 0) |
| 159 |
return 1; |
| 160 |
toMatch.resize(j); |
| 161 |
} while (child.assignNextChunkSibling() == accessOK); |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
void Pattern::ChildrenQualifier::contributeSpecificity(int *s) const |
| 166 |
{ |
| 167 |
for (IListIter<Element> iter(children_); !iter.done(); iter.next()) |
| 168 |
iter.cur()->contributeSpecificity(s); |
| 169 |
} |
| 170 |
|
| 171 |
Pattern::IdQualifier::IdQualifier(const StringC &id) |
| 172 |
: id_(id) |
| 173 |
{ |
| 174 |
} |
| 175 |
|
| 176 |
bool Pattern::IdQualifier::satisfies(const NodePtr &nd, MatchContext &context) const |
| 177 |
{ |
| 178 |
GroveString nodeId; |
| 179 |
if (nd->getId(nodeId) == accessOK) { |
| 180 |
size_t len = id_.size(); |
| 181 |
if (nodeId.size() == len) { |
| 182 |
StringC tem(id_); |
| 183 |
Interpreter::normalizeGeneralName(nd, tem); |
| 184 |
GroveString patternId(tem.data(), tem.size()); |
| 185 |
if (patternId == nodeId) |
| 186 |
return 1; |
| 187 |
} |
| 188 |
} |
| 189 |
const Vector<StringC> &idAtts = context.idAttributeNames(); |
| 190 |
for (size_t i = 0; i < idAtts.size(); i++) |
| 191 |
if (matchAttribute(idAtts[i], id_, nd, context)) |
| 192 |
return 1; |
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
void Pattern::IdQualifier::contributeSpecificity(int *s) const |
| 197 |
{ |
| 198 |
s[idSpecificity] += 1; |
| 199 |
} |
| 200 |
|
| 201 |
Pattern::ClassQualifier::ClassQualifier(const StringC &cls) |
| 202 |
: class_(cls) |
| 203 |
{ |
| 204 |
} |
| 205 |
|
| 206 |
bool Pattern::ClassQualifier::satisfies(const NodePtr &nd, MatchContext &context) const |
| 207 |
{ |
| 208 |
const Vector<StringC> &classAtts = context.classAttributeNames(); |
| 209 |
for (size_t i = 0; i < classAtts.size(); i++) |
| 210 |
if (matchAttribute(classAtts[i], class_, nd, context)) |
| 211 |
return 1; |
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
void Pattern::ClassQualifier::contributeSpecificity(int *s) const |
| 216 |
{ |
| 217 |
s[classSpecificity] += 1; |
| 218 |
} |
| 219 |
|
| 220 |
Pattern::AttributeHasValueQualifier::AttributeHasValueQualifier(const StringC &name) |
| 221 |
: name_(name) |
| 222 |
{ |
| 223 |
} |
| 224 |
|
| 225 |
bool Pattern::AttributeHasValueQualifier::satisfies(const NodePtr &nd, MatchContext &context) const |
| 226 |
{ |
| 227 |
NamedNodeListPtr atts; |
| 228 |
if (nd->getAttributes(atts) != accessOK) |
| 229 |
return 0; |
| 230 |
NodePtr att; |
| 231 |
if (atts->namedNode(GroveString(name_.data(), name_.size()), att) != accessOK) |
| 232 |
return 0; |
| 233 |
bool implied; |
| 234 |
if (att->getImplied(implied) == accessOK && implied) |
| 235 |
return 0; |
| 236 |
return 1; |
| 237 |
} |
| 238 |
|
| 239 |
void Pattern::AttributeHasValueQualifier::contributeSpecificity(int *s) const |
| 240 |
{ |
| 241 |
s[attributeSpecificity] += 1; |
| 242 |
} |
| 243 |
|
| 244 |
Pattern::AttributeMissingValueQualifier::AttributeMissingValueQualifier(const StringC &name) |
| 245 |
: name_(name) |
| 246 |
{ |
| 247 |
} |
| 248 |
|
| 249 |
bool Pattern::AttributeMissingValueQualifier::satisfies(const NodePtr &nd, MatchContext &context) const |
| 250 |
{ |
| 251 |
NamedNodeListPtr atts; |
| 252 |
if (nd->getAttributes(atts) != accessOK) |
| 253 |
return 1; |
| 254 |
NodePtr att; |
| 255 |
if (atts->namedNode(GroveString(name_.data(), name_.size()), att) != accessOK) |
| 256 |
return 1; |
| 257 |
bool implied; |
| 258 |
if (att->getImplied(implied) == accessOK && implied) |
| 259 |
return 1; |
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
void Pattern::AttributeMissingValueQualifier::contributeSpecificity(int *s) const |
| 264 |
{ |
| 265 |
s[attributeSpecificity] += 1; |
| 266 |
} |
| 267 |
|
| 268 |
Pattern::AttributeQualifier::AttributeQualifier(const StringC &name, const StringC &value) |
| 269 |
: name_(name), value_(value) |
| 270 |
{ |
| 271 |
} |
| 272 |
|
| 273 |
bool Pattern::AttributeQualifier::satisfies(const NodePtr &nd, MatchContext &context) const |
| 274 |
{ |
| 275 |
return matchAttribute(name_, value_, nd, context); |
| 276 |
} |
| 277 |
|
| 278 |
void Pattern::AttributeQualifier::contributeSpecificity(int *s) const |
| 279 |
{ |
| 280 |
s[attributeSpecificity] += 1; |
| 281 |
} |
| 282 |
|
| 283 |
void Pattern::PositionQualifier::contributeSpecificity(int *s) const |
| 284 |
{ |
| 285 |
s[positionSpecificity] += 1; |
| 286 |
} |
| 287 |
|
| 288 |
bool Pattern::FirstOfTypeQualifier::satisfies(const NodePtr &nd, MatchContext &context) const |
| 289 |
{ |
| 290 |
GroveString ndType; |
| 291 |
nd->getGi(ndType); |
| 292 |
NodePtr tem; |
| 293 |
if (nd->firstSibling(tem) != accessOK) |
| 294 |
return 1; // must be document element |
| 295 |
while (*tem != *nd) { |
| 296 |
GroveString temType; |
| 297 |
if (tem->getGi(temType) == accessOK && temType == ndType) |
| 298 |
return 0; |
| 299 |
tem.assignNextChunkSibling(); |
| 300 |
} |
| 301 |
return 1; |
| 302 |
} |
| 303 |
|
| 304 |
bool Pattern::LastOfTypeQualifier::satisfies(const NodePtr &nd, MatchContext &context) const |
| 305 |
{ |
| 306 |
NodePtr tem; |
| 307 |
if (nd->nextChunkSibling(tem) != accessOK) |
| 308 |
return 1; |
| 309 |
GroveString ndType; |
| 310 |
nd->getGi(ndType); |
| 311 |
do { |
| 312 |
GroveString temType; |
| 313 |
if (tem->getGi(temType) == accessOK && temType == ndType) |
| 314 |
return 0; |
| 315 |
} while (tem.assignNextChunkSibling() == accessOK); |
| 316 |
return 1; |
| 317 |
} |
| 318 |
|
| 319 |
bool Pattern::FirstOfAnyQualifier::satisfies(const NodePtr &nd, MatchContext &) const |
| 320 |
{ |
| 321 |
NodePtr tem; |
| 322 |
if (nd->firstSibling(tem) != accessOK) |
| 323 |
return 1; // must be document element |
| 324 |
while (*tem != *nd) { |
| 325 |
GroveString temType; |
| 326 |
if (tem->getGi(temType) == accessOK) |
| 327 |
return 0; |
| 328 |
tem.assignNextChunkSibling(); |
| 329 |
} |
| 330 |
return 1; |
| 331 |
} |
| 332 |
|
| 333 |
bool Pattern::LastOfAnyQualifier::satisfies(const NodePtr &nd, MatchContext &context) const |
| 334 |
{ |
| 335 |
NodePtr tem; |
| 336 |
if (nd->nextChunkSibling(tem) != accessOK) |
| 337 |
return 1; |
| 338 |
GroveString ndType; |
| 339 |
nd->getGi(ndType); |
| 340 |
do { |
| 341 |
GroveString temType; |
| 342 |
if (tem->getGi(temType) == accessOK) |
| 343 |
return 0; |
| 344 |
} while (tem.assignNextChunkSibling() == accessOK); |
| 345 |
return 1; |
| 346 |
} |
| 347 |
|
| 348 |
void Pattern::OnlyQualifier::contributeSpecificity(int *s) const |
| 349 |
{ |
| 350 |
s[onlySpecificity] += 1; |
| 351 |
} |
| 352 |
|
| 353 |
bool Pattern::OnlyOfTypeQualifier::satisfies(const NodePtr &nd, MatchContext &context) const |
| 354 |
{ |
| 355 |
GroveString ndType; |
| 356 |
nd->getGi(ndType); |
| 357 |
NodePtr tem; |
| 358 |
if (nd->firstSibling(tem) != accessOK) |
| 359 |
return 1; // must be document element |
| 360 |
unsigned count = 0; |
| 361 |
do { |
| 362 |
GroveString temType; |
| 363 |
if (tem->getGi(temType) == accessOK && temType == ndType) { |
| 364 |
if (count++) |
| 365 |
return 0; |
| 366 |
} |
| 367 |
} while (tem.assignNextChunkSibling() == accessOK); |
| 368 |
return 1; |
| 369 |
} |
| 370 |
|
| 371 |
bool Pattern::OnlyOfAnyQualifier::satisfies(const NodePtr &nd, MatchContext &context) const |
| 372 |
{ |
| 373 |
NodePtr tem; |
| 374 |
if (nd->firstSibling(tem) != accessOK) |
| 375 |
return 1; // must be document element |
| 376 |
unsigned count = 0; |
| 377 |
do { |
| 378 |
GroveString temType; |
| 379 |
if (tem->getGi(temType) == accessOK) { |
| 380 |
if (count++) |
| 381 |
return 0; |
| 382 |
} |
| 383 |
} while (tem.assignNextChunkSibling() == accessOK); |
| 384 |
return 1; |
| 385 |
} |
| 386 |
|
| 387 |
bool Pattern::VacuousQualifier::vacuous() const |
| 388 |
{ |
| 389 |
return 1; |
| 390 |
} |
| 391 |
|
| 392 |
Pattern::PriorityQualifier::PriorityQualifier(long n) |
| 393 |
: n_(n) |
| 394 |
{ |
| 395 |
} |
| 396 |
|
| 397 |
void Pattern::PriorityQualifier::contributeSpecificity(int *s) const |
| 398 |
{ |
| 399 |
s[prioritySpecificity] += n_; |
| 400 |
} |
| 401 |
|
| 402 |
bool Pattern::PriorityQualifier::satisfies(const NodePtr &, MatchContext &) const |
| 403 |
{ |
| 404 |
return 1; |
| 405 |
} |
| 406 |
|
| 407 |
Pattern::ImportanceQualifier::ImportanceQualifier(long n) |
| 408 |
: n_(n) |
| 409 |
{ |
| 410 |
} |
| 411 |
|
| 412 |
void Pattern::ImportanceQualifier::contributeSpecificity(int *s) const |
| 413 |
{ |
| 414 |
s[importanceSpecificity] += n_; |
| 415 |
} |
| 416 |
|
| 417 |
bool Pattern::ImportanceQualifier::satisfies(const NodePtr &, MatchContext &) const |
| 418 |
{ |
| 419 |
return 1; |
| 420 |
} |
| 421 |
|
| 422 |
bool Pattern::matchAncestors1(const IListIter<Element> &ancestors, |
| 423 |
const NodePtr &node, |
| 424 |
MatchContext &context) |
| 425 |
{ |
| 426 |
const Element &r = *ancestors.cur(); |
| 427 |
NodePtr tem(node); |
| 428 |
for (Repeat i = 0; i < r.minRepeat(); i++) { |
| 429 |
if (!tem || !r.matches(tem, context)) |
| 430 |
return 0; |
| 431 |
if (tem->getParent(tem) != accessOK) |
| 432 |
tem.clear(); |
| 433 |
} |
| 434 |
Repeat i = r.minRepeat(); |
| 435 |
for (;;) { |
| 436 |
IListIter<Element> up(ancestors); |
| 437 |
up.next(); |
| 438 |
if (matchAncestors(up, tem, context)) |
| 439 |
break; |
| 440 |
if (i == r.maxRepeat() || !tem || !r.matches(tem, context)) |
| 441 |
return 0; |
| 442 |
i++; |
| 443 |
if (tem->getParent(tem) != accessOK) |
| 444 |
tem.clear(); |
| 445 |
} |
| 446 |
return 1; |
| 447 |
} |
| 448 |
|
| 449 |
void Pattern::computeSpecificity(int *s) const |
| 450 |
{ |
| 451 |
for (int i = 0; i < nSpecificity; i++) |
| 452 |
s[i] = 0; |
| 453 |
for (IListIter<Element> iter(ancestors_); !iter.done(); iter.next()) |
| 454 |
iter.cur()->contributeSpecificity(s); |
| 455 |
} |
| 456 |
|
| 457 |
int Pattern::compareSpecificity(const Pattern &pattern1, const Pattern &pattern2) |
| 458 |
{ |
| 459 |
int s1[nSpecificity]; |
| 460 |
int s2[nSpecificity]; |
| 461 |
int i; // declare here to avoid gcc bug |
| 462 |
pattern1.computeSpecificity(s1); |
| 463 |
pattern2.computeSpecificity(s2); |
| 464 |
for (i = 0; i < nSpecificity; i++) { |
| 465 |
if (s1[i] != s2[i]) |
| 466 |
return s1[i] > s2[i] ? -1 : 1; |
| 467 |
} |
| 468 |
return 0; |
| 469 |
} |
| 470 |
|
| 471 |
#ifdef DSSSL_NAMESPACE |
| 472 |
} |
| 473 |
#endif |