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.examples.core.svl;
018
019import org.apache.juneau.commons.logging.*;
020import org.apache.juneau.commons.svl.*;
021
022/**
023 * Simple Variable Language examples.
024 *
025 */
026public class SvlExample {
027
028   /**
029    * Main method.
030    *
031    * @param args Unused.
032    */
033   @SuppressWarnings({
034      "java:S106" // Example/demo code: printing resolved values to stdout is the intended pedagogical behavior; a logger would obscure the demonstration.
035   })
036   public static void main(String[] args) {
037
038      var vr = VarResolver.DEFAULT;
039
040      // $E{key[,default]} for getting environment variables
041      System.out.println(vr.resolve("JAVA_HOME=$E{JAVA_HOME, not defined}"));
042
043      // $S{key[,default]} for getting system properties (uses System.getProperty() )
044      RichLogger.getLogger(SvlExample.class).info(vr.resolve("os.name=$S{os.name, not defined}"));
045
046      // #{if(cond, then, else)} general if or if-else condition // NOSONAR
047      // #{notEmpty(s)} returns true if not empty
048      RichLogger.getLogger(SvlExample.class).info(vr.resolve("TEST_VAR is #{if(#{notEmpty($E{TEST_VAR})}, not empty, empty)}"));
049
050      // #{switch(value, pattern1, val1, ..., default)} glob-pattern switch-case // NOSONAR
051      System.out.println(vr.resolve("#{switch(Carrot, *Ap*, Fruit, *Car*, Veg, *, N/A)}"));
052
053      // #{replaceRegex(s, regex, replacement)} pattern replace
054      RichLogger.getLogger(SvlExample.class).info(vr.resolve("Java version=#{replaceRegex($S{java.version}, \"(_([0-9]+))\", \" build=$2\")}"));
055
056      // #{upper(s)} / #{lower(s)} case conversion
057      RichLogger.getLogger(SvlExample.class).info(vr.resolve("#{lower(JAVA_HOME)} #{upper($E{JAVA_HOME})}"));
058
059      // #{len(s[,delimiter])} length / part count
060      RichLogger.getLogger(SvlExample.class).info(vr.resolve("parts = #{len($S{os.version}, \".\")}, charcount = #{len($S{os.version})}"));
061
062      // #{substring(s, start[, end])} substring extraction
063      RichLogger.getLogger(SvlExample.class).info(vr.resolve("version = #{substring($S{java.version}, 0, 3)}"));
064
065      // #{extract(s, regex[, group])} regex group extraction
066      RichLogger.getLogger(SvlExample.class).info(vr.resolve("update = #{extract($S{java.version}, \"_([0-9]+)\", 1)}"));
067
068      /*
069       *  See all supported variable types at,
070       *  http://juneau.apache.org/site/apidocs-8.0.0/overview-summary.html#juneau-marshall.SimpleVariableLanguage
071       *  NOTE - juneau-marshall.SimpleVariableLanguage supports nested variables well
072       * */
073   }
074}