KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > print > PrintTask


1 /**
2  * $Id: PrintTask.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-2004 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.print;
30
31 import java.io.BufferedOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.util.Hashtable JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 import org.apache.tools.ant.Project;
38
39 import com.idaremedia.antx.AntX;
40 import com.idaremedia.antx.FixtureExaminer;
41 import com.idaremedia.antx.helpers.Strings;
42 import com.idaremedia.antx.helpers.Tk;
43
44 /**
45  * Display arbitrary bits of build information; usually reference objects. Usually
46  * defined &lt;print&gt;. PrintTask extends the standard &lt;printenv&gt; task; so it
47  * can also be used to print project properties and AntX variables. If no display strategy
48  * is specified, a &lt;print&gt; will simply display the string returned by the
49  * displayed thing's <i>toString</i> method. If no default printer registry is installed
50  * &lt;print&gt; behaves exactly like &lt;printenv&gt;
51  * <p>
52  * <b>Examples:</b><pre>
53  * &lt;print msgid="banner.msgsbundle" reference="default.msgs"/&gt;
54  * &lt;print msgid="msg.error.default" reference="last.error" filter="..."/&gt;
55  * &lt;print properties="ant.file,ant.version,basedir"/&gt; &lt;-- <i>like printenv</i> --&gt;
56  * &lt;print if="things.debug" reference="mything.id" with="mythings.printer"/&gt;
57  * &lt;print message="Howdy World!" level="verbose"/&gt; &lt;-- <i>like echo</i> --&gt;
58  * </pre>
59  *
60  * @since JWare/AntX 0.2
61  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
62  * @version 0.5
63  * @.safety single
64  * @.group api,helper
65  * @see PrinterMapping
66  **/

67
68 public class PrintTask extends EchoItemsTask
69 {
70     /**
71      * Initializes a new PrintTask instance.
72      **/

73     public PrintTask()
74     {
75         super(AntX.print);
76     }
77
78
79     /**
80      * Initializes a new CV-labeled PrintTask instance.
81      * @param iam CV-label (non-null)
82      **/

83     protected PrintTask(String JavaDoc iam)
84     {
85         super(iam);
86     }
87
88
89     /**
90      * Sets the printer or printer registry this task will use to
91      * display project references.
92      **/

93     public void setWith(String JavaDoc with)
94     {
95         require_(with!=null,"setwith- nonzro refid");
96         m_with = with;
97     }
98
99
100     /**
101      * Returns the printer or printer registry this task will
102      * use to display items. Will return <i>null</i> if with
103      * parameter never set.
104      **/

105     public final String JavaDoc getWith()
106     {
107         return m_with;
108     }
109
110
111
112     /**
113      * Gives this printer a list of printer-specific filter options.
114      * Only really useful for single item printing or for common
115      * printer options (like "<span class="src">resolve</span>").
116      * @param filterstring the printer-specific filter string (non-null)
117      * @since JWare/AntX 0.5
118      **/

119     public void setFilter(String JavaDoc filterstring)
120     {
121         m_filterstring = filterstring;
122     }
123
124
125     /**
126      * Returns the custom printer filter string for this task. Will
127      * return <i>null</i> if never set explicitly.
128      * @since JWare/AntX 0.5
129      **/

130     public final String JavaDoc getFilterString()
131     {
132         return m_filterstring;
133     }
134
135
136
137     /**
138      * Returns the default printer registry used by this task.
139      * Never returns <i>null</i> but can return an empty registry.
140      * @see PrinterRegistryContext#getPrinterRegistryNoNull
141      **/

142     private PrinterRegistry getDefaultPrinterRegistry()
143     {
144         return PrinterRegistryContext.getPrinterRegistryNoNull();
145     }
146
147
148     /**
149      * Displays the references list with the strategy specified
150      * by the given mapping.
151      **/

152     protected void printReferencesWith(PrinterMapping mapping,
153                                        final Project P, String JavaDoc list,
154                                        OutputStream JavaDoc os)
155         throws IOException JavaDoc
156     {
157         require_(mapping!=null,"print- nonzro mappin");
158
159         PrinterRegistry registry = getDefaultPrinterRegistry();
160         DisplayStrategy printer = mapping.getPrinter();
161
162         Class JavaDoc filterClass = getKindOfFilterClass();
163         String JavaDoc cantprint;
164
165         Iterator JavaDoc keysitr;
166         if (Strings.ALL.equals(Tk.lowercaseFrom(list))) {
167             Hashtable JavaDoc copy = (Hashtable JavaDoc)P.getReferences().clone();
168             keysitr= copy.keySet().iterator();
169         } else {
170             keysitr= Tk.splitList(list).iterator();
171         }
172
173         BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(os,1024);
174         DisplayStrategy defaultPrinter= registry.getDefaultPrinter();
175
176         while (keysitr.hasNext()) {
177             String JavaDoc key = (String JavaDoc)keysitr.next();
178
179             Object JavaDoc object = FixtureExaminer.trueReference(P,key);//@since AntX 0.4
180
if (object==FixtureExaminer.IGNORED_REFERENCE) {
181                 if (!willIncludeUnknowns()) {
182                     continue;
183                 }
184                 object = unresolvedString(P,key);
185             }
186
187             if (filterClass==null/*allowAll*/ || object==null/*passthru*/ ||
188                 (filterClass.isInstance(object))) {
189
190                 DisplayRequest dr = new DisplayRequest(P,key,object);
191                 dr.setFilter(getFilterString());
192
193                 if (!mapping.isMatch(object)) {
194                     cantprint = uistrs().get("printer.cant.print.item",mapping.getId(),key);
195                     log(cantprint,Project.MSG_WARN);
196                     defaultPrinter.print(dr,bos);
197                 } else {
198                     printer.print(dr,bos);
199                 }
200                 dr = null;
201             }//do-print
202
}//while
203

204         keysitr=null;
205     }
206
207
208
209     /**
210      * Displays the references list with the printer registry
211      * specified.
212      **/

213     protected void printReferencesWith(PrinterRegistry registry,
214                                        final Project P, String JavaDoc list,
215                                        OutputStream JavaDoc os)
216         throws IOException JavaDoc
217     {
218         require_(registry!=null,"print- nonzro registry");
219
220         Class JavaDoc filterClass = getKindOfFilterClass();
221
222         Iterator JavaDoc keysitr;
223         if (Strings.ALL.equals(Tk.lowercaseFrom(list))) {
224             Hashtable JavaDoc copy = (Hashtable JavaDoc)P.getReferences().clone();
225             keysitr= copy.keySet().iterator();
226         } else {
227             keysitr= Tk.splitList(list).iterator();
228         }
229
230         BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(os,1024);
231         PrinterMapping prevHandler=null;
232         DisplayStrategy printer=null;
233         DisplayStrategy defaultPrinter= registry.getDefaultPrinter();
234
235         while (keysitr.hasNext()) {
236             String JavaDoc key = (String JavaDoc)keysitr.next();
237
238             Object JavaDoc object = FixtureExaminer.trueReference(P,key);//@since AntX 0.4
239
if (object==FixtureExaminer.IGNORED_REFERENCE) {
240                 if (!willIncludeUnknowns()) {
241                     continue;
242                 }
243                 object = unresolvedString(P,key);
244             }
245
246             if (filterClass==null/*allowAll*/ || object==null/*passthru*/ ||
247                 (filterClass.isInstance(object))) {
248
249                 if (prevHandler!=null && prevHandler.isMatch(object)) {
250                     printer= prevHandler.getPrinter();
251                 } else {
252                     PrinterMapping handler= registry.findMappingFor(object);
253                     if (handler==null) {
254                         printer = defaultPrinter;
255                     } else {
256                         printer = handler.getPrinter();
257                         prevHandler = handler;
258                     }
259                 }
260                 DisplayRequest dr = new DisplayRequest(P,key,object);
261                 dr.setFilter(getFilterString());
262                 printer.print(dr,bos);
263                 dr = null;
264             }//do-print
265
}//while
266

267         keysitr=null;
268     }
269
270
271     /**
272      * Returns <i>true</i> if this task should try to customize the
273      * display references. By default will check if either the 'with'
274      * parameter or the active registry is enabled. If yes, will try
275      * to use custom display strategy.
276      **/

277     private boolean tryCustomDisplay()
278     {
279         PrinterRegistry dfltPR= getDefaultPrinterRegistry();
280         if (dfltPR!=PrinterRegistryContext.getEmptyPrinterRegistry()) {
281             return true;
282         }
283         return false;
284     }
285
286
287     /**
288      * Displays references using the frontmost context printer registry.
289      **/

290     protected boolean echoReferences(final Project P, OutputStream JavaDoc os)
291         throws IOException JavaDoc
292     {
293         String JavaDoc list= getReferencesNameList();
294         if (list!=null) {
295             String JavaDoc with = getWith();
296             if (with!=null) {
297                 Object JavaDoc withObject = P.getReference(with);
298                 if (withObject instanceof PrinterRegistry) {
299                     printReferencesWith((PrinterRegistry)withObject,P,list,os);
300                 } else if (withObject instanceof PrinterMapping) {
301                     printReferencesWith((PrinterMapping)withObject,P,list,os);
302                 } else {
303                     String JavaDoc ermsg = uistrs().get("print.bad.with",with);
304                     log(ermsg, Project.MSG_WARN);
305                     if (tryCustomDisplay()) {
306                         printReferencesWith(getDefaultPrinterRegistry(),P,list,os);
307                     } else {
308                         return super.echoReferences(P,os);
309                     }
310                 }
311                 return true;
312             } else if (tryCustomDisplay()) {
313                 printReferencesWith(getDefaultPrinterRegistry(),P,list,os);
314             } else {
315                 return super.echoReferences(P,os);
316             }
317         }
318         return false;
319     }
320
321
322
323     private String JavaDoc m_with;//optional
324
private String JavaDoc m_filterstring;//optional
325
}
326
327 /* end-of-PrintTask.java */
328
Popular Tags