BrightSide Workbench Full Report + Source Code
HTMLHelper.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.html;
19 
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.io.Writer;
23 import java.util.Date;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import javax.servlet.ServletException;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.elephant.context.IConstructor;
29 import org.turro.elephant.impl.util.Localizer;
30 import org.turro.i18n.I_;
31 
36 public class HTMLHelper extends HTMLGenerator {
37  protected IConstructor constructor = null;
38 
41  super(out);
42  if(out == null) {
43  try {
45  } catch (IOException ex) {
46  Logger.getLogger(HTMLHelper.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
47  }
48  }
49  this.constructor = constructor;
50  }
51 
54  super(null);
55  this.constructor = constructor;
56  try {
58  } catch (IOException ex) {
59  Logger.getLogger(HTMLHelper.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
60  }
61  }
62 
64  public HTMLHelper(Writer out) {
65  super(out);
66  }
67 
68  @Override
69  public HTMLGenerator write(String value) {
70  if(value == null) return this;
71  if(constructor != null) {
72  try {
73  constructor.processOutput((PrintWriter) out, value);
74  } catch (ServletException ex) {
75  Logger.getLogger(HTMLHelper.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
76  } catch (IOException ex) {
77  Logger.getLogger(HTMLHelper.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
78  }
79  }
80  else {
81  super.write(value);
82  }
83  return this;
84  }
85 
86  public void writeMacro(String value) {
87  super.write(value);
88  }
89 
90  public void writeln(String value) {
91  if(value == null) return;
92  if(constructor != null) {
93  try {
94  constructor.processOutput((PrintWriter) out, value + "\n");
95  } catch (ServletException ex) {
96  Logger.getLogger(HTMLHelper.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
97  } catch (IOException ex) {
98  Logger.getLogger(HTMLHelper.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
99  }
100  }
101  else {
102  super.write(value + "\n");
103  }
104  }
105 
106  public void writeDate(Date date, String mode) {
107  Localizer localizer = constructor.getLocalizer();
108  write(localizer.parseValue("date", mode, date));
109  }
110 
111  public void writeNumber(Number number, String mode) {
112  Localizer localizer = constructor.getLocalizer();
113  write(localizer.parseValue("number", mode, number));
114  }
115 
116  public void writeBytes(long bytes) {
117  Localizer localizer = constructor.getLocalizer();
118  write(localizer.parseValue("bytes", null, Long.valueOf(bytes)));
119  }
120 
121  public void writeBoolean(boolean value) {
122  write(I_.get(value ? "true" : "false"));
123  }
124 
125 }
String parseValue(String type, String outFormat, Object value)
Definition: Localizer.java:96
void writeln(String value)
Definition: HTMLHelper.java:90
void writeBytes(long bytes)
void writeDate(Date date, String mode)
void writeMacro(String value)
Definition: HTMLHelper.java:86
void writeNumber(Number number, String mode)
HTMLGenerator write(String value)
Definition: HTMLHelper.java:69
HTMLHelper(IConstructor constructor, Writer out)
Definition: HTMLHelper.java:40
HTMLHelper(IConstructor constructor)
Definition: HTMLHelper.java:53
void writeBoolean(boolean value)
static String get(String msg)
Definition: I_.java:41