ImageMapParserImpl.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2019 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.handler.media.imagemap.impl;

  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import org.apache.commons.lang3.StringUtils;
  24. import org.apache.sling.api.SlingHttpServletRequest;
  25. import org.apache.sling.api.resource.Resource;
  26. import org.apache.sling.models.annotations.Model;
  27. import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
  28. import org.apache.sling.models.annotations.injectorspecific.OSGiService;
  29. import org.apache.sling.models.annotations.injectorspecific.SlingObject;
  30. import org.jetbrains.annotations.Nullable;

  31. import io.wcm.handler.media.imagemap.ImageMapArea;
  32. import io.wcm.handler.media.imagemap.ImageMapParser;
  33. import io.wcm.handler.media.spi.ImageMapLinkResolver;

  34. /**
  35.  * Creates {@link ImageMapArea} from strings.
  36.  */
  37. @Model(adaptables = {
  38.     SlingHttpServletRequest.class, Resource.class
  39. }, adapters = ImageMapParser.class)
  40. public class ImageMapParserImpl implements ImageMapParser {

  41.   @SlingObject
  42.   private Resource resource;

  43.   @OSGiService(injectionStrategy = InjectionStrategy.OPTIONAL)
  44.   @SuppressWarnings("java:S3740") // don't use generic here
  45.   private ImageMapLinkResolver linkResolver;

  46.   @Override
  47.   @SuppressWarnings({
  48.       "unchecked",
  49.       "java:S3776", "java:S135" // ignore complexity
  50.   })
  51.   public @Nullable List<ImageMapArea> parseMap(@Nullable String mapString) {
  52.     if (StringUtils.isBlank(mapString)) {
  53.       return null;
  54.     }

  55.     List<ImageMapArea> areas = new ArrayList<>();
  56.     // Parse the image map areas as defined at Image.PN_MAP
  57.     String[] areaStrings = StringUtils.split(mapString, "][");
  58.     for (String areaString : areaStrings) {
  59.       int coordinatesEndIndex = areaString.indexOf(')');
  60.       if (coordinatesEndIndex < 0) {
  61.         continue;
  62.       }
  63.       String shapeAndCoords = StringUtils.substring(areaString, 0, coordinatesEndIndex + 1);
  64.       String shape = StringUtils.substringBefore(shapeAndCoords, "(");
  65.       String coordinates = StringUtils.substringBetween(shapeAndCoords, "(", ")");
  66.       String remaining = StringUtils.substring(areaString, coordinatesEndIndex + 1);
  67.       String[] remainingTokens = StringUtils.split(remaining, "|");
  68.       if (StringUtils.isBlank(shape) || StringUtils.isBlank(coordinates)) {
  69.         continue;
  70.       }
  71.       if (remainingTokens.length > 0) {
  72.         String linkUrl = StringUtils.remove(remainingTokens[0], "\"");
  73.         String linkWindowTarget = remainingTokens.length > 1 ? StringUtils.remove(remainingTokens[1], "\"") : "";
  74.         String altText = remainingTokens.length > 2 ? StringUtils.remove(remainingTokens[2], "\"") : "";
  75.         String relativeCoordinates = remainingTokens.length > 3 ? remainingTokens[3] : "";
  76.         relativeCoordinates = StringUtils.substringBetween(relativeCoordinates, "(", ")");

  77.         // resolve and validate via link handler
  78.         Object link = null;
  79.         if (linkResolver != null) {
  80.           link = linkResolver.resolveLink(linkUrl, linkWindowTarget, resource);
  81.           if (link != null) {
  82.             linkUrl = linkResolver.getLinkUrl(link);
  83.           }
  84.         }

  85.         if (linkUrl == null || StringUtils.isBlank(linkUrl)) {
  86.           continue;
  87.         }

  88.         ImageMapArea area = new ImageMapAreaImpl(shape, coordinates,
  89.             StringUtils.trimToNull(relativeCoordinates),
  90.             link, linkUrl,
  91.             StringUtils.trimToNull(linkWindowTarget), StringUtils.trimToNull(altText));

  92.         areas.add(area);
  93.       }
  94.     }

  95.     if (areas.isEmpty()) {
  96.       return null;
  97.     }
  98.     else {
  99.       return areas;
  100.     }
  101.   }

  102. }