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