Search in sources :

Example 1 with ResourceLoader

use of games.strategy.triplea.ResourceLoader in project triplea by triplea-game.

the class LocalizeHtml method localizeImgLinksInHtml.

public static String localizeImgLinksInHtml(final String htmlText, final ResourceLoader resourceLoader, final String mapNameDir) {
    if (htmlText == null || (resourceLoader == null && (mapNameDir == null || mapNameDir.trim().length() == 0))) {
        return htmlText;
    }
    ResourceLoader ourResourceLoader = resourceLoader;
    String localizedHtmlText = htmlText;
    final Pattern patternTag = Pattern.compile(PATTERN_HTML_IMG_TAG);
    final Pattern patternLink = Pattern.compile(PATTERN_HTML_IMG_SRC_TAG);
    final Matcher matcherTag = patternTag.matcher(htmlText);
    Matcher matcherLink;
    while (matcherTag.find()) {
        // img tag
        final String href = matcherTag.group(1);
        if (href == null) {
            continue;
        }
        matcherLink = patternLink.matcher(href);
        while (matcherLink.find()) {
            // src link
            final String fullLink = matcherLink.group(1);
            if (fullLink != null && fullLink.length() > 2) {
                if (ourResourceLoader == null) {
                    ourResourceLoader = ResourceLoader.getMapResourceLoader(mapNameDir);
                }
                // remove quotes
                final String link = fullLink.substring(1, fullLink.length() - 1);
                // remove full parent path
                final String imageFileName = link.substring(Math.max((link.lastIndexOf("/") + 1), 0));
                // replace when testing with: "REPLACEMENTPATH/" + imageFileName;
                URL replacementUrl = ourResourceLoader.getResource(ASSET_IMAGE_FOLDER + imageFileName);
                if (replacementUrl == null || replacementUrl.toString().length() == 0) {
                    System.out.println("Could not find: " + mapNameDir + "/" + ASSET_IMAGE_FOLDER + imageFileName);
                    replacementUrl = ourResourceLoader.getResource(ASSET_IMAGE_FOLDER + ASSET_IMAGE_NOT_FOUND);
                }
                if (replacementUrl == null || replacementUrl.toString().length() == 0) {
                    System.err.println("Could not find: " + ASSET_IMAGE_FOLDER + ASSET_IMAGE_NOT_FOUND);
                    continue;
                }
                localizedHtmlText = localizedHtmlText.replaceAll(link, replacementUrl.toString());
            }
        }
    }
    return localizedHtmlText;
}
Also used : ResourceLoader(games.strategy.triplea.ResourceLoader) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) URL(java.net.URL)

Aggregations

ResourceLoader (games.strategy.triplea.ResourceLoader)1 URL (java.net.URL)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1