Skip to content

Commit

Permalink
Supporting Enum + Applying SonarCloud recommendations (#38)
Browse files Browse the repository at this point in the history
* Supporting Enum + Applying SonarCloud recommendations

* Rectification + redimentioning
  • Loading branch information
mohamed-ennahdi authored Jan 17, 2025
1 parent 9eae455 commit 57e9749
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.github.mohamedennahdi.objectmorph.app.exception;

public class ValidationException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;

public ValidationException() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -36,20 +37,13 @@ public String generateHTML(SourceCodeDto[] sourceCodes) throws Exception {
validateFilenames(sourceCodes);
String path = System.getProperty("user.home");
for (SourceCodeDto sourceCode: sourceCodes) {
sourceCode.setSourceCode(URLDecoder.decode(sourceCode.getSourceCode(), Charset.forName("UTF-8")));
sourceCode.setSourceCode(URLDecoder.decode(sourceCode.getSourceCode(), StandardCharsets.UTF_8));
String fileName = sourceCode.getFilename();
log.info("Filename: " + fileName);
File file = new File(path + File.separator + fileName);
try (Writer fileWriter = new FileWriter(file, false)) {
fileWriter.write(sourceCode.getSourceCode());
} catch (Exception e) {
throw e;
}
File file = saveFile(path + File.separator + fileName, sourceCode);
files.add(file);
}
return objectmorphLogic.getHtmlGenerator(files.toArray(new File[0])).generateFullHTML();
} catch (Exception e) {
throw e;
} finally {
log.info("Files removal.");
for (File file : files) {
Expand All @@ -60,9 +54,17 @@ public String generateHTML(SourceCodeDto[] sourceCodes) throws Exception {

private void validateFilenames(SourceCodeDto[] sourceCodes) throws ValidationException {
List<String> filenames = Arrays.asList(sourceCodes).stream().map(n -> n.getFilename()).collect(Collectors.toList());
Set<String> uniqueFilenames = new HashSet<String>(filenames);
Set<String> uniqueFilenames = new HashSet<>(filenames);
if (uniqueFilenames.size() != filenames.size()) {
throw new ValidationException("File names must be unique.");
}
}

private File saveFile(String path, SourceCodeDto sourceCode) throws IOException {
File file = new File(path);
try (Writer fileWriter = new FileWriter(file, false)) {
fileWriter.write(sourceCode.getSourceCode());
}
return file;
}
}
17 changes: 8 additions & 9 deletions objectmorph-app/src/main/resources/public/client.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Objectmorph-Sample-Client</title>
Expand All @@ -20,14 +20,14 @@ <h3>
<h4>
<i>Executable locally.</i>
</h4>
<div align="center">
<div style="text-align:center;" >
<textarea id="notepad" placeholder="drag and drop Java source code files here" cols="64" rows="16" ></textarea>
</div>
</body>
<footer >
<div align="right" >
<p align="right" >
Version: <b><label id="paragraphVersion" ></label></b>
<div style="text-align:right;" >
<p style="text-align:right;" >
Version: <b><span id="paragraphVersion" ></span></b>
</p>
</div>
</footer>
Expand Down Expand Up @@ -66,18 +66,17 @@ <h4>
if (this.status == 200) {
console.log(this.responseText);
const winUrl = URL.createObjectURL(new Blob([this.responseText], { type: "text/html" }));
const win = window.open(winUrl, "win", `width=512,height=512`);
window.open(winUrl, "win", `width=512,height=768`);
} else {
alert('Error: ' + this.status + " " + this.responseText);
console.log('Error: ' + this.status + " " + this.responseText);
}
};
}
var url = location.protocol + '//' + location.host + "/html";
let url = location.protocol + '//' + location.host + "/html";
xhttp.open("POST", url, true);
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
var data = new FormData();
data = JSON.stringify(file);
var data = JSON.stringify(file);
xhttp.send(data);
}
</script>
Expand Down
6 changes: 3 additions & 3 deletions objectmorph-app/src/main/resources/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ <h1>ObjectMorph application <i>(Beta version)</i></h1>
</div>
</body>
<footer>
<div align="right">
<p align="right">
Version: <b><label id="paragraphVersion"></label></b>
<div style="text-align:right;">
<p style="text-align:right;">
Version: <b><span id="paragraphVersion"></span></b>
</p>
</div>
</footer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.ConstructorDeclaration;
import com.github.javaparser.ast.body.EnumConstantDeclaration;
import com.github.javaparser.ast.body.EnumDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;

Expand All @@ -21,19 +26,16 @@ public class JavaClassInterpreter {


private ClassOrInterfaceDeclaration decl;
private EnumDeclaration enumDecl;

private String className;
private String packageName;

private static int INSTANCE_ID = 0;

private int instanceId;
private static int instanceId = 0;

public JavaClassInterpreter(File myClassSourceFile) throws FileNotFoundException, ParseException {

INSTANCE_ID ++;

this.instanceId = INSTANCE_ID;
instanceId ++;

JavaParser parser = new JavaParser();

Expand All @@ -56,6 +58,13 @@ public JavaClassInterpreter(File myClassSourceFile) throws FileNotFoundException
optional = cu.getInterfaceByName(this.className);
if (optional.isPresent()) {
this.decl = optional.get();
} else {
Optional<EnumDeclaration> enumOptional = cu.getEnumByName(this.className);
if (enumOptional.isPresent()) {
this.enumDecl = enumOptional.get();
} else {
throw new UnsupportedOperationException();
}
}
}
}
Expand All @@ -70,36 +79,67 @@ public String getClassName() {
}

public List<FieldDeclaration> getFields() {
return decl.getFields();
if (Objects.isNull(this.decl)) {
return new ArrayList<>();
} else {
return decl.getFields();
}
}

public NodeList<EnumConstantDeclaration> getEntries() {
if (Objects.isNull(this.enumDecl)) {
return new NodeList<>();
} else {
return enumDecl.getEntries();
}
}

public List<ConstructorDeclaration> getConstructors() {
if (Objects.isNull(this.decl)) {
return new ArrayList<>();
}
return decl.getConstructors();
}

public List<MethodDeclaration> getMethods() {
if (Objects.isNull(this.decl)) {
return new ArrayList<>();
}
return decl.getMethods();
}

public String getSuperClassName() {
if (decl.getExtendedTypes().size() > 0)
if (Objects.isNull(this.decl)) {
return "";
}
if (decl.getExtendedTypes().isNonEmpty())
return decl.getExtendedTypes(0).getNameAsString();
else return "";
}

public boolean isInterface() {
if (Objects.isNull(decl)) {
return false;
}
return decl.isInterface();
}

public boolean isEnum() {
if (Objects.isNull(enumDecl)) {
return false;
}
return enumDecl.isEnumDeclaration();
}

public String getPackageName() {
return packageName;
}

public int getInstanceId() {
return this.instanceId;
return instanceId;
}

public void resetInstanceId() {
INSTANCE_ID = 0;
public static void resetInstanceId() {
instanceId = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ public HTMLGenerator(File... sourceCodeClasses) throws Exception {
}
}

public String generateFullHTML() throws FileNotFoundException, URISyntaxException {
String draggableScript = "";
public String generateFullHTML() {
StringBuilder draggableScript = new StringBuilder();
short top = 0;
for (JavaClassInterpreter interpreter : interpreters) {
draggableScript += "\n var draggable" + interpreter.getInstanceId() +" = new PlainDraggable(document.getElementById('"+ interpreter.getClassName() +"'), {onMove: fixLine});";
draggableScript += "\n draggable" + interpreter.getInstanceId() +".top = "+ (top += 128) +";";
draggableScript.append("\n var draggable").append(interpreter.getInstanceId()).append(" = new PlainDraggable(document.getElementById('").append(interpreter.getClassName()).append("'), {onMove: fixLine});");
draggableScript.append("\n draggable").append(interpreter.getInstanceId()).append(".top = ").append(top).append(";");
top += 128;
}

List<Relation> relations = new ArrayList<>();
Expand All @@ -64,26 +65,27 @@ public String generateFullHTML() throws FileNotFoundException, URISyntaxExceptio
relations = new ArrayList<>(set);


String lineScript = "";
String fixeLineScript = "function fixLine() {";
StringBuilder lineScript = new StringBuilder();
StringBuilder fixeLineScript = new StringBuilder("function fixLine() {");
for (Relation relation : relations) {
String varName = "line" + relation.getInstanceId();
lineScript += "\n var " + varName + " = new LeaderLine(document.getElementById('" + relation.getFrom() + "'), document.getElementById('" + relation.getTo() + "'), " +
lineScript.append("\n var ").append(varName).append(" = new LeaderLine(document.getElementById('").append(relation.getFrom()).append("'), document.getElementById('").append(relation.getTo()).append("'), ");
lineScript.append(
(switch (relation.getLinkType()) {
case GENERALIZATION -> Relation.GENERALIZATION_PROPERTIES_SCRIPT;
case ASSOCIATION -> Relation.ASSOCIATION_PROPERTIES_SCRIPT;
case UNARY -> Relation.UNARY_PROPERTIES_SCRIPT;
default -> throw new IllegalArgumentException("Unexpected value: " + relation.getLinkType());
});
fixeLineScript += varName + ".position(); ";
}));
fixeLineScript.append(varName).append(".position(); ");
}
fixeLineScript += " } ";
fixeLineScript.append(" } ");

if (!relations.isEmpty()) {
relations.get(0).resetInstanceId();
Relation.resetInstanceId();
}
if (!interpreters.isEmpty()) {
interpreters.get(0).resetInstanceId();
JavaClassInterpreter.resetInstanceId();
}

return html(
Expand All @@ -95,7 +97,7 @@ public String generateFullHTML() throws FileNotFoundException, URISyntaxExceptio
body(
div(
tables.toArray(new TableTag[0])
).withStyle("background-color: gray; margin:0 auto; width: 1080px; height: 768px; border:1px solid black;"),
).withStyle("background-color: gray; margin:0 auto; width: 1536px; height: 768px; border:1px solid black;"),
script(
draggableScript + " \n" + lineScript + "\n " + fixeLineScript
)
Expand Down
Loading

0 comments on commit 57e9749

Please sign in to comment.