View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.qrcode;
23  
24  import java.io.*;
25  
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  import javax.faces.context.ResponseWriter;
29  
30  import org.primefaces.extensions.util.Attrs;
31  import org.primefaces.renderkit.CoreRenderer;
32  import org.primefaces.util.WidgetBuilder;
33  
34  /**
35   * Renderer for the {@link QRCode} component.
36   *
37   * @author Mauricio Fenoglio / last modified by Melloware
38   * @since 1.2.0
39   */
40  public class QRCodeRenderer extends CoreRenderer {
41  
42      @Override
43      public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
44          final QRCode qrCode = (QRCode) component;
45          encodeMarkup(context, qrCode);
46          encodeScript(context, qrCode);
47      }
48  
49      protected void encodeScript(final FacesContext context, final QRCode qrCode) throws IOException {
50          final WidgetBuilder wb = getWidgetBuilder(context);
51          wb.init("ExtQRCode", qrCode);
52          wb.attr("render", qrCode.getRenderMethod())
53                      .attr("mode", qrCode.getRenderMode())
54                      .attr("minVersion", qrCode.getMinVersion())
55                      .attr("maxVersion", qrCode.getMaxVersion())
56                      .attr("left", qrCode.getLeftOffset())
57                      .attr("top", qrCode.getTopOffset())
58                      .attr("size", qrCode.getSize())
59                      .attr("fill", qrCode.getFillColor())
60                      .attr("ecLevel", qrCode.getEcLevel())
61                      .attr("background", qrCode.getBackground())
62                      .attr("text", qrCode.getText())
63                      .attr("radius", qrCode.getRadius())
64                      .attr("quiet", qrCode.getQuiet())
65                      .attr("mSize", qrCode.getLabelSize())
66                      .attr("mPosX", qrCode.getLabelPosX())
67                      .attr("mPosY", qrCode.getLabelPosY())
68                      .attr(Attrs.LABEL, qrCode.getLabel())
69                      .attr("fontname", qrCode.getFontName())
70                      .attr("fontcolor", qrCode.getFontColor());
71  
72          wb.finish();
73      }
74  
75      private void encodeMarkup(final FacesContext context, final QRCode qrCode) throws IOException {
76          final ResponseWriter writer = context.getResponseWriter();
77          final String clientId = qrCode.getClientId(context);
78          final String styleClass = getStyleClassBuilder(context)
79                      .add(QRCode.STYLE_CLASS)
80                      .add(qrCode.getStyleClass())
81                      .build();
82          writer.startElement("span", null);
83          writer.writeAttribute("id", clientId, null);
84          writer.writeAttribute(Attrs.CLASS, styleClass, "styleClass");
85          if (qrCode.getStyle() != null) {
86              writer.writeAttribute(Attrs.STYLE, qrCode.getStyle(), Attrs.STYLE);
87          }
88          writer.endElement("span");
89      }
90  
91      @Override
92      public void encodeChildren(final FacesContext context, final UIComponent component) {
93          // do nothing
94      }
95  
96      @Override
97      public boolean getRendersChildren() {
98          return true;
99      }
100 }