01: /*
02: * Created on 26 Feb 2007
03: */
04: package uk.org.ponder.rsf.renderer;
05:
06: import java.util.HashMap;
07: import java.util.Map;
08:
09: import uk.org.ponder.rsf.components.UIComponent;
10: import uk.org.ponder.rsf.components.decorators.UIDecorator;
11: import uk.org.ponder.rsf.components.decorators.UIIDStrategyDecorator;
12: import uk.org.ponder.rsf.content.ContentTypeInfo;
13: import uk.org.ponder.rsf.template.XMLLump;
14:
15: /** Implements the strategy as represented by any {@link UIIDStrategyDecorator}
16: * registered either on a tag, or content type, for assigning an XML id to the
17: * tag in the output markup.
18: *
19: * @author Antranig Basman (antranig@caret.cam.ac.uk)
20: *
21: */
22:
23: public class IDAssigner {
24: private static final int[] USED_ONCE = new int[] { 1 };
25: private Map assigned = new HashMap();
26: private String defaultstrategy;
27:
28: public IDAssigner(String defaultstrategy) {
29: this .defaultstrategy = defaultstrategy;
30: }
31:
32: public void adjustForID(Map attrcopy, UIComponent component) {
33: String ID = null;
34: String IDstrategy = defaultstrategy;
35:
36: if (component.decorators != null) {
37: for (int i = 0; i < component.decorators.size(); ++i) {
38: UIDecorator dec = component.decorators.decoratorAt(i);
39: if (dec instanceof UIIDStrategyDecorator) {
40: UIIDStrategyDecorator ids = (UIIDStrategyDecorator) dec;
41: IDstrategy = ids.IDStrategy;
42: if (ids.IDStrategy
43: .equals(UIIDStrategyDecorator.ID_MANUAL)) {
44: ID = ids.ID;
45: }
46: break;
47: }
48: }
49: }
50: if (ID == null) {
51: if (IDstrategy.equals(ContentTypeInfo.ID_FORCE)
52: || (IDstrategy.equals(ContentTypeInfo.ID_FULL) && attrcopy
53: .get("id") != null)) {
54: ID = component.getFullID();
55: }
56: }
57: if (ID != null) {
58: int[] assnum = (int[]) assigned.get(ID);
59: if (assnum != null) {
60: ID = ID + "!" + Integer.toString(assnum[0]);
61: if (assnum == USED_ONCE) {
62: assnum = new int[] { 1 };
63: assigned.put(ID, assnum);
64: }
65: ++assnum[0];
66: } else {
67: assigned.put(ID, USED_ONCE);
68: }
69: attrcopy.put("id", ID);
70: }
71: if (!IDstrategy.equals(ContentTypeInfo.ID_RSF)) {
72: attrcopy.remove(XMLLump.ID_ATTRIBUTE);
73: }
74: }
75:
76: }
|