001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.console.freemarker; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020 021import org.apache.juneau.marshall.collections.*; 022import org.apache.juneau.console.*; 023 024/** 025 * Builds {@code $fn} markers: JSON values the chart glue replaces with a function from {@code JuneauCharts.fn}. 026 * Nothing is evaluated from a string; the marker is a name plus JSON arguments. 027 * 028 * <h5 class='section'>Example:</h5> 029 * <p class='bjava'> 030 * ChartDef.<jsm>line</jsm>(<js>"t"</js>).src(<js>"/s"</js>).x(<js>"d"</js>).series(<js>"v"</js>, <js>"V"</js>) 031 * .patch(JsonMap.<jsm>of</jsm>(<js>"tooltip"</js>, JsonMap.<jsm>of</jsm>(<js>"valueFormatter"</js>, Fn.<jsm>ref</jsm>(<js>"compact"</js>)))); 032 * </p> 033 * 034 * @since 10.0.0 035 */ 036public final class Fn { 037 038 private Fn() {} 039 040 /** @param name A built-in (bare) or app (dotted) fn name. @return The marker {@code {"$fn": name}}. */ 041 public static JsonMap ref(String name) { 042 return ref(name, null); 043 } 044 045 /** 046 * @param name A built-in (bare) or app (dotted) fn name. 047 * @param args The factory arguments, or <jk>null</jk>. 048 * @return The marker {@code {"$fn": name, "args": args}}. 049 * @throws IllegalArgumentException E-74 if the name is not a valid extension name. 050 */ 051 public static JsonMap ref(String name, JsonMap args) { 052 checkName(name, "$fn"); 053 var m = JsonMap.of("$fn", name); 054 if (nn(args)) 055 m.put("args", args); 056 return m; 057 } 058 059 // The one E-74 name predicate: Fn.checkName and ChartCardType both use it. 060 static boolean validName(String name) { 061 return name != null && BusWiringValidator.EXTENSION_NAME.matcher(name).matches(); 062 } 063 064 static void checkName(String name, String what) { 065 if (! validName(name)) 066 throw new IllegalArgumentException("E-74: " + f(BusWiringValidator.E74, name, what)); 067 } 068}