Code format, tabs to space, etc.
This commit is contained in:
parent
c707b342aa
commit
32a1bf3875
13 changed files with 588 additions and 516 deletions
|
@ -43,66 +43,55 @@ import java.util.TreeMap;
|
|||
* @created 2015-12-02
|
||||
* @since 1.0
|
||||
*/
|
||||
public class Reasons
|
||||
{
|
||||
/**
|
||||
* The resource bundle base name.
|
||||
*/
|
||||
public static final String BUNDLE_BASENAME = "net.thauvin.erik.httpstatus.reasons";
|
||||
public class Reasons {
|
||||
/**
|
||||
* The resource bundle base name.
|
||||
*/
|
||||
public static final String BUNDLE_BASENAME = "net.thauvin.erik.httpstatus.reasons";
|
||||
|
||||
/**
|
||||
* The reason phrases map.
|
||||
*/
|
||||
private static final Map<String, String> REASON_PHRASES = new TreeMap<String, String>();
|
||||
/**
|
||||
* The reason phrases map.
|
||||
*/
|
||||
private static final Map<String, String> REASON_PHRASES = new TreeMap<String, String>();
|
||||
|
||||
/**
|
||||
* Returns the reason phrase for the specified status code.
|
||||
*
|
||||
* @param statusCode The status code.
|
||||
*
|
||||
* @return The reason phrase, or <code>null</code>.
|
||||
*/
|
||||
public static String getReasonPhrase(final int statusCode)
|
||||
{
|
||||
return getReasonPhrase(Integer.toString(statusCode));
|
||||
}
|
||||
// Initializes the reason phrases map.
|
||||
static {
|
||||
final ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_BASENAME);
|
||||
for (final String key : bundle.keySet()) {
|
||||
REASON_PHRASES.put(key, bundle.getString(key));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the reason phrase for the specified status code.
|
||||
*
|
||||
* @param statusCode The status code.
|
||||
*
|
||||
* @return The reason phrase, or <code>null</code>.
|
||||
*/
|
||||
public static String getReasonPhrase(final String statusCode)
|
||||
{
|
||||
return REASON_PHRASES.get(statusCode);
|
||||
}
|
||||
/**
|
||||
* Returns the reason phrase for the specified status code.
|
||||
*
|
||||
* @param statusCode The status code.
|
||||
* @return The reason phrase, or <code>null</code>.
|
||||
*/
|
||||
public static String getReasonPhrase(final int statusCode) {
|
||||
return getReasonPhrase(Integer.toString(statusCode));
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints the status codes and reason phrases.
|
||||
*
|
||||
* @param args The command line arguments.
|
||||
*/
|
||||
public static void main(final String... args)
|
||||
{
|
||||
for (final Map.Entry<String, String> entry : REASON_PHRASES.entrySet())
|
||||
{
|
||||
System.out.println(entry.getKey() + ": " + entry.getValue());
|
||||
}
|
||||
/**
|
||||
* Returns the reason phrase for the specified status code.
|
||||
*
|
||||
* @param statusCode The status code.
|
||||
* @return The reason phrase, or <code>null</code>.
|
||||
*/
|
||||
public static String getReasonPhrase(final String statusCode) {
|
||||
return REASON_PHRASES.get(statusCode);
|
||||
}
|
||||
|
||||
System.out.println("Total: " + REASON_PHRASES.entrySet().size());
|
||||
}
|
||||
/**
|
||||
* Prints the status codes and reason phrases.
|
||||
*
|
||||
* @param args The command line arguments.
|
||||
*/
|
||||
public static void main(final String... args) {
|
||||
for (final Map.Entry<String, String> entry : REASON_PHRASES.entrySet()) {
|
||||
System.out.println(entry.getKey() + ": " + entry.getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the reason phrases map.
|
||||
*/
|
||||
static
|
||||
{
|
||||
final ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_BASENAME);
|
||||
for (final String key : bundle.keySet())
|
||||
{
|
||||
REASON_PHRASES.put(key, bundle.getString(key));
|
||||
}
|
||||
}
|
||||
System.out.println("Total: " + REASON_PHRASES.entrySet().size());
|
||||
}
|
||||
}
|
|
@ -41,95 +41,78 @@ import java.io.Writer;
|
|||
* @created 2015-12-03
|
||||
* @since 1.0
|
||||
*/
|
||||
public final class Utils
|
||||
{
|
||||
public final class Utils {
|
||||
|
||||
/**
|
||||
* Disables the default constructor.
|
||||
*
|
||||
* @throws UnsupportedOperationException If the constructor is called.
|
||||
*/
|
||||
private Utils()
|
||||
throws UnsupportedOperationException
|
||||
{
|
||||
throw new UnsupportedOperationException("Illegal constructor call.");
|
||||
}
|
||||
/**
|
||||
* Disables the default constructor.
|
||||
*
|
||||
* @throws UnsupportedOperationException If the constructor is called.
|
||||
*/
|
||||
private Utils()
|
||||
throws UnsupportedOperationException {
|
||||
throw new UnsupportedOperationException("Illegal constructor call.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a string value to the specified writer. The default value is used when the actual value is null.
|
||||
*
|
||||
* @param out The writer to output the value to.
|
||||
* @param value The string value.
|
||||
* @param defaultValue The default value.
|
||||
* @param xml The {@link #escapeXml(String) xml} flag.
|
||||
*
|
||||
* @throws IOException f an I/O error occurs.
|
||||
*/
|
||||
public static void outWrite(final Writer out, final String value, final String defaultValue, final boolean xml)
|
||||
throws IOException
|
||||
{
|
||||
if (xml)
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
out.write(escapeXml(value));
|
||||
}
|
||||
else if (defaultValue != null)
|
||||
{
|
||||
out.write(escapeXml(defaultValue));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
out.write(value);
|
||||
}
|
||||
else if (defaultValue != null)
|
||||
{
|
||||
out.write(defaultValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Converts <code><</code>, <code>></code>, <code>&</code>, <code>'</code>, <code>"</code>
|
||||
* to their corresponding entity codes.
|
||||
*
|
||||
* @param value The string value to convert.
|
||||
* @return The converted string value.
|
||||
*/
|
||||
public static String escapeXml(final String value) {
|
||||
final StringBuilder escaped = new StringBuilder();
|
||||
|
||||
/**
|
||||
* Converts <code><</code>, <code>></code>, <code>&</code>, <code>'</code>, <code>"</code>
|
||||
* to their corresponding entity codes.
|
||||
*
|
||||
* @param value The string value to convert.
|
||||
*
|
||||
* @return The converted string value.
|
||||
*/
|
||||
public static String escapeXml(final String value)
|
||||
{
|
||||
final StringBuilder escaped = new StringBuilder();
|
||||
for (int i = 0; i < value.length(); i++) {
|
||||
final char c = value.charAt(i);
|
||||
switch (c) {
|
||||
case '<':
|
||||
escaped.append("<");
|
||||
break;
|
||||
case '>':
|
||||
escaped.append(">");
|
||||
break;
|
||||
case '&':
|
||||
escaped.append("&");
|
||||
break;
|
||||
case '\'':
|
||||
escaped.append("'");
|
||||
break;
|
||||
case '"':
|
||||
escaped.append(""");
|
||||
break;
|
||||
default:
|
||||
escaped.append(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < value.length(); i++)
|
||||
{
|
||||
final char c = value.charAt(i);
|
||||
switch (c)
|
||||
{
|
||||
case '<':
|
||||
escaped.append("<");
|
||||
break;
|
||||
case '>':
|
||||
escaped.append(">");
|
||||
break;
|
||||
case '&':
|
||||
escaped.append("&");
|
||||
break;
|
||||
case '\'':
|
||||
escaped.append("'");
|
||||
break;
|
||||
case '"':
|
||||
escaped.append(""");
|
||||
break;
|
||||
default:
|
||||
escaped.append(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return escaped.toString();
|
||||
}
|
||||
|
||||
return escaped.toString();
|
||||
}
|
||||
/**
|
||||
* Writes a string value to the specified writer. The default value is used when the actual value is null.
|
||||
*
|
||||
* @param out The writer to output the value to.
|
||||
* @param value The string value.
|
||||
* @param defaultValue The default value.
|
||||
* @param xml The {@link #escapeXml(String) xml} flag.
|
||||
* @throws IOException f an I/O error occurs.
|
||||
*/
|
||||
public static void outWrite(final Writer out, final String value, final String defaultValue, final boolean xml)
|
||||
throws IOException {
|
||||
if (xml) {
|
||||
if (value != null) {
|
||||
out.write(escapeXml(value));
|
||||
} else if (defaultValue != null) {
|
||||
out.write(escapeXml(defaultValue));
|
||||
}
|
||||
} else {
|
||||
if (value != null) {
|
||||
out.write(value);
|
||||
} else if (defaultValue != null) {
|
||||
out.write(defaultValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -45,29 +45,24 @@ import java.io.IOException;
|
|||
* @created 2015-12-03
|
||||
* @since 1.0
|
||||
*/
|
||||
public class CauseTag extends XmlSupport
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void doTag()
|
||||
throws JspException, IOException
|
||||
{
|
||||
final PageContext pageContext = (PageContext) getJspContext();
|
||||
final JspWriter out = pageContext.getOut();
|
||||
public class CauseTag extends XmlSupport {
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void doTag()
|
||||
throws JspException, IOException {
|
||||
final PageContext pageContext = (PageContext) getJspContext();
|
||||
final JspWriter out = pageContext.getOut();
|
||||
|
||||
String cause;
|
||||
String cause;
|
||||
|
||||
try
|
||||
{
|
||||
cause = pageContext.getErrorData().getThrowable().getCause().getLocalizedMessage();
|
||||
}
|
||||
catch (NullPointerException ignore)
|
||||
{
|
||||
cause = defaultValue;
|
||||
}
|
||||
try {
|
||||
cause = pageContext.getErrorData().getThrowable().getCause().getLocalizedMessage();
|
||||
} catch (NullPointerException ignore) {
|
||||
cause = defaultValue;
|
||||
}
|
||||
|
||||
Utils.outWrite(out, cause, defaultValue, escapeXml);
|
||||
}
|
||||
Utils.outWrite(out, cause, defaultValue, escapeXml);
|
||||
}
|
||||
}
|
|
@ -44,18 +44,16 @@ import java.io.IOException;
|
|||
* @created 2015-12-03
|
||||
* @since 1.0
|
||||
*/
|
||||
public class CodeTag extends SimpleTagSupport
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void doTag()
|
||||
throws JspException, IOException
|
||||
{
|
||||
final PageContext pageContext = (PageContext) getJspContext();
|
||||
final JspWriter out = pageContext.getOut();
|
||||
public class CodeTag extends SimpleTagSupport {
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void doTag()
|
||||
throws JspException, IOException {
|
||||
final PageContext pageContext = (PageContext) getJspContext();
|
||||
final JspWriter out = pageContext.getOut();
|
||||
|
||||
out.write(String.valueOf(pageContext.getErrorData().getStatusCode()));
|
||||
}
|
||||
out.write(String.valueOf(pageContext.getErrorData().getStatusCode()));
|
||||
}
|
||||
}
|
|
@ -47,50 +47,41 @@ import java.io.IOException;
|
|||
* @created 2015-12-02
|
||||
* @since 1.0
|
||||
*/
|
||||
public class ReasonTag extends XmlSupport
|
||||
{
|
||||
private int statusCode;
|
||||
public class ReasonTag extends XmlSupport {
|
||||
private int statusCode;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void doTag()
|
||||
throws JspException, IOException
|
||||
{
|
||||
final PageContext pageContext = (PageContext) getJspContext();
|
||||
final JspWriter out = pageContext.getOut();
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void doTag()
|
||||
throws JspException, IOException {
|
||||
final PageContext pageContext = (PageContext) getJspContext();
|
||||
final JspWriter out = pageContext.getOut();
|
||||
|
||||
try
|
||||
{
|
||||
if (statusCode >= 0)
|
||||
{
|
||||
Utils.outWrite(out, Reasons.getReasonPhrase(statusCode), defaultValue, escapeXml);
|
||||
}
|
||||
else
|
||||
{
|
||||
Utils.outWrite(out,
|
||||
Reasons.getReasonPhrase(pageContext.getErrorData().getStatusCode()),
|
||||
defaultValue,
|
||||
escapeXml);
|
||||
}
|
||||
}
|
||||
catch (IOException ignore)
|
||||
{
|
||||
// Ignore.
|
||||
}
|
||||
}
|
||||
try {
|
||||
if (statusCode >= 0) {
|
||||
Utils.outWrite(out, Reasons.getReasonPhrase(statusCode), defaultValue, escapeXml);
|
||||
} else {
|
||||
Utils.outWrite(out,
|
||||
Reasons.getReasonPhrase(pageContext.getErrorData().getStatusCode()),
|
||||
defaultValue,
|
||||
escapeXml);
|
||||
}
|
||||
} catch (IOException ignore) {
|
||||
// Ignore.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the status code.
|
||||
*
|
||||
* @param statusCode The status code.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public void setCode(final int statusCode)
|
||||
{
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
/**
|
||||
* Sets the status code.
|
||||
*
|
||||
* @param statusCode The status code.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public void setCode(final int statusCode) {
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -40,37 +40,34 @@ import javax.servlet.jsp.tagext.SimpleTagSupport;
|
|||
* @created 2015-12-03
|
||||
* @since 1.0
|
||||
*/
|
||||
public abstract class XmlSupport extends SimpleTagSupport
|
||||
{
|
||||
/**
|
||||
* Default value string.
|
||||
*/
|
||||
protected String defaultValue;
|
||||
public abstract class XmlSupport extends SimpleTagSupport {
|
||||
/**
|
||||
* Default value string.
|
||||
*/
|
||||
protected String defaultValue;
|
||||
|
||||
/**
|
||||
* Escape XML flag.
|
||||
*/
|
||||
protected boolean escapeXml = true;
|
||||
/**
|
||||
* Escape XML flag.
|
||||
*/
|
||||
protected boolean escapeXml = true;
|
||||
|
||||
/**
|
||||
* Sets the default value.
|
||||
*
|
||||
* @param defaultValue The default value.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public void setDefault(final String defaultValue)
|
||||
{
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
/**
|
||||
* Sets the default value.
|
||||
*
|
||||
* @param defaultValue The default value.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public void setDefault(final String defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link net.thauvin.erik.httpstatus.Utils#escapeXml(String) xml} flag.
|
||||
*
|
||||
* @param escapeXml <code>true</code> or <code>false</code>
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public void setEscapeXml(final boolean escapeXml)
|
||||
{
|
||||
this.escapeXml = escapeXml;
|
||||
}
|
||||
/**
|
||||
* Sets the {@link net.thauvin.erik.httpstatus.Utils#escapeXml(String) xml} flag.
|
||||
*
|
||||
* @param escapeXml <code>true</code> or <code>false</code>
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public void setEscapeXml(final boolean escapeXml) {
|
||||
this.escapeXml = escapeXml;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue