Changeset 24007 for main/branches/64_bit_Greenstone/greenstone3/src/java/org/greenstone/gsdl3/core/TransformingReceptionist.java
- Timestamp:
- 2011-05-09T14:37:04+12:00 (13 years ago)
- Location:
- main/branches/64_bit_Greenstone/greenstone3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/branches/64_bit_Greenstone/greenstone3
- Property svn:mergeinfo changed
-
main/branches/64_bit_Greenstone/greenstone3/src/java/org/greenstone/gsdl3/core/TransformingReceptionist.java
r23405 r24007 7 7 import org.w3c.dom.Node; 8 8 import org.w3c.dom.NodeList; 9 import org.w3c.dom.Comment; 10 import org.w3c.dom.Text; 9 11 import org.w3c.dom.Document; 10 12 import org.w3c.dom.Element; 11 13 import org.xml.sax.InputSource; 14 import org.w3c.dom.NamedNodeMap; 12 15 13 16 // other java classes … … 26 29 import org.apache.xerces.dom.*; 27 30 import org.apache.xerces.parsers.DOMParser; 31 32 import org.apache.commons.lang3.StringUtils; 28 33 29 34 /** A receptionist that uses xslt to transform the page_data before returning it. . Receives requests consisting … … 81 86 public boolean configure() { 82 87 83 88 if (this.config_params==null) { 84 89 logger.error(" config variables must be set before calling configure"); 85 90 return false; … … 200 205 else if(excerptTag != null) 201 206 { 207 /* 202 208 // define a list 203 209 204 210 Node selectedElement = modifyNodesByTagRecursive(transformed_page, excerptTag); 211 */ 212 213 Node selectedElement = getNodeByTagRecursive(transformed_page, excerptTag); 205 214 return selectedElement; 215 206 216 } 207 217 return transformed_page; … … 279 289 * before transforming */ 280 290 protected Node transformPage(Element page) { 281 291 292 boolean allowsClientXSLT = (Boolean)config_params.get(GSConstants.ALLOW_CLIENT_SIDE_XSLT); 293 //System.out.println("Client side transforms allowed? " + allowsClientXSLT); 294 295 String currentInterface = (String)config_params.get(GSConstants.INTERFACE_NAME); 296 297 Element request = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM); 298 String output = request.getAttribute(GSXML.OUTPUT_ATT); 299 300 //System.out.println("Current output mode is: " + output + ", current interface name is: " + currentInterface); 301 302 if(allowsClientXSLT) { 303 if(!currentInterface.endsWith(GSConstants.CLIENT_SIDE_XSLT_INTERFACE_SUFFIX) && output.equals("html")) 304 { 305 System.out.println("output is html and we are not currently using a client side version, switching"); 306 // Switch the interface 307 config_params.put(GSConstants.INTERFACE_NAME, currentInterface.concat(GSConstants.CLIENT_SIDE_XSLT_INTERFACE_SUFFIX)); 308 } 309 else if((currentInterface.endsWith(GSConstants.CLIENT_SIDE_XSLT_INTERFACE_SUFFIX) && !output.equals("html")) || output.equals("server")) 310 { 311 // The reverse needs to happen too 312 config_params.put(GSConstants.INTERFACE_NAME, currentInterface.substring(0, currentInterface.length() - GSConstants.CLIENT_SIDE_XSLT_INTERFACE_SUFFIX.length())); 313 } 314 } 315 else if (currentInterface.endsWith(GSConstants.CLIENT_SIDE_XSLT_INTERFACE_SUFFIX)) 316 { 317 config_params.put(GSConstants.INTERFACE_NAME, currentInterface.substring(0, currentInterface.length() - GSConstants.CLIENT_SIDE_XSLT_INTERFACE_SUFFIX.length())); 318 } 319 320 // DocType defaults in case the skin doesn't have an "xsl:output" element 321 String qualifiedName = "html"; 322 String publicID = "-//W3C//DTD HTML 4.01 Transitional//EN"; 323 String systemID = "http://www.w3.org/TR/html4/loose.dtd"; 324 325 // We need to create an empty document with a predefined DocType, 326 // that will then be used for the transformation by the DOMResult 327 Document docWithDoctype = converter.newDOM(qualifiedName, publicID, systemID); 328 329 if(output.equals("xsltclient")) { 330 // If you're just getting the client-side transform page, why bother with the rest of this? 331 Element html = docWithDoctype.createElement("html"); 332 Element img = docWithDoctype.createElement("img"); 333 img.setAttribute("src", "interfaces/default/images/loading.gif"); // Make it dynamic 334 img.setAttribute("alt", "Please wait..."); 335 Text title_text = docWithDoctype.createTextNode("Please wait..."); // Make this language dependent 336 Element head = docWithDoctype.createElement("head"); 337 Element title = docWithDoctype.createElement("title"); 338 title.appendChild(title_text); 339 Element body = docWithDoctype.createElement("body"); 340 Element script = docWithDoctype.createElement("script"); 341 Element jquery = docWithDoctype.createElement("script"); 342 jquery.setAttribute("src", "jquery.js"); 343 jquery.setAttribute("type", "text/javascript"); 344 Comment jquery_comment = docWithDoctype.createComment("jQuery"); 345 Comment script_comment = docWithDoctype.createComment("Filler for browser"); 346 script.setAttribute("src", "test.js"); 347 script.setAttribute("type", "text/javascript"); 348 Element pagevar = docWithDoctype.createElement("script"); 349 Element style = docWithDoctype.createElement("style"); 350 style.setAttribute("type", "text/css"); 351 Text style_text = docWithDoctype.createTextNode("body { text-align: center; padding: 50px; font: 14pt Arial, sans-serif; font-weight: bold; }"); 352 pagevar.setAttribute("type", "text/javascript"); 353 Text page_var_text = docWithDoctype.createTextNode("var placeholder = true;"); 354 355 html.appendChild(head); 356 head.appendChild(title); 357 head.appendChild(style); 358 style.appendChild(style_text); 359 html.appendChild(body); 360 head.appendChild(pagevar); 361 head.appendChild(jquery); 362 head.appendChild(script); 363 pagevar.appendChild(page_var_text); 364 jquery.appendChild(jquery_comment); 365 script.appendChild(script_comment); 366 body.appendChild(img); 367 docWithDoctype.appendChild(html); 368 369 return (Node)docWithDoctype; 370 } 371 372 // Passing in the pretty string here means it needs to be generated even when not debugging; so use custom function to return blank when debug is off 282 373 logger.debug("page before transforming:"); 283 logger.debug(this.converter.getPrettyString(page)); 284 285 Element request = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM); 374 logger.debug(this.converter.getPrettyStringLogger(page, logger)); 375 286 376 String action = request.getAttribute(GSXML.ACTION_ATT); 287 377 String subaction = request.getAttribute(GSXML.SUBACTION_ATT); 288 289 String output = request.getAttribute(GSXML.OUTPUT_ATT); 378 290 379 // we should choose how to transform the data based on output, eg diff 291 380 // choice for html, and wml?? 292 381 // for now, if output=xml, we don't transform the page, we just return 293 382 // the page xml 294 if (output.equals("xml")) { 295 return page; 383 Document theXML = null; 384 385 if (output.equals("xml") || output.equals("clientside")) { 386 // Append some bits and pieces first... 387 theXML = converter.newDOM(); 388 // Import into new document first! 389 Node newPage = theXML.importNode(page, true); 390 theXML.appendChild(newPage); 391 Element root = theXML.createElement("xsltparams"); 392 newPage.appendChild(root); 393 394 Element libname = theXML.createElement("param"); 395 libname.setAttribute("name", "library_name"); 396 Text libnametext = theXML.createTextNode((String)config_params.get(GSConstants.LIBRARY_NAME)); 397 libname.appendChild(libnametext); 398 399 Element intname = theXML.createElement("param"); 400 intname.setAttribute("name", "interface_name"); 401 Text intnametext = theXML.createTextNode((String)config_params.get(GSConstants.INTERFACE_NAME)); 402 intname.appendChild(intnametext); 403 404 Element filepath = theXML.createElement("param"); 405 filepath.setAttribute("name", "filepath"); 406 Text filepathtext = theXML.createTextNode(GlobalProperties.getGSDL3Home()); 407 filepath.appendChild(filepathtext); 408 409 root.appendChild(libname); 410 root.appendChild(intname); 411 root.appendChild(filepath); 412 413 if(output.equals("xml")) 414 return theXML.getDocumentElement(); 296 415 } 297 416 … … 300 419 String collection = ""; 301 420 if (cgi_param_list != null) { 302 HashMap params = GSXML.extractParams(cgi_param_list, false); 421 // Don't waste time getting all the parameters 422 HashMap params = GSXML.extractParams(cgi_param_list, false, GSParams.COLLECTION); 303 423 collection = (String)params.get(GSParams.COLLECTION); 304 424 if (collection == null) collection = ""; … … 314 434 String errorPage = this.converter.getParseErrorMessage(); 315 435 if(errorPage != null) { 316 return XMLTransformer.constructErrorXHTMLPage( 317 "Cannot parse the xslt file: " + xslt_file + "\n" + errorPage); 436 return XMLTransformer.constructErrorXHTMLPage("Cannot parse the xslt file: " + xslt_file + "\n" + errorPage); 318 437 } 319 438 if (style_doc == null) { … … 335 454 if (format_elem != null) { 336 455 //page_response.removeChild(format_elem); 337 logger.debug("format elem="+this.converter.getPrettyString (format_elem));456 logger.debug("format elem="+this.converter.getPrettyStringLogger(format_elem, logger)); 338 457 // need to transform the format info 339 458 String configStylesheet_file = GSFile.stylesheetFile(GlobalProperties.getGSDL3Home(), (String)this.config_params.get(GSConstants.SITE_NAME), collection, (String)this.config_params.get(GSConstants.INTERFACE_NAME), base_interfaces, "config_format.xsl"); 340 459 Document configStylesheet_doc = this.converter.getDOM(new File(configStylesheet_file)); 460 341 461 if (configStylesheet_doc != null) { 342 462 Document format_doc = this.converter.newDOM(); 343 463 format_doc.appendChild(format_doc.importNode(format_elem, true)); 344 Node result = this.transformer.transform(configStylesheet_doc, format_doc); 464 Node result = this.transformer.transform(configStylesheet_doc, format_doc); // Needs addressing <- 345 465 346 466 // Since we started creating documents with DocTypes, we can end up with … … 353 473 new_format = (Element)result; 354 474 } 355 logger.debug("new format elem="+this.converter.getPrettyString (new_format));475 logger.debug("new format elem="+this.converter.getPrettyStringLogger(new_format, logger)); 356 476 if (output.equals("newformat")) { 357 477 return new_format; … … 370 490 } 371 491 logger.debug("the converted stylesheet is:"); 372 logger.debug(this.converter.getPrettyString (style_doc.getDocumentElement()));492 logger.debug(this.converter.getPrettyStringLogger(style_doc.getDocumentElement(), logger)); 373 493 } 374 494 … … 377 497 378 498 379 Document preprocessingXsl 499 Document preprocessingXsl; 380 500 try { 381 501 preprocessingXsl = getPreprocessDoc(); … … 537 657 return converter.getDOM(getStringFromDocument(skinAndLibraryXsl)); 538 658 } 539 if (output.equals("skinandlibdoc")) { 540 return converter.getDOM(getStringFromDocument(skinAndLibraryDoc)); 659 if (output.equals("skinandlibdoc") || output.equals("clientside")) { 660 661 Node skinAndLib = converter.getDOM(getStringFromDocument(skinAndLibraryDoc)); 662 663 if(output.equals("skinandlibdoc")) { 664 return skinAndLib; 665 } else { 666 // Send XML and skinandlibdoc down the line together 667 Document finalDoc = converter.newDOM(); 668 Node finalDocSkin = finalDoc.importNode(skinAndLibraryDoc.getDocumentElement(), true); 669 Node finalDocXML = finalDoc.importNode(theXML.getDocumentElement(), true); 670 Element root = finalDoc.createElement("skinlibPlusXML"); 671 root.appendChild(finalDocSkin); 672 root.appendChild(finalDocXML); 673 finalDoc.appendChild(root); 674 return (Node)finalDoc.getDocumentElement(); 675 } 541 676 } 542 677 if (output.equals("oldskindoc")) { 543 678 return converter.getDOM(getStringFromDocument(oldStyle_doc)); 544 679 } 545 546 // DocType defaults in case the skin doesn't have an "xsl:output" element547 String qualifiedName = "html";548 String publicID = "-//W3C//DTD HTML 4.01 Transitional//EN";549 String systemID = "http://www.w3.org/TR/html4/loose.dtd";550 680 551 681 // Try to get the system and public ID from the current skin xsl document … … 571 701 } 572 702 573 // We need to create an empty document with a predefined DocType,574 // that will then be used for the transformation by the DOMResult575 Document docWithDoctype = converter.newDOM(qualifiedName, publicID, systemID);576 577 703 //System.out.println(converter.getPrettyString(docWithDoctype)); 578 704 //System.out.println("Doctype vals: " + qualifiedName + " " + publicID + " " + systemID) ; 579 705 706 docWithDoctype = converter.newDOM(qualifiedName, publicID, systemID); 580 707 581 708 //System.out.println("Generate final HTML from current skin") ; 582 709 //Transformation of the XML message from the receptionist to HTML with doctype 583 return this.transformer.transform(skinAndLibraryDoc, doc, config_params, docWithDoctype); 584 585 710 711 return this.transformer.transform(skinAndLibraryDoc, doc, config_params, docWithDoctype); // The default 712 586 713 // The line below will do the transformation like we use to do before having Skin++ implemented, 587 714 // it will not contain any GS-Lib statements expanded, and the result will not contain any doctype. 588 715 589 716 //return (Element)this.transformer.transform(style_doc, doc, config_params); 590 717 //return null; // For now - change later 591 718 } 592 719 … … 606 733 content = writer.toString(); 607 734 System.out.println("Change the & to &Amp; for proper debug dispay") ; 608 content = content.replaceAll("&", "&");735 content = StringUtils.replace(content, "&", "&"); 609 736 writer.flush(); 610 737 } … … 657 784 String stylesheet = GSFile.stylesheetFile(GlobalProperties.getGSDL3Home(), (String)this.config_params.get(GSConstants.SITE_NAME), collection, (String)this.config_params.get(GSConstants.INTERFACE_NAME), base_interfaces, name); 658 785 if (stylesheet==null) { 659 logger.info(" cant find stylesheet for "+name);660 } 661 logger. error("stylesheet:"+stylesheet);786 logger.info(" Can't find stylesheet for "+name); 787 } 788 logger.debug("Stylesheet: "+stylesheet); 662 789 return stylesheet; 663 790 }
Note:
See TracChangeset
for help on using the changeset viewer.