Changeset 25982

Show
Ignore:
Timestamp:
19.07.2012 14:34:31 (7 years ago)
Author:
sjm84
Message:

Added a method for merging two format elements

Files:
1 modified

Legend:

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

    r25926 r25982  
    163163            { 
    164164                // if we have a name attribute, remove any other similarly named template 
    165                 GSXML.removeElementsWithAttributesNS(main, "http://www.w3.org/1999/XSL/Transform", "template", new String[]{"name", "match", "mode"}, new String[]{template_name, template_match, template_mode}); 
     165                GSXML.removeElementsWithAttributesNS(main, "http://www.w3.org/1999/XSL/Transform", "template", new String[] { "name", "match", "mode" }, new String[] { template_name, template_match, template_mode }); 
    166166 
    167167                // now add our good template in 
     
    174174                // there can't be any duplicate named templates, so just look for matches 
    175175                // we already have the one with highest import precedence (from the top most level) so don't add any more in 
    176                 if (GSXML.getElementsWithAttributesNS(main, "http://www.w3.org/1999/XSL/Transform", "template", new String[]{"name", "match", "mode"}, new String[]{template_name, template_match, template_mode}).getLength() == 0) 
     176                if (GSXML.getElementsWithAttributesNS(main, "http://www.w3.org/1999/XSL/Transform", "template", new String[] { "name", "match", "mode" }, new String[] { template_name, template_match, template_mode }).getLength() == 0) 
    177177                { 
    178178                    main.appendChild(main_xsl.importNode(node, true)); 
     
    371371    } 
    372372 
     373    public static void mergeFormatElements(Element mainFormat, Element secondaryFormat, boolean overwrite) 
     374    { 
     375        NodeList xslChildren = GSXML.getChildrenByTagNameNS(secondaryFormat, "http://www.w3.org/1999/XSL/Transform", "variable"); 
     376        NodeList gsfChildren = GSXML.getChildrenByTagNameNS(secondaryFormat, "http://www.greenstone.org/greenstone3/schema/ConfigFormat", "variable"); 
     377        for (int i = 0; i < xslChildren.getLength() + gsfChildren.getLength(); i++) 
     378        { 
     379            Element node = (Element) ((i < xslChildren.getLength()) ? xslChildren.item(i) : gsfChildren.item(i - xslChildren.getLength())); 
     380            if (GSXML.getNamedElementNS(mainFormat, "http://www.w3.org/1999/XSL/Transform", "variable", "name", node.getAttribute("name")) == null && GSXML.getNamedElementNS(mainFormat, "http://www.greenstone.org/greenstone3/schema/ConfigFormat", "variable", "name", node.getAttribute("name")) == null) 
     381            { 
     382                mainFormat.appendChild(node); 
     383            } 
     384        } 
     385 
     386        xslChildren = GSXML.getChildrenByTagNameNS(secondaryFormat, "http://www.w3.org/1999/XSL/Transform", "param"); 
     387        gsfChildren = GSXML.getChildrenByTagNameNS(secondaryFormat, "http://www.greenstone.org/greenstone3/schema/ConfigFormat", "param"); 
     388        for (int i = 0; i < xslChildren.getLength() + gsfChildren.getLength(); i++) 
     389        { 
     390            Element node = (Element) ((i < xslChildren.getLength()) ? xslChildren.item(i) : gsfChildren.item(i - xslChildren.getLength())); 
     391            if (GSXML.getNamedElementNS(mainFormat, "http://www.w3.org/1999/XSL/Transform", "param", "name", node.getAttribute("name")) == null && GSXML.getNamedElementNS(mainFormat, "http://www.greenstone.org/greenstone3/schema/ConfigFormat", "param", "name", node.getAttribute("name")) == null) 
     392            { 
     393                mainFormat.appendChild(node); 
     394            } 
     395        } 
     396 
     397        xslChildren = GSXML.getChildrenByTagNameNS(secondaryFormat, "http://www.w3.org/1999/XSL/Transform", "template"); 
     398        gsfChildren = GSXML.getChildrenByTagNameNS(secondaryFormat, "http://www.greenstone.org/greenstone3/schema/ConfigFormat", "template"); 
     399        for (int i = 0; i < xslChildren.getLength() + gsfChildren.getLength(); i++) 
     400        { 
     401            Element node = (Element) ((i < xslChildren.getLength()) ? xslChildren.item(i) : gsfChildren.item(i - xslChildren.getLength())); 
     402            // remove any previous occurrences of xsl:template with the same value for name or match  
     403            String template_match = node.getAttribute("match"); 
     404            String template_name = node.getAttribute("name"); 
     405            String template_mode = node.getAttribute("mode"); 
     406 
     407            String[] attributeNames = new String[] { "name", "match", "mode" }; 
     408            String[] attributeValues = new String[] { template_name, template_match, template_mode }; 
     409 
     410            if (overwrite) 
     411            { 
     412                // if we have a name attribute, remove any other similarly named template 
     413                GSXML.removeElementsWithAttributesNS(mainFormat, "http://www.w3.org/1999/XSL/Transform", "template", attributeNames, attributeValues); 
     414                GSXML.removeElementsWithAttributesNS(mainFormat, "http://www.greenstone.org/greenstone3/schema/ConfigFormat", "template", attributeNames, attributeValues); 
     415 
     416                // now add our good template in 
     417                mainFormat.appendChild(node); 
     418            } 
     419            else 
     420            { 
     421                // if overwrite is false, then we only add in templates if they don't match something else. 
     422                // In this case (eg from expanding imported stylesheets) 
     423                // there can't be any duplicate named templates, so just look for matches 
     424                // we already have the one with highest import precedence (from the top most level) so don't add any more in 
     425                if (GSXML.getElementsWithAttributesNS(mainFormat, "http://www.w3.org/1999/XSL/Transform", "template", attributeNames, attributeValues).getLength() == 0 && GSXML.getElementsWithAttributesNS(mainFormat, "http://www.greenstone.org/greenstone3/schema/ConfigFormat", "template", attributeNames, attributeValues).getLength() == 0) 
     426                { 
     427                    mainFormat.appendChild(node); 
     428                } 
     429            } 
     430        } 
     431 
     432        gsfChildren = GSXML.getChildrenByTagNameNS(secondaryFormat, "http://www.greenstone.org/greenstone3/schema/ConfigFormat", "option"); 
     433        for (int i = 0; i < gsfChildren.getLength(); i++) 
     434        { 
     435            Element node = (Element) gsfChildren.item(i); 
     436            if (GSXML.getNamedElementNS(mainFormat, "http://www.greenstone.org/greenstone3/schema/ConfigFormat", "option", "name", node.getAttribute("name")) == null) 
     437            { 
     438                mainFormat.appendChild(node); 
     439            } 
     440        } 
     441    } 
    373442}