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.commons.lang.StringUtils;
23  import org.apache.myfaces.tobago.component.Attributes;
24  import org.apache.myfaces.tobago.internal.util.Deprecation;
25  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
26  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import javax.faces.component.UIComponent;
31  import javax.faces.context.ResponseWriter;
32  import java.io.IOException;
33  import java.io.Writer;
34  import java.util.EmptyStackException;
35  import java.util.Stack;
36  
37  public class DebugResponseWriterWrapper extends TobagoResponseWriter {
38  
39    private Stack<String> stack = new Stack<String>();
40  
41    private static final Logger LOG = LoggerFactory.getLogger(DebugResponseWriterWrapper.class);
42  
43    private final TobagoResponseWriter responseWriter;
44  
45    public DebugResponseWriterWrapper(TobagoResponseWriter responseWriter) {
46      this.responseWriter = responseWriter;
47    }
48  
49    public void write(String string) throws IOException {
50      responseWriter.write(string);
51    }
52  
53    public void writeComment(Object comment) throws IOException {
54      String commentStr = comment.toString();
55      if (commentStr.indexOf("--") > 0) {
56        LOG.error("Comment must not contain the sequence '--', comment = '" + comment + "'.",
57            new IllegalArgumentException());
58  
59        commentStr = StringUtils.replace(commentStr, "--", "++");
60      }
61      responseWriter.writeComment(commentStr);
62    }
63  
64    public ResponseWriter cloneWithWriter(Writer writer) {
65      return new DebugResponseWriterWrapper((TobagoResponseWriter) responseWriter.cloneWithWriter(writer));
66    }
67  
68    @Deprecated
69    public void writeAttribute(String name, Object value, String property) throws IOException {
70      responseWriter.writeAttribute(name, value, property);
71    }
72  
73    @Deprecated
74    public void writeText(Object text, String property) throws IOException {
75      responseWriter.writeText(text, property);
76    }
77  
78    public void flush() throws IOException {
79      responseWriter.flush();
80    }
81  
82    public void writeAttribute(String name, String value, boolean escape) throws IOException {
83      responseWriter.writeAttribute(name, value, escape);
84    }
85  
86    @Override
87    @Deprecated
88    public String getStyleClasses() {
89      return responseWriter.getStyleClasses();
90    }
91  
92    /**
93     * @deprecated since Tobago 1.5.0
94     */
95    @Deprecated
96    public void writeClassAttribute() throws IOException {
97      Deprecation.LOG.warn("Please use writeClassAttribute(org.apache.myfaces.tobago.renderkit.css.Classes)");
98      responseWriter.writeAttribute(HtmlAttributes.CLASS, null, Attributes.STYLE_CLASS);
99    }
100 
101   public String getContentType() {
102     return responseWriter.getContentType();
103   }
104 
105   public String getCharacterEncoding() {
106     return responseWriter.getCharacterEncoding();
107   }
108 
109   public void startDocument() throws IOException {
110     responseWriter.startDocument();
111   }
112 
113   public void endDocument() throws IOException {
114     responseWriter.endDocument();
115   }
116 
117   @Override
118   public void writeJavascript(String script) throws IOException {
119     responseWriter.writeJavascript(script);
120   }
121 
122   @Override
123   public void endJavascript() throws IOException {
124     responseWriter.endJavascript();
125   }
126 
127   @Override
128   public void startJavascript() throws IOException {
129     responseWriter.startJavascript();
130   }
131 
132   public void writeURIAttribute(String name, Object value, String property) throws IOException {
133     responseWriter.writeURIAttribute(name, value, property);
134   }
135 
136   public void writeText(char[] text, int off, int len) throws IOException {
137     responseWriter.writeText(text, off, len);
138   }
139 
140   public void write(char[] chars, int i, int i1) throws IOException {
141     responseWriter.write(chars, i, i1);
142   }
143 
144   public void close() throws IOException {
145     responseWriter.close();
146   }
147 
148   @Override
149   public void startElement(String name, UIComponent currentComponent)
150       throws IOException {
151     if (LOG.isDebugEnabled()) {
152       LOG.debug("start element: '" + name + "'");
153     }
154     stack.push(name);
155     responseWriter.startElement(name, currentComponent);
156   }
157 
158   @Override
159   public void endElement(String name) throws IOException {
160     if (LOG.isDebugEnabled()) {
161       LOG.debug("end element: '" + name + "'");
162     }
163     String top = "";
164     try {
165       top = stack.pop();
166     } catch (EmptyStackException e) {
167       LOG.error("Failed to close element \"" + name + "\"!", e);
168     }
169 
170     if (!top.equals(name)) {
171       LOG.error("Element end with name='" + name + "' doesn't match with top element on the stack='" + top + "'.",
172           new IllegalArgumentException());
173     }
174     responseWriter.endElement(name);
175   }
176 }