| 18 | 18 | id 'org.openjfx.javafxplugin' version '0.1.0' |
| 19 | 19 | id 'com.palantir.git-version' version '3.0.0' |
| 20 | id "com.github.spotbugs" version "5.2.4" | |
| 20 | id 'com.github.spotbugs' version '5.2.4' | |
| 21 | 21 | } |
| 22 | 22 | |
| ... | ||
| 36 | 36 | |
| 37 | 37 | maven { |
| 38 | url "https://css4j.github.io/maven" | |
| 38 | url 'https://css4j.github.io/maven' | |
| 39 | 39 | mavenContent { |
| 40 | 40 | releasesOnly() |
| ... | ||
| 53 | 53 | if (project.hasProperty( 'targetOs' )) { |
| 54 | 54 | if ('windows' == targetOs) { |
| 55 | os = ["win"] | |
| 55 | os = ['win'] | |
| 56 | 56 | } else if ('macos' == targetOs) { |
| 57 | os = ["mac"] | |
| 57 | os = ['mac'] | |
| 58 | 58 | } else { |
| 59 | 59 | os = [targetOs] |
| ... | ||
| 165 | 165 | } |
| 166 | 166 | |
| 167 | testImplementation 'org.testfx:testfx-junit5:4.0.17' | |
| 168 | 167 | testImplementation "org.junit.jupiter:junit-jupiter-api:${v_junit}" |
| 169 | 168 | testImplementation "org.junit.jupiter:junit-jupiter-params:${v_junit}" |
| 169 | testImplementation 'org.testfx:testfx-junit5:4.0.17' | |
| 170 | 170 | testImplementation 'org.assertj:assertj-core:3.24.2' |
| 171 | 171 | testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' |
| ... | ||
| 209 | 209 | compileJava { |
| 210 | 210 | options.compilerArgs += [ |
| 211 | "-Xlint:unchecked", | |
| 212 | "-Xlint:deprecation", | |
| 211 | '-Xlint:unchecked', | |
| 212 | '-Xlint:deprecation', | |
| 213 | 213 | "-Aproject=${applicationPackage}/${applicationName}" |
| 214 | 214 | ] |
| ... | ||
| 235 | 235 | |
| 236 | 236 | from { |
| 237 | (configurations.runtimeClasspath.findAll { !it.path.endsWith( ".pom" ) }) | |
| 237 | (configurations.runtimeClasspath.findAll { !it.path.endsWith( '.pom' ) }) | |
| 238 | 238 | .collect { it.isDirectory() ? it : zipTree( it ) } |
| 239 | 239 | } |
| ... | ||
| 265 | 265 | |
| 266 | 266 | tasks.withType( JavaCompile ).configureEach { |
| 267 | options.compilerArgs += "--enable-preview" | |
| 267 | options.encoding = 'UTF-8' | |
| 268 | options.compilerArgs += '--enable-preview' | |
| 268 | 269 | } |
| 269 | 270 | |
| 270 | 271 | tasks.withType( JavaExec ).configureEach { |
| 271 | jvmArgs += "--enable-preview" | |
| 272 | jvmArgs += '--enable-preview' | |
| 272 | 273 | } |
| 273 | 274 | |
| 274 | 275 | tasks.withType( Test ).configureEach { |
| 275 | jvmArgs += "--enable-preview" | |
| 276 | jvmArgs += '--enable-preview' | |
| 276 | 277 | } |
| 277 | 278 | |
| 22 | 22 | import java.nio.file.Files; |
| 23 | 23 | import java.nio.file.Path; |
| 24 | import java.nio.file.Paths; | |
| 24 | 25 | import java.text.ParseException; |
| 25 | 26 | |
| ... | ||
| 63 | 64 | return createElement( raster ); |
| 64 | 65 | } catch( final Exception ex ) { |
| 65 | clue( "Main.status.image.request.error.rasterize", ex ); | |
| 66 | clue( "Main.status.image.request.error.create", ex ); | |
| 66 | 67 | } |
| 67 | 68 | |
| ... | ||
| 108 | 109 | if( raster == null && mediaType.isImage() ) { |
| 109 | 110 | uri = resolve( source, uac, e ); |
| 110 | final var path = Path.of( uri.getPath() ); | |
| 111 | ||
| 112 | final var path = Paths.get( uri ); | |
| 111 | 113 | |
| 112 | 114 | try( final var stream = Files.newInputStream( path ) ) { |
| 113 | 115 | raster = ImageIO.read( stream ); |
| 114 | 116 | } |
| 115 | 117 | } |
| 118 | ||
| 119 | // Image path resolved; image rendered successfully. | |
| 120 | clue(); | |
| 116 | 121 | |
| 117 | 122 | return raster; |
| 118 | 123 | } |
| 119 | 124 | |
| 125 | /** | |
| 126 | * Attempt to rasterize based on file name. | |
| 127 | * | |
| 128 | * @param source The source URI from the document. | |
| 129 | * @param uac A callback for retrieving the image resource. | |
| 130 | * @param e The HTML element containing a reference to a file. | |
| 131 | * @return The resolved URI to the file. | |
| 132 | * @throws URISyntaxException Could not resolve URI. | |
| 133 | */ | |
| 120 | 134 | private static URI resolve( |
| 121 | 135 | final String source, |
| 122 | 136 | final UserAgentCallback uac, |
| 123 | 137 | final Element e ) |
| 124 | 138 | throws URISyntaxException { |
| 125 | // Attempt to rasterize based on file name. | |
| 126 | final var baseUri = new URI( uac.getBaseURL() ); | |
| 127 | final var path = baseUri.resolve( source ).normalize(); | |
| 139 | final var nSource = source.replaceAll( "\\\\", "/" ); | |
| 128 | 140 | |
| 129 | if( path.isAbsolute() ) { | |
| 130 | return path; | |
| 131 | } | |
| 132 | else { | |
| 133 | final var base = new URI( e.getBaseURI() ).getPath(); | |
| 134 | return Path.of( base, source ).toUri(); | |
| 141 | try { | |
| 142 | final var baseUri = new URI( uac.getBaseURL() ); | |
| 143 | final var resolved = baseUri.resolve( nSource ); | |
| 144 | final var path = resolved.normalize(); | |
| 145 | clue( "Main.status.image.request.resolve", path ); | |
| 146 | ||
| 147 | return path.isAbsolute() ? path : resolve( nSource, e ); | |
| 148 | } catch( final Exception ex ) { | |
| 149 | clue( "Main.status.image.request.error.resolve", nSource ); | |
| 150 | throw new URISyntaxException( nSource, ex.getMessage() ); | |
| 135 | 151 | } |
| 152 | } | |
| 153 | ||
| 154 | private static URI resolve( final String source, final Element e ) | |
| 155 | throws URISyntaxException { | |
| 156 | final var base = new URI( e.getBaseURI() ).getPath(); | |
| 157 | return Path.of( base, source ).toUri(); | |
| 136 | 158 | } |
| 137 | 159 | |
| 179 | 179 | Main.status.image.request.fetch=Downloaded image ''{0}'' |
| 180 | 180 | Main.status.image.request.success=Determined content type ''{0}'' |
| 181 | Main.status.image.request.resolve=Resolved image path: ''{0}'' | |
| 181 | 182 | Main.status.image.request.error.media=No media type for ''{0}'' |
| 182 | 183 | Main.status.image.request.error.cert=Could not accept certificate for ''{0}'' |
| 183 | Main.status.image.request.error.rasterize=Rasterizer could not parse SVG image | |
| 184 | Main.status.image.request.error.create=Could not create image for preview document | |
| 185 | Main.status.image.request.error.resolve=Could not resolve image path: ''{0}'' | |
| 184 | 186 | |
| 185 | 187 | Main.status.image.xhtml.image.download=Downloading ''{0}'' |
| 10 | 10 | |
| 11 | 11 | import java.io.File; |
| 12 | import java.io.IOException; | |
| 12 | 13 | import java.net.URI; |
| 13 | 14 | import java.nio.file.Path; |
| ... | ||
| 20 | 21 | import static com.keenwrite.constants.Constants.DOCUMENT_DEFAULT; |
| 21 | 22 | import static java.lang.String.format; |
| 22 | import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 23 | import static org.junit.jupiter.api.Assertions.assertNotNull; | |
| 23 | import static org.junit.jupiter.api.Assertions.*; | |
| 24 | 24 | |
| 25 | 25 | /** |
| ... | ||
| 32 | 32 | private static final String URI_FILE = "kitten"; |
| 33 | 33 | private static final String URI_PATH = UIR_DIR + '/' + URI_FILE; |
| 34 | private static final String PATH_KITTEN_JPG = URI_PATH + ".jpg"; | |
| 35 | private static final String PATH_KITTEN_PNG = URI_PATH + ".png"; | |
| 34 | private static final String PATH_KITTEN_PNG = STR."\{URI_PATH}.png"; | |
| 35 | private static final String PATH_KITTEN_JPG = STR."\{URI_PATH}.jpg"; | |
| 36 | 36 | |
| 37 | 37 | private static final Map<String, String> IMAGES = new LinkedHashMap<>(); |
| 38 | 38 | |
| 39 | 39 | static { |
| 40 | add( PATH_KITTEN_PNG, URI_FILE ); | |
| 41 | 40 | add( PATH_KITTEN_PNG, URI_PATH ); |
| 41 | add( PATH_KITTEN_PNG, URI_FILE ); | |
| 42 | 42 | add( PATH_KITTEN_PNG, PATH_KITTEN_PNG ); |
| 43 | 43 | add( PATH_KITTEN_JPG, PATH_KITTEN_JPG ); |
| ... | ||
| 66 | 66 | */ |
| 67 | 67 | @Test |
| 68 | void test_ImageLookup_RelativePathWithExtension_ResolvedSuccessfully() { | |
| 68 | void test_ImageLookup_RelativePathWithExtension_ResolvedSuccessfully() | |
| 69 | throws IOException { | |
| 69 | 70 | final var resource = getResourcePath( PATH_KITTEN_PNG ); |
| 70 | 71 | final var imagePath = new File( PATH_KITTEN_PNG ).toPath(); |
| ... | ||
| 88 | 89 | final var parser = pBuilder.extensions( extensions ).build(); |
| 89 | 90 | final var renderer = hBuilder.extensions( extensions ).build(); |
| 91 | final var imageFile = resolved.resolve( imagePath ).toFile(); | |
| 92 | imageFile.deleteOnExit(); | |
| 90 | 93 | |
| 94 | assertTrue( imageFile.createNewFile() ); | |
| 91 | 95 | assertNotNull( parser ); |
| 92 | 96 | assertNotNull( renderer ); |
| ... | ||
| 100 | 104 | assertEquals( expectedHtml, actualHtml ); |
| 101 | 105 | } |
| 106 | ||
| 107 | assertTrue( imageFile.delete() ); | |
| 102 | 108 | } |
| 103 | 109 | |