Changeset 25820

Show
Ignore:
Timestamp:
26.06.2012 15:34:23 (7 years ago)
Author:
kjdon
Message:

a couple of methods renamed and moved to GSXML. mergeStylesheets updated - tries to add elements in the correct place. uses namespaces everywhere to avoid xsl:template and xslt:template equivalents not matching. hope I haven't stuffed anything up :-)

Files:
1 modified

Legend:

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

    r25656 r25820  
    4343     * takes a stylesheet Document, and adds in any child nodes from extra_xsl 
    4444     * named templates overwrite any existing one, while match templates are 
    45      * just added to the end of teh stylesheet 
     45     * just added to the end of the stylesheet 
     46 
     47     * elements are added in following order, and added to preserve original order with imported ones coming after existing ones 
     48     * import, include, output, variable, template 
    4649     */ 
    4750    public static void mergeStylesheetsDebug(Document main_xsl, Element extra_xsl, boolean overwrite, boolean debug, String firstDocFileName, String secondDocFileName) 
     
    5457 
    5558        Element main = main_xsl.getDocumentElement(); 
    56  
    57         NodeList children = GSXML.getChildrenByTagName(extra_xsl, "xsl:variable"); 
    58         for (int i = 0; i < children.getLength(); i++) 
    59         { 
    60             Node node = children.item(i); 
     59        Node insertion_point = null; 
     60          Element last_import = GSXML.getLastElementByTagNameNS(main, "http://www.w3.org/1999/XSL/Transform", "import"); 
     61          if (last_import != null) { 
     62            insertion_point = last_import.getNextSibling(); 
     63          } else { 
     64            insertion_point = main.getFirstChild(); 
     65          } 
     66         
     67        // imports 
     68        NodeList children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "import"); 
     69        for (int i = 0; i < children.getLength(); i++) 
     70        { 
     71          Element node = (Element) children.item(i); 
    6172            // If the new xsl:import element is identical (in terms of href attr value)  
    6273            // to any in the merged document, don't copy it over 
    63             if (!isDuplicateElement(main, node, "xsl:variable", "name")) 
    64             { 
    65                 main.appendChild(main_xsl.importNode(node, true)); 
    66             } 
    67         } 
    68  
    69         children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "import"); 
    70         for (int i = 0; i < children.getLength(); i++) 
    71         { 
    72             Node node = children.item(i); 
    73             // If the new xsl:import element is identical (in terms of href attr value)  
    74             // to any in the merged document, don't copy it over 
    75             if (!isDuplicateElement(main, node, "xsl:import", "href")) 
     74            if (GSXML.getNamedElementNS(main, "http://www.w3.org/1999/XSL/Transform", "import", "href", node.getAttribute("href")) == null) 
    7675            { 
    7776                // Import statements should be the first children of an xsl:stylesheet element 
     
    7978                // Although Node.insertBefore() will first remove identical nodes before inserting, we check 
    8079                // only the href attribute to see if they're "identical" to any pre-existing <xsl:import> 
    81                 main.insertBefore(main_xsl.importNode(node, true), main.getFirstChild()); 
    82             } 
    83         } 
    84  
     80                //main.insertBefore(main_xsl.importNode(node, true), main.getFirstChild()); 
     81              main.insertBefore(main_xsl.importNode(node, true), insertion_point); 
     82               
     83            } 
     84        } 
     85     
     86          // do we have a new insertion point?? 
     87           Element last_include = GSXML.getLastElementByTagNameNS(main, "http://www.w3.org/1999/XSL/Transform", "include"); 
     88           if (last_include != null) {               
     89             insertion_point = last_include.getNextSibling(); 
     90           } 
     91     
     92        // includes 
    8593        children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "include"); 
    8694        for (int i = 0; i < children.getLength(); i++) 
    8795        { 
    88             Node node = children.item(i); 
     96          Element node = (Element)children.item(i); 
    8997            // If the new xsl:include element is identical (in terms of href attr value) 
    9098            // to any in the merged document, don't copy it over 
    9199            // Although Node.appendChild() will first remove identical nodes before appending, we check 
    92100            // only the href attribute to see if they're "identical" to any pre-existing <xsl:include> 
    93             if (!isDuplicateElement(main, node, "xsl:include", "href")) 
    94             { 
    95                 main.appendChild(main_xsl.importNode(node, true)); 
    96             } 
    97         } 
    98  
     101            if (GSXML.getNamedElementNS(main, "http://www.w3.org/1999/XSL/Transform", "include", "href", node.getAttribute("href"))==null) 
     102            { 
     103              //main.appendChild(main_xsl.importNode(node, true)); 
     104              main.insertBefore(main_xsl.importNode(node, true), insertion_point); 
     105            } 
     106        } // for each include 
     107         
     108          // do we have a new insertion point?? 
     109           Element last_output = GSXML.getLastElementByTagNameNS(main, "http://www.w3.org/1999/XSL/Transform", "output"); 
     110           if (last_output != null) {                
     111             insertion_point = last_output.getNextSibling(); 
     112           } 
     113 
     114        // outputs 
    99115        children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "output"); 
    100116        for (int i = 0; i < children.getLength(); i++) 
    101117        { 
    102             Node node = children.item(i); 
     118          Element node = (Element)children.item(i); 
    103119            // If the new xsl:output element is identical (in terms of the value for the method attr)  
    104120            // to any in the merged document, don't copy it over 
    105             if (!isDuplicateElement(main, node, "xsl:output", "method")) 
     121            if (GSXML.getNamedElementNS(main, "http://www.w3.org/1999/XSL/Transform", "output", "method", node.getAttribute("method"))==null) 
     122            { 
     123                main.insertBefore(main_xsl.importNode(node, true), insertion_point); 
     124            } 
     125        } 
     126 
     127        // variables - only top level ones!! 
     128        // append to end of document 
     129        children = GSXML.getChildrenByTagNameNS(extra_xsl, "http://www.w3.org/1999/XSL/Transform", "variable"); 
     130        for (int i = 0; i < children.getLength(); i++) 
     131        { 
     132          Element node = (Element)children.item(i); 
     133            // If the new xsl:import element is identical (in terms of href attr value)  
     134            // to any in the merged document, don't copy it over 
     135            if (GSXML.getNamedElementNS(main, "http://www.w3.org/1999/XSL/Transform","variable", "name", node.getAttribute("name"))==null) 
    106136            { 
    107137                main.appendChild(main_xsl.importNode(node, true)); 
     
    109139        } 
    110140 
     141        // templates 
     142        // append to end of document 
    111143        children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "template"); 
    112144        for (int i = 0; i < children.getLength(); i++) 
    113145        { 
    114             Node node = children.item(i); 
    115             // remove any previous occurrences of xsl:template with the same value for name 
    116             // or even the same value for match (should we use priorities for match?) 
     146          Element node = (Element) children.item(i); 
     147            System.err.println("node name="+node.getNodeName()+", local name="+node.getLocalName()+", uri="+node.getNamespaceURI()); 
     148            // remove any previous occurrences of xsl:template with the same value for name or match  
     149              String template_match = node.getAttribute("match"); 
     150              String template_name = node.getAttribute("name"); 
    117151 
    118152            if (overwrite) 
    119153            { 
    120                 removeDuplicateElementsFrom(main, node, "xsl:template", "name"); 
    121                 removeDuplicateElementsFrom(main, node, "xsl:template", "match"); 
     154              // if we have a name attribute, remove any other similarly named template 
     155              if (!template_name.equals("")) { 
     156                GSXML.removeNamedElementNS(main, "http://www.w3.org/1999/XSL/Transform", "template", "name", template_name); 
     157              } 
     158              // if we have a match attribute, remove other templates that match -  
     159              if (!template_match.equals("")) { 
     160                GSXML.removeNamedElementsNS(main, "http://www.w3.org/1999/XSL/Transform", "template", "match", template_match); 
     161              } 
     162              // now add our good template in 
     163              main.appendChild(main_xsl.importNode(node, true)); 
     164            } 
     165            else  
     166              { 
     167            // if overwrite is false, then we only add in templates if they don't match something else. 
     168            // In this case (eg from expanding imported stylesheets) 
     169            // there can't be any duplicate named templates, so just look for matches 
     170                // we already have the one with highest import precedence (from the top most level) so don't add any more in 
     171                if (GSXML.getNamedElementNS(main, "http://www.w3.org/1999/XSL/Transform","template", "match", template_match) == null) 
     172            { 
    122173                main.appendChild(main_xsl.importNode(node, true)); 
    123174            } 
    124             else if (!isDuplicateElement(main, node, "xsl:template", "name")) 
    125             { 
    126                 main.appendChild(main_xsl.importNode(node, true)); 
    127             } 
     175              } 
    128176        } 
    129177 
     
    266314    } 
    267315 
    268     // In element main, tries to find any previous occurrence of elements with xsl-template-name=templateName,  
    269     // and whose named attribute (attributeName) has the same value as the same attribute in node. 
    270     // If this is the case, such a previous occurrence is removed from element main, since 
    271     // the new node will contain a more specific redefinition of this element. 
    272     public static void removeDuplicateElementsFrom(Element main, Node node, String templateName, String attrName) 
    273     { 
    274         String attr = ((Element) node).getAttribute(attrName); 
    275         if (!attr.equals("")) 
    276         { 
    277             Element old_template = GSXML.getNamedElement(main, templateName, attrName, attr); 
    278             if (old_template != null) 
    279             { 
    280                 main.removeChild(old_template); 
    281             } 
    282         } 
    283     } 
    284  
    285     // Call this method on elements like xsl:include, xsl:import and xsl:output 
    286     // In element main, tries to find any previous occurrence of elements with xsl-element-name=xslName,  
    287     // and whose named attribute (attributeName) has the same value as the same attribute in node. 
    288     // If this is the case, it returns true, since the element would a complete duplicate for our intents. 
    289     public static boolean isDuplicateElement(Element main, Node node, String xslName, String attrName) 
    290     { 
    291         String attr = ((Element) node).getAttribute(attrName); 
    292         if (!attr.equals("")) 
    293         { 
    294             Element old_element = GSXML.getNamedElement(main, xslName, attrName, attr); 
    295             if (old_element != null) 
    296             { 
    297                 return true; 
    298             } 
    299         } 
    300         return false; 
    301     } 
     316 
     317 
    302318 
    303319    /**