001package ball.util.ant.taskdefs; 002/*- 003 * ########################################################################## 004 * Utilities 005 * %% 006 * Copyright (C) 2008 - 2022 Allen D. Ball 007 * %% 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 * ########################################################################## 020 */ 021import ball.swing.table.SimpleTableModel; 022import java.io.StreamTokenizer; 023import java.io.StringReader; 024import lombok.Getter; 025import lombok.NoArgsConstructor; 026import lombok.Setter; 027import lombok.ToString; 028import lombok.experimental.Accessors; 029import org.apache.tools.ant.BuildException; 030import org.apache.tools.ant.Project; 031import org.apache.tools.ant.Task; 032import org.apache.tools.ant.util.ClasspathUtils; 033 034import static lombok.AccessLevel.PROTECTED; 035 036/** 037 * Abstract {@link.uri http://ant.apache.org/ Ant} {@link Task} base class 038 * for {@link StreamTokenizer}-specific tasks. 039 * 040 * {@ant.task} 041 * 042 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball} 043 */ 044@NoArgsConstructor(access = PROTECTED) 045public abstract class StreamTokenizerTask extends Task implements AnnotatedAntTask, 046 ClasspathDelegateAntTask, ConfigurableAntTask { 047 @Getter @Setter @Accessors(chain = true, fluent = true) 048 private ClasspathUtils.Delegate delegate = null; 049 050 @Override 051 public void init() throws BuildException { 052 super.init(); 053 ClasspathDelegateAntTask.super.init(); 054 ConfigurableAntTask.super.init(); 055 } 056 057 @Override 058 public void execute() throws BuildException { 059 super.execute(); 060 AnnotatedAntTask.super.execute(); 061 } 062 063 /** 064 * {@link StreamTokenizer} from {@link String} {@link Task}. 065 * 066 * {@ant.task} 067 */ 068 @NoArgsConstructor @ToString 069 @AntTask("tokenize-string") 070 public static class FromString extends StreamTokenizerTask { 071 @NotNull @Getter @Setter 072 private String string = null; 073 074 @Override 075 public void execute() throws BuildException { 076 super.execute(); 077 078 try { 079 String string = getString(); 080 String[] lines = string.split("\\R"); 081 SimpleTableModel input = new SimpleTableModel(new Object[][] { }, 2); 082 083 for (int i = 0; i < lines.length; i += 1) { 084 input.row(String.valueOf(i + 1), lines[i]); 085 } 086 087 log(input); 088 log(""); 089 090 try (StringReader reader = new StringReader(string)) { 091 StreamTokenizer tokenizer = new StreamTokenizer(reader); 092 093 for (;;) { 094 tokenizer.nextToken(); 095 096 log(tokenizer.toString()); 097 098 if (tokenizer.ttype == StreamTokenizer.TT_EOF) { 099 break; 100 } 101 } 102 } 103 } catch (BuildException exception) { 104 throw exception; 105 } catch (RuntimeException exception) { 106 throw exception; 107 } catch (Exception exception) { 108 throw new BuildException(exception); 109 } 110 } 111 } 112}