View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.document;
20  
21  import java.io.IOException;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.context.FacesContext;
25  import javax.faces.context.ResponseWriter;
26  import javax.faces.render.Renderer;
27  
28  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
29  
30  /**
31   * Base class to handle the document family
32   *
33   * @author Mario Ivankovits (latest modification by $Author: skitching $)
34   * @version $Revision: 673833 $ $Date: 2008-07-03 16:58:05 -0500 (jue, 03 jul 2008) $
35   */
36  public abstract class AbstractDocumentRenderer extends Renderer
37  {
38      protected abstract String getHtmlTag();
39      protected abstract Class getDocumentClass();
40  
41      public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
42              throws IOException
43      {
44          RendererUtils.checkParamValidity(facesContext, uiComponent,
45                  getDocumentClass());
46  
47          AbstractDocument document = (AbstractDocument) uiComponent;
48  
49          ResponseWriter writer = facesContext.getResponseWriter();
50  
51          if (document.hasState() && document.isEndState())
52          {
53              closeTag(facesContext, writer);
54          }
55          else
56          {
57              openTag(facesContext, writer, uiComponent);
58          }
59      }
60  
61      protected void openTag(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent)
62          throws IOException
63      {
64          writer.startElement(getHtmlTag(), uiComponent);
65      }
66  
67      protected void closeTag(FacesContext facesContext, ResponseWriter writer)
68          throws IOException
69      {
70          writeBeforeEnd(facesContext);
71          writer.endElement(getHtmlTag());
72      }
73  
74      public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
75              throws IOException
76      {
77          AbstractDocument document = (AbstractDocument) uiComponent;
78  
79          ResponseWriter writer = facesContext.getResponseWriter();
80  
81          if (!document.hasState())
82          {
83              closeTag(facesContext, writer);
84          }
85      }
86  
87      protected void writeBeforeEnd(FacesContext facesContext) throws IOException
88      {
89      }
90  }