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
|
/**
* ===========================================
* 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: DefaultStyleResolver.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.util.Arrays;
import java.util.ArrayList;
import org.jfree.layouting.DocumentContextUtility;
import org.jfree.layouting.LayoutProcess;
import org.jfree.layouting.State;
import org.jfree.layouting.StateException;
import org.jfree.layouting.input.style.CSSDeclarationRule;
import org.jfree.layouting.input.style.CSSPageAreaRule;
import org.jfree.layouting.input.style.CSSPageRule;
import org.jfree.layouting.input.style.CSSStyleRule;
import org.jfree.layouting.input.style.PageAreaType;
import org.jfree.layouting.input.style.PseudoPage;
import org.jfree.layouting.input.style.StyleKey;
import org.jfree.layouting.input.style.StyleRule;
import org.jfree.layouting.input.style.selectors.CSSSelector;
import org.jfree.layouting.input.style.selectors.SelectorWeight;
import org.jfree.layouting.input.style.values.CSSInheritValue;
import org.jfree.layouting.input.style.values.CSSValue;
import org.jfree.layouting.layouter.context.DocumentContext;
import org.jfree.layouting.layouter.context.LayoutContext;
import org.jfree.layouting.layouter.context.LayoutStyle;
import org.jfree.layouting.layouter.model.LayoutElement;
import org.jfree.layouting.layouter.style.CSSStyleRuleComparator;
import org.jfree.layouting.layouter.style.LayoutStyleImpl;
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.pentaho.reporting.libraries.base.util.DebugLog;
import org.pentaho.reporting.libraries.resourceloader.Resource;
import org.pentaho.reporting.libraries.resourceloader.ResourceKey;
import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
/**
* A cascading style resolver. This resolver follows the cascading rules
* as outlined by the Cascading Stylesheet Standard.
*
* @author Thomas Morgner
*/
public class DefaultStyleResolver extends AbstractStyleResolver
{
private static class DefaultStyleResolverState extends AbstractStyleResolverState
{
private boolean strictStyleMode;
private StyleRuleMatcher ruleMatcher;
private DefaultStyleResolverState()
{
}
public boolean isStrictStyleMode()
{
return strictStyleMode;
}
public void setStrictStyleMode(final boolean strictStyleMode)
{
this.strictStyleMode = strictStyleMode;
}
public StyleRuleMatcher getRuleMatcher()
{
return ruleMatcher;
}
public void setRuleMatcher(final StyleRuleMatcher ruleMatcher)
{
this.ruleMatcher = ruleMatcher;
}
protected AbstractStyleResolver create()
{
return new DefaultStyleResolver();
}
protected void fill(final AbstractStyleResolver resolver,
final LayoutProcess layoutProcess)
{
super.fill(resolver, layoutProcess);
final DefaultStyleResolver res = (DefaultStyleResolver) resolver;
res.styleRuleMatcher = ruleMatcher;
res.strictStyleMode = strictStyleMode;
}
}
private boolean strictStyleMode;
private StyleRuleMatcher styleRuleMatcher;
private StyleKey[] inheritedKeys;
public DefaultStyleResolver ()
{
}
public void initialize (final LayoutProcess layoutProcess)
{
super.initialize(layoutProcess);
final DocumentContext documentContext = layoutProcess.getDocumentContext();
this.styleRuleMatcher =
DocumentContextUtility.getStyleRuleMatcher(documentContext);
this.styleRuleMatcher.initialize(layoutProcess);
this.strictStyleMode = Boolean.TRUE.equals
(documentContext.getMetaAttribute(DocumentContext.STRICT_STYLE_MODE));
loadInitialStyle();
}
// This one is expensive too: 6%
protected void resolveOutOfContext (final LayoutElement element)
{
// as this styleresolver is not statefull, we can safely call the resolve
// style method. A statefull resolver would have to find other means.
resolveStyle(element);
}
/**
* Performs tests, whether there is a pseudo-element definition for the given
* element. The element itself can be a pseudo-element as well.
*
* @param element
* @param pseudo
* @return
*/
public boolean isPseudoElementStyleResolvable(final LayoutElement element,
final String pseudo)
{
return styleRuleMatcher.isMatchingPseudoElement(element, pseudo);
}
/**
* Resolves the style. This is guaranteed to be called in the order of the
* document elements traversing the document tree using the
* 'deepest-node-first' strategy.
* (8% just for the first class calls (not counting the calls comming from
* resolveAnonymous (which is another 6%))
* @param element the elemen that should be resolved.
*/
public void resolveStyle(final LayoutElement element)
{
// this is a three stage process
final LayoutContext layoutContext = element.getLayoutContext();
final StyleKey[] keys = getKeys();
// Log.debug ("Resolving style for " +
// layoutContext.getTagName() + ":" +
// layoutContext.getPseudoElement());
// Stage 0: Initialize with the built-in defaults
// Stage 1a: Add the parent styles (but only the one marked as inheritable).
final LayoutElement parent = element.getParent();
final LayoutStyle initialStyle = getInitialStyle();
if (layoutContext.copyFrom(initialStyle) == false)
{
// ok, manual copy ..
DebugLog.log("Failed to use fast-copy");
for (int i = 0; i < keys.length; i++)
{
final StyleKey key = keys[i];
layoutContext.setValue(key, initialStyle.getValue(key));
}
}
final LayoutStyle parentStyle;
if (parent != null)
{
parentStyle = parent.getLayoutContext();
final StyleKey[] inheritedKeys = getInheritedKeys();
for (int i = 0; i < inheritedKeys.length; i++)
{
final StyleKey key = inheritedKeys[i];
layoutContext.setValue(key, parentStyle.getValue(key));
}
}
else
{
parentStyle = initialStyle;
}
// Stage 1b: Find all matching stylesheets for the given element
performSelectionStep(element, layoutContext);
// Stage 1c: Add the contents of the style attribute, if there is one ..
// the libLayout style is always added: This is a computed style and the hook
// for a element neutral user defined tweaking ..
final AttributeMap attributes = layoutContext.getAttributes();
final Object libLayoutStyleValue = attributes.getAttribute
(Namespaces.LIBLAYOUT_NAMESPACE, "style");
// You cannot override element specific styles with that. So an HTML-style
// attribute has move value than a LibLayout-style attribute.
addStyleFromAttribute(element, libLayoutStyleValue);
if (strictStyleMode)
{
performStrictStyleAttr(element);
}
else
{
performCompleteStyleAttr(element);
}
// Stage 2: Compute the 'specified' set of values.
// Find all explicitly inherited styles and add them from the parent.
final CSSInheritValue inheritInstance = CSSInheritValue.getInstance();
for (int i = 0; i < keys.length; i++)
{
final StyleKey key = keys[i];
final Object value = layoutContext.getValue(key);
if (inheritInstance.equals(value))
{
layoutContext.setValue(key, parentStyle.getValue(key));
}
}
// Stage 3: Compute the computed value set.
ResolverFactory.getInstance().performResolve
(getLayoutProcess(), element);
}
private StyleKey[] getInheritedKeys()
{
if (inheritedKeys == null)
{
final StyleKey[] keys = getKeys();
final ArrayList inheritedKeysList = new ArrayList();
for (int i = 0; i < keys.length; i++)
{
final StyleKey key = keys[i];
if (key.isInherited())
{
inheritedKeysList.add(key);
}
}
inheritedKeys = (StyleKey[])
inheritedKeysList.toArray(new StyleKey[inheritedKeysList.size()]);
}
return inheritedKeys;
}
/**
* Check, whether there is a known style attribute for the element's namespace
* and if so, grab its value. This method uses strict conformance to the XML
* rules and thus it does not evaluate foreign styles.
* <p/>
*
* @param node
* @param style
*/
private void performStrictStyleAttr (final LayoutElement node)
{
final LayoutContext layoutContext = node.getLayoutContext();
final String namespace = layoutContext.getNamespace();
if (namespace == null)
{
return;
}
final NamespaceCollection namespaces = getNamespaces();
final NamespaceDefinition ndef = namespaces.getDefinition(namespace);
if (ndef == null)
{
return;
}
final AttributeMap attributes = layoutContext.getAttributes();
final String[] styleAttrs = ndef.getStyleAttribute
(layoutContext.getTagName());
for (int i = 0; i < styleAttrs.length; i++)
{
final String attr = styleAttrs[i];
final Object styleValue = attributes.getAttribute(namespace, attr);
addStyleFromAttribute(node, styleValue);
}
}
/**
* Check, whether there are known style attributes and if so, import them.
* This method uses a relaxed syntax and imports all known style attributes
* ignoring the element's defined namespace. This allows to add styles to
* elements which would not support styles otherwise, but may have ..
* chaotic .. side effects.
* <p/>
*
* @param node
* @param style
*/
private void performCompleteStyleAttr (final LayoutElement node)
{
final NamespaceCollection namespaces = getNamespaces();
final String[] namespaceNames = namespaces.getNamespaces();
final LayoutContext layoutContext = node.getLayoutContext();
final AttributeMap attributes = layoutContext.getAttributes();
for (int i = 0; i < namespaceNames.length; i++)
{
final String namespace = namespaceNames[i];
final NamespaceDefinition ndef = namespaces.getDefinition(namespace);
if (ndef == null)
{
continue;
}
final String[] styleAttrs = ndef.getStyleAttribute(layoutContext.getTagName());
for (int x = 0; x < styleAttrs.length; x++)
{
final String attr = styleAttrs[x];
final Object styleValue = attributes.getAttribute(namespace, attr);
addStyleFromAttribute(node, styleValue);
}
}
}
private void addStyleFromAttribute(final LayoutElement node,
final Object styleValue)
{
if (styleValue == null)
{
return;
}
if (styleValue instanceof String)
{
final String styleText = (String) styleValue;
try
{
final LayoutProcess layoutProcess = getLayoutProcess();
final byte[] bytes = styleText.getBytes("UTF-8");
final ResourceKey baseKey =
DocumentContextUtility.getBaseResource
(layoutProcess.getDocumentContext());
final ResourceManager manager = layoutProcess.getResourceManager();
final ResourceKey key = manager.createKey(bytes);
final Resource resource = manager.create(key, baseKey, StyleRule.class);
final CSSDeclarationRule rule =
(CSSDeclarationRule) resource.getResource();
if (rule != null)
{
copyStyleInformation(node.getLayoutContext(), rule, node);
}
}
catch (Exception e)
{
DebugLog.log("Unable to handle style attribute value.", e);
}
}
else if (styleValue instanceof CSSDeclarationRule)
{
final CSSDeclarationRule rule = (CSSDeclarationRule) styleValue;
copyStyleInformation(node.getLayoutContext(), rule, node);
}
}
/**
* Todo: Make sure that the 'activeStyles' are sorted and then apply them with the
* lowest style first. All Matching styles have to be added.
*
* @param node
* @param style
*/
private void performSelectionStep (final LayoutElement element,
final LayoutStyle style)
{
final CSSStyleRule[] activeStyleRules =
styleRuleMatcher.getMatchingRules(element);
// sort ...
Arrays.sort(activeStyleRules, new CSSStyleRuleComparator());
SelectorWeight oldSelectorWeight = null;
for (int i = 0; i < activeStyleRules.length; i++)
{
final CSSStyleRule activeStyleRule = activeStyleRules[i];
final CSSSelector selector = activeStyleRule.getSelector();
final SelectorWeight activeWeight = selector.getWeight();
if (oldSelectorWeight != null)
{
if (oldSelectorWeight.compareTo(activeWeight) > 0)
{
oldSelectorWeight = activeWeight;
continue;
}
}
oldSelectorWeight = activeWeight;
copyStyleInformation(style, activeStyleRule, element);
}
}
public StyleResolver deriveInstance ()
{
return this;
}
public State saveState() throws StateException
{
final DefaultStyleResolverState state = new DefaultStyleResolverState();
fillState(state);
state.setRuleMatcher(styleRuleMatcher);
state.setStrictStyleMode(strictStyleMode);
return state;
}
public LayoutStyle resolvePageStyle(final CSSValue pageName,
final PseudoPage[] pseudoPages,
final PageAreaType pageArea)
{
final LayoutStyleImpl style = new LayoutStyleImpl();
final CSSPageRule[] pageRule =
styleRuleMatcher.getPageRule(pageName, pseudoPages);
for (int i = 0; i < pageRule.length; i++)
{
final CSSPageRule cssPageRule = pageRule[i];
copyStyleInformation(style, cssPageRule, null);
final int rc = cssPageRule.getRuleCount();
for (int r = 0; r < rc; r++)
{
final CSSPageAreaRule rule = cssPageRule.getRule(r);
if (rule.getPageArea().equals(pageArea))
{
copyStyleInformation(style, rule, null);
}
}
}
return style;
}
}
|