001    package railo.transformer.library.tag;
002    
003    import java.io.IOException;
004    import java.lang.reflect.Method;
005    import java.util.Date;
006    
007    import railo.commons.lang.ClassUtil;
008    import railo.commons.lang.Md5;
009    import railo.commons.lang.StringUtil;
010    
011    
012    /**
013     * Die Klasse TagLibTagAttr repraesentiert ein einzelnes Attribute eines Tag 
014     * und haelt saemtliche Informationen zu diesem Attribut.
015     */
016    public final class TagLibTagAttr {
017    
018            public static final short SCRIPT_SUPPORT_NONE = 0;
019            public static final short SCRIPT_SUPPORT_OPTIONAL = 1;
020            public static final short SCRIPT_SUPPORT_REQUIRED = 2;
021            
022            private String name="noname";
023            private String type;
024            private String description="";
025            private boolean required;
026            private boolean rtexpr=true;
027            private Object defaultValue;
028        private TagLibTag tag;
029            private boolean hidden;
030            private boolean _default;
031            private boolean noname;
032            private short status=TagLib.STATUS_IMPLEMENTED;
033            private short scriptSupport=SCRIPT_SUPPORT_NONE;
034    
035            
036            public TagLibTagAttr duplicate(TagLibTag tag) {
037                    TagLibTagAttr tlta=new TagLibTagAttr(tag);
038                    tlta.name=name;
039                    tlta.type=type;
040                    tlta.description=description;
041                    tlta.required=required;
042                    tlta.rtexpr=rtexpr;
043                    tlta.defaultValue=defaultValue;
044                    tlta.hidden=hidden;
045                    tlta.noname=noname;
046                    tlta._default=_default;
047                    tlta.status=status;
048                    
049                    
050                    return tlta;
051            }
052            
053            
054            /**
055             * @return the status (TagLib.,TagLib.STATUS_IMPLEMENTED,TagLib.STATUS_DEPRECATED,TagLib.STATUS_UNIMPLEMENTED)
056             */
057            public short getStatus() {
058                    return status;
059            }
060    
061    
062            /**
063             * @param status the status to set (TagLib.,TagLib.STATUS_IMPLEMENTED,TagLib.STATUS_DEPRECATED,TagLib.STATUS_UNIMPLEMENTED)
064             */
065            public void setStatus(short status) {
066                    this.status = status;
067            }
068    
069            /**
070             * Geschuetzer Konstruktor ohne Argumente.
071             */
072            public TagLibTagAttr(TagLibTag tag) {
073                this.tag=tag;
074            }
075    
076            /**
077             * Gibt den Namen des Attribut zurueck.
078             * @return Name des Attribut.
079             */
080            public String getName() {
081                    return name;
082            }
083    
084            /**
085             * Gibt zurueck, ob das Attribut Pflicht ist oder nicht.
086             * @return Ist das Attribut Pflicht.
087             */
088            public boolean isRequired() {
089                    return required;
090            }
091    
092            /**
093             * Gibt den Typ des Attribut zur�ck (query, struct, string usw.)
094             * @return Typ des Attribut
095             */
096            public String getType() {
097                if(this.type==null) {
098                    try {
099                    String methodName=  "set"+
100                            (name.length()>0?""+Character.toUpperCase(name.charAt(0)):"")+
101                            (name.length()>1?name.substring(1):"");
102                    
103                    Class clazz= ClassUtil.loadClass(tag.getTagClassName(),(Class)null);//Class.orName(tag.getTagClassName());
104                if(clazz!=null) {
105                    Method[] methods = clazz.getMethods();
106                    for(int i=0;i<methods.length;i++) {
107                        Method method = methods[i];
108                        if(method.getName().equalsIgnoreCase(methodName)) {
109                            Class[] types = method.getParameterTypes();
110                            if(types.length==1) {
111                                    Class type=types[0];
112                                if(type==String.class)this.type="string";
113                                else if(type==double.class)this.type="number";
114                                else if(type==Date.class)this.type="datetime";
115                                else this.type=type.getName();
116                            }
117                        }
118                    }
119                }
120                    }
121                    catch(Throwable t) {
122                            
123                            return "string";
124                    }
125                }
126                    return this.type;
127            }
128    
129            /**
130             * Gibt zurueck ob das Attribute eines Tag, mithilfe des ExprTransformer, uebersetzt werden soll oder nicht.
131             * @return Soll das Attribut uebbersetzt werden
132             */
133            public boolean getRtexpr() {
134                    return rtexpr;
135            }
136    
137            /**
138             * Setzt den Namen des Attribut.
139             * @param name Name des Attribut.
140             */
141            public void setName(String name) {
142                    this.name = name.toLowerCase();
143            }
144    
145            /**
146             * Setzt, ob das Argument Pflicht ist oder nicht.
147             * @param required Ist das Attribut Pflicht.
148             */
149            public void setRequired(boolean required) {
150                    this.required = required;
151            }
152    
153            /**
154             * Setzt, ob das Attribute eines Tag, mithilfe des ExprTransformer, uebersetzt werden soll oder nicht.
155             * @param rtexpr Soll das Attribut uebbersetzt werden
156             */
157            public void setRtexpr(boolean rtexpr) {
158                    this.rtexpr = rtexpr;
159            }
160    
161            /**
162             * Setzt, den Typ des Attribut (query, struct, string usw.)
163             * @param type Typ des Attribut.
164             */
165            public void setType(String type) {
166                    this.type = type;
167            }
168    
169            /**
170             * @return Returns the description.
171             */
172            public String getDescription() {
173                    return description;
174            }
175    
176            /**
177             * @param description The description to set.
178             */
179            public void setDescription(String description) {
180                    this.description = description;
181            }
182    
183        /**
184         * @param defaultValue
185         */
186        public void setDefaultValue(Object defaultValue) {
187            this.defaultValue=defaultValue;
188            tag.setHasDefaultValue(true);
189        }
190    
191        /**
192         * @return Returns the defaultValue.
193         */
194        public Object getDefaultValue() {
195            return defaultValue;
196        }
197    
198        /**
199         * @return
200         */
201        public boolean hasDefaultValue() {
202            return defaultValue!=null;
203        }
204    
205            public void setHidden(boolean hidden) {
206                    this.hidden=hidden;
207            }
208            public boolean getHidden() {
209                    return hidden;
210            }
211    
212            public void setNoname(boolean noname) {
213                    this.noname=noname;
214            }
215            public boolean getNoname() {
216                    return noname;
217            }
218    
219            public String getHash() {
220                    StringBuffer sb=new StringBuffer();
221                    sb.append(this.getDefaultValue());
222                    sb.append(this.getName());
223                    sb.append(this.getRtexpr());
224                    sb.append(this.getType());
225                    
226                    try {
227                            return Md5.getDigestAsString(sb.toString());
228                    } catch (IOException e) {
229                            return "";
230                    }
231            }
232    
233            public void isDefault(boolean _default) {
234                    if(_default)
235                            tag.setDefaultAttribute(this);
236                    this._default=_default;
237            }
238    
239            public boolean isDefault() {
240                    return _default;
241            }
242    
243    
244            public void setScriptSupport(String str) {
245                    if(!StringUtil.isEmpty(str))  {
246                            str=str.trim().toLowerCase();
247                            if("optional".equals(str)) this.scriptSupport=SCRIPT_SUPPORT_OPTIONAL;
248                            else if("opt".equals(str)) this.scriptSupport=SCRIPT_SUPPORT_OPTIONAL;
249                            else if("required".equals(str)) this.scriptSupport=SCRIPT_SUPPORT_REQUIRED;
250                            else if("req".equals(str)) this.scriptSupport=SCRIPT_SUPPORT_REQUIRED;
251                    }
252            }
253    
254    
255            /**
256             * @return the scriptSupport
257             */
258            public short getScriptSupport() {
259                    return scriptSupport;
260            }
261    
262    
263            public Object getScriptSupportAsString() {
264                    if(scriptSupport==SCRIPT_SUPPORT_OPTIONAL) return "optional";
265                    if(scriptSupport==SCRIPT_SUPPORT_REQUIRED) return "required";
266                    return "none";
267            }
268    
269    }