| Author | DaveJarvis <email> |
|---|---|
| Date | 2022-12-17 17:52:22 GMT-0800 |
| Commit | f514a1a1af45a7681864e91857385c5a34831659 |
| Parent | 8d794de |
| Delta | 57 lines added, 102 lines removed, 45-line decrease |
|---|
| -/* Copyright 2022 White Magic Software, Ltd. -- All rights reserved. */ | ||
| -package com.keenwrite.io; | ||
| - | ||
| -import com.keenwrite.io.downloads.DownloadManager; | ||
| - | ||
| -import java.io.BufferedInputStream; | ||
| -import java.io.Closeable; | ||
| -import java.io.IOException; | ||
| -import java.net.URI; | ||
| -import java.net.URL; | ||
| -import java.net.URLConnection; | ||
| - | ||
| -import static com.keenwrite.events.StatusEvent.clue; | ||
| -import static com.keenwrite.io.downloads.DownloadManager.DownloadToken; | ||
| - | ||
| -/** | ||
| - * Responsible for making HTTP requests, a thin wrapper around the | ||
| - * {@link URLConnection} class. This will attempt to use compression. | ||
| - */ | ||
| -public class HttpFacade { | ||
| - | ||
| - public static Response httpGet( final String url ) throws IOException { | ||
| - return httpGet( url, MediaType.UNDEFINED ); | ||
| - } | ||
| - | ||
| - /** | ||
| - * Sends an HTTP GET request to a server. | ||
| - * | ||
| - * @param url The remote resource to fetch. | ||
| - * @return The server response. | ||
| - */ | ||
| - public static Response httpGet( final URL url, final MediaType mediaType ) | ||
| - throws IOException { | ||
| - return new Response( url, mediaType ); | ||
| - } | ||
| - | ||
| - /** | ||
| - * Convenience method to send an HTTP GET request to a server. | ||
| - * | ||
| - * @param uri The remote resource to fetch. | ||
| - * @return The server response. | ||
| - * @see #httpGet(URL, MediaType) | ||
| - */ | ||
| - public static Response httpGet( final URI uri, final MediaType mediaType ) | ||
| - throws IOException { | ||
| - return httpGet( uri.toURL(), mediaType ); | ||
| - } | ||
| - | ||
| - /** | ||
| - * Convenience method to send an HTTP GET request to a server. | ||
| - * | ||
| - * @param url The remote resource to fetch. | ||
| - * @return The server response. | ||
| - * @see #httpGet(URL, MediaType) | ||
| - */ | ||
| - public static Response httpGet( final String url, final MediaType mediaType ) | ||
| - throws IOException { | ||
| - return httpGet( new URL( url ), mediaType ); | ||
| - } | ||
| - | ||
| - /** | ||
| - * Callers are responsible for closing the response. | ||
| - */ | ||
| - public static final class Response implements Closeable { | ||
| - private final DownloadToken mDownloadToken; | ||
| - | ||
| - private Response( final URL url, final MediaType mediaType ) | ||
| - throws IOException { | ||
| - assert url != null; | ||
| - | ||
| - clue( "Main.status.image.request.init" ); | ||
| - | ||
| - mDownloadToken = DownloadManager.open( url, mediaType ); | ||
| - } | ||
| - | ||
| - @Override | ||
| - public void close() throws IOException { | ||
| - mDownloadToken.close(); | ||
| - } | ||
| - | ||
| - public BufferedInputStream getInputStream() { | ||
| - return mDownloadToken.getInputStream(); | ||
| - } | ||
| - | ||
| - public boolean isMediaType( final MediaType mediaType ) { | ||
| - return mDownloadToken.isMediaType( mediaType ); | ||
| - } | ||
| - | ||
| - public MediaType getMediaType() { | ||
| - return mDownloadToken.getMediaType(); | ||
| - } | ||
| - } | ||
| -} | ||
| */ | ||
| private static int[] ints( final int... data ) { | ||
| - final var magic = new int[ FORMAT_LENGTH ]; | ||
| + final var magic = new int[ FORMAT_LENGTH + 1 ]; | ||
| int i = -1; | ||
| import java.io.*; | ||
| import java.net.HttpURLConnection; | ||
| +import java.net.URI; | ||
| import java.net.URL; | ||
| import java.time.Duration; | ||
| import java.util.concurrent.Callable; | ||
| import java.util.zip.GZIPInputStream; | ||
| +import static com.keenwrite.events.StatusEvent.clue; | ||
| import static java.lang.Boolean.*; | ||
| import static java.lang.Math.toIntExact; | ||
| : is | ||
| ); | ||
| + } | ||
| + | ||
| + public static DownloadToken httpGet( final String url ) throws IOException { | ||
| + return httpGet( url, MediaType.UNDEFINED ); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Sends an HTTP GET request to a server. | ||
| + * | ||
| + * @param url The remote resource to fetch. | ||
| + * @return The server response. | ||
| + */ | ||
| + public static DownloadToken httpGet( final URL url, final MediaType mediaType ) | ||
| + throws IOException { | ||
| + clue( "Main.status.image.request.init" ); | ||
| + | ||
| + return open( url, mediaType ); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Convenience method to send an HTTP GET request to a server. | ||
| + * | ||
| + * @param uri The remote resource to fetch. | ||
| + * @return The server response. | ||
| + * @see DownloadManager#httpGet(URL, MediaType) | ||
| + */ | ||
| + public static DownloadToken httpGet( final URI uri, final MediaType mediaType ) | ||
| + throws IOException { | ||
| + return httpGet( uri.toURL(), mediaType ); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Convenience method to send an HTTP GET request to a server. | ||
| + * | ||
| + * @param url The remote resource to fetch. | ||
| + * @return The server response. | ||
| + * @see DownloadManager#httpGet(URL, MediaType) | ||
| + */ | ||
| + public static DownloadToken httpGet( final String url, final MediaType mediaType ) | ||
| + throws IOException { | ||
| + return httpGet( new URL( url ), mediaType ); | ||
| } | ||
| } | ||
| import static com.keenwrite.events.StatusEvent.clue; | ||
| -import static com.keenwrite.io.HttpFacade.httpGet; | ||
| +import static com.keenwrite.io.downloads.DownloadManager.httpGet; | ||
| import static com.keenwrite.io.MediaType.IMAGE_SVG_XML; | ||
| import static com.keenwrite.preview.MathRenderer.MATH_RENDERER; |
| import static com.keenwrite.dom.DocumentParser.visit; | ||
| import static com.keenwrite.events.StatusEvent.clue; | ||
| -import static com.keenwrite.io.HttpFacade.httpGet; | ||
| +import static com.keenwrite.io.downloads.DownloadManager.httpGet; | ||
| import static com.keenwrite.util.ProtocolScheme.getProtocol; | ||
| import static com.whitemagicsoftware.keenquotes.lex.FilterType.FILTER_XML; |
| import javafx.scene.control.TreeItem; | ||
| import javafx.stage.Stage; | ||
| +import org.assertj.core.util.Files; | ||
| import org.testfx.framework.junit5.Start; | ||
| final var transformer = new YamlTreeTransformer(); | ||
| final var editor = new DefinitionEditor( transformer ); | ||
| + final var file = Files.newTemporaryFile(); | ||
| final var tabPane1 = new DetachableTabPane(); | ||
| tabPane1.addTab( "Editor", editor ); | ||
| final var tabPane2 = new DetachableTabPane(); | ||
| final var tab21 = | ||
| tabPane2.addTab( "Picker", new ColorPicker() ); | ||
| final var tab22 = | ||
| - tabPane2.addTab( "Editor", new MarkdownEditor( workspace ) ); | ||
| + tabPane2.addTab( "Editor", new MarkdownEditor( file, workspace ) ); | ||
| tab21.setTooltip( new Tooltip( "Colour Picker" ) ); | ||
| tab22.setTooltip( new Tooltip( "Text Editor" ) ); | ||
| import com.keenwrite.AwaitFxExtension; | ||
| import com.keenwrite.preferences.Workspace; | ||
| +import org.assertj.core.util.Files; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.testfx.framework.junit5.ApplicationExtension; | ||
| +import java.io.File; | ||
| import java.util.regex.Pattern; | ||
| import static java.util.regex.Pattern.compile; | ||
| import static javafx.application.Platform.runLater; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| @ExtendWith( {ApplicationExtension.class, AwaitFxExtension.class} ) | ||
| public class MarkdownEditorTest { | ||
| + private static final File TEMP_FILE = Files.newTemporaryFile(); | ||
| + | ||
| private static final String[] WORDS = new String[]{ | ||
| "Italicize", | ||
| private MarkdownEditor createMarkdownEditor() { | ||
| final var workspace = new Workspace(); | ||
| - final var editor = new MarkdownEditor( workspace ); | ||
| + final var editor = new MarkdownEditor( TEMP_FILE, workspace ); | ||
| editor.setText( TEXT ); | ||
| return editor; | ||
| import org.junit.jupiter.api.Test; | ||
| -import java.net.URI; | ||
| import java.util.Map; | ||
| -import static com.keenwrite.io.HttpFacade.httpGet; | ||
| import static com.keenwrite.io.MediaType.*; | ||
| +import static com.keenwrite.io.downloads.DownloadManager.httpGet; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
| map.forEach( ( k, v ) -> { | ||
| - try( var response = httpGet( new URI( k ) ) ) { | ||
| + try( var response = httpGet( k ) ) { | ||
| assertEquals( v, response.getMediaType() ); | ||
| } catch( Exception e ) { | ||
| import com.whitemagicsoftware.tex.graphics.SvgDomGraphics2D; | ||
| import com.whitemagicsoftware.tex.graphics.SvgGraphics2D; | ||
| -import org.apache.batik.transcoder.TranscoderException; | ||
| +import io.sf.carte.echosvg.transcoder.TranscoderException; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.xml.sax.SAXException; |