juneau-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] jamesbognar closed pull request #13: Add PatternReplaceVar feature
Date Fri, 09 Nov 2018 15:35:08 GMT
jamesbognar closed pull request #13: Add PatternReplaceVar feature
URL: https://github.com/apache/juneau/pull/13
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/svl/vars/PatternReplaceVarTest.java
b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/svl/vars/PatternReplaceVarTest.java
new file mode 100644
index 000000000..57c8614ed
--- /dev/null
+++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/svl/vars/PatternReplaceVarTest.java
@@ -0,0 +1,46 @@
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
agreements.  See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership.
 The ASF licenses this file        *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance            *
+// * with the License.  You may obtain a copy of the License at                         
                                    *
+// *                                                                                    
                                    *
+// *  http://www.apache.org/licenses/LICENSE-2.0                                        
                                    *
+// *                                                                                    
                                    *
+// * Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an  *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the        *
+// * specific language governing permissions and limitations under the License.         
                                    *
+// ***************************************************************************************************************************
+package org.apache.juneau.svl.vars;
+
+import static org.junit.Assert.*;
+
+import org.apache.juneau.svl.*;
+import org.junit.*;
+
+public class PatternReplaceVarTest {
+
+	//====================================================================================================
+	// test - Basic tests
+	//====================================================================================================
+	@Test
+	public void test() throws Exception {
+		VarResolver vr = new VarResolverBuilder().vars(PatternReplaceVar.class, SystemPropertiesVar.class).build();
+
+		System.setProperty("PatternReplaceVarTest.test", "foo bar");
+		System.setProperty("PatternReplaceVarTest.test2", "size=23;display=none;");
+
+		// Simple replacements
+		assertEquals("bar bar", vr.resolve("$PR{$S{PatternReplaceVarTest.test},foo,bar}"));
+		assertEquals("coo bar", vr.resolve("$PR{$S{PatternReplaceVarTest.test},^f?,co}"));
+		assertEquals("fine", vr.resolve("$PR{$S{PatternReplaceVarTest.test},oo*,ine}"));
+		assertEquals("FOO FOO", vr.resolve("$PR{$S{PatternReplaceVarTest.test},([a-z]+),FOO}"));
+		
+		// Replacements using matched sub classes 
+		assertEquals("size=23px;display=none;", vr.resolve("$PR{$S{PatternReplaceVarTest.test2},(size=([\\d]+)),\\$1px}"));
+		assertEquals("size=??px;display=none;", vr.resolve("$PR{$S{PatternReplaceVarTest.test2},[\\d]+,??px}"));
+		assertEquals("size=23;display=none", vr.resolve("$PR{$S{PatternReplaceVarTest.test2},;\\$,}"));
+		assertEquals("size=23;none=display;", vr.resolve("$PR{$S{PatternReplaceVarTest.test2},;(*)=(*[^;]),;\\$2=\\$1}"));
+		
+
+	}
+}
diff --git a/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/VarResolverBuilder.java
b/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/VarResolverBuilder.java
index 129a82f50..634ac366f 100644
--- a/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/VarResolverBuilder.java
+++ b/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/VarResolverBuilder.java
@@ -71,6 +71,7 @@ public VarResolverBuilder vars(Class<?>...vars) {
 	 * 	<li>{@link IfVar}
 	 * 	<li>{@link CoalesceVar}
 	 * 	<li>{@link PatternMatchVar}
+	 * 	<li>{@link PatternReplaceVar}
 	 * 	<li>{@link UpperCaseVar}
 	 * 	<li>{@link LowerCaseVar}
 	 * 	<li>{@link NotEmptyVar}
@@ -88,6 +89,7 @@ public VarResolverBuilder defaultVars() {
 			IfVar.class,
 			CoalesceVar.class,
 			PatternMatchVar.class,
+			PatternReplaceVar.class,
 			UpperCaseVar.class,
 			LowerCaseVar.class,
 			NotEmptyVar.class);
diff --git a/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/vars/PatternReplaceVar.java
b/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/vars/PatternReplaceVar.java
new file mode 100644
index 000000000..ce38c790e
--- /dev/null
+++ b/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/vars/PatternReplaceVar.java
@@ -0,0 +1,76 @@
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
agreements.  See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership.
 The ASF licenses this file        *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance            *
+// * with the License.  You may obtain a copy of the License at                         
                                    * 
+// *                                                                                    
                                    *
+// *  http://www.apache.org/licenses/LICENSE-2.0                                        
                                    *
+// *                                                                                    
                                    *
+// * Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an  *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the        *
+// * specific language governing permissions and limitations under the License.         
                                    *
+// ***************************************************************************************************************************
+package org.apache.juneau.svl.vars;
+
+import static org.apache.juneau.internal.ThrowableUtils.*;
+
+import org.apache.juneau.svl.*;
+
+/**
+ * A transformational variable resolver that replaces matched patterns with given characters.
+ *
+ * <p>
+ * The format for this var is:
+ * <ul>
+ * 	<li><js>"$PR{stringArg, pattern, replace}"</js>
+ * </ul>
+ *
+ * <p>
+ * The pattern can be any string optionally containing <js>'*'</js> or <js>'?'</js>
representing any or one character
+ * respectively.
+ * 
+ * The replace can contain matched regex sub classes like \$1, \$2 ..
+ *
+ * <h5 class='section'>Example:</h5>
+ * <p class='bcode w800'>
+ * 	<jc>// Create a variable resolver that resolves system properties and $SW vars.</jc>
+ * 	VarResolver r = VarResolver.<jsm>create</jsm>().vars(PatternReplaceVar.<jk>class</jk>,
SystemPropertiesVar.<jk>class</jk>).build();
+ *
+ * 	<jc>// Use it!</jc>
+ * 	System.<jsf>out</jsf>.println(r.resolve(<js>"Java version=$PR{$S{java.version},
(_([0-9]+)), \\ build=\\$2}"</js>));
+ * </p>
+ *
+ * <p>
+ * Since this is a {@link MultipartVar}, any variables contained in the result will be recursively
resolved.
+ * <br>Likewise, if the arguments contain any variables, those will be resolved before
they are passed to this var.
+ *
+ * <h5 class='section'>See Also:</h5>
+ * <ul>
+ * 	<li class='link'>{@doc juneau-svl.SvlVariables}
+ * </ul>
+ */
+public class PatternReplaceVar extends MultipartVar {
+
+	/** The name of this variable. */
+	public static final String NAME = "PR";
+
+	/**
+	 * Constructor.
+	 */
+	public PatternReplaceVar() {
+		super(NAME);
+	}
+
+	@Override /* MultipartVar */
+	public String resolve(VarResolverSession session, String[] args) {
+		if (args.length < 3)
+			illegalArg("Invalid number of arguments passed to $PR var.  Must have 3 or more arguments.");
+
+		String stringArg = args[0];
+		String pattern = args[1];
+		String replace = args[2];
+		
+		pattern = pattern.replace("*", ".*").replace("?", ".");
+		return stringArg.replaceAll(pattern, replace);
+	}
+}
\ No newline at end of file


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message