1515
1616final class ImageViewContentParser extends AbstractParser {
1717
18- private static final String PLANTUML_GRAMMAR = "plantuml <file|url|viewKey>" ;
19- private static final String MERMAID_GRAMMAR = "mermaid <file|url|viewKey>" ;
20- private static final String KROKI_GRAMMAR = "kroki <format> <file|url>" ;
18+ private static final String PLANTUML_GRAMMAR = "plantuml <source| file|url|viewKey>" ;
19+ private static final String MERMAID_GRAMMAR = "mermaid <source| file|url|viewKey>" ;
20+ private static final String KROKI_GRAMMAR = "kroki <format> <source| file|url>" ;
2121 private static final String IMAGE_GRAMMAR = "image <file|url>" ;
2222
2323 private static final int PLANTUML_SOURCE_INDEX = 1 ;
@@ -33,7 +33,7 @@ final class ImageViewContentParser extends AbstractParser {
3333 }
3434
3535 void parsePlantUML (ImageViewDslContext context , File dslFile , Tokens tokens ) {
36- // plantuml <file|url|viewKey>
36+ // plantuml <source| file|url|viewKey>
3737
3838 if (tokens .hasMoreThan (PLANTUML_SOURCE_INDEX )) {
3939 throw new RuntimeException ("Too many tokens, expected: " + PLANTUML_GRAMMAR );
@@ -45,22 +45,31 @@ void parsePlantUML(ImageViewDslContext context, File dslFile, Tokens tokens) {
4545 String source = tokens .get (PLANTUML_SOURCE_INDEX );
4646
4747 try {
48- View viewWithKey = context .getWorkspace ().getViews ().getViewWithKey (source );
49- if (viewWithKey instanceof ModelView ) {
50- StructurizrPlantUMLExporter exporter = new StructurizrPlantUMLExporter ();
51- String plantuml = exporter .export ((ModelView )viewWithKey ).getDefinition ();
52- new PlantUMLImporter ().importDiagram (context .getView (), plantuml );
48+ if (source .contains ("\n " )) {
49+ // inline source
50+ new PlantUMLImporter ().importDiagram (context .getView (), source );
5351 } else {
54- if (Url .isUrl (source )) {
55- RemoteContent content = readFromUrl (source );
56- new PlantUMLImporter ().importDiagram (context .getView (), content .getContent ());
57- context .getView ().setTitle (source .substring (source .lastIndexOf ("/" ) + 1 ));
52+ View viewWithKey = context .getWorkspace ().getViews ().getViewWithKey (source );
53+ if (viewWithKey instanceof ModelView ) {
54+ StructurizrPlantUMLExporter exporter = new StructurizrPlantUMLExporter ();
55+ String plantuml = exporter .export ((ModelView ) viewWithKey ).getDefinition ();
56+ new PlantUMLImporter ().importDiagram (context .getView (), plantuml );
5857 } else {
59- if (!restricted ) {
60- File file = new File (dslFile .getParentFile (), source );
61- new PlantUMLImporter ().importDiagram (context .getView (), file );
58+ if (Url .isUrl (source )) {
59+ RemoteContent content = readFromUrl (source );
60+ new PlantUMLImporter ().importDiagram (context .getView (), content .getContent ());
61+ context .getView ().setTitle (source .substring (source .lastIndexOf ("/" ) + 1 ));
6262 } else {
63- throw new RuntimeException ("PlantUML source must be specified as a URL when running in restricted mode" );
63+ if (!restricted ) {
64+ File file = new File (dslFile .getParentFile (), source );
65+ if (file .exists ()) {
66+ new PlantUMLImporter ().importDiagram (context .getView (), file );
67+ } else {
68+ throw new RuntimeException ("The file at " + file .getAbsolutePath () + " does not exist" );
69+ }
70+ } else {
71+ throw new RuntimeException ("PlantUML source must be specified as a URL when running in restricted mode" );
72+ }
6473 }
6574 }
6675 }
@@ -74,7 +83,7 @@ void parsePlantUML(ImageViewDslContext context, File dslFile, Tokens tokens) {
7483 }
7584
7685 void parseMermaid (ImageViewDslContext context , File dslFile , Tokens tokens ) {
77- // mermaid <file|url|viewKey>
86+ // mermaid <source| file|url|viewKey>
7887
7988 if (tokens .hasMoreThan (MERMAID_SOURCE_INDEX )) {
8089 throw new RuntimeException ("Too many tokens, expected: " + MERMAID_GRAMMAR );
@@ -86,22 +95,31 @@ void parseMermaid(ImageViewDslContext context, File dslFile, Tokens tokens) {
8695 String source = tokens .get (MERMAID_SOURCE_INDEX );
8796
8897 try {
89- View viewWithKey = context .getWorkspace ().getViews ().getViewWithKey (source );
90- if (viewWithKey instanceof ModelView ) {
91- MermaidDiagramExporter exporter = new MermaidDiagramExporter ();
92- String mermaid = exporter .export ((ModelView )viewWithKey ).getDefinition ();
93- new MermaidImporter ().importDiagram (context .getView (), mermaid );
98+ if (source .contains ("\n " )) {
99+ // inline source
100+ new MermaidImporter ().importDiagram (context .getView (), source );
94101 } else {
95- if (Url .isUrl (source )) {
96- RemoteContent content = readFromUrl (source );
97- new MermaidImporter ().importDiagram (context .getView (), content .getContent ());
98- context .getView ().setTitle (source .substring (source .lastIndexOf ("/" ) + 1 ));
102+ View viewWithKey = context .getWorkspace ().getViews ().getViewWithKey (source );
103+ if (viewWithKey instanceof ModelView ) {
104+ MermaidDiagramExporter exporter = new MermaidDiagramExporter ();
105+ String mermaid = exporter .export ((ModelView ) viewWithKey ).getDefinition ();
106+ new MermaidImporter ().importDiagram (context .getView (), mermaid );
99107 } else {
100- if (!restricted ) {
101- File file = new File (dslFile .getParentFile (), source );
102- new MermaidImporter ().importDiagram (context .getView (), file );
108+ if (Url .isUrl (source )) {
109+ RemoteContent content = readFromUrl (source );
110+ new MermaidImporter ().importDiagram (context .getView (), content .getContent ());
111+ context .getView ().setTitle (source .substring (source .lastIndexOf ("/" ) + 1 ));
103112 } else {
104- throw new RuntimeException ("Mermaid source must be specified as a URL when running in restricted mode" );
113+ if (!restricted ) {
114+ File file = new File (dslFile .getParentFile (), source );
115+ if (file .exists ()) {
116+ new MermaidImporter ().importDiagram (context .getView (), file );
117+ } else {
118+ throw new RuntimeException ("The file at " + file .getAbsolutePath () + " does not exist" );
119+ }
120+ } else {
121+ throw new RuntimeException ("Mermaid source must be specified as a URL when running in restricted mode" );
122+ }
105123 }
106124 }
107125 }
@@ -115,7 +133,7 @@ void parseMermaid(ImageViewDslContext context, File dslFile, Tokens tokens) {
115133 }
116134
117135 void parseKroki (ImageViewDslContext context , File dslFile , Tokens tokens ) {
118- // kroki <format> <file|url>
136+ // kroki <format> <source| file|url>
119137
120138 if (tokens .hasMoreThan (KROKI_SOURCE_INDEX )) {
121139 throw new RuntimeException ("Too many tokens, expected: " + KROKI_GRAMMAR );
@@ -128,16 +146,25 @@ void parseKroki(ImageViewDslContext context, File dslFile, Tokens tokens) {
128146 String source = tokens .get (KROKI_SOURCE_INDEX );
129147
130148 try {
131- if (Url .isUrl (source )) {
132- RemoteContent content = readFromUrl (source );
133- new KrokiImporter ().importDiagram (context .getView (), format , content .getContent ());
134- context .getView ().setTitle (source .substring (source .lastIndexOf ("/" )+1 ));
149+ if (source .contains ("\n " )) {
150+ // inline source
151+ new KrokiImporter ().importDiagram (context .getView (), format , source );
135152 } else {
136- if (!restricted ) {
137- File file = new File (dslFile .getParentFile (), source );
138- new KrokiImporter ().importDiagram (context .getView (), format , file );
153+ if (Url .isUrl (source )) {
154+ RemoteContent content = readFromUrl (source );
155+ new KrokiImporter ().importDiagram (context .getView (), format , content .getContent ());
156+ context .getView ().setTitle (source .substring (source .lastIndexOf ("/" ) + 1 ));
139157 } else {
140- throw new RuntimeException ("Kroki source must be specified as a URL when running in restricted mode" );
158+ if (!restricted ) {
159+ File file = new File (dslFile .getParentFile (), source );
160+ if (file .exists ()) {
161+ new KrokiImporter ().importDiagram (context .getView (), format , file );
162+ } else {
163+ throw new RuntimeException ("The file at " + file .getAbsolutePath () + " does not exist" );
164+ }
165+ } else {
166+ throw new RuntimeException ("Kroki source must be specified as a URL when running in restricted mode" );
167+ }
141168 }
142169 }
143170 } catch (Exception e ) {
@@ -168,8 +195,12 @@ void parseImage(ImageViewDslContext context, File dslFile, Tokens tokens) {
168195 } else {
169196 if (!restricted ) {
170197 File file = new File (dslFile .getParentFile (), source );
171- context .getView ().setContent (ImageUtils .getImageAsDataUri (file ));
172- context .getView ().setTitle (file .getName ());
198+ if (file .exists ()) {
199+ context .getView ().setContent (ImageUtils .getImageAsDataUri (file ));
200+ context .getView ().setTitle (file .getName ());
201+ } else {
202+ throw new RuntimeException ("The file at " + file .getAbsolutePath () + " does not exist" );
203+ }
173204 } else {
174205 throw new RuntimeException ("Images must be specified as a URL when running in restricted mode" );
175206 }
0 commit comments