KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > tagshandler > TagDefTagsHandler


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.tagshandler;
6
7 import java.util.Properties JavaDoc;
8
9 import org.apache.commons.logging.Log;
10
11 import xdoclet.XDocletException;
12 import xdoclet.XDocletMessages;
13 import xdoclet.XDocletTagSupport;
14 import xdoclet.template.TemplateException;
15 import xdoclet.template.TemplateTagHandler;
16 import xdoclet.util.LogUtil;
17 import xdoclet.util.Translator;
18
19 /**
20  * The implementation of TagDef template tag. It's mainly designed for use by end users that want to define template
21  * tags of their own but don't want to touch xdoclet's tag mapping file and modify the jar file.
22  *
23  * @author Ara Abrahamian (ara_e@email.com)
24  * @created Nov 11, 2001
25  * @xdoclet.taghandler namespace="TagDef"
26  * @version $Revision: 1.8 $
27  */

28 public class TagDefTagsHandler extends XDocletTagSupport
29 {
30     /**
31      * Defines a template tag handler for a template tag to TemplateEngine.
32      *
33      * @param attributes The attributes of the template tag
34      * @return Description of the Returned Value
35      * @exception XDocletException Description of Exception
36      * @doc.tag type="content"
37      * @doc.param name="namespace" optional="false" description="The template namespace name, 'Merge'
38      * for example if we were to define template namespace 'Merge' this way."
39      * @doc.param name="handler" optional="false" description="The template tag handler full qualified
40      * class name. It's the class that implements tags of namespace. It should be a public class, with a no
41      * argument public constructor, and should extend xdoclet.XDocletTagSupport."
42      */

43     public String JavaDoc tagDef(Properties JavaDoc attributes) throws XDocletException
44     {
45         Log log = LogUtil.getLog(TagDefTagsHandler.class, "tagDef");
46
47         String JavaDoc namespace = attributes.getProperty("namespace");
48         String JavaDoc handlerFullClassName = attributes.getProperty("handler");
49
50         if (log.isDebugEnabled()) {
51             log.debug("namespace=" + namespace);
52             log.debug("handler=" + handlerFullClassName);
53         }
54
55         if (namespace == null) {
56             mandatoryTemplateTagParamNotFound("tagDef", "namespace");
57         }
58
59         if (handlerFullClassName == null) {
60             mandatoryTemplateTagParamNotFound("tagDef", "handler");
61         }
62
63         Class JavaDoc handlerClass = null;
64
65         try {
66             handlerClass = getClass().getClassLoader().loadClass(handlerFullClassName);
67
68             Object JavaDoc handlerInstance = handlerClass.newInstance();
69
70             getEngine().setTagHandlerFor(namespace, (TemplateTagHandler) handlerInstance);
71         }
72         catch (ClassNotFoundException JavaDoc e) {
73             String JavaDoc msg = Translator.getString(XDocletMessages.class, XDocletMessages.CLASS_NOT_FOUND,
74                 new String JavaDoc[]{handlerFullClassName});
75
76             log.error(msg, e);
77             throw new XDocletException(e, msg);
78         }
79         catch (InstantiationException JavaDoc e) {
80             String JavaDoc msg = Translator.getString(XDocletMessages.class, XDocletTagshandlerMessages.TAGDEF_INSTANTIATION_EXCEPTION,
81                 new String JavaDoc[]{handlerClass.toString()});
82
83             log.error(msg, e);
84             throw new XDocletException(e, msg);
85         }
86         catch (IllegalAccessException JavaDoc e) {
87             String JavaDoc msg = Translator.getString(XDocletMessages.class, XDocletTagshandlerMessages.TAGDEF_ILLEGALACCESS_EXCEPTION,
88                 new String JavaDoc[]{handlerClass.toString()});
89
90             log.error(msg, e);
91             throw new XDocletException(e, msg);
92         }
93         catch (TemplateException e) {
94             String JavaDoc msg = Translator.getString(XDocletMessages.class, XDocletTagshandlerMessages.TAGDEF_COULDNT_DEF_HANDLER,
95                 new String JavaDoc[]{handlerFullClassName, namespace});
96
97             log.error(msg, e);
98             throw new XDocletException(e, msg);
99         }
100
101         return "";
102     }
103 }
104
Popular Tags