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 import java.io.IOException ; 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.InputStreamReader ; 22 import java.io.FileNotFoundException ; 23 import java.io.PrintWriter ; 24 import javax.servlet.*; 25 import javax.servlet.http.*; 26 27 import org.apache.xerces.parsers.*; 28 import org.xml.sax.InputSource ; 29 import org.xml.sax.XMLReader ; 30 import org.apache.log4j.Logger; 31 import org.apache.log4j.PropertyConfigurator; 32 33 public class XmlEngine extends HttpServlet { 34 XMLReader xmlParser; 35 XMLReader htmlParser; 36 Hashtable <String , XmlTemplate> hasXmlTemplate; 37 Stack <XmlTemplate> stcRead; 38 Hashtable <String , FormatCouple> formatHashtable; 39 Hashtable <String , Vector <ReplaceElement>> replaceHashtable; 40 String strDriverDefault; 41 String strUrlDefault; 42 public String strBaseLocation; 43 String strFormatFile; 44 public File fileXmlEngineFormat; 45 public File fileBaseLocation; 46 public String strReplaceWhat; 47 public String strReplaceWith; 48 public boolean isResource = false; 49 50 static public String strTextDividedByZero; 51 52 static Logger log4jXmlEngine = Logger.getLogger(XmlEngine.class); 53 static Logger log4jReloadXml = Logger.getLogger("reloadXml"); 54 55 public XmlEngine() { 56 } 58 59 public void init(ServletConfig config) 60 throws ServletException 61 { 62 log4jXmlEngine.info("XmlEngine v0.846-2"); 63 super.init(config); 64 configureLog4j(getInitParameter("fileConfigurationLog4j")); 65 strBaseLocation = getInitParameter("BaseLocation"); 66 strDriverDefault = getInitParameter("driver"); 67 strUrlDefault = getInitParameter("URL"); 68 log4jXmlEngine.info("driver: " + strDriverDefault + " URL: " + strUrlDefault); 69 strFormatFile = getInitParameter("FormatFile"); 70 fileBaseLocation = new File (strBaseLocation); 71 fileXmlEngineFormat = new File (fileBaseLocation, strFormatFile); 72 log4jXmlEngine.info("BaseLocation: " + strBaseLocation); 73 strReplaceWhat = getInitParameter("ReplaceWhat"); 74 strReplaceWith = getInitParameter("ReplaceWith"); 75 log4jXmlEngine.info("Replace attribute value: \"" + strReplaceWhat + "\" with: \"" + strReplaceWith + "\"."); 76 strTextDividedByZero = getInitParameter("TextDividedByZero"); 77 log4jXmlEngine.info("TextDividedByZero: " + strTextDividedByZero); 78 try { 79 log4jXmlEngine.info("fileBaseLocation: " + fileBaseLocation.getCanonicalPath()); 80 } 81 catch (IOException e) { 82 log4jXmlEngine.error("Error in BaseLocation: " + strBaseLocation); 83 } 84 initialize(); 85 } 86 87 public void loadParams() { 88 111 replaceHashtable = new Hashtable <String , Vector <ReplaceElement>>(); 112 Vector <ReplaceElement> htmlReplaceVector = new Vector <ReplaceElement>(); 113 htmlReplaceVector.addElement(new ReplaceElement("&","&")); htmlReplaceVector.addElement(new ReplaceElement("\"",""")); 115 htmlReplaceVector.addElement(new ReplaceElement("\n"," ")); 117 htmlReplaceVector.addElement(new ReplaceElement("\r"," ")); 118 htmlReplaceVector.addElement(new ReplaceElement("<","<")); 119 htmlReplaceVector.addElement(new ReplaceElement(">",">")); 120 htmlReplaceVector.addElement(new ReplaceElement("®","®")); 121 htmlReplaceVector.addElement(new ReplaceElement("€","€")); 122 htmlReplaceVector.addElement(new ReplaceElement("ñ","ñ")); 123 htmlReplaceVector.addElement(new ReplaceElement("Ñ","Ñ")); 124 replaceHashtable.put("html", htmlReplaceVector); 125 Vector <ReplaceElement> foReplaceVector = new Vector <ReplaceElement>(); 126 foReplaceVector.addElement(new ReplaceElement("&","&")); 127 foReplaceVector.addElement(new ReplaceElement("<","<")); 128 foReplaceVector.addElement(new ReplaceElement(">",">")); 129 foReplaceVector.addElement(new ReplaceElement("\\","\")); 130 foReplaceVector.addElement(new ReplaceElement("º","º")); 131 foReplaceVector.addElement(new ReplaceElement("ª","ª")); 132 foReplaceVector.addElement(new ReplaceElement("®","®")); 133 foReplaceVector.addElement(new ReplaceElement("€","€")); 134 foReplaceVector.addElement(new ReplaceElement("\n"," ")); 135 replaceHashtable.put("fo", foReplaceVector); 136 Vector <ReplaceElement> htmlPreformatedReplaceVector = new Vector <ReplaceElement>(); 137 htmlPreformatedReplaceVector.addElement(new ReplaceElement("&","&")); 138 htmlPreformatedReplaceVector.addElement(new ReplaceElement("\"",""")); 139 htmlPreformatedReplaceVector.addElement(new ReplaceElement("<","<")); 141 htmlPreformatedReplaceVector.addElement(new ReplaceElement(">",">")); 142 htmlPreformatedReplaceVector.addElement(new ReplaceElement("\n","<BR>")); 143 htmlPreformatedReplaceVector.addElement(new ReplaceElement("\r"," ")); 144 htmlPreformatedReplaceVector.addElement(new ReplaceElement("®","®")); 145 replaceHashtable.put("htmlPreformated", htmlPreformatedReplaceVector); 146 Vector <ReplaceElement> htmlHelpReplaceVector = new Vector <ReplaceElement>(); 147 htmlHelpReplaceVector.addElement(new ReplaceElement("\n","<BR>")); 148 htmlHelpReplaceVector.addElement(new ReplaceElement("\r","")); 149 replaceHashtable.put("htmlHelp", htmlHelpReplaceVector); 150 Vector <ReplaceElement> htmlPreformatedTextareaReplaceVector = new Vector <ReplaceElement>(); 151 htmlPreformatedTextareaReplaceVector.addElement(new ReplaceElement("&","&")); 152 htmlPreformatedTextareaReplaceVector.addElement(new ReplaceElement("\"",""")); 153 htmlPreformatedTextareaReplaceVector.addElement(new ReplaceElement("<","<")); 155 htmlPreformatedTextareaReplaceVector.addElement(new ReplaceElement(">",">")); 156 htmlPreformatedTextareaReplaceVector.addElement(new ReplaceElement("®","®")); 159 replaceHashtable.put("htmlPreformatedTextarea", htmlPreformatedTextareaReplaceVector); 160 Vector <ReplaceElement> htmlJavaScriptReplaceVector = new Vector <ReplaceElement>(); 161 htmlJavaScriptReplaceVector.addElement(new ReplaceElement("'","\\'")); 162 htmlJavaScriptReplaceVector.addElement(new ReplaceElement("\"",""")); 163 htmlJavaScriptReplaceVector.addElement(new ReplaceElement("\n","\\n")); 164 replaceHashtable.put("htmlJavaScript", htmlJavaScriptReplaceVector); 165 } 166 167 public void initialize() { 168 hasXmlTemplate = new Hashtable <String , XmlTemplate>(); stcRead = new Stack <XmlTemplate>(); formatHashtable = new Hashtable <String , FormatCouple>(); 171 XMLReader xmlParserFormat = new SAXParser(); 172 xmlParserFormat.setContentHandler(new FormatRead(formatHashtable)); 173 log4jXmlEngine.info("XmlEngine file formats: " + strFormatFile); 174 log4jXmlEngine.info("fileXmlEngineFormat: " + fileXmlEngineFormat.toString()); 178 try { 179 xmlParserFormat.parse(new InputSource (new InputStreamReader (new FileInputStream (fileXmlEngineFormat), "UTF-8"))); 181 } catch (FileNotFoundException e) { 182 log4jXmlEngine.error("not found fileXmlEngineFormat: " + fileXmlEngineFormat + "\n" + 183 e.getMessage()); 184 return; 185 } catch (IOException e) { 186 log4jXmlEngine.error("IOException in fileXmlEngineFormat: " + fileXmlEngineFormat); 187 e.printStackTrace(); 188 return; 189 } catch (Exception e) { 190 log4jXmlEngine.error("Exception in fileXmlEngineFormat: " + fileXmlEngineFormat); 191 e.printStackTrace(); 192 return; 193 } 194 loadParams(); 195 } 196 197 201 public XmlTemplate readXmlTemplate(String strXmlTemplateFile) { 202 return readXmlTemplate(strXmlTemplateFile, new String [0]); 203 } 204 205 211 public synchronized XmlTemplate readXmlTemplate(String strXmlTemplateFile, String [] discard) { 212 String xmlTemplateName = strXmlTemplateFile; 213 xmlTemplateName = fileBaseLocation.getName() + xmlTemplateName; 214 for (int i=0; i<discard.length; i++ ) { 215 xmlTemplateName = xmlTemplateName + "?" + discard[i]; 216 } 217 if (log4jReloadXml.isDebugEnabled()) { 218 initialize(); 219 log4jReloadXml.debug("XmlEngine 29-11-2001 Initialized"); 220 } 221 return readAllXmlTemplates(xmlTemplateName, strXmlTemplateFile, discard); 222 } 223 224 232 public XmlTemplate readAllXmlTemplates(String strXmlTemplateName, String strXmlTemplateFile, String [] discard) { 233 XmlTemplate xmlTemplate = addXmlTemplate(strXmlTemplateName, strXmlTemplateFile, discard); 234 while (!stcRead.empty()) { 235 XmlTemplate xmlTemplateRead = stcRead.pop(); 236 readFile(xmlTemplateRead); 237 } 238 return xmlTemplate; 239 } 240 241 248 private XmlTemplate addXmlTemplate(String strXmlTemplateName, String strXmlTemplateFile, String [] discard) { 249 log4jXmlEngine.info("Adding: " + strXmlTemplateName); 250 for (Enumeration <XmlTemplate> e = hasXmlTemplate.elements() ; e.hasMoreElements() ;) { 251 XmlTemplate xmlTemplate = e.nextElement(); 252 log4jXmlEngine.debug("Comparing: xmlTemplate.name():" + xmlTemplate.name() + 253 "and strXmlTemplateName:" + strXmlTemplateName); 254 if (xmlTemplate.name().equals(strXmlTemplateName)) { 255 return xmlTemplate; 256 } 257 } 258 log4jXmlEngine.debug("Before the new XmlTemplate"); 259 XmlTemplate xmlTemplate = new XmlTemplate(strXmlTemplateName, strXmlTemplateFile, discard, this); 260 xmlTemplate.configuration.strDriverDefault = strDriverDefault; 261 xmlTemplate.configuration.strUrlDefault = strUrlDefault; 262 hasXmlTemplate.put(strXmlTemplateName, xmlTemplate); 263 log4jXmlEngine.debug("created de new XmlTemplate"); 264 stcRead.push(xmlTemplate); 265 log4jXmlEngine.debug("push xmlTemplate"); 266 return xmlTemplate; 267 } 268 269 270 274 public void readFile(XmlTemplate xmlTemplate) { 275 xmlParser = new SAXParser(); 276 htmlParser = new org.cyberneko.html.parsers.SAXParser(); 277 278 xmlParser.setContentHandler(xmlTemplate.configuration); 280 String strFile = xmlTemplate.fileConfiguration() + ".xml"; 281 log4jXmlEngine.info("XmlEngine name: " + strFile); 282 File fileXmlEngineConfiguration = null; 283 if (!isResource) { 284 fileXmlEngineConfiguration = new File (fileBaseLocation, strFile); 285 log4jXmlEngine.info("fileXmlEngineConfiguration: " + fileXmlEngineConfiguration.toString()); 286 log4jXmlEngine.info("Parent fileXmlEngineConfiguration: " + fileXmlEngineConfiguration.getParent()); 287 } 288 xmlTemplate.clear(); 289 try { 290 if (!isResource) xmlParser.parse(new InputSource (new InputStreamReader (new FileInputStream (fileXmlEngineConfiguration), "UTF-8"))); 292 else xmlParser.parse(new InputSource (ClassLoader.getSystemResourceAsStream(strFile))); 293 } catch (FileNotFoundException e) { 294 if (!isResource) log4jXmlEngine.error("not found fileXmlEngineConfiguration: " + fileXmlEngineConfiguration + "\n" + 295 e.getMessage()); 296 else log4jXmlEngine.error("not found fileXmlEngineConfiguration: " + strFile + "\n" + 297 e.getMessage()); 298 return; 299 } catch (IOException e) { 300 if (!isResource) log4jXmlEngine.error("IOException in fileXmlEngineConfiguration: " + fileXmlEngineConfiguration); 301 else log4jXmlEngine.error("IOException in fileXmlEngineConfiguration: " + strFile); 302 e.printStackTrace(); 303 return; 304 } catch (Exception e) { 305 if (!isResource) log4jXmlEngine.error("Exception in fileXmlEngineConfiguration: " + fileXmlEngineConfiguration); 306 else log4jXmlEngine.error("Exception in fileXmlEngineConfiguration: " + strFile); 307 e.printStackTrace(); 308 return; 309 } 310 311 int posExtension = xmlTemplate.configuration.strTemplate.lastIndexOf("."); 313 XMLReader templateParser; 314 if (xmlTemplate.configuration.strTemplate.substring(posExtension).equals(".html")) { 315 log4jXmlEngine.info("Fichero html: -" + xmlTemplate.configuration.strTemplate.substring(posExtension) + "-"); 316 templateParser = htmlParser; 317 } else { 318 log4jXmlEngine.info("Fichero xml: -" + xmlTemplate.configuration.strTemplate.substring(posExtension) + "-"); 319 templateParser = xmlParser; 320 } 321 templateParser.setContentHandler(xmlTemplate); 322 log4jXmlEngine.info("XmlEngine file template: " + xmlTemplate.configuration.strTemplate); 323 File fileXmlEngineTemplate = null; 324 String strPath = ""; 325 if (!isResource) { 326 fileXmlEngineTemplate = new File (fileXmlEngineConfiguration.getParent(), 327 xmlTemplate.configuration.strTemplate); 328 log4jXmlEngine.info("fileXmlEngineTemplate: " + fileXmlEngineTemplate.toString()); 329 } else { 330 int finPath = -1; 331 if ((finPath=strFile.lastIndexOf("/"))!=-1) { 332 strPath = strFile.substring(0, finPath); 333 if (!strPath.endsWith("/")) strPath += "/"; 334 } 335 } 336 try { 337 if (!isResource) templateParser.parse(new InputSource (new InputStreamReader (new FileInputStream (fileXmlEngineTemplate), "UTF-8"))); 339 else templateParser.parse(new InputSource (ClassLoader.getSystemResourceAsStream(strPath + xmlTemplate.configuration.strTemplate))); 340 } catch (FileNotFoundException e) { 341 if (!isResource) log4jXmlEngine.error("not found fileXmlEngineTemplate: " + fileXmlEngineTemplate + "\n" + 342 e.getMessage()); 343 else log4jXmlEngine.error("not found fileXmlEngineTemplate: " + strPath + xmlTemplate.configuration.strTemplate + "\n" + 344 e.getMessage()); 345 return; 346 } catch (IOException e) { 347 if (!isResource) log4jXmlEngine.error("IOException in fileXmlEngineTemplate: " + fileXmlEngineTemplate); 348 else log4jXmlEngine.error("IOException in fileXmlEngineTemplate: " + strPath + xmlTemplate.configuration.strTemplate); 349 e.printStackTrace(); 350 return; 351 } catch (Exception e) { 352 if (!isResource) log4jXmlEngine.error("Exception in fileXmlEngineTemplate: " + fileXmlEngineTemplate); 353 else log4jXmlEngine.error("Exception in fileXmlEngineTemplate: " + strPath + xmlTemplate.configuration.strTemplate); 354 e.printStackTrace(); 355 return; 356 } 357 } 358 359 public void doPost ( HttpServletRequest request, 360 HttpServletResponse response) 361 throws ServletException, IOException { 362 doGet(request, response); 363 } 364 365 public void doGet ( HttpServletRequest request, 366 HttpServletResponse response) 367 throws ServletException, IOException 368 { 369 String strReportName = request.getParameter("report"); 370 String strReload = request.getParameter("reload"); 371 if (strReload != null) { 372 initialize(); 373 } 374 Report report = readReportConfiguration(strReportName); 375 for (Enumeration <DataValue> e1 = report.xmlDocument.hasDataValue.elements() ; e1.hasMoreElements();) { 377 DataValue elementDataValue = e1.nextElement(); 378 elementDataValue.connect(); for (Enumeration e2 = elementDataValue.vecParameterValue.elements() ; e2.hasMoreElements();) { 380 ParameterValue parameter = (ParameterValue)e2.nextElement(); 381 parameter.strValue = request.getParameter(parameter.parameterTemplate.strName); 382 if (parameter.strValue == null) { 383 log4jXmlEngine.info("getParameter: default assigned"); 384 parameter.strValue = parameter.parameterTemplate.strDefault; 385 } 386 log4jXmlEngine.debug("getParameter: " + parameter.parameterTemplate.strName + " valor: " + parameter.strValue ); 387 } 388 } 389 390 for (Enumeration <XmlDocument> e1 = report.xmlDocument.hasSubXmlDocuments.elements() ; e1.hasMoreElements();) { 392 XmlDocument subXmlDocument = e1.nextElement(); 393 for (Enumeration <DataValue> e2 = subXmlDocument.hasDataValue.elements() ; e2.hasMoreElements();) { 394 DataValue elementDataValue = e2.nextElement(); 395 elementDataValue.connect(); } 397 } 398 399 for (Enumeration <ParameterValue> e3 = report.xmlDocument.hasParameterValue.elements() ; e3.hasMoreElements();) { 401 ParameterValue parameter = e3.nextElement(); 402 parameter.strValue = request.getParameter(parameter.parameterTemplate.strName); 403 if (parameter.strValue == null) { 404 log4jXmlEngine.debug("getParameter of: " + parameter.parameterTemplate.strName + " default assigned"); 405 parameter.strValue = parameter.parameterTemplate.strDefault; 406 } 407 log4jXmlEngine.debug("getParameter: " + parameter.parameterTemplate.strName + " value: " + parameter.strValue ); 408 } 409 410 response.setContentType("text/html; charset=UTF-8"); 411 PrintWriter out = response.getWriter(); 412 String strBlank = request.getParameter("blank"); 413 if (strBlank != null) { 414 out.println(report.xmlDocument.print(strBlank)); 415 } else { 416 out.println(report.xmlDocument.print()); 417 } 418 out.close(); 419 } 420 421 public void connect(){ 422 for (Enumeration e1 = hasXmlTemplate.elements() ; e1.hasMoreElements() ;) { 423 Report report = (Report)e1.nextElement(); for (Enumeration <DataValue> e2 = report.xmlDocument.hasDataValue.elements() ; e2.hasMoreElements();) { 425 DataValue elementDataValue = e2.nextElement(); 426 elementDataValue.connect(); 427 } 428 } 429 } 430 431 public void closeConnections () { 432 for (Enumeration e1 = hasXmlTemplate.elements() ; e1.hasMoreElements() ;) { 433 Report report = (Report)e1.nextElement(); 434 for (Enumeration <DataValue> e2 = report.xmlDocument.hasDataValue.elements() ; e2.hasMoreElements();) { 435 DataValue elementDataValue = e2.nextElement(); 436 elementDataValue.closeConnection(); 437 } 438 } 439 } 440 441 public void destroy() { 442 closeConnections(); 443 } 444 445 public static void configureLog4j(String file) { 446 if (file != null) { 447 PropertyConfigurator.configure(file); 448 } else { 449 PropertyConfigurator.configure("log4j.lcf"); 450 } 451 } 452 453 public static void main(String argv[]) { 454 int i; 455 configureLog4j(null); 456 String strFile; 457 if (argv.length < 1) { 458 log4jXmlEngine.error("Usage: java XmlEngine [driver URL] file"); 459 return; 460 } 461 XmlEngine xmlEngine = new XmlEngine(); 462 if (argv[0].equals("-d")) { 463 xmlEngine.strDriverDefault = argv[1]; 464 xmlEngine.strUrlDefault = argv[2]; 465 strFile = argv[3]; 466 i = 3; 467 } else { 468 strFile = argv[0]; 469 i = 0; 470 } 471 472 xmlEngine.initialize(); 473 Report report = xmlEngine.readReportConfiguration(strFile); 474 for (Enumeration <DataValue> e1 = report.xmlDocument.hasDataValue.elements() ; e1.hasMoreElements();) { 475 DataValue elementDataValue = e1.nextElement(); 476 for (Enumeration e2 = elementDataValue.vecParameterValue.elements() ; e2.hasMoreElements();) { 477 ParameterValue parameter = (ParameterValue)e2.nextElement(); 478 i++; 479 parameter.strValue = argv[i]; 480 if (parameter.strValue == null) { 481 log4jXmlEngine.info("Parameter(main): default assigned"); 482 parameter.strValue = parameter.parameterTemplate.strDefault; 483 } 484 log4jXmlEngine.debug("Parameter(main): " + parameter.parameterTemplate.strName + " valor: " + parameter.strValue ); 485 } 486 } 487 488 log4jXmlEngine.info("Hashtable: "); 489 for (Enumeration <String > e = report.xmlDocument.xmlTemplate.configuration.hashtable.vecKeys.elements() ; e.hasMoreElements() ;) { 490 String id = e.nextElement(); 491 IDComponent iDComponent = (IDComponent)report.xmlDocument.xmlTemplate.configuration.hashtable.get(id); 492 log4jXmlEngine.info("id: " + id + " type: "+ iDComponent.type()); 493 } 494 log4jXmlEngine.info("Template: " + report.xmlDocument.xmlTemplate.configuration.strTemplate); 495 log4jXmlEngine.debug(report.xmlDocument.print()); 496 } 497 498 public Report readReportConfiguration(String strReportFile) { 500 return readReportConfiguration(strReportFile, new String [0]); 501 } 502 503 public Report readReportConfiguration(String strReportFile, String [] discard) { 504 return new Report(strReportFile, discard, this); 505 } 506 507 } 508 | Popular Tags |