1 12 package org.openbravo.xmlEngine; 13 14 import java.util.Vector ; 15 import java.util.Enumeration ; 16 import java.util.Stack ; 17 import java.util.Hashtable ; 18 19 import org.xml.sax.helpers.DefaultHandler ; 20 import org.xml.sax.Attributes ; 21 22 import org.apache.log4j.Logger ; 23 24 public class XmlTemplate extends DefaultHandler implements XmlComponentTemplate, IDComponent { 25 String strName; 26 TemplateConfiguration configuration; 27 Hashtable <Object , Object > hasSubXmlTemplates; Vector <Object > hasDataTemplate; SectionTemplate activeSection; 30 XmlVectorTemplate activeXmlVector; XmlVectorTemplate vecXmlVector; Hashtable <String , ParameterTemplate> hasParameterTemplate; Stack <Object > stcElement; String strElement; 35 String strPreviousElement; 36 String strFunctionOfElement; 37 String strFieldOfElement; 38 String strFunction; 39 XmlEngine xmlEngine; 40 StackElement stackElement; 41 String fileConfiguration; 42 String [] discard; 43 String prefix; 44 String uri; 45 46 static Logger log4jXmlTemplate = Logger.getLogger(XmlTemplate.class); 47 48 public XmlTemplate(String strName, String fileConfiguration, String [] discard, XmlEngine xmlEngine) { 49 this.strName = strName; 50 this.fileConfiguration = fileConfiguration; 51 this.discard = discard; 52 this.xmlEngine = xmlEngine; 53 hasSubXmlTemplates = new Hashtable <Object , Object >(); 54 hasDataTemplate = new Vector <Object >(); 55 hasParameterTemplate = new Hashtable <String , ParameterTemplate>(); 56 configuration = new TemplateConfiguration(hasDataTemplate, xmlEngine, hasParameterTemplate, this); 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 name() { 64 return strName; 65 } 66 67 public String fileConfiguration() { 68 return fileConfiguration; 69 } 70 71 public int type() { 72 return REPORT; 73 } 74 75 public void clear() { 77 83 activeSection = null; 84 vecXmlVector = new XmlVectorTemplate(); 85 activeXmlVector = vecXmlVector; 86 stcElement = new Stack <Object >(); 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 prefix, 116 java.lang.String 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 uri, 122 java.lang.String name, 123 java.lang.String qName, 124 Attributes amap) { 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 id = null; 137 String 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 <String > e = configuration.hashtable.vecKeys.elements() ; e.hasMoreElements() ;) { 152 String 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 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 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(); FieldTemplate field = (FieldTemplate)iDComponent; 194 activeXmlVector.addElement(field); 195 break; 196 case IDComponent.PARAMETER: 197 log4jXmlTemplate.debug("Case PARAMETER"); 198 stackElement.setSkipCharacters(); ParameterTemplate parameter = (ParameterTemplate)iDComponent; 200 activeXmlVector.addElement(parameter); 202 break; 203 case IDComponent.ATTRIBUTE: 204 log4jXmlTemplate.debug("Case ATTRIBUTE"); 205 AttributeComponentTemplate attributeComponent = (AttributeComponentTemplate)iDComponent; 207 FunctionTemplate functionAttributeComponent = attributeComponent.functionTemplate(); 208 if (functionAttributeComponent != null) { 209 if (activeSection == null) { 210 functionAttributeComponent.dataTemplate.vecFunctionTemplateOutSection.addElement(functionAttributeComponent); } 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(); FunctionTemplate function = (FunctionTemplate)iDComponent; 222 if (activeSection == null) { 223 function.dataTemplate.vecFunctionTemplateOutSection.addElement(function); } else { 225 activeSection.addFunction(function); 226 } 227 activeXmlVector.addElement(function); 228 break; 229 case IDComponent.REPORT: 230 log4jXmlTemplate.debug("Case REPORT"); 231 stackElement.setSkipCharacters(); XmlTemplate subDocument = (XmlTemplate)iDComponent; 233 activeXmlVector.addElement(subDocument); 234 break; 235 } 236 } } 238 239 } i++; 241 } if (!tagAdded) { 243 if(stackElement.printEnabled()) { 244 activeXmlVector.addElement(tag); 245 } 246 } 247 248 249 256 } else { 257 if(stackElement.printEnabled()) { 258 activeXmlVector.addElement(tag); 259 } 260 } 261 262 263 } 264 265 public void endElement(java.lang.String uri, 266 java.lang.String name, 267 java.lang.String qName) { 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 if (!stcElement.isEmpty()) stackElement = (StackElement)stcElement.peek(); 301 } 302 303 public void characters(char[] ch, int start, int length) { log4jXmlTemplate.info("XmlTemplate: characters is called: " + new String (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 (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 |