KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 import org.xml.sax.helpers.DefaultHandler JavaDoc;
20 import org.xml.sax.Attributes JavaDoc;
21
22 import org.apache.log4j.Logger ;
23
24 public class XmlTemplate extends DefaultHandler JavaDoc implements XmlComponentTemplate, IDComponent {
25   String JavaDoc strName;
26   TemplateConfiguration configuration;
27   Hashtable JavaDoc<Object JavaDoc, Object JavaDoc> hasSubXmlTemplates; // hashtable of SubXmlTemplates
28
Vector JavaDoc<Object JavaDoc> hasDataTemplate; // hashtable of DataTemplate (the same that the hashtable DataTemplate in Configuration
29
SectionTemplate activeSection;
30   XmlVectorTemplate activeXmlVector; // it may be the vecXmlVector, the vecDetail of a DataTemplate or the head or foot of a SectionTemplate
31
XmlVectorTemplate vecXmlVector; //contains XmlComponents before, after and between DataTemplates and the DataTemplate
32
Hashtable JavaDoc<String JavaDoc, ParameterTemplate> hasParameterTemplate; // contains the ParameterTemplates that not are for the SQL query
33
Stack JavaDoc<Object JavaDoc> stcElement; // Stack of Elements
34
String JavaDoc strElement;
35   String JavaDoc strPreviousElement;
36   String JavaDoc strFunctionOfElement;
37   String JavaDoc strFieldOfElement;
38   String JavaDoc strFunction;
39   XmlEngine xmlEngine;
40   StackElement stackElement;
41   String JavaDoc fileConfiguration;
42   String JavaDoc[] discard;
43   String JavaDoc prefix;
44   String JavaDoc uri;
45
46   static Logger log4jXmlTemplate = Logger.getLogger(XmlTemplate.class);
47
48   public XmlTemplate(String JavaDoc strName, String JavaDoc fileConfiguration, String JavaDoc[] discard, XmlEngine xmlEngine) {
49     this.strName = strName;
50     this.fileConfiguration = fileConfiguration;
51     this.discard = discard;
52     this.xmlEngine = xmlEngine;
53     hasSubXmlTemplates = new Hashtable JavaDoc<Object JavaDoc, Object JavaDoc>();
54     hasDataTemplate = new Vector JavaDoc<Object JavaDoc>();
55     hasParameterTemplate = new Hashtable JavaDoc<String JavaDoc, ParameterTemplate>();
56     configuration = new TemplateConfiguration(hasDataTemplate, xmlEngine, hasParameterTemplate, this); // XmlEngineNP: unique argument in the cosntructor: this
57
for (int i=0; i<discard.length; i++ ) {
58       configuration.hashtable.put (discard[i], new Discard());
59     }
60     prefix = null;
61   }
62
63   public String JavaDoc name() {
64     return strName;
65   }
66
67   public String JavaDoc fileConfiguration() {
68     return fileConfiguration;
69   }
70
71   public int type() {
72     return REPORT;
73   }
74
75   //XmlEngineNP: make sure this functions can be addede or deleted from the constructor, because it is necessary to make clear, or also for the subreports
76
public void clear() {
77     /* pass to XmlDocument
78        for (Enumeration e = hasDataTemplate.elements() ; e.hasMoreElements();) {
79        DataTemplate elementDataTemplate = (DataTemplate)e.nextElement();
80        elementDataTemplate.clear();
81        } //*** see if this loop necessary or if it is here whre the DataTemplate clear is executed
82        */

83     activeSection = null;
84     vecXmlVector = new XmlVectorTemplate();
85     activeXmlVector = vecXmlVector;
86     stcElement = new Stack JavaDoc<Object JavaDoc>();
87   }
88
89   private void pushElement(StackElement stackElement) {
90     stcElement.push(stackElement);
91     strElement = stackElement.name();
92   }
93
94   private StackElement popElement() {
95     if (stcElement.isEmpty()) return null;
96     StackElement stackElement = (StackElement)stcElement.pop();
97     if (stcElement.isEmpty()) {
98       strElement = null;
99     } else {
100       strElement = ((StackElement)stcElement.peek()).name();
101     }
102     return stackElement;
103   }
104
105   private StackElement peekElement() {
106     StackElement stackElement;
107     if (stcElement.isEmpty()) {
108       stackElement = null;
109     } else {
110       stackElement = (StackElement)stcElement.peek();
111     }
112     return stackElement;
113   }
114
115   public void startPrefixMapping(java.lang.String JavaDoc prefix,
116       java.lang.String JavaDoc uri) {
117     log4jXmlTemplate.info("XmlTemplate: startPrefixMapping is called, prefix:" + prefix + " uri: " + uri);
118     this.prefix = prefix;
119   }
120
121   public void startElement(java.lang.String JavaDoc uri,
122       java.lang.String JavaDoc name,
123       java.lang.String JavaDoc qName,
124       Attributes JavaDoc amap) { //throws SAXException {
125
log4jXmlTemplate.info("XmlTemplate: startElement is called3:" + name + " strElement: " + strElement);
126     StackElement previousStackElement = peekElement();
127     stackElement = new StackElement(qName);
128     pushElement(stackElement);
129     if (previousStackElement != null) {
130       stackElement.setPrintEnabled(previousStackElement.printEnabled());
131     }
132     log4jXmlTemplate.debug("Call to CharacterComponent");
133     TagTemplate tag = new TagTemplate(qName, amap, xmlEngine.strReplaceWhat, xmlEngine.strReplaceWith, prefix, uri);
134     prefix = null;
135
136     String JavaDoc id = null;
137     String JavaDoc strClass = null;
138
139     for (int i = 0; i < amap.getLength(); i++) {
140       log4jXmlTemplate.info(" XmlTemplate (attribute list): attribute name=" + amap.getQName(i) + " value=" + amap.getValue(i));
141       if (amap.getQName(i).equals("id")) {
142         id = amap.getValue(i);
143       } else if (amap.getQName(i).equals("class")) {
144         strClass = amap.getValue(i);
145       }
146     }
147     if (id != null || strClass != null) {
148       IDComponent iDComponent = null;
149       int i = 0;
150       boolean tagAdded = false;
151       for (Enumeration JavaDoc<String JavaDoc> e = configuration.hashtable.vecKeys.elements() ; e.hasMoreElements() ;) {
152         String JavaDoc strKey = e.nextElement();
153         if (strKey.equals(id) || strKey.equals(strClass)) {
154           iDComponent = (IDComponent)configuration.hashtable.vecObjects.elementAt(i);
155
156           if (iDComponent != null) {
157             log4jXmlTemplate.debug("id: " + id + " tipo: "+ iDComponent.type());
158             // first test if print the tag
159
switch(iDComponent.type()) {
160               case IDComponent.DISCARD:
161                 log4jXmlTemplate.debug("Case DISCARD");
162                 stackElement.setPrintEnabled(false);
163                 break;
164             }
165             if(stackElement.printEnabled()) {
166               if (!tagAdded) {
167                 activeXmlVector.addElement(tag);
168                 tagAdded = true;
169               }
170               //Discard2 }
171

172             switch(iDComponent.type()) {
173               case IDComponent.SECTION:
174                 log4jXmlTemplate.debug("Case SECTION");
175                 stackElement.setSection(activeSection);
176                 activeSection = (SectionTemplate)iDComponent;
177                 activeSection.dataTemplate.vecSectionTemplate.addElement(activeSection);
178                 if (activeSection.dataTemplate.firstSectionTemplate == null) {
179                   activeSection.dataTemplate.firstSectionTemplate = activeSection;
180                   vecXmlVector.addElement(activeSection.dataTemplate);
181                 }
182                 if (activeSection.breakFieldTemplate == null) {
183                   log4jXmlTemplate.debug("Add to detail");
184                   activeXmlVector = activeSection.dataTemplate().vecDetailTemplate;
185                 } else {
186                   log4jXmlTemplate.debug("Add to Head");
187                   activeXmlVector = activeSection.vecHeadTemplate;
188                 }
189                 break;
190               case IDComponent.FIELD:
191                 log4jXmlTemplate.debug("Case FIELD");
192                 stackElement.setSkipCharacters(); // to remove the characters
193
FieldTemplate field = (FieldTemplate)iDComponent;
194                 activeXmlVector.addElement(field);
195                 break;
196               case IDComponent.PARAMETER:
197                 log4jXmlTemplate.debug("Case PARAMETER");
198                 stackElement.setSkipCharacters(); // to remove the characters
199
ParameterTemplate parameter = (ParameterTemplate)iDComponent;
200                 // vecParameter.addElement(parameter);
201
activeXmlVector.addElement(parameter);
202                 break;
203               case IDComponent.ATTRIBUTE:
204                 log4jXmlTemplate.debug("Case ATTRIBUTE");
205                 // not remove the characters
206
AttributeComponentTemplate attributeComponent = (AttributeComponentTemplate)iDComponent;
207                 FunctionTemplate functionAttributeComponent = attributeComponent.functionTemplate();
208                 if (functionAttributeComponent != null) {
209                   if (activeSection == null) {
210                     functionAttributeComponent.dataTemplate.vecFunctionTemplateOutSection.addElement(functionAttributeComponent); //XmlEngineNP (it had problems compiling,verify it is vecFunctionTemplateOutSection)
211
} else {
212                     activeSection.addFunction(functionAttributeComponent);
213                   }
214                 }
215
216                 tag.setAttribute(attributeComponent);
217                 break;
218               case IDComponent.FUNCTION:
219                 log4jXmlTemplate.debug("Case FUNCTION");
220                 stackElement.setSkipCharacters(); // to remove the characters
221
FunctionTemplate function = (FunctionTemplate)iDComponent;
222                 if (activeSection == null) {
223                   function.dataTemplate.vecFunctionTemplateOutSection.addElement(function); //XmlEngineNP like the last one
224
} else {
225                   activeSection.addFunction(function);
226                 }
227                 activeXmlVector.addElement(function);
228                 break;
229               case IDComponent.REPORT:
230                 log4jXmlTemplate.debug("Case REPORT");
231                 stackElement.setSkipCharacters(); // to remove the characters
232
XmlTemplate subDocument = (XmlTemplate)iDComponent;
233                 activeXmlVector.addElement(subDocument);
234                 break;
235             }
236           } // Discard2
237
}
238
239       } // id = key
240
i++;
241     } // bucle del hashtable
242
if (!tagAdded) {
243       if(stackElement.printEnabled()) {
244         activeXmlVector.addElement(tag);
245       }
246     }
247
248
249     /*
250             else {
251             if(stackElement.printEnabled()) {
252             activeXmlVector.addElement(tag);
253             }
254             }
255             */

256   } else {
257     if(stackElement.printEnabled()) {
258       activeXmlVector.addElement(tag);
259     }
260   }
261
262
263   }
264
265   public void endElement(java.lang.String JavaDoc uri,
266       java.lang.String JavaDoc name,
267       java.lang.String JavaDoc qName) { //throws SAXException {
268
do {
269       log4jXmlTemplate.info("XmlTemplate: endElement is called: " + name + " strElement: " + strElement);
270       stackElement = popElement();
271       if (stackElement == null) {
272         log4jXmlTemplate.warn("XmlTemplate: not begin tag for " + name);
273         return;
274       }
275     } while (!qName.trim().equalsIgnoreCase(stackElement.name().trim()));
276
277
278     if (stackElement.isSection()) {
279       activeSection = stackElement.section();
280       if (activeSection == null) {
281         activeXmlVector = vecXmlVector;
282       } else {
283         activeXmlVector = activeSection.vecFootTemplate;
284       }
285       log4jXmlTemplate.debug("Add to Foot");
286     }
287
288     log4jXmlTemplate.debug(" strElement (after pop): " + strElement);
289
290     CharacterComponent character;
291     if (qName.equals("DIVFO") || qName.endsWith("_TMP")) {
292       character = new CharacterComponent("");
293     } else {
294       character = new CharacterComponent("</" + qName + ">");
295     }
296     if (stackElement.printEnabled()) {
297       activeXmlVector.addElement(character);
298     }
299     // the stackElement is changed once it is verified if the element could be printed
300
if (!stcElement.isEmpty()) stackElement = (StackElement)stcElement.peek();
301   }
302
303   public void characters(char[] ch, int start, int length) { //throws SAXException {
304
log4jXmlTemplate.info("XmlTemplate: characters is called: " + new String JavaDoc(ch, start, length) +
305         " element:" + strElement + " function of:" + strFunctionOfElement +
306         " previousElement:" + strPreviousElement);
307     if (strElement == null) return;
308     if(!stackElement.printEnabled()) return;
309     if (!peekElement().skipCharacters()) {
310       CharacterComponent character = new CharacterComponent(new String JavaDoc(ch, start, length));
311       activeXmlVector.addElement(character);
312     }
313   }
314
315   public XmlComponentValue createXmlComponentValue(XmlDocument xmlDocument) {
316     log4jXmlTemplate.debug("Creation of XmlDocument: " + strName);
317     XmlDocument subXmlDocument = xmlDocument.hasSubXmlDocuments.get(strName);
318     if (subXmlDocument == null) {
319       log4jXmlTemplate.info("new subXmlDocument : " + strName + " in createXmlComponentValue of " +
320           xmlDocument.xmlTemplate.strName);
321       subXmlDocument = new XmlDocument(this, xmlDocument);
322     }
323     return subXmlDocument;
324   }
325
326   public XmlDocument createXmlDocument() {
327     return createXmlDocument(null);
328   }
329
330   public XmlDocument createXmlDocument(XmlDocument parentXmlDocument) {
331     XmlDocument subXmlDocument = new XmlDocument(this, parentXmlDocument);
332     return subXmlDocument;
333   }
334
335   }
336
Popular Tags