KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > xmlEngine > TemplateConfiguration


1 /*
2  ************************************************************************************
3  * Copyright (C) 2001-2006 Openbravo S.L.
4  * Licensed under the Apache Software License version 2.0
5  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software distributed
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations under the License.
10  ************************************************************************************
11 */

12 package org.openbravo.xmlEngine;
13
14 import java.util.Vector JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.Stack JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import org.xml.sax.Attributes JavaDoc;
19 import org.xml.sax.helpers.DefaultHandler JavaDoc;
20
21 import org.apache.log4j.Logger ;
22 /**
23   The configuration information of a XmlTemplate
24  **/

25 class TemplateConfiguration extends DefaultHandler JavaDoc {
26   Stack JavaDoc<Object JavaDoc> stcElement; // Stack of Elements
27
String JavaDoc strElement;
28   DataTemplate activeDataTemplate = null;
29   Vector JavaDoc<Object JavaDoc> hasDataTemplate;
30   XmlEngine xmlEngine; //need for add subreports to the xmlEngine
31
Hashtable JavaDoc<String JavaDoc, ParameterTemplate> hasParameter;
32   XmlTemplate xmlTemplate;
33   HashtableMultiple hashtable;
34   String JavaDoc id;
35   String JavaDoc strAttribute;
36   String JavaDoc strReplace;
37   String JavaDoc strBooleanAttribute = null;
38   XmlComponentTemplate xmlComponentBooleanWithId = null;
39   XmlComponentTemplate xmlComponentArg1;
40   XmlComponentTemplate xmlComponentArg2;
41   String JavaDoc strFunction;
42   FormatCouple formatCouple;
43   Vector JavaDoc<ReplaceElement> vecReplace;
44   String JavaDoc strTemplate;
45   String JavaDoc strDriverDefault;
46   String JavaDoc strUrlDefault;
47   XmlTemplate subreport;
48   String JavaDoc sectionSubreport;
49   static Logger log4jTemplateConfiguration = Logger.getLogger(TemplateConfiguration.class);
50   StringBuffer JavaDoc buffer;
51
52
53   public TemplateConfiguration (Vector JavaDoc<Object JavaDoc> hasDataTemplate, XmlEngine xmlEngine, Hashtable JavaDoc<String JavaDoc, ParameterTemplate> hasParameter, XmlTemplate xmlTemplate) { // XmlEngineNP: unico argumento en el constructor: this, y eliminar las tres variables actuales, guardar solo XmlTemplate
54
this.hasDataTemplate = hasDataTemplate;
55     this.xmlEngine = xmlEngine;
56     this.hasParameter = hasParameter;
57     this.xmlTemplate = xmlTemplate;
58     stcElement = new Stack JavaDoc<Object JavaDoc>();
59     hashtable = new HashtableMultiple(); // XmlEngineNP: pass XmlTemplatepasarlo to XmlTemplate, in order to keep it after the reading, due
60
// to it will be necessary to create the XmlDocument-s, look for the element with its Id
61
}
62
63   private void pushElement(String JavaDoc name) {
64     stcElement.push(name);
65     strElement = name;
66   }
67
68   private void popElement() {
69     strElement = (String JavaDoc)stcElement.pop();
70     if (!stcElement.isEmpty()) strElement = (String JavaDoc)stcElement.peek();
71   }
72
73   public void processingInstruction (String JavaDoc name, String JavaDoc remainder) {
74     log4jTemplateConfiguration.info("TemplateConfiguration: processingInstruction " + name);
75   }
76
77   public void startElement(java.lang.String JavaDoc uri,
78       java.lang.String JavaDoc name,
79       java.lang.String JavaDoc qName,
80       Attributes JavaDoc amap) { //throws SAXException {
81
log4jTemplateConfiguration.info("TemplateConfiguration: startElement is called: " + name);
82     readBuffer();
83     if (name.trim().equalsIgnoreCase("FIELD")) {
84       pushElement(name);
85       id = null;
86       strAttribute = null;
87       strReplace = null;
88       strBooleanAttribute = null;
89       String JavaDoc strBooleanWithId = null;
90       formatCouple = new FormatCouple();
91       vecReplace = null;
92       for (int i = 0; i < amap.getLength(); i++) {
93         if (amap.getQName(i).equals("id")) {
94           id = amap.getValue(i);
95         } else if (amap.getQName(i).equals("attribute")) {
96           strAttribute = amap.getValue(i);
97         } else if (amap.getQName(i).equals("replace")) {
98           strReplace = amap.getValue(i);
99         } else if (amap.getQName(i).equals("boolean")) {
100           strBooleanAttribute = amap.getValue(i);
101         } else if (amap.getQName(i).equals("withId")) {
102           strBooleanWithId = amap.getValue(i);
103           xmlComponentBooleanWithId = (XmlComponentTemplate)hashtable.get(strBooleanWithId);
104         } else if (amap.getQName(i).equals("format")) {
105           formatCouple = xmlEngine.formatHashtable.get(amap.getValue(i));
106           if (formatCouple == null) {
107             log4jTemplateConfiguration.warn(" format " + amap.getValue(i) + " not found");
108           }
109         } else if (amap.getQName(i).equals("replaceCharacters")) {
110           vecReplace = xmlEngine.replaceHashtable.get(amap.getValue(i));
111           if (vecReplace == null) {
112             log4jTemplateConfiguration.warn(" replaceCharacters " + amap.getValue(i) + " not found");
113           }
114         }
115       }
116     } else if (name.trim().equalsIgnoreCase("FUNCTION")) {
117       pushElement(name);
118       id = null;
119       strAttribute = null;
120       strReplace = null;
121       strBooleanAttribute = null;
122       formatCouple = new FormatCouple();
123       String JavaDoc strBooleanWithId = null;
124       xmlComponentArg1 = null;
125       xmlComponentArg2 = null;
126       for (int i = 0; i < amap.getLength(); i++) {
127         log4jTemplateConfiguration.info(" FUNCTION: attribute name="+amap.getQName(i)+" value="+amap.getValue(i));
128         if (amap.getQName(i).equals("id")) {
129           id = amap.getValue(i);
130         } else if (amap.getQName(i).equals("attribute")) {
131           strAttribute = amap.getValue(i);
132         } else if (amap.getQName(i).equals("replace")) {
133           strReplace = amap.getValue(i);
134         } else if (amap.getQName(i).equals("boolean")) {
135           strBooleanAttribute = amap.getValue(i);
136         } else if (amap.getQName(i).equals("withId")) {
137           strBooleanWithId = amap.getValue(i);
138           xmlComponentBooleanWithId = (XmlComponentTemplate)hashtable.get(strBooleanWithId);
139         } else if (amap.getQName(i).equals("name")) {
140           // strFunctionOfElement = new String(strElement);
141
strFunction = amap.getValue(i);
142         } else if (amap.getQName(i).equals("format")) {
143           formatCouple = xmlEngine.formatHashtable.get(amap.getValue(i));
144           if (formatCouple == null) {
145             log4jTemplateConfiguration.warn(" format " + amap.getValue(i) + " not found");
146           }
147         } else if (amap.getQName(i).equals("arg1")) {
148           String JavaDoc strArg1 = amap.getValue(i);
149           xmlComponentArg1 = (XmlComponentTemplate)hashtable.get(strArg1);
150         } else if (amap.getQName(i).equals("arg2")) {
151           String JavaDoc strArg2 = amap.getValue(i);
152           xmlComponentArg2 = (XmlComponentTemplate)hashtable.get(strArg2);
153         }
154       }
155     } else if (name.trim().equalsIgnoreCase("SUBREPORT")) {
156       pushElement(name);
157       String JavaDoc strSubreportName = null;
158       String JavaDoc strSubreportFile = null;
159       id = null;
160       sectionSubreport = null;
161       for (int i = 0; i < amap.getLength(); i++) {
162         log4jTemplateConfiguration.info(" SUBREPORT: attribute name="+amap.getQName(i)+" value="+amap.getValue(i));
163         if (amap.getQName(i).equals("id")) {
164           id = amap.getValue(i);
165         } else if (amap.getQName(i).equals("name")) {
166           strSubreportName = amap.getValue(i);
167         } else if (amap.getQName(i).equals("report")) {
168           strSubreportFile = amap.getValue(i);
169         } else if (amap.getQName(i).equals("section")) {
170           sectionSubreport = amap.getValue(i);
171         }
172       }
173       subreport = xmlEngine.readAllXmlTemplates(xmlTemplate.strName + "&" + strSubreportName, strSubreportFile, new String JavaDoc[0]);
174       xmlTemplate.hasSubXmlTemplates.put(strSubreportName, subreport);
175       if (id != null) {
176         hashtable.put (id, subreport);
177       }
178     } else if (name.trim().equalsIgnoreCase("ARGUMENT")) {
179       String JavaDoc withId = null;
180       String JavaDoc strArgumentName = null;
181       for (int i = 0; i < amap.getLength(); i++) {
182         log4jTemplateConfiguration.info(" ARGUMENT: attribute name="+amap.getQName(i)+" value="+amap.getValue(i));
183         if (amap.getQName(i).equals("withId")) {
184           withId = amap.getValue(i);
185         } else if (amap.getQName(i).equals("name")) {
186           strArgumentName = amap.getValue(i);
187         }
188       }
189       XmlComponentTemplate xmlComponent = (XmlComponentTemplate)hashtable.get(withId);
190       log4jTemplateConfiguration.debug("XmlComponent of id: " + withId + " xmlComponent: " + xmlComponent);
191       // Search in the parameterSQL of the structuers of the subreport
192
for (Enumeration JavaDoc e1 = subreport.hasDataTemplate.elements() ; e1.hasMoreElements();) {
193         DataTemplate elementDataTemplate = (DataTemplate)e1.nextElement();
194         for (Enumeration JavaDoc e2 = elementDataTemplate.vecParameterTemplate.elements() ; e2.hasMoreElements();) {
195           ParameterTemplate parameterTemplate = (ParameterTemplate)e2.nextElement();
196           if (parameterTemplate.strName.equals(strArgumentName)) {
197             parameterTemplate.xmlComponentTemplate = xmlComponent; // XmlEngineNP: the value should be saved in parameter
198
parameterTemplate.section = sectionSubreport;
199             log4jTemplateConfiguration.info("Argument: asigned to parametro_SQL: " + parameterTemplate.strName);
200           }
201         }
202       }
203       // Search in the parameters of the subreport
204
ParameterTemplate parameterTemplate = subreport.hasParameterTemplate.get(strArgumentName);
205       if (parameterTemplate != null) {
206         parameterTemplate.xmlComponentTemplate = xmlComponent;
207         parameterTemplate.section = sectionSubreport;
208         log4jTemplateConfiguration.info("Argument: asigned to parameter: " + parameterTemplate.strName);
209       }
210     } else if (name.trim().equalsIgnoreCase("SECTION")) {
211       id = null;
212       String JavaDoc strField = null;
213       for (int i = 0; i < amap.getLength(); i++) {
214         if (amap.getQName(i).equals("id")) {
215           id = amap.getValue(i);
216         } else if (amap.getQName(i).equals("field")) {
217           strField = amap.getValue(i);
218         }
219       }
220       SectionTemplate section = new SectionTemplate(id, activeDataTemplate);
221       if (strField != null) {
222         section.breakFieldTemplate = activeDataTemplate.addField(strField);
223       }
224       if (id != null) {
225         hashtable.put (id, section);
226       }
227     } else if (name.trim().equalsIgnoreCase("DISCARD")) {
228       id = null;
229       for (int i = 0; i < amap.getLength(); i++) {
230         if (amap.getQName(i).equals("id")) {
231           id = amap.getValue(i);
232         }
233       }
234       Discard discard = new Discard();
235       if (id != null) {
236         hashtable.put (id, discard);
237       }
238     } else if (name.trim().equalsIgnoreCase("CONNECTION")) {
239       pushElement(name);
240       for (int i = 0; i < amap.getLength(); i++) {
241         log4jTemplateConfiguration.info(" TemplateConfiguration (CONNECTION): attribute name="+amap.getQName(i)+" value="+amap.getValue(i));
242         if (amap.getQName(i).equals("driver")) {
243           log4jTemplateConfiguration.debug(" adding driver");
244           activeDataTemplate.strDriver = amap.getValue(i);
245           log4jTemplateConfiguration.debug(" driver added");
246         } else if (amap.getQName(i).equals("URL")) {
247           activeDataTemplate.strURL = amap.getValue(i);
248           // } else if (amap.getQName(i).equals("SQL")) {
249
// activeDataTemplate.strSQL = amap.getValue(i);
250
}
251       }
252     } else if (name.trim().equalsIgnoreCase("STRUCTURE")) {
253       pushElement(name);
254       for (int i = 0; i < amap.getLength(); i++) {
255         log4jTemplateConfiguration.info(" TemplateConfiguration (STRUCTURE): attribute name="+amap.getQName(i)+" value="+amap.getValue(i));
256         if (amap.getQName(i).equals("name")) {
257           activeDataTemplate = new DataTemplate();
258           // activeDataTemplate.clear(); // XmlEngineNP: hay que pasarlo a XmlDocument, si es necesario
259
activeDataTemplate.strDriver = strDriverDefault;
260           activeDataTemplate.strURL = strUrlDefault;
261           activeDataTemplate.strName = amap.getValue(i);
262           //hasDataVector hasDataTemplate.put(amap.getValue(i), activeDataTemplate);
263
hasDataTemplate.addElement(activeDataTemplate);
264         }
265       }
266     } else if (name.trim().equalsIgnoreCase("SQL")) {
267       pushElement(name);
268     } else if (name.trim().equalsIgnoreCase("TEMPLATE")) {
269       pushElement(name);
270       for (int i = 0; i < amap.getLength(); i++) {
271         log4jTemplateConfiguration.info(" TemplateConfiguration (TEMPLATE): attribute name="+amap.getQName(i)+" value="+amap.getValue(i));
272         if (amap.getQName(i).equals("file")) {
273           strTemplate = amap.getValue(i);
274         }
275       }
276     } else if (name.equals("PARAMETER_SQL") || name.equals("PARAMETER")) {
277       ParameterTemplate parameter = new ParameterTemplate();
278       pushElement(name);
279       id = null;
280       strAttribute = null;
281       strReplace = null;
282       strBooleanAttribute = null;
283       String JavaDoc strBooleanWithId = null;
284       for (int i = 0; i < amap.getLength(); i++) {
285         if (amap.getQName(i).equals("name")) {
286           parameter.strName = amap.getValue(i);
287         } else if (amap.getQName(i).equals("id")) {
288           id = amap.getValue(i);
289         } else if (amap.getQName(i).equals("attribute")) {
290           strAttribute = amap.getValue(i);
291         } else if (amap.getQName(i).equals("replace")) {
292           strReplace = amap.getValue(i);
293         } else if (amap.getQName(i).equals("boolean")) {
294           strBooleanAttribute = amap.getValue(i);
295         } else if (amap.getQName(i).equals("withId")) {
296           strBooleanWithId = amap.getValue(i);
297           xmlComponentBooleanWithId = (XmlComponentTemplate)hashtable.get(strBooleanWithId);
298         } else if (amap.getQName(i).equals("default")) {
299           parameter.strDefault = amap.getValue(i);
300         } else if (amap.getQName(i).equals("type")) {
301           if (amap.getValue(i).equals("integer")) {
302             parameter.type = java.sql.Types.INTEGER;
303           } else if (amap.getValue(i).equals("string")) {
304             parameter.type = java.sql.Types.VARCHAR;
305           }
306         } else if (amap.getQName(i).equals("replaceCharacters")) {
307           parameter.vecReplace = xmlEngine.replaceHashtable.get(amap.getValue(i));
308           if (parameter.vecReplace == null) {
309             log4jTemplateConfiguration.warn(" replaceCharacters " + amap.getValue(i) + " not found");
310           }
311         }
312       }
313       if (name.equals("PARAMETER_SQL")) {
314         activeDataTemplate.vecParameterTemplate.addElement(parameter);
315       } else {
316         hasParameter.put(parameter.strName, parameter);
317       }
318       if (id != null) {
319         // attribute of parameter
320
if (strAttribute != null || strBooleanAttribute != null) {
321           log4jTemplateConfiguration.debug("Inside strAttribute in Parameter");
322           AttributeComponentTemplate attributeComponentTemplate = new AttributeComponentTemplate(parameter, strAttribute, strReplace,
323               strBooleanAttribute, xmlComponentBooleanWithId);
324           hashtable.put (id, attributeComponentTemplate);
325         } else {
326           log4jTemplateConfiguration.debug("Inside id");
327           hashtable.put (id, parameter);
328         }
329       }
330     }
331   }
332
333   public void endElement(java.lang.String JavaDoc uri,
334       java.lang.String JavaDoc name,
335       java.lang.String JavaDoc qName) { //throws SAXException {
336
log4jTemplateConfiguration.info("TemplateConfiguration: endElement is called: " + name);
337     readBuffer();
338     if (name.equals("SUBREPORT")) {
339       popElement();
340     }
341   }
342
343   public void characters(char[] ch, int start, int length) { //throws SAXException {
344
log4jTemplateConfiguration.info("TemplateConfiguration: characters is called: " + new String JavaDoc(ch, start, length));
345     log4jTemplateConfiguration.info("TemplateConfiguration: characters is called: " + strElement);
346     if (buffer == null) buffer = new StringBuffer JavaDoc();
347     buffer.append(ch, start, length);
348   }
349
350   public void readBuffer() {
351     if (strElement == null) return;
352     if (buffer != null) {
353       String JavaDoc strBuffer = buffer.toString();
354       log4jTemplateConfiguration.debug("Configuration("+strElement+"): characters is called: " + strBuffer);
355       if (strElement.trim().equalsIgnoreCase("FIELD")) {
356         log4jTemplateConfiguration.debug("characters: (" + strElement + ") antes de addField");
357         FieldTemplate fieldTemplate = activeDataTemplate.addField(strBuffer,
358             formatCouple.formatOutput, formatCouple.formatSimple, vecReplace);
359         log4jTemplateConfiguration.debug("characters: (" + strElement + ") despues de addField");
360         if (id != null) {
361           if (strAttribute != null || strBooleanAttribute != null) {
362             log4jTemplateConfiguration.debug("Inside strAttribute");
363             AttributeComponentTemplate attributeComponentTemplate = new AttributeComponentTemplate(fieldTemplate, strAttribute, strReplace,
364                 strBooleanAttribute, xmlComponentBooleanWithId);
365             hashtable.put (id, attributeComponentTemplate);
366           } else {
367             log4jTemplateConfiguration.debug("Inside id");
368             hashtable.put (id, fieldTemplate);
369           }
370         }
371         popElement();
372       } else if (strElement.trim().equalsIgnoreCase("FUNCTION")) {
373         log4jTemplateConfiguration.debug("characters: (" + strElement + ") before addFunction");
374         FunctionTemplate functionTemplate = activeDataTemplate.addFunction(strFunction, strBuffer,
375             formatCouple.formatOutput, formatCouple.formatSimple, xmlComponentArg1, xmlComponentArg2, id);
376         log4jTemplateConfiguration.debug("characters: (" + strElement + ") after de addFunction");
377         if (id != null) {
378           if (strAttribute != null || strBooleanAttribute != null) {
379             log4jTemplateConfiguration.debug("Inside strAttribute");
380             AttributeComponentTemplate attributeComponentTemplate = new AttributeComponentTemplate(functionTemplate, strAttribute, strReplace,
381                 strBooleanAttribute, xmlComponentBooleanWithId);
382             hashtable.put (id, attributeComponentTemplate);
383           } else {
384             log4jTemplateConfiguration.debug("Inside id");
385             hashtable.put (id, functionTemplate);
386           }
387         }
388         popElement();
389       } else if (strElement.trim().equalsIgnoreCase("SQL")) {
390         activeDataTemplate.strSQL = strBuffer;
391         popElement();
392       }
393       buffer = null;
394     }
395   }
396
397   }
398
Popular Tags