1 5 package xdoclet.tagshandler; 6 7 import java.util.HashMap ; 8 import java.util.Map ; 9 import java.util.Properties ; 10 import java.util.StringTokenizer ; 11 import org.apache.commons.logging.Log; 12 13 import xdoclet.XDocletException; 14 import xdoclet.XDocletMessages; 15 import xdoclet.XDocletTagSupport; 16 import xdoclet.util.LogUtil; 17 import xdoclet.util.Translator; 18 import xdoclet.util.TypeConversionUtil; 19 20 26 public class IdTagsHandler extends XDocletTagSupport 27 { 28 private static Map prefixHash = new HashMap (); 29 30 33 public static void reset() 34 { 35 prefixHash.clear(); 36 } 37 38 44 private static String getIdByTagValues(String tagName, String paramNames) throws XDocletException 45 { 46 Log log = LogUtil.getLog(IdTagsHandler.class, "getIdByTagValues"); 47 48 if (tagName == null) { 49 log.error(Translator.getString(XDocletMessages.class, XDocletTagshandlerMessages.ID_PARAM_MISSING, new String []{"tagName"})); 50 return ""; 51 } 52 53 if (paramNames == null) { 54 log.error(Translator.getString(XDocletMessages.class, XDocletTagshandlerMessages.ID_PARAM_MISSING, new String []{"paramNames"})); 55 return ""; 56 } 57 58 StringTokenizer st = new StringTokenizer (paramNames, ",", false); 59 60 while (st.hasMoreTokens()) { 61 String paramValue = getTagValue( 62 FOR_CLASS, 63 tagName, 64 st.nextToken(), 65 null, 66 null, 67 true, 68 false 69 ); 70 71 if (paramValue != null) { 72 return paramValue.replace('/', '_'); 73 } 74 } 75 76 return tagName; 77 } 78 79 89 public String prefixedId(Properties attributes) throws XDocletException 90 { 91 String useIdsParamName = getDocletContext().getActiveSubTask().getSubTaskName() + ".useIds"; 92 boolean useIds = ((Boolean ) getDocletContext().getConfigParam(useIdsParamName)).booleanValue(); 93 94 if (useIds == false) 95 return ""; 96 97 String prefixName = attributes.getProperty("prefix"); 98 String wrapInIdEqualsStr = attributes.getProperty("wrapInIdEquals"); 99 boolean wrapInIdEquals = TypeConversionUtil.stringToBoolean(wrapInIdEqualsStr, true); 100 101 if (prefixName == null) 102 throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletTagshandlerMessages.ATTRIBUTE_NOT_SET_ERROR, new String []{"prefix"})); 103 104 StringBuffer sbuf = new StringBuffer (""); 105 106 if (wrapInIdEquals) 107 sbuf.append("id=\""); 108 109 if (prefixHash.containsKey(prefixName)) { 110 Integer val = (Integer ) prefixHash.get(prefixName); 111 Integer valPlusOne = new Integer ((val.intValue()) + 1); 112 113 prefixHash.put(prefixName, valPlusOne); 114 sbuf.append(prefixName); 115 sbuf.append('_'); 116 sbuf.append(valPlusOne); 117 } 118 else { 119 prefixHash.put(prefixName, new Integer (1)); 120 sbuf.append(prefixName); 121 sbuf.append("_1"); 122 } 123 124 if (wrapInIdEquals) { 125 sbuf.append('"'); 126 } 127 128 return sbuf.toString(); 129 } 130 131 145 public String id(Properties attributes) throws XDocletException 146 { 147 String tagName = attributes.getProperty("tagName"); 148 String paramNames = attributes.getProperty("paramNames"); 149 150 return getIdByTagValues(tagName, paramNames); 151 } 152 } 153 | Popular Tags |