Skip to content

Commit

Permalink
chore: jaws refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudroques committed Nov 21, 2024
1 parent 4ff99ab commit 27c22a9
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import net.sourceforge.plantuml.decoration.LinkDecor;
import net.sourceforge.plantuml.decoration.LinkType;
import net.sourceforge.plantuml.descdiagram.command.CommandLinkElement;
import net.sourceforge.plantuml.jaws.Jaws;
import net.sourceforge.plantuml.klimt.color.ColorType;
import net.sourceforge.plantuml.klimt.color.NoSuchColorException;
import net.sourceforge.plantuml.klimt.creole.Display;
Expand Down Expand Up @@ -140,7 +141,7 @@ protected CommandExecutionResult executeNow(final ActivityDiagram diagram, BlocL
urlActivity = extractUrlString(diagram, desc0);
if (urlActivity == null) {
sb.append(desc0);
sb.append(BackSlash.BS_BS_N);
sb.append(Jaws.BLOCK_E1_NEWLINE);
}
}
int i = 0;
Expand All @@ -154,21 +155,21 @@ protected CommandExecutionResult executeNow(final ActivityDiagram diagram, BlocL
}
sb.append(cs.getString());
if (i < lines.size() - 2)
sb.append(BackSlash.BS_BS_N);
sb.append(Jaws.BLOCK_E1_NEWLINE);

}

final List<String> lineLast = StringUtils.getSplit(MyPattern.cmpile(getPatternEnd()),
lines.getLast().getString());
if (StringUtils.isNotEmpty(lineLast.get(0))) {
if (sb.length() > 0 && sb.toString().endsWith(BackSlash.BS_BS_N) == false)
sb.append(BackSlash.BS_BS_N);
if (sb.length() > 0 && sb.toString().endsWith("" + Jaws.BLOCK_E1_NEWLINE) == false)
sb.append(Jaws.BLOCK_E1_NEWLINE);

sb.append(lineLast.get(0));
}

final String display = sb.toString();
final String idShort = lineLast.get(1) == null ? display : lineLast.get(1);
final String displayString = sb.toString();
final String idShort = lineLast.get(1) == null ? displayString : lineLast.get(1);

String partition = null;
if (lineLast.get(3) != null) {
Expand All @@ -183,7 +184,7 @@ protected CommandExecutionResult executeNow(final ActivityDiagram diagram, BlocL

Entity entity2 = ident.getData();
if (entity2 == null)
entity2 = diagram.reallyCreateLeaf(ident, Display.getWithNewlines(display), LeafType.ACTIVITY, null);
entity2 = diagram.reallyCreateLeaf(ident, Display.getWithNewlines(displayString), LeafType.ACTIVITY, null);

diagram.setLastEntityConsulted(entity2);

Expand Down Expand Up @@ -216,8 +217,7 @@ protected CommandExecutionResult executeNow(final ActivityDiagram diagram, BlocL
type = type.goDotted();

final LinkArg linkArg = LinkArg.build(linkLabel, lenght, diagram.getSkinParam().classAttributeIconSize() > 0);
Link link = new Link(diagram, diagram.getSkinParam().getCurrentStyleBuilder(), entity1,
entity2, type, linkArg);
Link link = new Link(diagram, diagram.getSkinParam().getCurrentStyleBuilder(), entity1, entity2, type, linkArg);
final Direction direction = StringUtils.getArrowDirection(arrowBody1 + arrowDirection + arrowBody2 + ">");
if (direction == Direction.LEFT || direction == Direction.UP)
link = link.getInv();
Expand Down
2 changes: 2 additions & 0 deletions src/net/sourceforge/plantuml/eggs/PSystemColors.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import net.sourceforge.plantuml.PlainDiagram;
import net.sourceforge.plantuml.core.DiagramDescription;
import net.sourceforge.plantuml.core.UmlSource;
import net.sourceforge.plantuml.jaws.JawsStrange;
import net.sourceforge.plantuml.klimt.UStroke;
import net.sourceforge.plantuml.klimt.UTranslate;
import net.sourceforge.plantuml.klimt.color.HColor;
Expand Down Expand Up @@ -161,6 +162,7 @@ private void drawOneHexa(UGraphic ug, String colorName, int i, int j, UPolygon h
tt.drawU(ug.apply(new UTranslate(-dimText.getWidth() / 2, -dimText.getHeight() / 2)));
}

@JawsStrange
private String findShortest(StringBounder stringBounder, UFont font, String colorName) {
String result = null;
double min = Double.MAX_VALUE;
Expand Down
13 changes: 13 additions & 0 deletions src/net/sourceforge/plantuml/jaws/JawsStrange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.sourceforge.plantuml.jaws;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.SOURCE)
@Target({ ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.TYPE, ElementType.FIELD })
public @interface JawsStrange {

String comment() default "";
}
6 changes: 6 additions & 0 deletions src/net/sourceforge/plantuml/klimt/creole/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import java.util.regex.Pattern;

import net.sourceforge.plantuml.abel.Entity;
import net.sourceforge.plantuml.jaws.Jaws;
import net.sourceforge.plantuml.jaws.JawsStrange;
import net.sourceforge.plantuml.klimt.LineBreakStrategy;
import net.sourceforge.plantuml.klimt.UStroke;
import net.sourceforge.plantuml.klimt.color.HColor;
Expand Down Expand Up @@ -251,6 +253,9 @@ else if (c2 == 'l')
current.append(c);
current.append(c2);
}
} else if (c == Jaws.BLOCK_E1_NEWLINE) {
result.add(current.toString());
current.setLength(0);
} else if (c == BackSlash.hiddenNewLine()) {
result.add(current.toString());
current.setLength(0);
Expand Down Expand Up @@ -619,6 +624,7 @@ public boolean hasSeveralGuideLines() {
return hasSeveralGuideLines(displayData);
}

@JawsStrange
public static boolean hasSeveralGuideLines(String s) {
final List<String> splitted = Arrays.asList(s.split("\\\\n"));
return hasSeveralGuideLines(splitted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ public class PlainText implements HtmlCommand {

private final String text;

public static final PlainText TEXT_BS_BS_N = new PlainText(BackSlash.BS_BS_N);
// public static final PlainText TEXT_BS_BS_N = new PlainText(BackSlash.BS_BS_N);

PlainText(String text) {
this.text = text.replaceAll("\\\\\\[", "[").replaceAll("\\\\\\]", "]");
// this.text = text.replaceAll("\\\\\\[", "[").replaceAll("\\\\\\]", "]");
this.text = text;
if (text.indexOf(BackSlash.CHAR_NEWLINE) != -1)
throw new IllegalArgumentException();

Expand All @@ -57,8 +58,4 @@ public String getText() {
assert text.length() > 0;
return text;
}

public boolean isNewline() {
return text.equals(BackSlash.BS_BS_N);
}
}
46 changes: 24 additions & 22 deletions src/net/sourceforge/plantuml/klimt/creole/command/Splitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
package net.sourceforge.plantuml.klimt.creole.command;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;

import net.sourceforge.plantuml.StringUtils;
import net.sourceforge.plantuml.emoji.Emoji;
import net.sourceforge.plantuml.jaws.JawsStrange;
import net.sourceforge.plantuml.klimt.font.FontStyle;
import net.sourceforge.plantuml.klimt.sprite.SpriteUtils;
import net.sourceforge.plantuml.regex.Matcher2;
Expand Down Expand Up @@ -147,34 +147,36 @@ public static String purgeAllTag(String s) {
return s.replaceAll(htmlTag, "");
}

@JawsStrange
public List<HtmlCommand> getHtmlCommands(boolean newLineAlone) {
final HtmlCommandFactory factory = new HtmlCommandFactory();
final List<HtmlCommand> result = new ArrayList<>();
for (String s : getSplittedInternal()) {
final HtmlCommand cmd = factory.getHtmlCommand(s);
if (newLineAlone && cmd instanceof PlainText) {
result.addAll(splitText((PlainText) cmd));
} else {
result.add(cmd);
}
// if (newLineAlone && cmd instanceof PlainText) {
// result.addAll(splitText((PlainText) cmd));
// } else {
result.add(cmd);
// }
}
return Collections.unmodifiableList(result);
}

private Collection<PlainText> splitText(PlainText cmd) {
String s = cmd.getText();
final Collection<PlainText> result = new ArrayList<>();
while (true) {
final int x = s.indexOf(PlainText.TEXT_BS_BS_N.getText());
if (x == -1) {
result.add(new PlainText(s));
return result;
}
if (x > 0) {
result.add(new PlainText(s.substring(0, x)));
}
result.add(PlainText.TEXT_BS_BS_N);
s = s.substring(x + 2);
}
}
// @JawsStrange
// private Collection<PlainText> splitText(PlainText cmd) {
// String s = cmd.getText();
// final Collection<PlainText> result = new ArrayList<>();
// while (true) {
// final int x = s.indexOf(PlainText.TEXT_BS_BS_N.getText());
// if (x == -1) {
// result.add(new PlainText(s));
// return result;
// }
// if (x > 0) {
// result.add(new PlainText(s.substring(0, x)));
// }
// result.add(PlainText.TEXT_BS_BS_N);
// s = s.substring(x + 2);
// }
// }
}
32 changes: 19 additions & 13 deletions src/net/sourceforge/plantuml/text/BackSlash.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,35 @@
import java.util.List;

import net.sourceforge.plantuml.StringUtils;
import net.sourceforge.plantuml.jaws.Jaws;
import net.sourceforge.plantuml.jaws.JawsStrange;

public class BackSlash {

@JawsStrange(comment = "Ok because not really used")
public static final String BS_BS_N = "\\n";

public static final String NEWLINE = "\n";
public static final char CHAR_NEWLINE = '\n';

public static final String lineSeparator() {
return System.lineSeparator();
}

@JawsStrange
public static char hiddenNewLine() {
return StringUtils.PRIVATE_BLOCK + BackSlash.CHAR_NEWLINE;
return Jaws.BLOCK_E1_NEWLINE;
}

@JawsStrange
public static String manageNewLine(String string) {
return string.replace(hiddenNewLine(), BackSlash.CHAR_NEWLINE);
return string;
}

public static List<String> getWithNewlines(CharSequence s) {
if (s == null)
if (s == null)
return null;

final List<String> result = new ArrayList<>();
final StringBuilder current = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
Expand All @@ -87,9 +93,9 @@ public static List<String> getWithNewlines(CharSequence s) {
}

public static String translateBackSlashes(CharSequence s) {
if (s == null)
if (s == null)
return null;

// final String tmps = s.toString();
// if (tmps.indexOf('\\') == -1) {
// return tmps;
Expand All @@ -114,24 +120,24 @@ private static boolean isEnglishLetterOfBackSlash(char c) {
}

public static String untranslateBackSlashes(CharSequence s) {
if (s == null)
if (s == null)
return null;

final StringBuilder result = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c > StringUtils.PRIVATE_BLOCK && c < '\uE07F')
if (c > StringUtils.PRIVATE_BLOCK && c < '\uE07F')
c = (char) (c - StringUtils.PRIVATE_BLOCK);

result.append(c);
}
return result.toString();
}

private static char translateChar(char c) {
if (c > 128)
if (c > 128)
throw new IllegalArgumentException();

return (char) (StringUtils.PRIVATE_BLOCK + c);
}

Expand Down

0 comments on commit 27c22a9

Please sign in to comment.