001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.examples.core.svl;
014
015import org.apache.juneau.svl.*;
016
017/**
018 * Simple Variable Language examples.
019 *
020 * <h5 class='section'>See Also:</h5><ul>
021 * </ul>
022 */
023public class SvlExample {
024
025   /**
026    * Main method.
027    *
028    * @param args Unused.
029    */
030   public static void main(String[] args) {
031
032      VarResolver vr = VarResolver.DEFAULT;
033
034      // $E{key[,default]} for getting environment variables
035      System.out.println(vr.resolve("JAVA_HOME=$E{JAVA_HOME, not defined}"));
036
037      // $S{key[,default]} for getting system properties (uses System.getProperty() )
038      System.out.println(vr.resolve("os.name=$S{os.name, not defined}"));
039
040      // $IF{key[,default]} general if or if-else condition
041      // $NE{arg} will return true if not empty
042      System.out.println(vr.resolve("TEST_VAR is $IF{$NE{$E{TEST_VAR}}, not empty, empty}"));
043
044      // $SW{arg,pattern1:then1[,pattern2:then2...]} switch-case
045      System.out.println(vr.resolve("$SW{Carrot, *Ap*:Fruit, *Car*:Veg, *:N/A}"));
046
047      // $PR{arg,pattern,replace} pattern replace
048      System.out.println(vr.resolve("Java version=$PR{$S{java.version}, (_([0-9]+)), \\ build=\\$2}"));
049
050      // $UC{arg} uppercase $LC{arg} lowecase
051      System.out.println(vr.resolve("$LC{JAVA_HOME} $UC{$E{JAVA_HOME}}"));
052
053      // $LN{arg[,delimiter]} length var example
054      System.out.println(vr.resolve("parts = $LN{$S{os.version},.}, charcount = $LN{$S{os.version}}"));
055
056      // $ST{arg,start[,end]} substring var example
057      System.out.println(vr.resolve("version = $ST{$S{java.version}, 0, 3}"));
058
059      // $PE{arg,start[,end]} pattern extractor var example
060      System.out.println(vr.resolve("update = $PE{$S{java.version},_([0-9]+),1}"));
061
062      /*
063       *  See all supported variable types at,
064       *  http://juneau.apache.org/site/apidocs-8.0.0/overview-summary.html#juneau-marshall.SimpleVariableLanguage
065       *  NOTE - juneau-marshall.SimpleVariableLanguage supports nested variables well
066       * */
067   }
068
069}