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  
20  package org.apache.myfaces.tobago.internal.webapp;
21  
22  import org.apache.myfaces.tobago.internal.util.HtmlWriterHelper;
23  import org.apache.myfaces.tobago.internal.util.WriterHelper;
24  
25  import javax.faces.context.ResponseWriter;
26  import java.io.IOException;
27  import java.io.Writer;
28  import java.nio.charset.Charset;
29  import java.util.Arrays;
30  
31  public class HtmlResponseWriter extends TobagoResponseWriterBase {
32  
33    private static final String HTML_DOCTYPE = "<!DOCTYPE html>";
34  
35    private final WriterHelper helper;
36  
37    /**
38     * @deprecated since 4.3.0
39     */
40    @Deprecated
41    public HtmlResponseWriter(
42        final Writer writer, final String contentType, final String characterEncoding) {
43      this(writer, contentType, Charset.forName(characterEncoding));
44    }
45  
46    public HtmlResponseWriter(
47        final Writer writer, final String contentType, final Charset charset) {
48      super(writer, contentType, charset);
49      this.helper = new HtmlWriterHelper(writer, charset);
50    }
51  
52    @Override
53    public void write(final String string) throws IOException {
54      writeInternal(getWriter(), string);
55    }
56  
57    public final WriterHelper getHelper() {
58      return helper;
59    }
60  
61    @Override
62    public void writeText(final Object text, final String property)
63        throws IOException {
64      closeOpenTag();
65      final String value = findValue(text, property);
66      helper.writeText(value);
67    }
68  
69    @Override
70    public void writeText(final char[] text, final int offset, final int length)
71        throws IOException {
72      closeOpenTag();
73      helper.writeText(text, offset, length);
74    }
75  
76    @Override
77    public void write(final char[] cbuf, final int off, final int len) throws IOException {
78      // Related to http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-696
79      if (Arrays.equals(cbuf, XML_VERSION_1_0_ENCODING_UTF_8_CHARS)) {
80        // drop
81      } else {
82        super.write(cbuf, off, len);
83      }
84    }
85  
86    @Override
87    protected void closeEmptyTag() throws IOException {
88      getWriter().write(">");
89    }
90  
91    @Override
92    protected void writerAttributeValue(final String value, final boolean escape) throws IOException {
93      if (escape) {
94        helper.writeAttributeValue(value);
95      } else {
96        getWriter().write(value);
97      }
98    }
99  
100   @Override
101   public ResponseWriter cloneWithWriter(final Writer originalWriter) {
102     return new HtmlResponseWriter(originalWriter, getContentType(), Charset.forName(getCharacterEncoding()));
103   }
104 
105   @Override
106   public void startDocument() throws IOException {
107     getWriter().write(HTML_DOCTYPE);
108     getWriter().write('\n');
109   }
110 
111   @Override
112   public void endElement(final String name) throws IOException {
113     super.endElement(name);
114   }
115 
116   @Override
117   public void endDocument() throws IOException {
118   }
119 }