Ignore:
Timestamp:
2011-05-09T14:37:04+12:00 (13 years ago)
Author:
sjm84
Message:

Updating this branch to match the latest Greenstone3 changes

Location:
main/branches/64_bit_Greenstone/greenstone3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/branches/64_bit_Greenstone/greenstone3

  • main/branches/64_bit_Greenstone/greenstone3/src/java/org/greenstone/gsdl3/core/TransformingReceptionist.java

    r23405 r24007  
    77import org.w3c.dom.Node;
    88import org.w3c.dom.NodeList;
     9import org.w3c.dom.Comment;
     10import org.w3c.dom.Text;
    911import org.w3c.dom.Document;
    1012import org.w3c.dom.Element;
    1113import org.xml.sax.InputSource;
     14import org.w3c.dom.NamedNodeMap;
    1215
    1316// other java classes
     
    2629import org.apache.xerces.dom.*;
    2730import org.apache.xerces.parsers.DOMParser;
     31
     32import org.apache.commons.lang3.StringUtils;
    2833
    2934/** A receptionist that uses xslt to transform the page_data before returning it. . Receives requests consisting
     
    8186  public boolean configure() {
    8287   
    83     if (this.config_params==null) {
     88   if (this.config_params==null) {
    8489      logger.error(" config variables must be set before calling configure");
    8590      return false;
     
    200205    else if(excerptTag != null)
    201206    {
     207        /*
    202208        // define a list
    203209       
    204210        Node selectedElement = modifyNodesByTagRecursive(transformed_page, excerptTag);
     211        */
     212       
     213        Node selectedElement = getNodeByTagRecursive(transformed_page, excerptTag);
    205214        return selectedElement;
     215       
    206216    }
    207217    return transformed_page;
     
    279289   * before transforming */
    280290  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
    282373    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
    286376    String action = request.getAttribute(GSXML.ACTION_ATT);
    287377    String subaction = request.getAttribute(GSXML.SUBACTION_ATT);
    288        
    289     String output = request.getAttribute(GSXML.OUTPUT_ATT);
     378
    290379    // we should choose how to transform the data based on output, eg diff
    291380    // choice for html, and wml??
    292381    // for now, if output=xml, we don't transform the page, we just return
    293382    // 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();
    296415    }
    297416       
     
    300419    String collection = "";
    301420    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);
    303423      collection = (String)params.get(GSParams.COLLECTION);
    304424      if (collection == null) collection = "";
     
    314434    String errorPage = this.converter.getParseErrorMessage();
    315435    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);
    318437    }
    319438    if (style_doc == null) {
     
    335454    if (format_elem != null) {
    336455      //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));
    338457      // need to transform the format info
    339458      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");
    340459      Document configStylesheet_doc = this.converter.getDOM(new File(configStylesheet_file));
     460
    341461      if (configStylesheet_doc != null) {
    342462    Document format_doc = this.converter.newDOM();
    343463    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 <-
    345465               
    346466    // Since we started creating documents with DocTypes, we can end up with
     
    353473      new_format = (Element)result;
    354474    }
    355     logger.debug("new format elem="+this.converter.getPrettyString(new_format));
     475    logger.debug("new format elem="+this.converter.getPrettyStringLogger(new_format, logger));
    356476    if (output.equals("newformat")) {
    357477      return new_format;
     
    370490      }
    371491      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));
    373493    }
    374494       
     
    377497
    378498
    379     Document preprocessingXsl  ;
     499    Document preprocessingXsl;
    380500    try {
    381501      preprocessingXsl = getPreprocessDoc();
     
    537657      return converter.getDOM(getStringFromDocument(skinAndLibraryXsl));
    538658    }
    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       }
    541676    }
    542677    if (output.equals("oldskindoc")) {
    543678      return converter.getDOM(getStringFromDocument(oldStyle_doc));
    544679    }
    545 
    546     // DocType defaults in case the skin doesn't have an "xsl:output" element
    547     String qualifiedName = "html";
    548     String publicID = "-//W3C//DTD HTML 4.01 Transitional//EN";
    549     String systemID = "http://www.w3.org/TR/html4/loose.dtd";
    550680
    551681    // Try to get the system and public ID from the current skin xsl document
     
    571701    }
    572702       
    573     // We need to create an empty document with a predefined DocType,
    574     // that will then be used for the transformation by the DOMResult
    575     Document docWithDoctype = converter.newDOM(qualifiedName, publicID, systemID);
    576        
    577703    //System.out.println(converter.getPrettyString(docWithDoctype));
    578704    //System.out.println("Doctype vals: " + qualifiedName + " " + publicID + " " + systemID) ;
    579705       
     706     docWithDoctype = converter.newDOM(qualifiedName, publicID, systemID);         
    580707       
    581708    //System.out.println("Generate final HTML from current skin") ;
    582709    //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   
    586713    // The line below will do the transformation like we use to do before having Skin++ implemented,
    587714    // it will not contain any GS-Lib statements expanded, and the result will not contain any doctype.
    588715
    589716    //return (Element)this.transformer.transform(style_doc, doc, config_params); 
    590 
     717    //return null; // For now - change later
    591718  }
    592719   
     
    606733    content = writer.toString();
    607734    System.out.println("Change the & to &Amp; for proper debug dispay") ;
    608     content = content.replaceAll("&", "&amp;");
     735    content = StringUtils.replace(content, "&", "&amp;");
    609736    writer.flush();
    610737      }
     
    657784    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);
    658785    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);
    662789    return stylesheet;
    663790  }
Note: See TracChangeset for help on using the changeset viewer.