All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.squarespace.template.plugins.PluginUtils Maven / Gradle / Ivy

/**
 * Copyright (c) 2014 SQUARESPACE, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.squarespace.template.plugins;

import static com.squarespace.template.Patterns.WHITESPACE_RE;
import static java.util.regex.Pattern.CASE_INSENSITIVE;
import static java.util.regex.Pattern.MULTILINE;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import java.util.regex.Pattern;

import com.squarespace.cldrengine.CLDR;
import com.squarespace.cldrengine.api.CurrencyFormatOptions;
import com.squarespace.cldrengine.api.CurrencyType;
import com.squarespace.cldrengine.api.Decimal;


public class PluginUtils {

  private static final Pattern SLUG_KILLCHARS = Pattern.compile("[^a-zA-Z0-9\\s-]+");

  private static final Pattern SCRIPT_TAG = Pattern.compile("= '0' && ch <= '9') {
      return ch - '0';
    } else if (ch >= 'a' && ch <= 'f') {
      return ch - 'a' + 10;
    } else if (ch >= 'A' && ch <= 'F') {
      return ch - 'A' + 10;
    }
    return -1;
  }

  /**
   * Escape instances of HTML script tags.
   */
  public static String escapeScriptTags(String str) {
    return SCRIPT_TAG.matcher(str).replaceAll("<\\\\/");
  }

  public static void escapeHtml(String str, StringBuilder buf) {
    int length = str.length();
    for (int i = 0; i < length; i++) {
      char ch = str.charAt(i);
      switch (ch) {
        case '&':
          buf.append("&");
          break;
        case '<':
          buf.append("<");
          break;
        case '>':
          buf.append(">");
          break;
        default:
          buf.append(ch);
      }
    }
  }

  public static void escapeHtmlAttribute(String str, StringBuilder buf) {
    int length = str.length();
    for (int i = 0; i < length; i++) {
      char ch = str.charAt(i);
      switch (ch) {
        case '&':
          buf.append("&");
          break;
        case '<':
          buf.append("<");
          break;
        case '>':
          buf.append(">");
          break;
        case '"':
          buf.append(""");
          break;
        default:
          buf.append(ch);
      }
    }
  }

  public static String formatMoney(Decimal input, Locale locale) {
    double cents = Double.parseDouble(input.toString());
    cents /= 100;
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
    DecimalFormat format = new DecimalFormat("#,##0.00", symbols);
    return format.format(cents);
  }

  public static String formatMoney(Decimal amount, String currencyCode, CLDR cldr) {
    CurrencyType currency = CurrencyType.fromString(currencyCode);
    return cldr.Numbers.formatCurrency(amount, currency, CLDR_DEFAULT_OPTIONS);
  }

  public static String removeTags(String str) {
    StringBuilder buf = new StringBuilder();
    boolean inTag = false;
    for (int i = 0; i < str.length(); i++) {
      char ch = str.charAt(i);
      switch (ch) {

        case '<':
          inTag = true;
          break;

        case '>':
          inTag = false;
          buf.append(' ');
          break;

       default:
          if (!inTag) {
            buf.append(ch);
          }
      }
    }
    return buf.toString();
  }

  public static String slugify(String value) {
    value = SLUG_KILLCHARS.matcher(value).replaceAll("");
    value = WHITESPACE_RE.matcher(value).replaceAll("-");
    return value.toLowerCase();
  }

  public static String truncate(String value, int maxLen, String ellipses) {
    if (value.length() <= maxLen) {
      return value;
    }

    int end = maxLen;
    for (int i = maxLen - 1; i >= 0; i--) {
      if (Character.isWhitespace(value.charAt(i))) {
        end = i + 1;
        break;
      }
    }
    return value.substring(0, end) + ellipses;
  }

  /**
   * Left-pads values where 0 <= n.
   */
  public static void leftPad(long value, char padChar, int maxDigits, StringBuilder buf) {
    int digits = (value == 0) ? 1 : (int) Math.log10(value) + 1;
    for (int i = 0; i < maxDigits - digits; i++) {
      buf.append(padChar);
    }
    buf.append(value);
  }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy