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.svl.*; 020 021/** 022 * Simple Variable Language examples. 023 * 024 */ 025public class SvlExample { 026 027 /** 028 * Main method. 029 * 030 * @param args Unused. 031 */ 032 public static void main(String[] args) { 033 034 var vr = VarResolver.DEFAULT; 035 036 // $E{key[,default]} for getting environment variables 037 System.out.println(vr.resolve("JAVA_HOME=$E{JAVA_HOME, not defined}")); 038 039 // $S{key[,default]} for getting system properties (uses System.getProperty() ) 040 System.out.println(vr.resolve("os.name=$S{os.name, not defined}")); 041 042 // $IF{key[,default]} general if or if-else condition 043 // $NE{arg} will return true if not empty 044 System.out.println(vr.resolve("TEST_VAR is $IF{$NE{$E{TEST_VAR}}, not empty, empty}")); 045 046 // $SW{arg,pattern1:then1[,pattern2:then2...]} switch-case 047 System.out.println(vr.resolve("$SW{Carrot, *Ap*:Fruit, *Car*:Veg, *:N/A}")); 048 049 // $PR{arg,pattern,replace} pattern replace 050 System.out.println(vr.resolve("Java version=$PR{$S{java.version}, (_([0-9]+)), \\ build=\\$2}")); 051 052 // $UC{arg} uppercase $LC{arg} lowecase 053 System.out.println(vr.resolve("$LC{JAVA_HOME} $UC{$E{JAVA_HOME}}")); 054 055 // $LN{arg[,delimiter]} length var example 056 System.out.println(vr.resolve("parts = $LN{$S{os.version},.}, charcount = $LN{$S{os.version}}")); 057 058 // $ST{arg,start[,end]} substring var example 059 System.out.println(vr.resolve("version = $ST{$S{java.version}, 0, 3}")); 060 061 // $PE{arg,start[,end]} pattern extractor var example 062 System.out.println(vr.resolve("update = $PE{$S{java.version},_([0-9]+),1}")); 063 064 /* 065 * See all supported variable types at, 066 * http://juneau.apache.org/site/apidocs-8.0.0/overview-summary.html#juneau-marshall.SimpleVariableLanguage 067 * NOTE - juneau-marshall.SimpleVariableLanguage supports nested variables well 068 * */ 069 } 070}