KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: AnyPrinter.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
35 import org.apache.tools.ant.BuildException;
36 import org.apache.tools.ant.Project;
37
38 import com.idaremedia.antx.AntX;
39 import com.idaremedia.antx.AssertableDataType;
40 import com.idaremedia.antx.helpers.Strings;
41 import com.idaremedia.antx.helpers.Tk;
42
43 /**
44  * Basically a printer that spews whatever 'String.valueOf(&#46;&#46;&#46;)' returns.
45  * Also acts as a handle to a default printer specified in the build script. Usually
46  * defined in the context of a &lt;printer-registry&gt; as the default printer like:<pre>
47  * &lt;printer-registry id="antx.default.printers"&gt;
48  * &lt;defaultprinter id="antx.default.printer"/&gt; <b>&lt;-- ME --&gt;</b>
49  * &lt;printer id="printer.0" .../&gt;
50  * ...
51  * &lt;/printer-registry&gt;
52  * -OR-
53  * &lt;printer-registry id="antx.default.printers"&gt;
54  * &lt;defaultprinter id="antx.default.printer" <b>&lt;-- ME too --&gt;</b>
55  * classname="mycompany.custom.PrinterImpl"/&gt;
56  * &lt;printer id="printer.0" .../&gt;
57  * ...
58  * &lt;/printer-registry&gt;
59  * </pre>
60  *
61  * @since JWare/AntX 0.2
62  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
63  * @version 0.5
64  * @.safety n/a
65  * @.group api,helper
66  * @see PrinterRegistry
67  **/

68
69 public class AnyPrinter extends AssertableDataType implements DisplayStrategy
70 {
71     /**
72      * Creates a new AnyPrinter instance.
73      **/

74     public AnyPrinter()
75     {
76         super(AntX.print);
77     }
78
79
80     /**
81      * Capture our identifier for feedback since types don't always
82      * get correct location information.
83      **/

84     public void setId(String JavaDoc id)
85     {
86         m_Id= id;
87     }
88
89
90     /**
91      * Tries to return an identifier for this printer.
92      **/

93     public final String JavaDoc getId()
94     {
95         if (m_Id!=null) {
96             return m_Id;
97         }
98         if (isReference()) {
99             return getPrinterRef().getId();
100         }
101         return super.getId();
102     }
103
104
105     /**
106      * Tells this printer to exclude forced newlines between each item
107      * written. Newlines are written by default.
108      * @param splitEm <i>true</i> to separate messages entries
109      * @since JWare/AntX 0.4
110      **/

111     public void setSplitEntries(boolean splitEm)
112     {
113         if (isReference()) {
114             throw tooManyAttributes();
115         }
116         m_forceNL = splitEm;
117         edited("splitEntries");
118     }
119
120
121     /**
122      * Returns <i>true</i> if this task will insert a platform-specific
123      * newline sequence between each item printed. Is on by default.
124      * @since JWare/AntX 0.4
125      **/

126     public boolean willSplitEntries()
127     {
128         if (isReference()) {
129             return getPrinterRef().willSplitEntries();
130         }
131         return m_forceNL;
132     }
133
134
135
136     /**
137      * Force this printer to delegate to another
138      * {@linkplain com.idaremedia.antx.print.DisplayStrategy display}
139      * implementation.
140      * @param classname name of DisplayStrategy class (non-null)
141      * @throws BuildException if unable to create a printer instance from classname
142      **/

143     public void setClassName(String JavaDoc classname)
144     {
145         require_(classname!=null,"setClaz- nonzro name");
146         if (isReference()) {
147             throw tooManyAttributes();
148         }
149         if (!AnyPrinter.class.getName().equals(classname)) {//don't point to self
150
try {
151                 Class JavaDoc strategyClass = Class.forName(classname);
152                 m_printWorker = (DisplayStrategy)strategyClass.newInstance();
153
154             } catch (Exception JavaDoc anyX) {
155                 String JavaDoc ermsg = uistrs().get("printer.bad.impl.class",
156                                             getId(), classname);
157                 log(ermsg, Project.MSG_ERR);
158                 throw new BuildException(ermsg,anyX);
159             }
160         }
161         edited("setClassName");
162     }
163
164
165     /**
166      * Returns the name of the display strategy this handle represents.
167      * Return's this class's name if this printer doesn't delegate to
168      * any other strategy.
169      * @see #setClassName
170      **/

171     public final String JavaDoc getStrategyClassName()
172     {
173         if (isReference()) {
174             return getPrinterRef().getStrategyClassName();
175         }
176         return m_printWorker!=null ?
177             m_printWorker.getClass().getName() :
178             AnyPrinter.class.getName();
179     }
180
181
182
183     /**
184      * Convert to-be-displayed <i>thing</i> to a string. Log4J bridge
185      * point. Can never return <i>null</i>. If the <i>thing</i> signals
186      * an exception from its <span class="src">toString()</span> handler,
187      * this method returns a generic string of form:
188      * "<span class="src">classname@objectid</span>" where 'classname'
189      * is the object's class name and 'objectid' is the object reference
190      * identity hash value.
191      **/

192     public String JavaDoc stringFrom(Object JavaDoc thing)
193     {
194         if (isReference()) {
195             return getPrinterRef().stringFrom(thing);
196         }
197         return Tk.stringFrom(thing,getProject());
198     }
199
200
201     /**
202      * Prints the given <i>thing</i> to the given output stream.
203      * @throws IOException if any I/O problems occur
204      * @throws BuildException if any other error occurs
205      **/

206     public void print(DisplayRequest req, OutputStream JavaDoc out)
207         throws IOException JavaDoc, BuildException
208     {
209         if (isReference()) {
210             getPrinterRef().print(req,out);
211
212         } else if (m_printWorker!=null) {
213             m_printWorker.print(req,out);
214
215         } else {
216             String JavaDoc s = stringFrom(req.getObjectToBeDisplayed());
217             ensure_(s!=null,"print- strFrm is non-nul");
218
219             BufferedOutputStream JavaDoc bout;
220             if (out instanceof BufferedOutputStream JavaDoc) {
221                 bout = (BufferedOutputStream JavaDoc)out;
222             } else {
223                 bout = new BufferedOutputStream JavaDoc(out,s.length());//ick
224
}
225
226             if (willSplitEntries()) {
227                 bout.write(NL_);
228             }
229
230             byte[] raw = s.getBytes();
231             bout.write(raw,0,raw.length);
232             bout.flush();
233
234             //NB:good-gravy...
235
raw = null;
236             s = null;
237             bout = null;
238         }
239     }
240
241
242     /**
243      * Returns this printer's reference strongly typed.
244      **/

245     protected final AnyPrinter getPrinterRef()
246     {
247         return (AnyPrinter)getCheckedRef(AnyPrinter.class,"defaultprinter");
248     }
249
250
251     private String JavaDoc m_Id;
252     private DisplayStrategy m_printWorker;
253     private boolean m_forceNL=true;
254     private static final byte[] NL_= Strings.NL.getBytes();
255 }
256
257 /* end-of-Printer.java */
258
Popular Tags