Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix classpath and code formatting #238

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -255,8 +255,8 @@
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${jacoco.agent.arg} -javaagent:${org.mockito:mockito-core:jar}
--add-opens java.base/java.util=ALL-UNNAMED -Xshare:off</argLine>
<argLine>${jacoco.agent.arg} -javaagent:${org.mockito:mockito-core:jar} -Xshare:off
--add-opens java.base/java.util=ALL-UNNAMED</argLine>
<environmentVariables>
<HTTP_PORT>${test.http.port}</HTTP_PORT>
</environmentVariables>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

package info.freelibrary.iiif.webrepl;

import static info.freelibrary.util.Constants.EMPTY;
import static info.freelibrary.util.Constants.EOL;

import java.util.Locale;
Expand Down Expand Up @@ -70,14 +71,13 @@ public void accept(final Diag aDiagnostic) {
myBuffer.delete(start, myBuffer.length()).delete(0, end);
}

// Add line numbers to what's returned
code = StringUtils.addLineNumbers(myBuffer.toString().trim());
code = StringUtils.addLineNumbers(myBuffer.toString().stripTrailing().replaceAll("(?m)^\\s{3,4}", EMPTY));

// Zero out the output buffer
myBuffer.setLength(0);

// Format the output and prepare to return it
myBuffer.append(INTRO).append(EOLX2).append(code).append(EOLX2).append(HEADER).append(message).append(EOL);
myBuffer.append(INTRO).append(EOLX2).append(code).append(EOLX2).append(HEADER).append(message);
}

}
48 changes: 27 additions & 21 deletions src/main/java/info/freelibrary/iiif/webrepl/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import static info.freelibrary.iiif.webrepl.Status.METHOD_NOT_ALLOWED;
import static info.freelibrary.iiif.webrepl.Status.NOT_FOUND;
import static info.freelibrary.iiif.webrepl.Status.OK;
import static info.freelibrary.util.Constants.EMPTY;
import static info.freelibrary.util.Constants.EOL;
import static info.freelibrary.util.StringUtils.addLineNumbers;
import static info.freelibrary.util.StringUtils.indent;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -166,6 +169,11 @@ static class JPv3Handler implements Handler {

// Just add a new line to distinguish between the startup import load and what follows
System.err.println();

// Add the executable jar with JPv3's code to JShell's Classpath
myShell.addToClasspath(
URLDecoder.decode(Server.class.getProtectionDomain().getCodeSource().getLocation().getPath(),
StandardCharsets.UTF_8));
}

@Override
Expand Down Expand Up @@ -193,7 +201,7 @@ private String decodeSubmission(final byte[] aSubmission) {
}

// If submission wasn't valid, just return an empty string which will evaluate to nothing
return Constants.EMPTY;
return EMPTY;
}

/**
Expand Down Expand Up @@ -241,7 +249,7 @@ private Response getResponse(final info.freelibrary.iiif.webrepl.Status aEnum, f
private Response handleGet(final Request aRequest) {
final String uri = aRequest.uri();

System.err.println(aRequest.method() + Constants.SPACE + uri + Constants.EOL);
System.err.println(aRequest.method() + Constants.SPACE + uri + EOL);

if (!EDITOR_PATTERN.matcher(uri).find()) {
return getResponse(NOT_FOUND, TEXT_CONTENT_TYPE, EMPTY_BODY);
Expand Down Expand Up @@ -272,42 +280,39 @@ private Response handlePost(final Request aRequest) {

body = decodeSubmission(aRequest.body()).trim();

System.err.println("Body: " + Constants.EOL + indent(StringUtils.addLineNumbers(body), 2));
System.err.println("Body: " + EOL + indent(addLineNumbers(body), 2));

try {
myShell.eval(getCode(body)).forEach(event -> {
final Status status = event.status();

switch (status) {
case VALID -> {
// The submitted code is valid, so get its result to write out
final SnippetEvent output = myShell.eval(MAIN_METHOD).get(0);

// Before returning the output though, log the VALID status
System.err.println(Constants.EOL + STATUS_LABEL + status);

if (Status.VALID.equals(output.status())) {
System.out.println(output.value());
}

System.err.println(STATUS_LABEL + status + EOL);
}
case REJECTED, RECOVERABLE_DEFINED -> {
final Snippet snippet = event.snippet();
final StringBuilder buffer = new StringBuilder(snippet.source());

// Just check one at a time, and let the editor iterate
// The diagnostic consumer adds line numbers to the code in the output
myShell.diagnostics(snippet).findFirst().ifPresentOrElse(new DiagnosticConsumer(buffer),
() -> buffer.delete(0, buffer.length())
.append(new ParsingError(StringUtils.addLineNumbers(body))));
() -> buffer.delete(0, buffer.length()).append(new ParsingError(body)));
try {
final String output = buffer.toString().trim();
final String output = buffer.toString();

// Write correctly formatted code to the user
myOutputStream.write(output.getBytes(StandardCharsets.UTF_8));

// Wrap the user's response in another format
System.err.println("Error: ");
System.err.println(indent(reformat(output), 2));
System.err.println(Constants.EOL + STATUS_LABEL + status + Constants.EOL);
// Wrap the user's response in another format for writing to StdErr
System.err.println(EOL + "Error: ");
System.err.println(indent(addLineNumbers(stripLineNumbers(output)), 2));
System.err.println(EOL + STATUS_LABEL + status + EOL);
} catch (final IOException details) {
System.err.println(details);
} finally {
Expand Down Expand Up @@ -348,14 +353,15 @@ private Response handlePost(final Request aRequest) {
}

/**
* Strips line numbers from a user error message's code block so that they can be re-added across the whole
* message.
* Strips line numbers from the front of each line in a multi-line string. This is used when only part of a
* string has line numbers, but we want to number all the lines in the string. We strip the old line numbers,
* and then add new line numbers.
*
* @param aMessage An error message that's being returned to the user
* @return An error message that has reformatted what was returned to the user
* @param aString A multi-line string from which to strip line numbers
* @return A multi-line string without line numbers
*/
private String reformat(final String aMessage) {
return StringUtils.addLineNumbers(aMessage.replaceAll("(?m)^\\d+\\s+(?=[A-Za-z])", Constants.EMPTY));
private String stripLineNumbers(final String aString) {
return aString.replaceAll("(?m)^\\d+\\s", " ");
}

/**
Expand Down
Loading