001    /**
002     * ===========================================
003     * LibLayout : a free Java layouting library
004     * ===========================================
005     *
006     * Project Info:  http://reporting.pentaho.org/liblayout/
007     *
008     * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * $Id: DocumentContextUtility.java 3524 2007-10-16 11:26:31Z tmorgner $
027     * ------------
028     * (C) Copyright 2006-2007, by Pentaho Corporation.
029     */
030    package org.jfree.layouting;
031    
032    import java.util.Date;
033    
034    import org.jfree.layouting.layouter.context.DocumentContext;
035    import org.jfree.layouting.layouter.context.LayoutStyle;
036    import org.jfree.layouting.layouter.i18n.DefaultLocalizationContext;
037    import org.jfree.layouting.layouter.i18n.LocalizationContext;
038    import org.jfree.layouting.layouter.style.resolver.SimpleStyleRuleMatcher;
039    import org.jfree.layouting.layouter.style.resolver.StyleRuleMatcher;
040    import org.jfree.resourceloader.ResourceKey;
041    import org.jfree.resourceloader.ResourceManager;
042    
043    /**
044     * Creation-Date: 13.04.2006, 12:11:20
045     *
046     * @author Thomas Morgner
047     */
048    public class DocumentContextUtility
049    {
050      private DocumentContextUtility()
051      {
052      }
053    
054    
055      public static ResourceKey getBaseResource(final DocumentContext context)
056      {
057        final Object o = context.getMetaAttribute
058                (DocumentContext.BASE_RESOURCE_ATTR);
059        if (o instanceof ResourceKey == false)
060        {
061          return null;
062        }
063        return (ResourceKey) o;
064      }
065    
066      public static LocalizationContext getLocalizationContext(final DocumentContext context)
067      {
068        final Object o = context.getMetaAttribute
069                (DocumentContext.LOCALIZATION_ATTR);
070        if (o instanceof LocalizationContext == false)
071        {
072          final DefaultLocalizationContext value = new DefaultLocalizationContext();
073          context.setMetaAttribute(DocumentContext.LOCALIZATION_ATTR, value);
074          return value;
075        }
076        return (LocalizationContext) o;
077      }
078    
079      public static ResourceManager getResourceManager(final DocumentContext context)
080      {
081        final Object o = context.getMetaAttribute(DocumentContext.RESOURCE_MANAGER_ATTR);
082        if (o instanceof ResourceManager == false)
083        {
084          final ResourceManager value = new ResourceManager();
085          value.registerDefaults();
086          context.setMetaAttribute(DocumentContext.RESOURCE_MANAGER_ATTR, value);
087          return value;
088        }
089        return (ResourceManager) o;
090      }
091    
092      public static Date getDate(final DocumentContext context)
093      {
094        final Object o = context.getMetaAttribute(DocumentContext.DATE_ATTR);
095        if (o instanceof Date == false)
096        {
097          final Date value = new Date();
098          context.setMetaAttribute(DocumentContext.DATE_ATTR, value);
099          return value;
100        }
101        return (Date) o;
102      }
103    
104      public static StyleRuleMatcher getStyleRuleMatcher(final DocumentContext context)
105      {
106        final Object o = context.getMetaAttribute(DocumentContext.STYLE_MATCHER_ATTR);
107        if (o instanceof StyleRuleMatcher == false)
108        {
109          final StyleRuleMatcher value = new SimpleStyleRuleMatcher();
110          context.setMetaAttribute(DocumentContext.STYLE_MATCHER_ATTR, value);
111          return value;
112        }
113        return (StyleRuleMatcher) o;
114      }
115    
116      public static LayoutStyle getInitialStyle(final DocumentContext context)
117      {
118        final Object o = context.getMetaAttribute(DocumentContext.INITIAL_STYLE);
119        if (o instanceof LayoutStyle == false)
120        {
121          throw new IllegalStateException();
122        }
123        return (LayoutStyle) o;
124      }
125    
126    }