001package ball.util.ant.taskdefs; 002/*- 003 * ########################################################################## 004 * Utilities 005 * $Id: StreamTokenizerTask.java 7434 2021-02-03 17:21:17Z ball $ 006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/util/ant/taskdefs/StreamTokenizerTask.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.io.StreamTokenizer; 025import java.io.StringReader; 026import lombok.Getter; 027import lombok.NoArgsConstructor; 028import lombok.Setter; 029import lombok.ToString; 030import lombok.experimental.Accessors; 031import org.apache.tools.ant.BuildException; 032import org.apache.tools.ant.Project; 033import org.apache.tools.ant.Task; 034import org.apache.tools.ant.util.ClasspathUtils; 035 036import static lombok.AccessLevel.PROTECTED; 037 038/** 039 * Abstract {@link.uri http://ant.apache.org/ Ant} {@link Task} base class 040 * for {@link StreamTokenizer}-specific tasks. 041 * 042 * {@ant.task} 043 * 044 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball} 045 * @version $Revision: 7434 $ 046 */ 047@NoArgsConstructor(access = PROTECTED) 048public abstract class StreamTokenizerTask extends Task 049 implements AnnotatedAntTask, 050 ClasspathDelegateAntTask, 051 ConfigurableAntTask { 052 @Getter @Setter @Accessors(chain = true, fluent = true) 053 private ClasspathUtils.Delegate delegate = null; 054 055 @Override 056 public void init() throws BuildException { 057 super.init(); 058 ClasspathDelegateAntTask.super.init(); 059 ConfigurableAntTask.super.init(); 060 } 061 062 @Override 063 public void execute() throws BuildException { 064 super.execute(); 065 AnnotatedAntTask.super.execute(); 066 } 067 068 /** 069 * {@link StreamTokenizer} from {@link String} {@link Task}. 070 * 071 * {@ant.task} 072 */ 073 @NoArgsConstructor @ToString 074 @AntTask("tokenize-string") 075 public static class FromString extends StreamTokenizerTask { 076 @NotNull @Getter @Setter 077 private String string = null; 078 079 @Override 080 public void execute() throws BuildException { 081 super.execute(); 082 083 try { 084 String string = getString(); 085 String[] lines = string.split("\\R"); 086 SimpleTableModel input = 087 new SimpleTableModel(new Object[][] { }, 2); 088 089 for (int i = 0; i < lines.length; i += 1) { 090 input.row(String.valueOf(i + 1), lines[i]); 091 } 092 093 log(input); 094 log(""); 095 096 try (StringReader reader = new StringReader(string)) { 097 StreamTokenizer tokenizer = new StreamTokenizer(reader); 098 099 for (;;) { 100 tokenizer.nextToken(); 101 102 log(tokenizer.toString()); 103 104 if (tokenizer.ttype == StreamTokenizer.TT_EOF) { 105 break; 106 } 107 } 108 } 109 } catch (BuildException exception) { 110 throw exception; 111 } catch (RuntimeException exception) { 112 throw exception; 113 } catch (Exception exception) { 114 throw new BuildException(exception); 115 } 116 } 117 } 118}