Changeset 24864 for main


Ignore:
Timestamp:
2011-12-06T13:04:42+13:00 (12 years ago)
Author:
sjm84
Message:

Reformatting this file ahead of some changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXSLT.java

    r20139 r24864  
    2929import java.util.ArrayList;
    3030
    31 /** various functions for manipulating Greenstone xslt  */
    32 public class GSXSLT {
     31/** various functions for manipulating Greenstone xslt */
     32public class GSXSLT
     33{
     34    /**
     35     * takes a stylesheet Document, and adds in any child nodes from extra_xsl
     36     * named templates overwrite any existing one, while match templates are
     37     * just added to the end of teh stylesheet
     38     */
     39    public static void mergeStylesheets(Document main_xsl, Element extra_xsl)
     40    {
    3341
    34     /** takes a stylesheet Document, and adds in any child nodes from extra_xsl
    35      * named templates overwrite any existing one, while match templates are
    36 just added to the end of teh stylesheet
    37      */
    38     public static void mergeStylesheets(Document main_xsl, Element extra_xsl) {
    39    
    40     Element main = main_xsl.getDocumentElement();
    41    
    42     NodeList children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "template");
    43     for (int i=0; i<children.getLength(); i++) {
    44         String name = ((Element)children.item(i)).getAttribute("name");
    45         if (!name.equals("")) {
    46         Element old_template = GSXML.getNamedElement(main, "xsl:template", "name", name);
    47         if (old_template != null) {
    48             main.removeChild(old_template);
     42        Element main = main_xsl.getDocumentElement();
     43
     44        NodeList children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "template");
     45        for (int i = 0; i < children.getLength(); i++)
     46        {
     47            String name = ((Element) children.item(i)).getAttribute("name");
     48            if (!name.equals(""))
     49            {
     50                Element old_template = GSXML.getNamedElement(main, "xsl:template", "name", name);
     51                if (old_template != null)
     52                {
     53                    main.removeChild(old_template);
     54                }
     55            }
     56
     57            main.appendChild(main_xsl.importNode(children.item(i), true));
    4958        }
    50         }
    51        
    52         main.appendChild(main_xsl.importNode(children.item(i), true));
     59
    5360    }
    5461
    55     }
     62    /**
     63     * takes any import or include nodes, and creates absolute path names for
     64     * the files
     65     */
     66    public static void absoluteIncludePaths(Document stylesheet, String gsdl3_home, String site_name, String collection, String interface_name, ArrayList base_interfaces)
     67    {
    5668
    57     /** takes any import or include nodes, and creates absolute path names
    58      * for the files
    59      */
    60     public static void absoluteIncludePaths(Document stylesheet,
    61                         String gsdl3_home,
    62                         String site_name,
    63                         String collection,
    64                         String interface_name,
    65                         ArrayList base_interfaces) {
    66    
    67    
    68     Element base_node = stylesheet.getDocumentElement();
    69     if ( base_node == null ) {
    70         return;
     69        Element base_node = stylesheet.getDocumentElement();
     70        if (base_node == null)
     71        {
     72            return;
     73        }
     74        Node child = base_node.getFirstChild();
     75        while (child != null)
     76        {
     77            String name = child.getNodeName();
     78            if (name.equals("xsl:import") || name.equals("xsl:include"))
     79            {
     80                ((Element) child).setAttribute("href", GSFile.stylesheetFile(gsdl3_home, site_name, collection, interface_name, base_interfaces, ((Element) child).getAttribute("href")));
     81            }
     82            child = child.getNextSibling();
     83        }
     84
    7185    }
    72     Node child = base_node.getFirstChild();
    73     while(child != null) {
    74         String name = child.getNodeName();
    75         if (name.equals("xsl:import") || name.equals("xsl:include")) {
    76         ((Element)child).setAttribute("href", GSFile.stylesheetFile(gsdl3_home, site_name, collection, interface_name, base_interfaces, ((Element)child).getAttribute("href")));
    77         }
    78         child = child.getNextSibling();
     86
     87    /**
     88     * looks through a stylesheet for <xxx:template match='template_name'>
     89     * inside this template it looks for any <xxx:value-of
     90     * select='metadataList/metadata[@name=yyy]> elements, and extracts the
     91     * metadata names into a Vector
     92     */
     93    public static Vector extractWantedMetadata(Document stylesheet, String template_name)
     94    {
     95
     96        Vector metadata = new Vector();
     97        Element base_node = stylesheet.getDocumentElement();
     98        NodeList templates = base_node.getElementsByTagNameNS("*", "template");
     99        for (int i = 0; i < templates.getLength(); i++)
     100        {
     101            Element template = (Element) templates.item(i);
     102            String match_name = template.getAttribute("match");
     103            if (!match_name.equals(template_name))
     104            {
     105                continue; // we're only looking for specific templates
     106            }
     107            String mode = template.getAttribute("mode");
     108            if (!mode.equals(""))
     109            {
     110                continue; // we only want ones without modes - these are processing ones, not display ones
     111            }
     112            // we have one that we want to look through
     113            NodeList values = template.getElementsByTagNameNS("*", "value-of");
     114            for (int v = 0; v < values.getLength(); v++)
     115            {
     116                String select = ((Element) values.item(v)).getAttribute("select");
     117                if (select.startsWith("metadataList/metadata[@name="))
     118                {
     119                    String[] bits = select.split("'|\"");
     120                    // there should be two quotes in teh string, therefore 3 items, and the second one is teh one we want
     121                    String name = bits[1];
     122                    metadata.add(name);
     123                }
     124            }
     125        }
     126        return metadata;
    79127    }
    80    
    81     }
    82 
    83     /**
    84      * looks through a stylesheet for <xxx:template match='template_name'>
    85      * inside this template it looks for any <xxx:value-of select='metadataList/metadata[@name=yyy]> elements, and extracts the metadata names into a Vector
    86      */
    87     public static Vector extractWantedMetadata(Document stylesheet, String template_name) {
    88    
    89     Vector metadata = new Vector();
    90     Element base_node = stylesheet.getDocumentElement();
    91     NodeList templates = base_node.getElementsByTagNameNS("*","template");
    92     for (int i=0; i<templates.getLength(); i++) {
    93         Element template = (Element)templates.item(i);
    94         String match_name = template.getAttribute("match");
    95         if (!match_name.equals(template_name)) {
    96         continue; // we're only looking for specific templates
    97         }
    98         String mode = template.getAttribute("mode");
    99         if (!mode.equals("")) {   
    100         continue; // we only want ones without modes - these are processing ones, not display ones
    101         }
    102         // we have one that we want to look through
    103         NodeList values = template.getElementsByTagNameNS("*", "value-of");
    104         for (int v=0; v<values.getLength(); v++) {
    105         String select = ((Element)values.item(v)).getAttribute("select");
    106         if (select.startsWith("metadataList/metadata[@name=")) {
    107             String []bits = select.split("'|\"");
    108             // there should be two quotes in teh string, therefore 3 items, and the second one is teh one we want
    109             String name = bits[1];
    110             metadata.add(name);
    111         }
    112         }
    113     }
    114     return metadata;   
    115     }
    116 
    117    
    118128
    119129}
    120 
Note: See TracChangeset for help on using the changeset viewer.