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