001package ball.util.ant.taskdefs; 002/*- 003 * ########################################################################## 004 * Utilities 005 * $Id: PatternTask.java 7419 2021-02-01 21:26:40Z ball $ 006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/util/ant/taskdefs/PatternTask.java $ 007 * %% 008 * Copyright (C) 2008 - 2021 Allen D. Ball 009 * %% 010 * Licensed under the Apache License, Version 2.0 (the "License"); 011 * you may not use this file except in compliance with the License. 012 * You may obtain a copy of the License at 013 * 014 * http://www.apache.org/licenses/LICENSE-2.0 015 * 016 * Unless required by applicable law or agreed to in writing, software 017 * distributed under the License is distributed on an "AS IS" BASIS, 018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 019 * See the License for the specific language governing permissions and 020 * limitations under the License. 021 * ########################################################################## 022 */ 023import ball.swing.table.SimpleTableModel; 024import java.util.Collections; 025import java.util.regex.Matcher; 026import java.util.regex.Pattern; 027import java.util.regex.PatternSyntaxException; 028import lombok.Getter; 029import lombok.NoArgsConstructor; 030import lombok.Setter; 031import lombok.ToString; 032import lombok.experimental.Accessors; 033import org.apache.tools.ant.BuildException; 034import org.apache.tools.ant.Project; 035import org.apache.tools.ant.Task; 036import org.apache.tools.ant.util.ClasspathUtils; 037 038import static lombok.AccessLevel.PROTECTED; 039import static org.apache.commons.lang3.StringUtils.EMPTY; 040import static org.apache.commons.lang3.StringUtils.SPACE; 041 042/** 043 * Abstract {@link.uri http://ant.apache.org/ Ant} {@link Task} base class 044 * for {@link Pattern}-specific tasks. 045 * 046 * {@ant.task} 047 * 048 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball} 049 * @version $Revision: 7419 $ 050 */ 051@NoArgsConstructor(access = PROTECTED) 052public abstract class PatternTask extends Task 053 implements AnnotatedAntTask, 054 ClasspathDelegateAntTask, 055 ConfigurableAntTask { 056 @Getter @Setter @Accessors(chain = true, fluent = true) 057 private ClasspathUtils.Delegate delegate = null; 058 @NotNull @Getter @Setter 059 private String pattern = null; 060 @NotNull @Getter @Setter 061 private String input = null; 062 063 @Override 064 public void init() throws BuildException { 065 super.init(); 066 ClasspathDelegateAntTask.super.init(); 067 ConfigurableAntTask.super.init(); 068 } 069 070 @Override 071 public void execute() throws BuildException { 072 super.execute(); 073 AnnotatedAntTask.super.execute(); 074 } 075 076 /** 077 * {@link Pattern} {@link Matcher#matches()} {@link Task}. 078 * 079 * {@ant.task} 080 */ 081 @NoArgsConstructor @ToString 082 @AntTask("pattern-matches") 083 public static class Matches extends PatternTask { 084 @Override 085 public void execute() throws BuildException { 086 super.execute(); 087 088 try { 089 SimpleTableModel table = 090 new SimpleTableModel(new Object[][] { }, 2); 091 Pattern pattern = Pattern.compile(getPattern()); 092 093 table.row(EMPTY, String.valueOf(pattern)); 094 095 String input = getInput(); 096 097 table.row(EMPTY, String.valueOf(input)); 098 099 Matcher matcher = pattern.matcher(input); 100 boolean matches = matcher.matches(); 101 102 table.row(EMPTY, String.valueOf(matches)); 103 104 if (matches) { 105 for (int i = 0, n = matcher.groupCount(); i <= n; i += 1) { 106 table.row(String.valueOf(i), 107 tab(matcher.start(i)) + matcher.group(i)); 108 } 109 } 110 111 log(table); 112 } catch (PatternSyntaxException exception) { 113 log(exception.getMessage(), Project.MSG_ERR); 114 throw new BuildException(); 115 } catch (BuildException exception) { 116 throw exception; 117 } catch (RuntimeException exception) { 118 throw exception; 119 } catch (Exception exception) { 120 throw new BuildException(exception); 121 } 122 } 123 124 private String tab(int count) { 125 return String.join(EMPTY, Collections.nCopies(count, SPACE)); 126 } 127 } 128 129 /** 130 * {@link Pattern#split(CharSequence)} {@link Task}. 131 * 132 * {@ant.task} 133 */ 134 @NoArgsConstructor @ToString 135 @AntTask("pattern-split") 136 public static class Split extends PatternTask { 137 @Override 138 public void execute() throws BuildException { 139 super.execute(); 140 141 try { 142 SimpleTableModel table = 143 new SimpleTableModel(new Object[][] { }, 2); 144 Pattern pattern = Pattern.compile(getPattern()); 145 146 table.row(EMPTY, String.valueOf(pattern)); 147 148 String input = getInput(); 149 150 table.row(EMPTY, String.valueOf(input)); 151 152 String[] strings = pattern.split(input); 153 154 for (int i = 0; i < strings.length; i += 1) { 155 table.row(String.valueOf(i), strings[i]); 156 } 157 158 log(table); 159 } catch (PatternSyntaxException exception) { 160 log(exception.getMessage(), Project.MSG_ERR); 161 throw new BuildException(); 162 } catch (BuildException exception) { 163 throw exception; 164 } catch (RuntimeException exception) { 165 throw exception; 166 } catch (Exception exception) { 167 throw new BuildException(exception); 168 } 169 } 170 } 171}