1 28 29 package com.idaremedia.antx.starters; 30 31 import java.io.ByteArrayOutputStream ; 32 import java.io.File ; 33 import java.io.FileOutputStream ; 34 import java.io.IOException ; 35 import java.io.OutputStream ; 36 37 import org.apache.tools.ant.BuildException; 38 import org.apache.tools.ant.Project; 39 40 import com.idaremedia.antx.helpers.Strings; 41 import com.idaremedia.antx.parameters.InformationSaver; 42 43 57 58 public abstract class EchoThingTask extends MsgTask 59 implements InformationSaver 60 { 61 64 protected static final String NL= Strings.NL; 65 66 67 68 72 protected EchoThingTask(String iam) 73 { 74 super(iam); 75 } 76 77 81 87 public void setToFile(String outputFile) 88 { 89 require_(outputFile!=null,"setToFil- nonzro file"); 90 m_toFile= getProject().resolveFile(outputFile); 91 } 92 93 94 95 101 public final void setToProjectFile(File outputFile) 102 { 103 require_(outputFile!=null,"setToProjFile- nonzro file"); 104 m_toFile = outputFile; 105 } 106 107 108 113 public final String getToFilePath() 114 { 115 File f = getToFile(); 116 return f!=null ? f.getPath() : null; 117 } 118 119 120 124 public File getToFile() 125 { 126 return m_toFile; 127 } 128 129 130 135 public void setAppend(boolean append) 136 { 137 m_tryAppend= append; 138 } 139 140 141 145 public final boolean willTryAppend() 146 { 147 return m_tryAppend; 148 } 149 150 154 157 protected final void setThingRefId(String refId) 158 { 159 require_(refId!=null,"setrefId- nonzro refid"); 160 m_refId = refId; 161 } 162 163 164 168 protected final String getThingRefId() 169 { 170 return m_refId; 171 } 172 173 174 179 protected String getThingRefParameterName() 180 { 181 return "refid"; } 183 184 185 192 protected Object getReferencedThing(Class ofKind, String msgid) 193 throws BuildException 194 { 195 require_(ofKind!=null,"getRefTing- nonzro clas"); 196 String refid = getThingRefId(); 197 if (msgid==null) { 198 return getReferencedObject(null,refid,ofKind); 199 } 200 Object object = getProject().getReference(refid); 201 if (!ofKind.isInstance(object)) { 202 String ermsg = uistrs().get(msgid,refid); 203 log(ermsg, Project.MSG_ERR); 204 throw new BuildException(ermsg,getLocation()); 205 } 206 return object; 207 } 208 209 210 216 protected void verifyCanExecute_(String calr) 217 { 218 super.verifyCanExecute_(calr); 219 220 if (getThingRefId()==null) { 221 String ermsg = uistrs().get("task.needs.this.attr", 222 getTaskName(),getThingRefParameterName()); 223 log(ermsg, Project.MSG_ERR); 224 throw new BuildException(ermsg, getLocation()); 225 } 226 } 227 228 229 233 protected OutputStream getOutputStream() 234 { 235 File f = getToFile(); 236 if (f!=null) { 237 try { 238 return new FileOutputStream (f.getPath(), willTryAppend()); 239 } catch(IOException ioX) { 240 String ermsg = uistrs().get("task.echo.unable.use.file",f.getPath(), 241 ioX.getMessage()); 242 log(ermsg,Project.MSG_WARN); 243 } 244 } 245 return new ByteArrayOutputStream (1024); 246 } 247 248 249 253 protected boolean tryAntLog(OutputStream os) 254 { 255 return (os instanceof ByteArrayOutputStream ); 256 } 257 258 259 264 protected String getAntLogString(OutputStream os) 265 { 266 if (os instanceof ByteArrayOutputStream ) { 267 return ((ByteArrayOutputStream )os).toString(); 268 } 269 return ""; 270 } 271 272 273 281 public void execute() throws BuildException 282 { 283 verifyCanExecute_("execute"); 284 285 if (testIfCondition() && testUnlessCondition()) { 286 echoThing(); 287 } 288 } 289 290 291 296 protected abstract void echoThing() throws BuildException; 297 298 299 300 private String m_refId; 301 private File m_toFile; 302 private boolean m_tryAppend;} 304 305 306 | Popular Tags |