Code format, tabs to space, etc.

This commit is contained in:
Erik C. Thauvin 2016-07-18 18:17:22 -07:00
parent c707b342aa
commit 32a1bf3875
13 changed files with 588 additions and 516 deletions

View file

@ -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());
}
}

View file

@ -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>&lt;</code>, <code>&gt;</code>, <code>&amp;</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>&lt;</code>, <code>&gt;</code>, <code>&amp;</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("&lt;");
break;
case '>':
escaped.append("&gt;");
break;
case '&':
escaped.append("&amp;");
break;
case '\'':
escaped.append("&#039;");
break;
case '"':
escaped.append("&#034;");
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("&lt;");
break;
case '>':
escaped.append("&gt;");
break;
case '&':
escaped.append("&amp;");
break;
case '\'':
escaped.append("&#039;");
break;
case '"':
escaped.append("&#034;");
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);
}
}
}
}

View file

@ -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);
}
}

View file

@ -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()));
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -1,35 +1,33 @@
/*
* ReasonsTest.java
*
* Copyright (c) 2015, Erik C. Thauvin (erik@thauvin.net)
* Copyright (c) 2015-2016, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the author nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.httpstatus;
@ -47,27 +45,23 @@ import java.util.ResourceBundle;
* @since 1.0
*/
@SuppressWarnings("unused")
public class ReasonsTest
{
@DataProvider(name = "reasons")
public Object[][] reasons()
{
final ResourceBundle bundle = ResourceBundle.getBundle(Reasons.BUNDLE_BASENAME);
final Object[][] reasons = new String[bundle.keySet().size()][2];
int i = 0;
for (final String key : bundle.keySet())
{
reasons[i][0] = key;
reasons[i][1] = bundle.getString(key);
i++;
}
return reasons;
}
public class ReasonsTest {
@DataProvider(name = "reasons")
public Object[][] reasons() {
final ResourceBundle bundle = ResourceBundle.getBundle(Reasons.BUNDLE_BASENAME);
final Object[][] reasons = new String[bundle.keySet().size()][2];
int i = 0;
for (final String key : bundle.keySet()) {
reasons[i][0] = key;
reasons[i][1] = bundle.getString(key);
i++;
}
return reasons;
}
@Test(dataProvider = "reasons")
public void testGetReasonPhrase(String code, String reason)
throws Exception
{
Assert.assertEquals(reason, Reasons.getReasonPhrase(code));
}
@Test(dataProvider = "reasons")
public void testGetReasonPhrase(String code, String reason)
throws Exception {
Assert.assertEquals(reason, Reasons.getReasonPhrase(code));
}
}

View file

@ -1,35 +1,33 @@
/*
* UtilsTest.java
*
* Copyright (c) 2015, Erik C. Thauvin (erik@thauvin.net)
* Copyright (c) 2015-2016, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the author nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.httpstatus;
@ -44,15 +42,13 @@ import org.testng.annotations.Test;
* @since 1.0
*/
@SuppressWarnings("unused")
public class UtilsTest
{
@Test
public void testEscapeXml()
throws Exception
{
Assert.assertEquals(
"This is a test. We wan&#039;t to make sure that everything is &lt;encoded&gt; according the &#034;encoding&#034; parameter &amp; value.",
Utils.escapeXml(
"This is a test. We wan't to make sure that everything is <encoded> according the \"encoding\" parameter & value."));
}
public class UtilsTest {
@Test
public void testEscapeXml()
throws Exception {
Assert.assertEquals(
"This is a test. We wan&#039;t to make sure that everything is &lt;encoded&gt; according the &#034;encoding&#034; parameter &amp; value.",
Utils.escapeXml(
"This is a test. We wan't to make sure that everything is <encoded> according the \"encoding\" parameter & value."));
}
}