KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > lib > templates > TKListTag


1 package com.teamkonzept.lib.templates;
2
3 import java.io.*;
4 import java.util.*;
5 import java.text.*;
6
7 import com.oroinc.text.regex.*;
8
9 import com.teamkonzept.lib.*;
10 import org.apache.log4j.Category;
11
12 /**
13  * Listen werden aus dem Template gefiltert und gesetzt
14  * @author $Author: alex $
15  * @version $Revision: 1.38.2.1 $
16  */

17 public class TKListTag extends TKLabelTag
18 {
19     /** in welche Richtung soll sortiert werden ? */
20     private final static String JavaDoc DIRECTION = "DIRECTION";
21     
22     private final static String JavaDoc UP = "UP";
23     private final static String JavaDoc DOWN = "DOWN";
24     
25     /** Was fuer eine Typ hat das Sortierkriterium ? */
26     private final static String JavaDoc TYPE = "TYPE";
27     
28     /** numerisches Kriterium */
29     private final static String JavaDoc NUMBER = "NUMBER";
30     
31     /** Datum Kriterium - KEINE Zeit !!*/
32     private final static String JavaDoc DATE = "DATE";
33     
34     /** String Kriterium */
35     private final static String JavaDoc STRING = "STRING";
36     
37     /** Nach welchem Feld der Liste soll sortiert werden ?*/
38     private final static String JavaDoc FIELD = "FIELD";
39     
40     /** Kontrollvariable Start*/
41     private static final String JavaDoc START = "START";
42     
43     /** Kontrollvariable Ende*/
44     private static final String JavaDoc END = "END";
45     
46     /** Abbruchsvariable */
47     private static final String JavaDoc CONTROL = "CONTROL";
48     
49     /** das Innere der Liste */
50     public TKTemplateSyntax listText;
51     
52     public static final int TAG_TYPE = TKIfTag.TAG_TYPE+1;
53     
54
55     // Hier noch die Locale Problematik beachten !!!!
56
// Date formatiert ohne Zeit, deswegen hier der entsprechende Formatter
57
/** wird in TKListTag nicht mehr benutzt siehe Bugfix 492 */
58     static DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT);
59     /** zum formatieren eines Datums */
60     static DateFormat DATE_TIME_FORMAT = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
61
62
63
64
65     /** Parameter String */
66     public String JavaDoc paramsString = null;
67     
68     /** Parameter Werte */
69     private String JavaDoc direction = UP;
70     private String JavaDoc field = null;
71     private String JavaDoc type = STRING;
72     private String JavaDoc control = null;
73     private int start = 0;
74     private int end = Integer.MAX_VALUE;
75     
76     // Alte Notation
77
private boolean legacy = false;
78     
79     private final static Category CAT = Category.getInstance(TKListTag.class);
80     
81     /**
82      * Konstuktor
83      * Listen, die im Template existieren
84      *
85      * @param String def, Name des Tags
86      * @param PatternMatcherInput matcherInput
87      * @param boolean hasSubTags
88      */

89     public TKListTag( TKTemplateSyntax parent, String JavaDoc def, PatternMatcherInput matcherInput, boolean hasSubTags ) throws TKTemplateSyntaxException
90     {
91         super( parent, def, hasSubTags );
92         if( hasSubTags ) paramsString = def;
93         else label = getParams (def,false,null);
94         try {
95             listText = parent.newChild( matcherInput, parent.getSource() );
96         }
97         catch (ArrayIndexOutOfBoundsException JavaDoc e) { //thrown by TKTemplateSyntax.newTag()
98
throw new TKTemplateSyntaxException(parent.getSource(),"NOEND","LIST", def);
99         }
100         checkError(listText,"LIST", def);
101     }
102
103     /**
104         ist speziell angepasst, um nur die gueltigen Parameter zu erlauben,
105         die gueltigen Parameter sind oben in Konstanten definiert
106     */

107     protected void makeParam (StringBuffer JavaDoc name, StringBuffer JavaDoc value, boolean hasSubTags, TKTemplateData td)
108         throws TKTemplateSyntaxException
109     {
110         String JavaDoc valString = value == null ? "" : value.toString();
111         if (hasSubTags)
112         {
113             TKTemplateSyntax syntax = parent.newChild (valString,parent.getSource(),true);
114             
115             if (td != null)
116             {
117                 TKTemplateData subTd = new TKTemplateData (td);
118                 td = subTd;
119                 td.setLocalVariable("TEMPLATE_SYNTAX_DEFAULT_ENCODING",TKAnsiConverter.CONV_ID);
120             }
121             valString = syntax.apply (td);
122         }
123         String JavaDoc sName = name.toString().toUpperCase().trim();
124         if (sName.equals(DIRECTION))
125         {
126             if (valString.toUpperCase().equals(DOWN))
127                 direction = DOWN;
128             else if (valString.toUpperCase().equals(UP))
129                 direction = UP;
130             else
131                 throw new TKTemplateSyntaxException(parent.getSource(),"Ungueltiger Value fuer Direction : " , "LIST", valString);
132                 
133         }
134         else if (sName.equals(TYPE))
135         {
136             if (valString.toUpperCase().equals(STRING))
137                 type = STRING;
138             else if (valString.toUpperCase().equals(NUMBER))
139                 type = NUMBER;
140             else if (valString.toUpperCase().equals(DATE))
141                 type = DATE;
142             else
143                 throw new TKTemplateSyntaxException(parent.getSource(),"Ungueltiger Value fuer Type : " , "LIST", valString);
144         }
145         else if (sName.equals(FIELD))
146         {
147             field = valString;
148         }
149         else if (sName.equals(END))
150         {
151             end = toInt(valString);
152         }
153         else if (sName.equals(START))
154         {
155             start = toInt(valString);
156         }
157         else if (sName.equals(CONTROL))
158         {
159             control = valString;
160         }
161         else
162         {
163             throw new TKTemplateSyntaxException(parent.getSource(), "Ungueltiger Listparameter : " , "LIST", sName);
164         }
165     }
166
167     private int toInt(String JavaDoc in) throws TKTemplateSyntaxException
168     {
169         try
170         {
171             return Integer.parseInt(in);
172         }
173         catch (NumberFormatException JavaDoc e)
174         {
175             throw new TKTemplateSyntaxException(parent.getSource(), "Ungueltiger Listparameter : " , "LIST", in);
176         }
177     }
178     
179     protected String JavaDoc getParams( String JavaDoc def, boolean hasSubTags, TKTemplateData td) throws TKTemplateSyntaxException
180     {
181         int pos = def.indexOf(':');
182         if (pos < 0)
183         {
184             if (hasSubTags) return parent.newChild(def,parent.getSource(),hasSubTags).apply (td);
185             else return def;
186         }
187         int legacyPos = def.indexOf('=');
188         if (legacyPos < 0)
189         {
190             // Hier ist die Abbruchsbedingung nicht in der Form key=value angegeben
191
legacy = true;
192             return getRealLabel( td );
193         }
194         String JavaDoc label = def.substring( 0, pos++ );
195         if (hasSubTags) label = parent.newChild(label,parent.getSource(),hasSubTags).apply (td);
196         
197         StringBuffer JavaDoc name = null;
198         StringBuffer JavaDoc value = null;
199         boolean inName = true;
200
201         while (pos < def.length()) {
202         
203             char chr = def.charAt (pos++);
204
205             if (chr == ';') {
206             
207                 if (name != null) makeParam (name, value, hasSubTags, td);
208                 inName = true;
209                 name = null;
210                 value = null;
211                 continue;
212             }
213             
214             if (chr == '=') { inName = false; continue; }
215
216             if ((chr == '\\') && (pos < def.length())) chr = def.charAt (pos++);
217
218             (inName ? (name == null ? (name = new StringBuffer JavaDoc()) : name) :
219                   (value == null ? (value = new StringBuffer JavaDoc()) : value)) .append (chr);
220         }
221         
222         if (name != null) makeParam (name, value, hasSubTags, td);
223
224         return label;
225     }
226
227     /**
228      * Die entsprechenden Listen werden gefuellt und so bearbeitet,
229      * dass sie in das Template eingesetzt werden koennen.
230      *
231      * @param TKTemplateData td
232      * @return das Ergebnis als String
233      */

234     public String JavaDoc apply( TKTemplateData td ) throws TKTemplateSyntaxException
235     {
236         StringWriter wr = new StringWriter();
237         try
238         {
239             apply(td, wr);
240         }
241         catch (Exception JavaDoc e)
242         {
243             CAT.error("apply", e);
244         }
245         return wr.getBuffer().toString();
246     }
247
248     /**
249      * Die entsprechenden Listen werden gefuellt und so bearbeitet,
250      * dass sie in das Template eingesetzt werden koennen.
251      *
252      * @param td TKTemplateData
253      * @param writer Writer
254      */

255     public void apply(TKTemplateData td, Writer writer)
256         throws TKTemplateSyntaxException, IOException
257     {
258         String JavaDoc label = getRealLabel( td );
259         // LEGACY -> Frueher gab es nur die Control Variable !!
260
if (paramsString != null && !legacy) label = getParams(paramsString,true,td);
261         if (legacy)
262         {
263             int pos = label.indexOf(':');
264             if (pos >= 0)
265             {
266                 control = label.substring (pos+1,label.length());
267                 label = label.substring (0,pos);
268             }
269         }
270         // kann das bei XML gehen ?
271
if( td.getListIterator() == null )
272         {
273             return;
274         }
275         
276         int count = 0;
277         boolean cont = true;
278         LinkedList set = new LinkedList();
279         ComparableKey comp = new ComparableKey();
280         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
281         if (field != null)
282         {
283                 boolean loop = true;
284                 while (loop)
285                 {
286                     TKTemplate template = listText.getNewTemplate();
287                     template.setListIterator( td.getListIterator() );
288                     
289                     template.td.parent = td; // td sammeln und entsprechend des Kriteriums sortieren
290
loop = td.getListIterator().apply( template, count++, label );
291                     if (loop)
292                     {
293                         // Object key = template.td.variables.get(field);
294
int index = 0;
295                         boolean greater =true;
296                         while (greater)
297                         {
298                             if (set.size() == index)
299                             {
300                                 set.add(template);
301                                 greater = false;
302                             }
303                             else
304                             {
305                                 TKTemplate temp = (TKTemplate)set.get(index);
306                                 if (comp.compare(temp, template) <= 0)
307                                 {
308                                     set.add(index, template);
309                                     greater = false;
310                                 }
311                             }
312                             index++;
313                         }
314                         // set.add(template);
315
}
316                 }
317                 Iterator iterator = set.iterator();
318                 
319                 count = 0;
320                 boolean noCont = true;
321                 while (iterator.hasNext() && noCont)
322                 {
323                     TKTemplate temp = (TKTemplate)iterator.next();
324                     if (count >= start && count < end)
325                     {
326                         temp.td.setMergedApply(td );
327                         result.append( listText.apply( temp.td ) );
328                         String JavaDoc controlVal = control == null ? null :(String JavaDoc) temp.td.getVariable(control);
329                         if (controlVal != null && controlVal.trim().equals("0"))
330                             noCont = false;
331                     }
332                     count++;
333                 }
334         }
335         else
336         {
337             while( cont )
338             {
339                 TKTemplate template = listText.getNewTemplate();
340                 template.setListIterator( td.getListIterator() );
341                 template.td.parent = td;
342                 cont = (td.getListIterator().apply( template, count++, label ) );
343                 if( cont )
344                 {
345                     template.td.setMergedApply( td ); // Variablen von aussen nach innen geben
346
String JavaDoc chunk = listText.apply( template.td );
347                     
348                     String JavaDoc controlVal = control == null ? null :
349                     (String JavaDoc) template.td.getVariable(control);
350                     if (controlVal != null && controlVal.trim().equals("-1"))
351                     {
352                         cont = false;
353                     }
354                     else if (controlVal != null && controlVal.trim().equals("0"))
355                     {
356                         cont = false;
357                         result.append(chunk);
358                     }
359                     else
360                     {
361                         result.append(chunk);
362                     }
363                 }
364                 template = null;
365             }
366         }
367         writer.write(result.toString());
368     }
369     
370     class ComparableKey implements Comparator
371     {
372         ComparableKey()
373         {
374         }
375         
376         /**
377             uerbergeben werden TKTemplates !!!
378         */

379         public int compare(Object JavaDoc o1, Object JavaDoc o2)
380         {
381             TKTemplate t1 = (TKTemplate)o1;
382             TKTemplate t2 = (TKTemplate)o2;
383             Object JavaDoc value1 = t1.td.getVariable(field);
384             Object JavaDoc value2 = t2.td.getVariable(field);
385             try
386             {
387                 if (value1 == null && value2 != null)
388                     return direction.equals(UP) ? -1:1;
389                 if (value1 == null && value2 == null)
390                     return 0;
391                 if (value1 != null && value2 == null)
392                     return direction.equals(UP) ? 1:-1;
393                 if (value1 != null && value2 != null)
394                 {
395                     if (type.equals(STRING))
396                     {
397                         if (direction.equals(UP))
398                             return ((String JavaDoc)value1).compareTo((String JavaDoc)value2);
399                         else
400                             return ((String JavaDoc)value2).compareTo((String JavaDoc)value1);
401                     }
402                     else if (type.equals(NUMBER))
403                     {
404                         Integer JavaDoc i1 = new Integer JavaDoc(value1.toString());
405                         Integer JavaDoc i2 = new Integer JavaDoc(value2.toString());
406                         if (direction.equals(UP))
407                             return i1.intValue() - i2.intValue();
408                         else
409                             return i2.intValue() - i1.intValue();
410                     }
411                     else if (type.equals(DATE))
412                     {
413                         Date d1;
414                         try
415                         {
416                             d1 = TKListTag.DATE_TIME_FORMAT.parse((String JavaDoc)value1);
417                         }
418                         catch (Exception JavaDoc parseError)
419                         {
420                             // vielleicht klappt Parsen ohne Zeit ?
421
d1 = TKListTag.DATE_FORMAT.parse((String JavaDoc)value1);
422                         }
423                         Date d2;
424                         try
425                         {
426                             d2 = TKListTag.DATE_TIME_FORMAT.parse((String JavaDoc)value2);
427                         }
428                         catch (Exception JavaDoc parseError)
429                         {
430                             // vielleicht klappt Parsen ohne Zeit ?
431
d2 = TKListTag.DATE_FORMAT.parse((String JavaDoc)value2);
432                         }
433                         if (d1.before(d2))
434                             return direction.equals(UP) ? -1:1;
435                         else if (d1.after(d2))
436                             return direction.equals(UP) ? 1:-1;
437                         return 0;
438                     }
439                 }
440             }
441             catch (Exception JavaDoc e)
442             {
443                 CAT.error("ComparableList : " ,e);
444             }
445             return 0;
446         }
447     }
448 }
449
450
451
Popular Tags