Changeset 25397

Show
Ignore:
Timestamp:
17.04.2012 16:24:48 (8 years ago)
Author:
ak19
Message:

Correction to previous commit where xsl:import statements during merging of stylesheets were considered. The way they were handled was wrong, since xsl:import statements must come at the top as first children of the xsl:stylesheet parent. This updated code corrects this and also improves the way xsl:include and xsl:output statements are handled.

Files:
1 modified

Legend:

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

    r25389 r25397  
    3939        Element main = main_xsl.getDocumentElement(); 
    4040 
    41         NodeList children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "include"); 
     41        NodeList children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "import"); 
    4242        for (int i = 0; i < children.getLength(); i++) { 
    4343                Node node = children.item(i); 
    44             // remove any previous occurrences of xsl:include with the same href value 
    45             removeDuplicateElementsFrom(main, node, "xsl:include", "href"); 
    46             main.appendChild(main_xsl.importNode(node, true)); 
     44            // If the new xsl:import element is identical (in terms of href attr value)  
     45            // to any in the merged document, don't copy it over 
     46            if(!isDuplicateElement(main, node, "xsl:import", "href")) { 
     47                // Import statements should be the first children of an xsl:stylesheet element 
     48                // If firstchild is null, then this xsl:import element will be inserted at the "end" 
     49                // Although Node.insertBefore() will first remove identical nodes before inserting, we check 
     50                // only the href attribute to see if they're "identical" to any pre-existing <xsl:import> 
     51                main.insertBefore(main_xsl.importNode(node, true), main.getFirstChild()); 
     52            } 
    4753        } 
    48  
    49         children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "import"); 
     54         
     55        children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "include"); 
    5056        for (int i = 0; i < children.getLength(); i++) { 
    5157                Node node = children.item(i); 
    52             // remove any previous occurrences of xsl:output with the same method value 
    53             removeDuplicateElementsFrom(main, node, "xsl:import", "href"); 
    54             main.appendChild(main_xsl.importNode(node, true)); 
     58            // If the new xsl:include element is identical (in terms of href attr value) 
     59            // to any in the merged document, don't copy it over 
     60            // Although Node.appendChild() will first remove identical nodes before appending, we check 
     61            // only the href attribute to see if they're "identical" to any pre-existing <xsl:include> 
     62            if(!isDuplicateElement(main, node, "xsl:include", "href")) { 
     63                main.appendChild(main_xsl.importNode(node, true)); 
     64            } 
    5565        } 
    5666 
     
    5868        for (int i = 0; i < children.getLength(); i++) { 
    5969                Node node = children.item(i); 
    60             // remove any previous occurrences of xsl:output with the same method value 
    61             removeDuplicateElementsFrom(main, node, "xsl:output", "method");             
    62             main.appendChild(main_xsl.importNode(node, true)); 
     70            // If the new xsl:output element is identical (in terms of the value for the method attr)  
     71            // to any in the merged document, don't copy it over 
     72            if(!isDuplicateElement(main, node, "xsl:output", "method")) { 
     73                main.appendChild(main_xsl.importNode(node, true)); 
     74            } 
    6375        } 
    6476 
     
    7587    } 
    7688 
    77     // In element main, tries to find if any previous occurrence of elements with template=templateName,  
     89    // In element main, tries to find any previous occurrence of elements with xsl-template-name=templateName,  
    7890    // and whose named attribute (attributeName) has the same value as the same attribute in node. 
    79     // If this is the case, such a previous occurrence is removed it from element main 
     91    // If this is the case, such a previous occurrence is removed from element main, since 
     92    // the new node will contain a more specific redefinition of this element. 
    8093    public static void removeDuplicateElementsFrom(Element main, Node node, String templateName, String attrName) { 
    8194    String attr = ((Element) node).getAttribute(attrName); 
     
    90103    } 
    91104 
    92  
     105    // Call this method on elements like xsl:include, xsl:import and xsl:output 
     106    // In element main, tries to find any previous occurrence of elements with xsl-element-name=xslName,  
     107    // and whose named attribute (attributeName) has the same value as the same attribute in node. 
     108    // If this is the case, it returns true, since the element would a complete duplicate for our intents. 
     109    public static boolean isDuplicateElement(Element main, Node node, String xslName, String attrName) { 
     110    String attr = ((Element) node).getAttribute(attrName); 
     111    if (!attr.equals("")) 
     112        { 
     113        Element old_element = GSXML.getNamedElement(main, xslName, attrName, attr); 
     114        if (old_element != null) 
     115            { 
     116            return true; 
     117            } 
     118        } 
     119    return false; 
     120    } 
    93121 
    94122    /**