Sort command line output. Closes #10

Updated copyright

Code cleanup
This commit is contained in:
Erik C. Thauvin 2024-06-07 14:12:11 -07:00
parent 8c79c0e17a
commit 053dcba26b
Signed by: erik
GPG key ID: 776702A6A2DA330E
17 changed files with 100 additions and 85 deletions

View file

@ -1,7 +1,7 @@
/*
* HttpStatusBuild.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -140,6 +140,11 @@ public class HttpStatusBuild extends Project {
pmdOp.includeLineNumber(false).execute();
}
private void pomRoot() throws FileUtilsErrorException {
PomBuilder.generateInto(publishOperation().info(), dependencies(),
Path.of(workDirectory.getPath(), "pom.xml").toFile());
}
@Override
public void publish() throws Exception {
super.publish();
@ -151,9 +156,4 @@ public class HttpStatusBuild extends Project {
super.publishLocal();
pomRoot();
}
private void pomRoot() throws FileUtilsErrorException {
PomBuilder.generateInto(publishOperation().info(), dependencies(),
Path.of(workDirectory.getPath(), "pom.xml").toFile());
}
}

View file

@ -1,7 +1,7 @@
/*
* Reasons.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -34,6 +34,7 @@ package net.thauvin.erik.httpstatus;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
/**
@ -56,8 +57,8 @@ public final class Reasons {
// Initializes the reason phrases map.
static {
final ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_BASENAME);
for (final String key : bundle.keySet()) {
var bundle = ResourceBundle.getBundle(BUNDLE_BASENAME);
for (var key : bundle.keySet()) {
REASON_PHRASES.put(key, bundle.getString(key));
}
}
@ -75,7 +76,7 @@ public final class Reasons {
* @param statusCode The status code.
* @return The reason phrase, or <code>null</code>.
*/
public static String getReasonPhrase(final int statusCode) {
public static String getReasonPhrase(int statusCode) {
return getReasonPhrase(Integer.toString(statusCode));
}
@ -85,7 +86,7 @@ public final class Reasons {
* @param statusCode The status code.
* @return The reason phrase, or <code>null</code>.
*/
public static String getReasonPhrase(final String statusCode) {
public static String getReasonPhrase(String statusCode) {
return REASON_PHRASES.get(statusCode);
}
@ -95,29 +96,26 @@ public final class Reasons {
* @param args The status code(s) or response class(es), prints all if none.
*/
@SuppressWarnings("PMD.SystemPrintln")
public static void main(final String... args) {
public static void main(String... args) {
var keys = new TreeSet<>(REASON_PHRASES.keySet());
if (args.length >= 1) {
for (final String key : args) {
for (var key : args) {
if (key.endsWith("xx")) {
var responseClass = key.charAt(0);
REASON_PHRASES.forEach((k, v) -> {
keys.forEach((k) -> {
if (k.charAt(0) == responseClass) {
System.out.println(k + ": " + v);
System.out.println(k + ": " + REASON_PHRASES.get(k));
}
});
} else {
final String value = REASON_PHRASES.get(key);
var value = REASON_PHRASES.get(key);
if (value != null) {
System.out.println(key + ": " + value);
}
}
}
} else {
REASON_PHRASES.forEach((k, v) -> {
if (v != null) {
System.out.println(k + ": " + v);
}
});
keys.forEach((k) -> System.out.println(k + ": " + REASON_PHRASES.get(k)));
System.out.println("Total: " + REASON_PHRASES.size());
}
}

View file

@ -1,7 +1,7 @@
/*
* StatusCode.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -58,7 +58,7 @@ public class StatusCode implements Serializable {
*
* @param code The status code.
*/
public StatusCode(final int code) {
public StatusCode(int code) {
this.code = code;
}
@ -147,7 +147,7 @@ public class StatusCode implements Serializable {
*
* @param code The HTTP status code.
*/
public void setCode(final int code) {
public void setCode(int code) {
this.code = code;
}
}

View file

@ -1,7 +1,7 @@
/*
* Utils.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -59,11 +59,11 @@ public final class Utils {
* @param value The string value to convert.
* @return The converted string value.
*/
public static String escapeXml(final String value) {
final StringBuilder escaped = new StringBuilder();
public static String escapeXml(String value) {
var escaped = new StringBuilder();
for (int i = 0; i < value.length(); i++) {
final char c = value.charAt(i);
for (var i = 0; i < value.length(); i++) {
var c = value.charAt(i);
switch (c) {
case '<' -> escaped.append("&lt;");
case '>' -> escaped.append("&gt;");
@ -86,7 +86,7 @@ public final class Utils {
* @param xml The {@link #escapeXml(String) xml} flag.
* @throws IOException If an I/O error occurs.
*/
public static void outWrite(final Writer out, final String value, final String defaultValue, final boolean xml)
public static void outWrite(Writer out, String value, String defaultValue, boolean xml)
throws IOException {
if (value != null) {
out.write(xml ? escapeXml(value) : value);

View file

@ -1,7 +1,7 @@
/*
* CauseTag.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -32,10 +32,11 @@
package net.thauvin.erik.httpstatus.taglibs;
import jakarta.servlet.jsp.PageContext;
import java.io.IOException;
import net.thauvin.erik.httpstatus.Utils;
import jakarta.servlet.jsp.JspWriter;
import jakarta.servlet.jsp.PageContext;
import net.thauvin.erik.httpstatus.Utils;
import java.io.IOException;
/**
@ -51,12 +52,10 @@ public class CauseTag extends XmlSupport {
*/
@Override
public void doTag() throws IOException {
final PageContext pageContext = (PageContext) getJspContext();
final JspWriter out = pageContext.getOut();
PageContext pageContext = (PageContext) getJspContext();
JspWriter out = pageContext.getOut();
final Throwable cause = pageContext.getErrorData().getThrowable().getCause();
Throwable cause = pageContext.getErrorData().getThrowable().getCause();
Utils.outWrite(out, getCause(cause), defaultValue, escapeXml);
}

View file

@ -1,7 +1,7 @@
/*
* CodeTag.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -51,8 +51,8 @@ public class CodeTag extends SimpleTagSupport {
*/
@Override
public void doTag() throws IOException {
final PageContext pageContext = (PageContext) getJspContext();
final JspWriter out = pageContext.getOut();
PageContext pageContext = (PageContext) getJspContext();
JspWriter out = pageContext.getOut();
out.write(String.valueOf(pageContext.getErrorData().getStatusCode()));
}

View file

@ -1,7 +1,7 @@
/*
* MessageTag.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -51,10 +51,10 @@ public class MessageTag extends XmlSupport {
*/
@Override
public void doTag() throws IOException {
final PageContext pageContext = (PageContext) getJspContext();
final JspWriter out = pageContext.getOut();
PageContext pageContext = (PageContext) getJspContext();
JspWriter out = pageContext.getOut();
final String message = (String) pageContext.getRequest().getAttribute(
String message = (String) pageContext.getRequest().getAttribute(
jakarta.servlet.RequestDispatcher.ERROR_MESSAGE);
Utils.outWrite(out, message, defaultValue, escapeXml);

View file

@ -1,7 +1,7 @@
/*
* ReasonTag.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -55,8 +55,8 @@ public class ReasonTag extends XmlSupport {
*/
@Override
public void doTag() {
final PageContext pageContext = (PageContext) getJspContext();
final JspWriter out = pageContext.getOut();
PageContext pageContext = (PageContext) getJspContext();
JspWriter out = pageContext.getOut();
try {
if (statusCode > -1) {
@ -75,7 +75,7 @@ public class ReasonTag extends XmlSupport {
*
* @param statusCode The status code.
*/
public void setCode(final int statusCode) {
public void setCode(int statusCode) {
this.statusCode = statusCode;
}
}

View file

@ -1,7 +1,7 @@
/*
* XmlSupport.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -58,7 +58,7 @@ public abstract class XmlSupport extends SimpleTagSupport {
* @param defaultValue The default value.
*/
@SuppressWarnings("unused")
public void setDefault(final String defaultValue) {
public void setDefault(String defaultValue) {
this.defaultValue = defaultValue;
}
@ -68,7 +68,7 @@ public abstract class XmlSupport extends SimpleTagSupport {
* @param escapeXml <code>true</code> or <code>false</code>
*/
@SuppressWarnings("unused")
public void setEscapeXml(final boolean escapeXml) {
public void setEscapeXml(boolean escapeXml) {
this.escapeXml = escapeXml;
}
}

View file

@ -2,7 +2,7 @@
<!--
~ httpstatus.tld
~
~ Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
~ Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
~ All rights reserved.
~
~ Redistribution and use in source and binary forms, with or without
@ -37,7 +37,7 @@
<description>HttpStatus JSP Tag Library</description>
<display-name>HttpStatus JSP Tags</display-name>
<tlib-version>1.1.0</tlib-version>
<tlib-version>1.1.1</tlib-version>
<short-name>hs</short-name>
<uri>http://erik.thauvin.net/taglibs/httpstatus</uri>

View file

@ -1,7 +1,7 @@
/*
* CauseTagTest.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without

View file

@ -1,7 +1,7 @@
/*
* ReasonsMainTest.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without

View file

@ -1,7 +1,7 @@
/*
* ReasonsTest.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -48,8 +48,8 @@ import static org.assertj.core.api.Assertions.assertThat;
class ReasonsTest {
@Test
void testGetReasonPhrase() {
final ResourceBundle bundle = ResourceBundle.getBundle(Reasons.BUNDLE_BASENAME);
for (final String key : bundle.keySet()) {
var bundle = ResourceBundle.getBundle(Reasons.BUNDLE_BASENAME);
for (var key : bundle.keySet()) {
assertThat(Reasons.getReasonPhrase(key)).as("getReasonPhrase(" + key + ')').isEqualTo(bundle.getString(key));
assertThat(Reasons.getReasonPhrase(Integer.parseInt(key)))
.as("getReasonPhrase(int: " + key + ')').isEqualTo(bundle.getString(key));

View file

@ -1,7 +1,7 @@
/*
* StatusCodeTest.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -47,10 +47,10 @@ import static org.assertj.core.api.Assertions.assertThat;
class StatusCodeTest {
@Test
void testStatusCode() {
final ResourceBundle bundle = ResourceBundle.getBundle(Reasons.BUNDLE_BASENAME);
StatusCode statusCode = new StatusCode();
for (final String key : bundle.keySet()) {
final int code = Integer.parseInt(key);
var bundle = ResourceBundle.getBundle(Reasons.BUNDLE_BASENAME);
var statusCode = new StatusCode();
for (var key : bundle.keySet()) {
int code = Integer.parseInt(key);
statusCode.setCode(code);
assertThat(statusCode.getCode()).as("is not " + code).isEqualTo(code);
assertThat(statusCode.isInfo()).as(code + " is info").isEqualTo(code >= 100 && code < 200);
@ -65,8 +65,8 @@ class StatusCodeTest {
.isEqualTo(Reasons.getReasonPhrase(code));
}
final int[] unknowns = {0, 99, 600};
for (final int code : unknowns) {
int[] unknowns = {0, 99, 600};
for (var code : unknowns) {
statusCode.setCode(code);
assertThat(statusCode.getCode()).as("is not " + code).isEqualTo(code);
assertThat(statusCode.isInfo()).as(code + " is info").isFalse();

View file

@ -1,7 +1,7 @@
/*
* UtilsTest.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -59,7 +59,7 @@ class UtilsTest {
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
@Test
void testOutWrite() throws IOException {
try (StringWriter sw = new StringWriter()) {
try (var sw = new StringWriter()) {
Utils.outWrite(sw, null, "default", false);
assertThat(sw.toString()).isEqualTo("default").as("outWrite(default)");