View Javadoc

1   /*
2    * Copyright (c) 2001-2003 The XDoclet team
3    * All rights reserved.
4    */
5   package xjavadoc;
6   
7   import java.util.HashMap;
8   import java.util.Map;
9   import java.util.StringTokenizer;
10  
11  /***
12   * Creates XTag instances.
13   *
14   * @author    Aslak Hellesøy
15   * @created   10. februar 2002
16   */
17  public final class XTagFactory
18  {
19  
20  	/***
21  	 * Maps tag name to XTag class.
22  	 */
23  	private final Map _tagClasses = new HashMap();
24  	private boolean _isValidating = false;
25  
26      public XTagFactory() {
27  		// ignore standard tags. See:
28  		// http://java.sun.com/j2se/1.4.1/docs/tooldocs/windows/javadoc.html#javadoctags
29  		setIgnoredTags( "author,deprecated,exception,param,return,see,serial,serialData,serialField,since,throws,version" );
30  	}
31  
32  	public boolean isValidating()
33  	{
34  		return _isValidating;
35  	}
36  
37  	public void setValidating( boolean isValidating )
38  	{
39  		_isValidating = isValidating;
40  	}
41  
42  	/***
43  	 * Set the name of the tags that shouldn't be validated against.
44  	 *
45  	 * @param tags
46  	 */
47  	public void setIgnoredTags( String tags )
48  	{
49  		StringTokenizer st = new StringTokenizer( tags, "," );
50  
51  		while( st.hasMoreTokens() )
52  		{
53  			registerTagClass( st.nextToken(), DefaultXTag.class );
54  		}
55  	}
56  
57  	/***
58  	 * Creates a new XTag. If a special tag class has been previously registeres,
59  	 * an instance of the corresponding class will be returned. This allows for
60  	 * special tag implementations.
61  	 *
62  	 * @param tagName                  name of the tag, without the '@'
63  	 * @param text                     content of the tag. Will be parsed into
64  	 *      attributes.
65  	 * @param doc
66  	 * @param lineNumber
67  	 * @return                         an instance of XTag
68  	 * @exception UnknownTagException
69  	 * @throws TagValidationException  if validation is activated and an unknown
70  	 *      tag was encountered.
71  	 */
72  	public XTag createTag( String tagName, String text, XDoc doc, int lineNumber ) throws UnknownTagException
73  	{
74  		tagName = XDoc.dotted( tagName );
75  
76  		// Let's see if there is a custom class for that tag
77  		Class tagClass = ( Class ) _tagClasses.get( tagName );
78  		DefaultXTag tag;
79  
80  		if( tagClass != null )
81  		{
82  			try
83  			{
84  				tag = ( DefaultXTag ) tagClass.newInstance();
85  			}
86  			catch( InstantiationException e )
87  			{
88  				e.printStackTrace();
89  				throw new IllegalStateException( e.getMessage() );
90  			}
91  			catch( IllegalAccessException e )
92  			{
93  				e.printStackTrace();
94  				throw new IllegalStateException( e.getMessage() );
95  			}
96  		}
97  		else
98  		{
99  			tag = new DefaultXTag();
100 		}
101 		tag.init( tagName, text, doc, lineNumber );
102 
103 		// Throw validation ex if validation is on and the tag is unknown
104 		if( _isValidating && ( tagClass == null ) )
105 		{
106 			throw new UnknownTagException( tag );
107 		}
108 		return tag;
109 	}
110 
111 	public void registerTagClass( String tagName, Class tagClass )
112 	{
113         Class old = (Class) _tagClasses.get( XDoc.dotted(tagName) );
114         if( old != null ) {
115             throw new IllegalStateException( "The tag @" + XDoc.dotted(tagName) +
116                     " has already been mapped to " + old.getName() +
117                     ". Can't reregister it to " + tagClass.getName());
118         }
119 		_tagClasses.put( XDoc.dotted( tagName ), tagClass );
120 	}
121 }