KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: PrinterRegistry.java 187 2007-03-25 17:59:16Z ssmc $
3  * Copyright 2002-2005,2007 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://antxtras.sf.net/ EMAIL- jware[at]users[dot]sourceforge[dot]net
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.print;
30
31 import java.io.File JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Vector JavaDoc;
36
37 import org.apache.tools.ant.BuildException;
38 import org.apache.tools.ant.DirectoryScanner;
39 import org.apache.tools.ant.types.FileSet;
40 import org.apache.tools.ant.types.Reference;
41 import org.apache.tools.ant.util.FileUtils;
42
43 import com.idaremedia.antx.AntX;
44 import com.idaremedia.antx.AssertableDataType;
45 import com.idaremedia.antx.FixtureComponent;
46 import com.idaremedia.antx.apis.AntLibFriendly;
47 import com.idaremedia.antx.parameters.FreeFormEnabled;
48 import com.idaremedia.antx.parameters.RecoveryEnabled;
49
50 /**
51  * A collection of {@linkplain com.idaremedia.antx.print.DisplayStrategy display
52  * strategy} to object class mappings. Mappings are searched (for matches) in the
53  * order they are inserted. Build scripts don't have to define an explicit default
54  * print strategy for every registry; a {@linkplain AnyPrinter default} is always
55  * defined. However, an explicit default strategy must be specified if the
56  * builder wants to use a custom
57  * {@linkplain com.idaremedia.antx.print.DisplayStrategy display strategy}
58  * implementation as the registry's default.
59  * <p>
60  * PrinterRegistries are useful for clumping all printer mapping under a single collection
61  * and then installing them all at once using a &lt;{@linkplain ManagePrintersTask
62  * manageprinters}&gt; declaration. As of Ant 1.6 registries will accept your custom
63  * printer mappings by typedef'd name so you no longer need to use default &lt;printer&gt;
64  * shell declaration.
65  * <p>
66  * <b>Examples:</b><pre>
67  * &lt;typedef name="myprinter" classname="mycompany.MyPrinter"/&gt;
68  * ...
69  * &lt;printer-registry id="jware.antx.printers"&gt;
70  * &lt;defaultprinter id="jware.antx.printer.DEFAULT"/&gt;
71  * &lt;printer id="bundle.printer" classname="jware.antx.init.BundlePrinter"&gt;
72  * &lt;forclass name="jware.antx.strings.UISMBundle"/&gt;
73  * &lt;/printer&gt;
74  * &lt;printer id="errors.printer" classname="jware.antx.print.ErrorPrinter"&gt;
75  * &lt;forclass name="jware.antx.ErrorSnapshot"/&gt;
76  * &lt;forclass name="org.apache.tools.ant.BuildException"/&gt;
77  * &lt;/printer&gt;
78  * &lt;printer id="rules.printer" classname="jware.antx.print.MappingPrinter"&gt;
79  * &lt;forclass name="jware.antx.print.PrinterMapping"/&gt;
80  * &lt;/printer&gt;
81  * <i>&lt;-- Ant 1.6 or later only --&gt</i>
82  * &lt;myprinter id="mytypes.printer"/&gt;
83  * &lt;/printer-registry&gt;
84  * </pre>
85  *
86  * @since JWare/AntX 0.2
87  * @author ssmc, &copy;2002-2005,2007 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
88  * @version 0.5.1
89  * @.safety multiple (once <em>fully</em> configured)
90  * @.group api,helper
91  * @see PrinterMapping
92  **/

93
94 public class PrinterRegistry extends AssertableDataType
95     implements Cloneable JavaDoc, FixtureComponent, FreeFormEnabled, AntLibFriendly, RecoveryEnabled
96 {
97     /**
98      * Initializes a new printer registry instance.
99      **/

100     public PrinterRegistry()
101     {
102         super(AntX.print);
103         m_registry = new Vector JavaDoc(10,7);
104     }
105
106
107     /**
108      * Initializes a new optionally locked printer registry.
109      **/

110     PrinterRegistry(boolean locked)
111     {
112         super(AntX.print);
113         if (locked) {
114             m_registry = Collections.EMPTY_LIST;
115         } else {
116             m_registry = new Vector JavaDoc(10,7);
117         }
118     }
119
120
121     /**
122      * Returns a clone of this printer registry. Supports copying
123      * registries between parent and child projects.
124      * @since JWare/AntX 0.3
125      **/

126     public Object JavaDoc clone()
127     {
128         if (isReference()) {
129             return getRegistryRef().clone();
130         }
131         try {
132             PrinterRegistry copy = (PrinterRegistry)super.clone();
133             if (m_registry!=Collections.EMPTY_LIST) {
134                 Vector JavaDoc mappings = (Vector JavaDoc)((Vector JavaDoc)m_registry).clone();
135                 for (int i=0,n=mappings.size();i<n;i++) {
136                     mappings.set(i, ((PrinterMapping)mappings.get(i)).clone());
137                 }
138                 copy.m_registry = mappings;
139             }
140             return copy;
141         } catch(CloneNotSupportedException JavaDoc clnx) {
142             throw new Error JavaDoc(uistrs().get(AntX.CLONE_BROKEN_MSGID));
143         }
144     }
145
146
147     /**
148      * Capture our identifier for feedback since types don't always
149      * get correct location information.
150      **/

151     public void setId(String JavaDoc id)
152     {
153         m_Id= id;
154     }
155
156
157     /**
158      * Returns a unique identifier for this registry.
159      **/

160     public final String JavaDoc getId()
161     {
162         if (m_Id!=null) {
163             return m_Id;
164         }
165         if (isReference()) {
166             return getRegistryRef().getId();
167         }
168         return super.getId();
169     }
170
171
172     /**
173      * Tells this registry whether to propagate a build error if
174      * unable to load a file-based printer mapping.
175      * @param b <i>false</i> to ignored missing and malformed files
176      * @since JWare/AntX 0.4
177      * @see #addConfiguredFileSet
178      **/

179     public void setHaltIfError(boolean b)
180     {
181         if (isReference()) {
182             throw tooManyAttributes();
183         }
184         m_haltIfError = b;
185     }
186
187
188     /**
189      * Returns <i>true</i> if this registry will generate a build
190      * error if unable to load a printer mapping from a file.
191      * @since JWare/AntX 0.4
192      **/

193     public boolean isHaltIfError()
194     {
195         if (isReference()) {
196             return getRegistryRef().isHaltIfError();
197         }
198         return m_haltIfError;
199     }
200
201
202     /**
203      * Adds a set of mappings to this registry via a standard Ant
204      * fileset. Each file in the given file set must point to a
205      * printer mapping definition <span class="src">Properties</span>
206      * file. A build exception is thrown if any file is malformed
207      * unless the {@linkplain #setHaltIfError} option is turned off.
208      * @param fs configured fileset (non-null)
209      * @since JWare/AntX 0.4
210      * @throws BuildException if any file is missing or malformed
211      **/

212     public void addConfiguredFileSet(FileSet fs)
213     {
214         require_(fs!=null,"addFileset- nonzro fileset");
215         if (isReference()) {
216             throw tooManyAttributes();
217         }
218
219         DirectoryScanner dirscan = fs.getDirectoryScanner(getProject());
220         String JavaDoc[] files = dirscan.getIncludedFiles();
221
222         if (files.length>0) {
223             FileUtils fileUtils = FileUtils.getFileUtils();
224             File JavaDoc wrt = dirscan.getBasedir();
225             int Nadded=0;
226             for (int i=0;i<files.length;i++) {
227                 File JavaDoc rf= fileUtils.resolveFile(wrt,files[i]);
228                 try {
229                     PrinterMapping pm = new PrinterMapping();
230                     pm.setProjectFile(rf);
231                     m_registry.add(pm);
232                     Nadded++;
233                 } catch(BuildException bX) {
234                     if (isHaltIfError()) {
235                         throw bX;
236                     }
237                 }
238             }//for
239

240             if (Nadded>0) {
241                 edited("addFileSet");
242             }
243             fileUtils= null;
244         }
245
246         dirscan= null;
247     }
248
249
250     /**
251      * Shorthand version of {@linkplain #addConfiguredFileSet
252      * addConfiguredFileSet}.
253      * @param r reference id for fileset
254      * @since JWare/AntX 0.4
255      **/

256     public final void setFileSetId(Reference r)
257     {
258         require_(r!=null,"setFileset- nonzro ref");
259         if (isReference()) {
260             throw tooManyAttributes();
261         }
262         FileSet fs = new FileSet();
263         fs.setProject(getProject());
264         fs.setRefid(r);
265         addConfiguredFileSet(fs);
266     }
267
268
269
270     /**
271      * Adds a new mapping to this registry. The inserted mapping and
272      * this registry's ClassLoader must be compatible.
273      * @param pm new mapping (non-null)
274      * @throws UnsupportedOperationException if this registry is locked
275      **/

276     public void addConfiguredPrinter(PrinterMapping pm)
277     {
278         require_(pm!=null,"addMapin- nonzro");
279         if (isReference()) {
280             throw tooManyAttributes();
281         }
282         m_registry.add(pm);
283         edited("addPrinter");
284     }
285
286
287
288     /**
289      * Adds a new (arbitrary) mapping subclass to this registry. The
290      * inserted mapping and this registry's ClassLoader must be
291      * compatible.
292      * @param pm new mapping (non-null)
293      * @.expects Ant 1.6 or later
294      * @since JWare/AntX 0.4
295      **/

296     public final void addConfigured(PrinterMapping pm)
297     {
298         addConfiguredPrinter(pm);
299     }
300
301
302     /**
303      * Adds a new default mapping to this registry. The inserted
304      * mapping and this registry's ClassLoader must be compatible.
305      * @throws UnsupportedOperationException if this registry is locked
306      **/

307     public void addConfiguredDefaultPrinter(AnyPrinter ap)
308     {
309         require_(ap!=null,"addDfltPrntr- nonzro prntr");
310         if (isReference()) {
311             throw tooManyAttributes();
312         }
313         m_defaultPrinter = ap;
314         edited("addDefaultPrinter");
315     }
316
317
318     /**
319      * Adds a new (arbitrary) default mapping to this registry.
320      * The inserted mapping and this registry's ClassLoader must be
321      * compatible.
322      * @param ap new default mapping (non-null)
323      * @.expects Ant 1.6 or later
324      * @since JWare/AntX 0.4
325      **/

326     public final void addConfigured(AnyPrinter ap)
327     {
328         addConfiguredDefaultPrinter(ap);
329     }
330
331
332     /**
333      * Returns this registry's default printer. Never returns <i>null</i>
334      * @see AnyPrinter
335      **/

336     public DisplayStrategy getDefaultPrinter()
337     {
338         if (isReference()) {
339             return getRegistryRef().getDefaultPrinter();
340         }
341         return m_defaultPrinter;
342     }
343
344
345     /**
346      * Returns the first mapping that can display the given object.
347      * Returns <i>null</i> if unable to find a suitable mapping.
348      * @param instance object to be displayed (can be <i>null</i>)
349      **/

350     public PrinterMapping findMappingFor(Object JavaDoc instance)
351     {
352         if (isReference()) {
353             return getRegistryRef().findMappingFor(instance);
354         }
355         if (instance!=null) {
356             synchronized(m_registry) {//this doesn't happen often enuf to copy here
357
if (!m_registry.isEmpty()) {
358                     Iterator JavaDoc itr= m_registry.iterator();
359                     while (itr.hasNext()) {
360                         PrinterMapping pm = (PrinterMapping)itr.next();
361                         if (pm.isMatch(instance)) {
362                             return pm;
363                         }
364                     }
365                     itr=null;
366                 }
367             }//lock
368
}
369         return null;
370     }
371
372
373     /**
374      * Returns a best-fit display stratgy for the given object.
375      * If a this registry doesn't contain an explicit mapping,
376      * this method returns this registry's default strategy.
377      **/

378     public final DisplayStrategy getPrinterForNoNull(Object JavaDoc instance)
379     {
380         PrinterMapping pm = findMappingFor(instance);
381         return (pm!=null) ? pm.getPrinter() : getDefaultPrinter();
382     }
383
384
385     /**
386      * Returns this registry's reference strongly typed.
387      **/

388     protected final PrinterRegistry getRegistryRef()
389     {
390         return (PrinterRegistry)getCheckedRef(PrinterRegistry.class,"printer-registry");
391     }
392
393
394     private String JavaDoc m_Id;
395     private Collection JavaDoc m_registry;//NB:cannot be final to support 'clone' (ssmc)
396
private AnyPrinter m_defaultPrinter = new AnyPrinter();
397     private boolean m_haltIfError = true; //NB:barfs by default
398
}
399
400 /* end-of-PrinterRegistry.java */
401
Popular Tags