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