1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
|
/**
* ===========================================
* LibLayout : a free Java layouting library
* ===========================================
*
* Project Info: http://reporting.pentaho.org/liblayout/
*
* (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------
* $Id: SimpleStyleRuleMatcher.java 6489 2008-11-28 14:53:40Z tmorgner $
* ------------
* (C) Copyright 2006-2007, by Pentaho Corporation.
*/
package org.jfree.layouting.layouter.style.resolver;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Locale;
import java.util.StringTokenizer;
import org.jfree.layouting.DocumentContextUtility;
import org.jfree.layouting.LayoutProcess;
import org.jfree.layouting.input.style.CSSCounterRule;
import org.jfree.layouting.input.style.CSSPageRule;
import org.jfree.layouting.input.style.CSSStyleRule;
import org.jfree.layouting.input.style.PseudoPage;
import org.jfree.layouting.input.style.StyleRule;
import org.jfree.layouting.input.style.StyleSheet;
import org.jfree.layouting.input.style.selectors.CSSSelector;
import org.jfree.layouting.input.style.values.CSSValue;
import org.jfree.layouting.layouter.context.DocumentContext;
import org.jfree.layouting.layouter.context.DocumentMetaNode;
import org.jfree.layouting.layouter.context.LayoutContext;
import org.jfree.layouting.layouter.model.LayoutElement;
import org.jfree.layouting.namespace.NamespaceCollection;
import org.jfree.layouting.namespace.NamespaceDefinition;
import org.jfree.layouting.namespace.Namespaces;
import org.jfree.layouting.util.AttributeMap;
import org.w3c.css.sac.AttributeCondition;
import org.w3c.css.sac.CombinatorCondition;
import org.w3c.css.sac.Condition;
import org.w3c.css.sac.ConditionalSelector;
import org.w3c.css.sac.DescendantSelector;
import org.w3c.css.sac.ElementSelector;
import org.w3c.css.sac.NegativeCondition;
import org.w3c.css.sac.NegativeSelector;
import org.w3c.css.sac.Selector;
import org.w3c.css.sac.SiblingSelector;
import org.w3c.css.sac.SimpleSelector;
import org.pentaho.reporting.libraries.resourceloader.ResourceKey;
import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
import org.pentaho.reporting.libraries.resourceloader.ResourceKeyCreationException;
import org.pentaho.reporting.libraries.resourceloader.ResourceException;
import org.pentaho.reporting.libraries.resourceloader.Resource;
import org.pentaho.reporting.libraries.base.util.DebugLog;
import org.pentaho.reporting.libraries.base.util.ObjectUtilities;
/**
* A stateless implementation of the style rule matching. This implementation is
* stateless within the current layout process.
*
* @author Thomas Morgner
*/
public class SimpleStyleRuleMatcher implements StyleRuleMatcher
{
private LayoutProcess layoutProcess;
private ResourceManager resourceManager;
private CSSStyleRule[] activeStyleRules;
private CSSStyleRule[] activePseudoStyleRules;
private CSSPageRule[] pageRules;
private CSSCounterRule[] counterRules;
private NamespaceCollection namespaces;
public SimpleStyleRuleMatcher()
{
}
public void initialize(final LayoutProcess layoutProcess)
{
if (layoutProcess == null)
{
throw new NullPointerException();
}
this.layoutProcess = layoutProcess;
this.resourceManager = layoutProcess.getResourceManager();
final ArrayList pageRules = new ArrayList();
final ArrayList counterRules = new ArrayList();
final ArrayList styleRules = new ArrayList();
final DocumentContext dc = this.layoutProcess.getDocumentContext();
namespaces = dc.getNamespaces();
final String[] nsUri = namespaces.getNamespaces();
for (int i = 0; i < nsUri.length; i++)
{
final String uri = nsUri[i];
final NamespaceDefinition nsDef = namespaces.getDefinition(uri);
final ResourceKey rawKey = nsDef.getDefaultStyleSheetLocation();
if (rawKey == null)
{
// there is no default stylesheet for that namespace.
continue;
}
final ResourceKey baseKey =
DocumentContextUtility.getBaseResource
(layoutProcess.getDocumentContext());
final StyleSheet styleSheet = parseStyleSheet(rawKey, baseKey);
if (styleSheet == null)
{
continue;
}
addStyleRules(styleSheet, styleRules);
addPageRules(styleSheet, pageRules);
addCounterRules(styleSheet, counterRules);
}
final int metaNodeCount = dc.getMetaNodeCount();
for (int i = 0; i < metaNodeCount; i++)
{
final DocumentMetaNode dmn = dc.getMetaNode(i);
final String type = (String) dmn.getMetaAttribute("type");
if ("link".equals(type))
{
handleLinkNode(dmn, styleRules, pageRules, counterRules);
}
else if ("style".equals(type))
{
handleStyleNode(dmn, styleRules, pageRules, counterRules);
}
}
activeStyleRules = (CSSStyleRule[])
styleRules.toArray(new CSSStyleRule[styleRules.size()]);
this.pageRules = (CSSPageRule[])
pageRules.toArray(new CSSPageRule[pageRules.size()]);
this.counterRules = (CSSCounterRule[])
counterRules.toArray(new CSSCounterRule[counterRules.size()]);
styleRules.clear();
for (int i = 0; i < activeStyleRules.length; i++)
{
final CSSStyleRule activeStyleRule = activeStyleRules[i];
if (isPseudoElementRule(activeStyleRule) == false)
{
continue;
}
styleRules.add(activeStyleRule);
}
activePseudoStyleRules = (CSSStyleRule[])
styleRules.toArray(new CSSStyleRule[styleRules.size()]);
}
private void handleLinkNode(final DocumentMetaNode node,
final ArrayList styleRules,
final ArrayList pageRules,
final ArrayList counterRules)
{
// do some external parsing
// (Same as the <link> element of HTML)
try
{
final Object href = node.getMetaAttribute("href");
final ResourceKey baseKey =
DocumentContextUtility.getBaseResource
(layoutProcess.getDocumentContext());
final ResourceKey derivedKey;
if (baseKey == null)
{
derivedKey = resourceManager.createKey(href);
}
else
{
derivedKey = resourceManager.deriveKey(baseKey, String.valueOf(href));
}
final StyleSheet styleSheet = parseStyleSheet(derivedKey, null);
if (styleSheet == null)
{
return;
}
addStyleRules(styleSheet, styleRules);
addPageRules(styleSheet, pageRules);
addCounterRules(styleSheet, counterRules);
}
catch (ResourceKeyCreationException e)
{
e.printStackTrace();
}
}
private void handleStyleNode(final DocumentMetaNode node,
final ArrayList styleRules,
final ArrayList pageRules,
final ArrayList counterRules)
{
// do some inline parsing
// (Same as the <style> element of HTML)
// we also accept preparsed content ...
final Object style = node.getMetaAttribute("#pcdata");
if (style == null)
{
final Object content = node.getMetaAttribute("#content");
if (content instanceof StyleSheet)
{
final StyleSheet styleSheet = (StyleSheet) content;
addStyleRules(styleSheet, styleRules);
addPageRules(styleSheet, pageRules);
addCounterRules(styleSheet, counterRules);
return;
}
}
final String styleText = String.valueOf(style);
try
{
final byte[] bytes = styleText.getBytes("UTF-8");
final ResourceKey rawKey = resourceManager.createKey(bytes);
final ResourceKey baseKey =
DocumentContextUtility.getBaseResource
(layoutProcess.getDocumentContext());
final StyleSheet styleSheet = parseStyleSheet(rawKey, baseKey);
if (styleSheet == null)
{
return;
}
addStyleRules(styleSheet, styleRules);
addPageRules(styleSheet, pageRules);
addCounterRules(styleSheet, counterRules);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (ResourceKeyCreationException e)
{
e.printStackTrace();
}
}
private void addCounterRules(final StyleSheet styleSheet,
final ArrayList rules)
{
final int sc = styleSheet.getStyleSheetCount();
for (int i = 0; i < sc; i++)
{
addCounterRules(styleSheet.getStyleSheet(i), rules);
}
final int rc = styleSheet.getRuleCount();
for (int i = 0; i < rc; i++)
{
final StyleRule rule = styleSheet.getRule(i);
if (rule instanceof CSSCounterRule)
{
final CSSCounterRule drule = (CSSCounterRule) rule;
rules.add(drule);
}
}
}
private void addPageRules(final StyleSheet styleSheet,
final ArrayList rules)
{
final int sc = styleSheet.getStyleSheetCount();
for (int i = 0; i < sc; i++)
{
addPageRules(styleSheet.getStyleSheet(i), rules);
}
final int rc = styleSheet.getRuleCount();
for (int i = 0; i < rc; i++)
{
final StyleRule rule = styleSheet.getRule(i);
if (rule instanceof CSSPageRule)
{
final CSSPageRule drule = (CSSPageRule) rule;
rules.add(drule);
}
}
}
private void addStyleRules(final StyleSheet styleSheet,
final ArrayList activeStyleRules)
{
final int sc = styleSheet.getStyleSheetCount();
for (int i = 0; i < sc; i++)
{
addStyleRules(styleSheet.getStyleSheet(i), activeStyleRules);
}
final int rc = styleSheet.getRuleCount();
for (int i = 0; i < rc; i++)
{
final StyleRule rule = styleSheet.getRule(i);
if (rule instanceof CSSStyleRule)
{
final CSSStyleRule drule = (CSSStyleRule) rule;
activeStyleRules.add(drule);
}
}
}
private StyleSheet parseStyleSheet(final ResourceKey key,
final ResourceKey context)
{
try
{
final Resource resource = resourceManager.create
(key, context, StyleSheet.class);
return (StyleSheet) resource.getResource();
}
catch (ResourceException e)
{
DebugLog.log("Unable to parse StyleSheet: " + e.getLocalizedMessage());
}
return null;
}
private boolean isPseudoElementRule(final CSSStyleRule rule)
{
final CSSSelector selector = rule.getSelector();
if (selector == null)
{
return false;
}
if (selector.getSelectorType() != Selector.SAC_CONDITIONAL_SELECTOR)
{
return false;
}
final ConditionalSelector cs = (ConditionalSelector) selector;
final Condition condition = cs.getCondition();
if (condition.getConditionType() != Condition.SAC_PSEUDO_CLASS_CONDITION)
{
return false;
}
return true;
}
public boolean isMatchingPseudoElement(final LayoutElement element, final String pseudo)
{
for (int i = 0; i < activePseudoStyleRules.length; i++)
{
final CSSStyleRule activeStyleRule = activePseudoStyleRules[i];
final CSSSelector selector = activeStyleRule.getSelector();
final ConditionalSelector cs = (ConditionalSelector) selector;
final Condition condition = cs.getCondition();
final AttributeCondition ac = (AttributeCondition) condition;
if (ObjectUtilities.equal(ac.getValue(), pseudo) == false)
{
continue;
}
final SimpleSelector simpleSelector = cs.getSimpleSelector();
if (isMatch(element, simpleSelector))
{
return true;
}
}
return false;
}
/**
* Creates an independent copy of this style rule matcher.
*
* @return this instance, as this implementation is stateless
*/
public StyleRuleMatcher deriveInstance()
{
return this;
}
public CSSStyleRule[] getMatchingRules(final LayoutElement element)
{
final ArrayList retvals = new ArrayList();
for (int i = 0; i < activeStyleRules.length; i++)
{
final CSSStyleRule activeStyleRule = activeStyleRules[i];
final CSSSelector selector = activeStyleRule.getSelector();
if (selector == null)
{
continue;
}
if (isMatch(element, selector))
{
retvals.add(activeStyleRule);
}
}
// Log.debug ("Got " + retvals.size() + " matching rules for " +
// layoutContext.getTagName() + ":" +
// layoutContext.getPseudoElement());
return (CSSStyleRule[]) retvals.toArray
(new CSSStyleRule[retvals.size()]);
}
private boolean isMatch(final LayoutElement node, final Selector selector)
{
final short selectorType = selector.getSelectorType();
switch (selectorType)
{
case Selector.SAC_ANY_NODE_SELECTOR:
return true;
case Selector.SAC_ROOT_NODE_SELECTOR:
return node.getParent() == null;
case Selector.SAC_NEGATIVE_SELECTOR:
{
final NegativeSelector negativeSelector = (NegativeSelector) selector;
return isMatch(node, negativeSelector) == false;
}
case Selector.SAC_DIRECT_ADJACENT_SELECTOR:
{
final SiblingSelector silbSelect = (SiblingSelector) selector;
return isSilblingMatch(node, silbSelect);
}
case Selector.SAC_PSEUDO_ELEMENT_SELECTOR:
{
final LayoutContext layoutContext = node.getLayoutContext();
return layoutContext.isPseudoElement();
}
case Selector.SAC_ELEMENT_NODE_SELECTOR:
{
final ElementSelector es = (ElementSelector) selector;
final LayoutContext layoutContext = node.getLayoutContext();
final String localName = es.getLocalName();
if (localName != null)
{
if (localName.equals(layoutContext.getTagName()) == false)
{
return false;
}
}
final String namespaceURI = es.getNamespaceURI();
if (namespaceURI != null)
{
if (namespaceURI.equals(layoutContext.getNamespace()) == false)
{
return false;
}
}
return true;
}
case Selector.SAC_CHILD_SELECTOR:
{
final DescendantSelector ds = (DescendantSelector) selector;
if (isMatch(node, ds.getSimpleSelector()) == false)
{
return false;
}
final LayoutElement parent = node.getParent();
return (isMatch(parent, ds.getAncestorSelector()));
}
case Selector.SAC_DESCENDANT_SELECTOR:
{
final DescendantSelector ds = (DescendantSelector) selector;
if (isMatch(node, ds.getSimpleSelector()) == false)
{
return false;
}
return (isDescendantMatch(node, ds.getAncestorSelector()));
}
case Selector.SAC_CONDITIONAL_SELECTOR:
{
final ConditionalSelector cs = (ConditionalSelector) selector;
if (evaluateCondition(node, cs.getCondition()) == false)
{
return false;
}
if (isMatch(node, cs.getSimpleSelector()) == false)
{
return false;
}
return true;
}
default:
return false;
}
}
private boolean evaluateCondition(final LayoutElement node,
final Condition condition)
{
switch (condition.getConditionType())
{
case Condition.SAC_AND_CONDITION:
{
final CombinatorCondition cc = (CombinatorCondition) condition;
return (evaluateCondition(node, cc.getFirstCondition()) &&
evaluateCondition(node, cc.getSecondCondition()));
}
case Condition.SAC_OR_CONDITION:
{
final CombinatorCondition cc = (CombinatorCondition) condition;
return (evaluateCondition(node, cc.getFirstCondition()) ||
evaluateCondition(node, cc.getSecondCondition()));
}
case Condition.SAC_ATTRIBUTE_CONDITION:
{
final AttributeCondition ac = (AttributeCondition) condition;
final LayoutContext layoutContext = node.getLayoutContext();
String namespaceURI = ac.getNamespaceURI();
if (namespaceURI == null)
{
namespaceURI = layoutContext.getNamespace();
}
final AttributeMap attributes = layoutContext.getAttributes();
final Object attr = attributes.getAttribute
(namespaceURI, ac.getLocalName());
if (ac.getValue() == null)
{
// dont care what's inside, as long as there is a value ..
return attr != null;
}
else
{
return ObjectUtilities.equal(attr, ac.getValue());
}
}
case Condition.SAC_CLASS_CONDITION:
{
final AttributeCondition ac = (AttributeCondition) condition;
final LayoutContext layoutContext = node.getLayoutContext();
final String namespace = layoutContext.getNamespace();
if (namespace == null)
{
return false;
}
final NamespaceDefinition ndef = namespaces.getDefinition(namespace);
if (ndef == null)
{
return false;
}
final String[] classAttribute = ndef.getClassAttribute(
layoutContext.getTagName());
for (int i = 0; i < classAttribute.length; i++)
{
final String attr = classAttribute[i];
final String htmlAttr = (String)
layoutContext.getAttributes().getAttribute(
namespace, attr);
if (isOneOfAttributes(htmlAttr, ac.getValue()))
{
return true;
}
}
return false;
}
case Condition.SAC_ID_CONDITION:
{
final AttributeCondition ac = (AttributeCondition) condition;
final LayoutContext layoutContext = node.getLayoutContext();
final AttributeMap attributes = layoutContext.getAttributes();
final Object id = attributes.getAttribute(Namespaces.XML_NAMESPACE,
"id");
return ObjectUtilities.equal(ac.getValue(), id);
}
case Condition.SAC_LANG_CONDITION:
{
final AttributeCondition ac = (AttributeCondition) condition;
final LayoutContext layoutContext = node.getLayoutContext();
final Locale locale = layoutContext.getLanguage();
final String lang = locale.getLanguage();
return isBeginHyphenAttribute(lang, ac.getValue());
}
case Condition.SAC_NEGATIVE_CONDITION:
{
final NegativeCondition nc = (NegativeCondition) condition;
return evaluateCondition(node, nc.getCondition()) == false;
}
case Condition.SAC_ONE_OF_ATTRIBUTE_CONDITION:
{
final AttributeCondition ac = (AttributeCondition) condition;
final LayoutContext layoutContext = node.getLayoutContext();
final String attr = (String)
layoutContext.getAttributes().getAttribute
(ac.getNamespaceURI(), ac.getLocalName());
return isOneOfAttributes(attr, ac.getValue());
}
case Condition.SAC_PSEUDO_CLASS_CONDITION:
{
final AttributeCondition ac = (AttributeCondition) condition;
final LayoutContext layoutContext = node.getLayoutContext();
final String pseudoClass = layoutContext.getPseudoElement();
if (pseudoClass == null)
{
return false;
}
if (pseudoClass.equals(ac.getValue()))
{
return true;
}
return false;
}
case Condition.SAC_ONLY_CHILD_CONDITION:
case Condition.SAC_ONLY_TYPE_CONDITION:
case Condition.SAC_POSITIONAL_CONDITION:
case Condition.SAC_CONTENT_CONDITION:
default:
{
// todo
return false;
}
}
}
private boolean isOneOfAttributes(final String attrValue, final String value)
{
if (attrValue == null)
{
return false;
}
if (attrValue.equals(value))
{
return true;
}
final StringTokenizer strTok = new StringTokenizer(attrValue);
while (strTok.hasMoreTokens())
{
final String token = strTok.nextToken();
if (token.equals(value))
{
return true;
}
}
return false;
}
private boolean isBeginHyphenAttribute(final String attrValue, final String value)
{
if (attrValue == null)
{
return false;
}
if (value == null)
{
return false;
}
return (attrValue.startsWith(value));
}
private boolean isDescendantMatch(final LayoutElement node,
final Selector selector)
{
LayoutElement parent = node.getParent();
while (parent != null)
{
if (isMatch(parent, selector))
{
return true;
}
parent = parent.getParent();
}
return false;
}
private boolean isSilblingMatch(final LayoutElement node,
final SiblingSelector select)
{
LayoutElement pred = node.getPrevious();
while (pred != null)
{
if (isMatch(pred, select))
{
return true;
}
pred = pred.getPrevious();
}
return false;
}
public CSSPageRule[] getPageRule(final CSSValue pageName, final PseudoPage[] pseudoPages)
{
final CSSPageRule[] pageRules = this.pageRules;
final ArrayList rules = new ArrayList();
for (int i = 0; i < pageRules.length; i++)
{
final CSSPageRule rule = pageRules[i];
final String rulePageName = rule.getName();
// Check the page name.
if (rulePageName != null)
{
if (rulePageName.equals(pageName) == false)
{
continue;
}
}
// And the pseudo page ..
final String rulePseudoPage = rule.getPseudoPage();
if (rulePseudoPage != null)
{
for (int j = 0; j < pseudoPages.length; j++)
{
final PseudoPage pseudoPage = pseudoPages[j];
if (pseudoPage.toString().equalsIgnoreCase(rulePseudoPage))
{
rules.add(rule);
}
}
continue;
}
rules.add(rule);
}
return (CSSPageRule[]) rules.toArray(new CSSPageRule[rules.size()]);
}
}
|