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: AbstractLayoutProcess.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 org.jfree.layouting.input.style.PseudoPage;
033    import org.jfree.layouting.input.style.values.CSSValue;
034    import org.jfree.layouting.layouter.context.DefaultDocumentContext;
035    import org.jfree.layouting.layouter.context.DocumentContext;
036    import org.jfree.layouting.layouter.feed.InputFeed;
037    import org.jfree.layouting.layouter.style.resolver.DefaultStyleResolver;
038    import org.jfree.layouting.layouter.style.resolver.StyleResolver;
039    import org.jfree.layouting.normalizer.content.NormalizationException;
040    import org.jfree.layouting.normalizer.content.Normalizer;
041    import org.jfree.layouting.output.OutputProcessor;
042    import org.jfree.layouting.output.OutputProcessorMetaData;
043    import org.jfree.resourceloader.ResourceManager;
044    import org.jfree.util.Log;
045    
046    /**
047     * Creation-Date: 05.12.2005, 19:17:21
048     *
049     * @author Thomas Morgner
050     */
051    public abstract class AbstractLayoutProcess implements LayoutProcess
052    {
053      protected abstract static class AbstractLayoutProcessState
054          implements LayoutProcessState
055      {
056        private State inputFeedState;
057        private DocumentContext documentContext;
058        private State styleResolver;
059    
060        protected AbstractLayoutProcessState(final AbstractLayoutProcess lp)
061            throws StateException
062        {
063          if (lp.styleResolver != null)
064          {
065            this.styleResolver = lp.styleResolver.saveState();
066          }
067          this.documentContext = lp.documentContext;
068          if (lp.inputFeed != null)
069          {
070            this.inputFeedState = lp.inputFeed.saveState();
071          }
072        }
073    
074        protected AbstractLayoutProcess restore(final OutputProcessor outputProcessor,
075                                                final AbstractLayoutProcess layoutProcess)
076            throws StateException
077        {
078    //      Log.error ("START ************************************************* ");
079          layoutProcess.documentContext = documentContext;
080          if (styleResolver != null)
081          {
082            layoutProcess.styleResolver =
083                (StyleResolver) styleResolver.restore(layoutProcess);
084          }
085          if (inputFeedState != null)
086          {
087            layoutProcess.inputFeed = (InputFeed) inputFeedState.restore(
088                layoutProcess);
089          }
090    //      Log.error ("DONE  ************************************************* ");
091          return layoutProcess;
092        }
093      }
094    
095      private InputFeed inputFeed;
096      private DocumentContext documentContext;
097      private OutputProcessor outputProcessor;
098      private StyleResolver styleResolver;
099    
100      protected AbstractLayoutProcess(final OutputProcessor outputProcessor)
101      {
102        if (outputProcessor == null)
103        {
104          throw new NullPointerException();
105        }
106    
107        this.outputProcessor = outputProcessor;
108        this.documentContext = new DefaultDocumentContext();
109        this.styleResolver = new DefaultStyleResolver();
110    
111      }
112    
113      public OutputProcessorMetaData getOutputMetaData()
114      {
115        return outputProcessor.getMetaData();
116      }
117    
118      public OutputProcessor getOutputProcessor()
119      {
120        return outputProcessor;
121      }
122    
123      public InputFeed getInputFeed()
124      {
125        if (inputFeed == null)
126        {
127          inputFeed = getOutputProcessor().createInputFeed(this);
128        }
129        return inputFeed;
130      }
131    
132      protected abstract InputFeed createInputFeed();
133    
134      /**
135       * The document context holds global information, like the used stylesheets.
136       * It also holds the caches for loading external contents.
137       *
138       * @return the document context.
139       */
140      public DocumentContext getDocumentContext()
141      {
142        return documentContext;
143      }
144    
145      public ResourceManager getResourceManager()
146      {
147        return documentContext.getResourceManager();
148      }
149    
150      public void pageBreakEncountered(final CSSValue pageName,
151                                       final PseudoPage[] pseudoPages)
152          throws NormalizationException
153      {
154        getInputFeed().handlePageBreakEncountered(pageName, pseudoPages);
155      }
156    
157      public boolean isPagebreakEncountered()
158      {
159        return getInputFeed().isPagebreakEncountered();
160      }
161    
162    //  protected abstract AbstractLayoutProcessState createState()
163    //      throws StateException;
164    
165    //  protected void fillState(AbstractLayoutProcessState state) throws
166    //      StateException
167    //  {
168    //    state.setDocumentContext(documentContext);
169    //    if (inputFeed != null)
170    //    {
171    //      state.setInputFeedState(inputFeed.saveState());
172    //    }
173    //  }
174    
175    //
176    //  public LayoutProcessState saveState() throws StateException
177    //  {
178    //    AbstractLayoutProcessState state = createState();
179    //    fillState(state);
180    //    return state;
181    //  }
182    
183      public Normalizer getNormalizer()
184      {
185        return getInputFeed().getCurrentNormalizer();
186      }
187    
188      public StyleResolver getStyleResolver()
189      {
190        return styleResolver;
191      }
192    }