001    package org.apache.myfaces.tobago.webapp;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.myfaces.tobago.renderkit.html.StyleClasses;
021    import org.apache.myfaces.tobago.renderkit.html.HtmlStyleMap;
022    import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
023    import org.apache.myfaces.tobago.renderkit.html.HtmlConstants;
024    
025    import javax.faces.component.UIComponent;
026    import javax.faces.context.ResponseWriter;
027    import java.io.IOException;
028    import java.io.Writer;
029    
030    /**
031     * This provides an alternative ResponseWriter interfaces, which allows optimizations.
032     * E. g. some attributes needed to to be escaped.
033     *
034     * User: lofwyr
035     * Date: 08.05.2007 13:51:43
036     */
037    public abstract class TobagoResponseWriter extends ResponseWriter {
038    
039      // same as in ResponseWriter
040    
041      public abstract void startElement(String name, UIComponent component) throws IOException;
042    
043      /**
044       * @deprecated Use {@link #startElement(String, UIComponent) startElement(name, null)} instead.
045       */
046      @Deprecated
047      public void startElement(String name) throws IOException {
048        startElement(name, null);
049      }
050    
051      public abstract void endElement(String name) throws IOException;
052    
053      public abstract void write(String string) throws IOException;
054    
055      public abstract void writeComment(Object comment) throws IOException;
056    
057      public abstract ResponseWriter cloneWithWriter(Writer writer);
058      /**
059       * @deprecated Should not directly called via this interface. There is be a special method which might be better.
060       */
061      @Deprecated
062      public abstract void writeAttribute(String name, Object value, final String property) throws IOException;
063    
064      /**
065       * @deprecated Should not directly called via this interface. There is be a special method which might be better.
066       */
067      @Deprecated
068      public abstract void writeText(Object text, String property) throws IOException;
069    
070      public abstract void flush() throws IOException;
071    
072      // others (not from ResponseWriter)
073    
074      /**
075       * Writes a string attribute. The renderer may set escape=false to switch of escaping of the string,
076       * if it is not necessary.
077       */
078      public abstract void writeAttribute(String name, String string, boolean escape) throws IOException;
079    
080      /**
081       * Writes a boolean attribute. The value will not escaped.
082       */
083      public void writeAttribute(String name, boolean on) throws IOException {
084        if (on) {
085          writeAttribute(name, name, false);
086        }
087      }
088    
089      /**
090       * Writes a integer attribute. The value will not escaped.
091       */
092      public void writeAttribute(String name, int number) throws IOException {
093        writeAttribute(name, Integer.toString(number), false);
094      }
095    
096      /**
097       * Writes a propery as attribute. The value will be escaped.
098       */
099      public void writeAttributeFromComponent(String name, String property) throws IOException {
100        writeAttribute(name, null, property);
101      }
102    
103      /**
104       * Write the id attribute. The value will not escaped.
105       */
106      public void writeIdAttribute(String id) throws IOException {
107        writeAttribute(HtmlAttributes.ID, id, false);
108      }
109    
110      /**
111       * Write the name attribute. The value will not escaped.
112       */
113      public void writeNameAttribute(String name) throws IOException {
114        writeAttribute(HtmlAttributes.NAME, name, false);
115      }
116    
117      /**
118       * Write the class attribute. The value will not escaped.
119       */
120      public void writeClassAttribute(String cssClass) throws IOException {
121        writeAttribute(HtmlAttributes.CLASS, cssClass, false);
122      }
123    
124      /**
125       * Write the class attribute. The value will not escaped.
126       */
127      public void writeClassAttribute(StyleClasses styleClasses) throws IOException {
128         writeAttribute(HtmlAttributes.CLASS, styleClasses.toString(), false);
129      }
130    
131      /**
132       * Write the class attribute. The value will not escaped.
133       */
134      public abstract void writeClassAttribute() throws IOException;
135    
136      /**
137       * Write the style attribute. The value will not escaped.
138       */
139      public void writeStyleAttribute(HtmlStyleMap style) throws IOException {
140        if (style != null) {
141          writeAttribute(HtmlAttributes.STYLE, style.toString(), false);
142        }
143      }
144    
145      /**
146       * Write the style attribute. The value will not escaped.
147       */
148      public void writeStyleAttribute(String style) throws IOException {
149        writeAttribute(HtmlAttributes.STYLE, style, false);
150      }
151    
152      /**
153       * Write the style attribute. The value will not escaped.
154       */
155      public abstract void writeStyleAttribute() throws IOException;
156    
157      public void writeJavascript(String script) throws IOException {
158        startElement(HtmlConstants.SCRIPT, null);
159        writeAttribute(HtmlAttributes.TYPE, "text/javascript", false);
160        write("\n<!--\n");
161        write(script);
162        write("\n// -->\n");
163        endElement(HtmlConstants.SCRIPT);
164      }
165    
166      /**
167       * Write text content. The text will be escaped.
168       */
169      public void writeText(String text) throws IOException {
170        writeText(text, null);
171      }
172    
173      /**
174       * Writes a propery as text. The text will be escaped.
175       */
176      public void writeTextFromComponent(String property) throws IOException {
177        writeText(null, property);
178      }
179    
180    }