KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > martiansoftware > nailgun > builtins > NGAlias


1 /*
2
3   Copyright 2004, Martian Software, Inc.
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9   http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16
17 */

18
19 package com.martiansoftware.nailgun.builtins;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import com.martiansoftware.nailgun.Alias;
25 import com.martiansoftware.nailgun.NGContext;
26 import com.martiansoftware.nailgun.NGServer;
27
28 /**
29  * <p>Provides a means to view and add aliases. This is aliased by default
30  * to the command "<code>ng-alias</code>".</p>
31  *
32  * <p>No command line validation is performed. If you trigger an exception,
33  * your client will display it.</p>
34  *
35  * <p><b>To view the current alias list</b>, issue the command:
36  * <pre><code>ng-alias</code></pre>
37  * with no arguments.</p>
38  *
39  * <p><b>To add or replace an alias</b>, issue the command:
40  * <pre><code>ng-alias [alias name] [fully qualified aliased class name]</code></pre>
41  * </p>
42  *
43  * @author <a HREF="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
44  */

45 public class NGAlias {
46
47     private static String JavaDoc padl(String JavaDoc s, int len) {
48         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(s);
49         while(buf.length() < len) buf.append(" ");
50         return (buf.toString());
51     }
52     
53     public static void nailMain(NGContext context) throws ClassNotFoundException JavaDoc {
54         
55         String JavaDoc[] args = context.getArgs();
56         NGServer server = context.getNGServer();
57         
58         if (args.length == 0) {
59             Set JavaDoc aliases = server.getAliasManager().getAliases();
60             
61             // let's pad this nicely. first, find the longest alias
62
// name. then pad the others to that width.
63
int maxAliasLength = 0;
64             int maxClassnameLength = 0;
65             for (Iterator JavaDoc i = aliases.iterator(); i.hasNext();) {
66                 Alias alias = (Alias) i.next();
67                 maxAliasLength = Math.max(maxAliasLength, alias.getName().length());
68                 maxClassnameLength = Math.max(maxClassnameLength, alias.getAliasedClass().getName().length());
69             }
70             for (Iterator JavaDoc i = aliases.iterator(); i.hasNext();) {
71                 Alias alias = (Alias) i.next();
72                 context.out.println(padl(alias.getName(), maxAliasLength)
73                                         + "\t"
74                                         + padl(alias.getAliasedClass().getName(), maxClassnameLength));
75                 context.out.println(padl("", maxAliasLength) + "\t" + alias.getDescription());
76                 context.out.println();
77             }
78         } else if (args.length == 2) {
79             server.getAliasManager().addAlias(new Alias(args[0], "", Class.forName(args[1])));
80         }
81     }
82 }
83
Popular Tags