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

com.squarespace.template.plugins.platform.PlatformUtils Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
/**
 * Copyright (c) 2015 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.platform;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

import com.fasterxml.jackson.databind.JsonNode;
import com.squarespace.template.GeneralUtils;
import com.squarespace.template.plugins.PluginUtils;


/**
 * Extracted from Commons library at commit ab4ba7a6f2b872a31cb6449ae9e96f5f5b30f471
 */
public class PlatformUtils {

  private PlatformUtils() {
  }

  /**
   * Like money-format (e.g. 0.00) but with a currency (dollar) sign and parentheses on negative values.
   * Fully internationalized. Uses NumberFormat under the hood.
   */
  public static String formatBookkeeperMoney(double cents, Locale locale) {
    NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);
    return formatter.format(cents / 100);
  }

  public static String formatPercentage(double percentage, boolean trim, Locale locale) {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
    String pattern = trim ? "#,##0.###" : "#,##0.00#";
    DecimalFormat format = new DecimalFormat(pattern, symbols);
    return format.format(percentage);
  }

  public static void makeSocialButton(JsonNode website, JsonNode item, boolean inline, StringBuilder buf) {
    JsonNode options = website.path("shareButtonOptions");
    if (website.isMissingNode() || options.isMissingNode() || options.size() == 0) {
      return;
    }

    JsonNode node = GeneralUtils.getFirstMatchingNode(item, "systemDataId", "mainImageId");
    String imageId = node.asText();
    node = item.path("assetUrl");
    String assetUrl = node.asText();
    if (node.isMissingNode()) {
      node = item.path("mainImage").path("assetUrl");
      assetUrl = node.asText();
    }
    String style = (inline) ? "inline-style" : "button-style";
    if (inline) {
      buf.append("");
    if (inline) {
      buf.append("");
    } else {
      buf.append("
"); } } }



© 2015 - 2024 Weber Informatics LLC | Privacy Policy