Changeset 26325

Show
Ignore:
Timestamp:
17.10.2012 13:51:35 (7 years ago)
Author:
sjm84
Message:

Changing how collection text is retrieved so that they can contain gsf elements. The downside is that it <fragment> elements turn up in the HTML. Possibly need a better solution in the future

Location:
main/trunk/greenstone3/src/java/org/greenstone/gsdl3
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/core/TransformingReceptionist.java

    r26320 r26325  
    2121import org.greenstone.gsdl3.util.GSFile; 
    2222import org.greenstone.gsdl3.util.GSParams; 
    23 import org.greenstone.gsdl3.util.GSPath; 
    2423import org.greenstone.gsdl3.util.GSXML; 
    2524import org.greenstone.gsdl3.util.GSXSLT; 
     
    181180        } 
    182181 
    183         GetRequiredMetadataNamesFromXSLFiles(); 
     182        getRequiredMetadataNamesFromXSLFiles(); 
    184183 
    185184        return true; 
    186185    } 
    187186 
    188     protected void GetRequiredMetadataNamesFromXSLFiles() 
     187    protected void getRequiredMetadataNamesFromXSLFiles() 
    189188    { 
    190189        ArrayList<File> xslFiles = GSFile.getAllXSLFiles((String) this.config_params.get(GSConstants.INTERFACE_NAME), (String) this.config_params.get(GSConstants.SITE_NAME)); 
     
    345344        ArrayList<String> requiredMetadata = _metadataRequiredMap.get(stylesheetFile); 
    346345 
    347         if(requiredMetadata !=null){ 
    348             Element extraMetadataList = this.doc.createElement(GSXML.EXTRA_METADATA + GSXML.LIST_MODIFIER); 
    349  
    350             for (String metadataString : requiredMetadata) 
    351             { 
    352                 Element metadataElem = this.doc.createElement(GSXML.EXTRA_METADATA); 
    353                 metadataElem.setAttribute(GSXML.NAME_ATT, metadataString); 
    354                 extraMetadataList.appendChild(metadataElem); 
    355             } 
    356             request.appendChild(request.getOwnerDocument().importNode(extraMetadataList, true)); 
    357         } 
    358     } 
    359      
    360     protected Node postProcessPage(Element page) 
    361     { 
     346        if (requiredMetadata != null) 
     347        { 
     348            Element extraMetadataList = this.doc.createElement(GSXML.EXTRA_METADATA + GSXML.LIST_MODIFIER); 
     349 
     350            for (String metadataString : requiredMetadata) 
     351            { 
     352                Element metadataElem = this.doc.createElement(GSXML.EXTRA_METADATA); 
     353                metadataElem.setAttribute(GSXML.NAME_ATT, metadataString); 
     354                extraMetadataList.appendChild(metadataElem); 
     355            } 
     356            request.appendChild(request.getOwnerDocument().importNode(extraMetadataList, true)); 
     357        } 
     358    } 
     359 
     360    protected Node postProcessPage(Element page) 
     361    { 
    362362        // might need to add some data to the page 
    363363        addExtraInfo(page); 
     
    633633        } 
    634634 
     635        config_params.put("collName", collection); 
     636 
    635637        Document style_doc = getXSLTDocument(action, subaction, collection); 
    636638        if (style_doc == null) 
     
    909911 
    910912        GSXSLT.inlineImportAndIncludeFiles(skinAndLibraryDoc, null, (String) this.config_params.get(GSConstants.SITE_NAME), collection, (String) this.config_params.get(GSConstants.INTERFACE_NAME), base_interfaces); 
    911         skinAndLibraryDoc = (Document) secondConfigFormatPass(collection, skinAndLibraryDoc, doc, new UserContext(request)); 
     913        skinAndLibraryDoc = (Document) performTextFormatPass(collection, skinAndLibraryDoc, doc, new UserContext(request)); 
     914        skinAndLibraryDoc = (Document) performConfigFormatPass(collection, skinAndLibraryDoc, doc, new UserContext(request)); 
    912915 
    913916        if (_debug) 
     
    935938    } 
    936939 
    937     protected Node secondConfigFormatPass(String collection, Document skinAndLibraryDoc, Document doc, UserContext userContext) 
     940    protected Node performConfigFormatPass(String collection, Document skinAndLibraryDoc, Document doc, UserContext userContext) 
    938941    { 
    939942        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"); 
     943        Document configStylesheet_doc = this.converter.getDOM(new File(configStylesheet_file)); 
     944 
     945        if (configStylesheet_doc != null) 
     946        { 
     947            return this.transformer.transform(configStylesheet_doc, skinAndLibraryDoc, config_params); 
     948        } 
     949        return skinAndLibraryDoc; 
     950    } 
     951 
     952    protected Node performTextFormatPass(String collection, Document skinAndLibraryDoc, Document doc, UserContext userContext) 
     953    { 
     954        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, "text_fragment_format.xsl"); 
    940955        Document configStylesheet_doc = this.converter.getDOM(new File(configStylesheet_file)); 
    941956 
     
    9971012            name = this.xslt_map.get(action); 
    9981013        } 
    999         if (name== null) { 
    1000           // so we can reandomly create any named page 
    1001           if (action.equals("p") && !subaction.equals("")) { 
    1002             // TODO: pages/ won't work for interface other than default!! 
    1003             name="pages/"+subaction+".xsl"; 
    1004           } 
    1005            
     1014        if (name == null) 
     1015        { 
     1016            // so we can reandomly create any named page 
     1017            if (action.equals("p") && !subaction.equals("")) 
     1018            { 
     1019                // TODO: pages/ won't work for interface other than default!! 
     1020                name = "pages/" + subaction + ".xsl"; 
     1021            } 
     1022 
    10061023        } 
    10071024        Document finalDoc = GSXSLT.mergedXSLTDocumentCascade(name, (String) this.config_params.get(GSConstants.SITE_NAME), collection, (String) this.config_params.get(GSConstants.INTERFACE_NAME), base_interfaces, _debug); 
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/XSLTUtil.java

    r26315 r26325  
    1919package org.greenstone.gsdl3.util; 
    2020 
     21import java.io.ByteArrayInputStream; 
    2122import java.io.File; 
    2223import java.text.SimpleDateFormat; 
     
    2627import java.util.HashMap; 
    2728import java.util.Locale; 
     29 
     30import javax.xml.parsers.DocumentBuilder; 
     31import javax.xml.parsers.DocumentBuilderFactory; 
    2832 
    2933import net.tanesha.recaptcha.ReCaptcha; 
     
    5054        _stringVariables.put(name, value); 
    5155    } 
    52      
     56 
    5357    public static String getString(String name) 
    5458    { 
    5559        return _stringVariables.get(name); 
    5660    } 
    57      
     61 
    5862    /* some tests */ 
    5963    public static boolean equals(String s1, String s2) 
     
    292296    } 
    293297 
    294     public static String getCollectionText(String collection, String site_name, String lang, String key) 
    295     { 
    296       return getCollectionTextWithArgs(collection, site_name, lang, key, null); 
    297     } 
    298  
    299   public static String getCollectionText(String collection, String site_name, String lang, String key, String args_str) 
    300   { 
    301  
    302     String[] args = null; 
    303     if (args_str != null && !args_str.equals("")) 
    304       { 
    305     args = StringUtils.split(args_str, ";"); 
    306       } 
    307  
    308     return getCollectionTextWithArgs(collection, site_name, lang, key, args); 
    309   } 
     298    public static Node getCollectionText(String collection, String site_name, String lang, String key) 
     299    { 
     300        return getCollectionTextWithArgs(collection, site_name, lang, key, null); 
     301    } 
     302 
     303    public static Node getCollectionText(String collection, String site_name, String lang, String key, String args_str) 
     304    { 
     305 
     306        String[] args = null; 
     307        if (args_str != null && !args_str.equals("")) 
     308        { 
     309            args = StringUtils.split(args_str, ";"); 
     310        } 
     311 
     312        return getCollectionTextWithArgs(collection, site_name, lang, key, args); 
     313    } 
    310314 
    311315    // xslt didn't like calling the function with Node varargs, so have this hack for now 
    312     public static String getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1) 
     316    public static Node getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1) 
    313317    { 
    314318        return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1); 
    315319    } 
    316320 
    317     public static String getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1, Node n2) 
     321    public static Node getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1, Node n2) 
    318322    { 
    319323        return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1, n2); 
    320324    } 
    321325 
    322     public static String getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1, Node n2, Node n3) 
     326    public static Node getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1, Node n2, Node n3) 
    323327    { 
    324328        return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1, n2, n3); 
    325329    } 
    326330 
    327     public static String getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1, Node n2, Node n3, Node n4) 
     331    public static Node getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1, Node n2, Node n3, Node n4) 
    328332    { 
    329333        return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1, n2, n3, n4); 
    330334    } 
    331335 
    332     public static String getCollectionTextWithDOMMulti(String collection, String site_name, String lang, String key, Node... nodes) 
     336    public static Node getCollectionTextWithDOMMulti(String collection, String site_name, String lang, String key, Node... nodes) 
    333337    { 
    334338        int num_nodes = nodes.length; 
     
    340344            for (int i = 0; i < num_nodes; i++) 
    341345            { 
    342  
    343346                String node_str = XMLConverter.getString(nodes[i]); 
    344347                args[i] = node_str; 
     
    348351    } 
    349352 
    350   public static String getCollectionTextWithArgs(String collection, String site_name, String lang, String key, String [] args) { 
    351  
    352         CollectionClassLoader class_loader = new CollectionClassLoader(XSLTUtil.class.getClassLoader(), GSFile.siteHome(GlobalProperties.getGSDL3Home(), site_name), collection); 
    353         Dictionary dict = new Dictionary(collection, lang, class_loader); 
    354         String result = dict.get(key, args); 
    355         if (result != null) 
    356         { 
    357             return result; 
    358         } 
    359         return "text:" + collection + ":" + key; 
    360  
     353    public static Node getCollectionTextWithArgs(String collection, String site_name, String lang, String key, String[] args) 
     354    { 
     355        try 
     356        { 
     357            DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
     358 
     359            CollectionClassLoader class_loader = new CollectionClassLoader(XSLTUtil.class.getClassLoader(), GSFile.siteHome(GlobalProperties.getGSDL3Home(), site_name), collection); 
     360            Dictionary dict = new Dictionary(collection, lang, class_loader); 
     361            String result = dict.get(key, args); 
     362            if (result != null) 
     363            { 
     364                return docBuilder.parse(new ByteArrayInputStream(("<fragment>" + result + "</fragment>").getBytes())).getDocumentElement(); 
     365            } 
     366            return docBuilder.parse(new ByteArrayInputStream(("<fragment>" + "text:" + collection + ":" + key + "</fragment>").getBytes())).getDocumentElement(); 
     367        } 
     368        catch (Exception ex) 
     369        { 
     370            return null; 
     371        } 
    361372    } 
    362373