Changeset 26561 for main/trunk


Ignore:
Timestamp:
2012-12-06T15:48:18+13:00 (11 years ago)
Author:
sjm84
Message:

Make sure angle brackets in attributes are properly escaped

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/DebugService.java

    r26556 r26561  
    1616import org.w3c.dom.Document;
    1717import org.w3c.dom.Element;
     18import org.w3c.dom.NamedNodeMap;
     19import org.w3c.dom.Node;
    1820import org.w3c.dom.NodeList;
    1921
     
    127129                    if (template.getAttribute("name").equals(nameToGet))
    128130                    {
     131                        fixAttributes(template);
     132
    129133                        Element requestedTemplate = this.doc.createElement("requestedNameTemplate");
    130134                        requestedTemplate.appendChild(this.doc.importNode(template, true));
     
    142146                    if (template.getAttribute("match").equals(matchToGet))
    143147                    {
     148                        fixAttributes(template);
     149
    144150                        Element requestedTemplate = this.doc.createElement("requestedMatchTemplate");
    145151                        requestedTemplate.appendChild(this.doc.importNode(template, true));
     
    151157
    152158        return result;
     159    }
     160
     161    protected void fixAttributes(Element template)
     162    {
     163        NodeList nodes = template.getElementsByTagName("*");
     164        for (int j = 0; j < nodes.getLength(); j++)
     165        {
     166            Node current = nodes.item(j);
     167            NamedNodeMap attributes = current.getAttributes();
     168            for (int k = 0; k < attributes.getLength(); k++)
     169            {
     170                Node currentAttr = attributes.item(k);
     171                String value = currentAttr.getNodeValue();
     172                if (value.contains("&") || value.contains("<") || value.contains(">"))
     173                {
     174                    currentAttr.setNodeValue(value.replace("&", "&amp;amp;").replace("<", "&lt;").replace(">", "&gt;"));
     175                }
     176            }
     177        }
    153178    }
    154179
     
    207232            NodeList templateElems = xslDoc.getElementsByTagNameNS(fullNamespace, nodeName);
    208233
    209             /*
    210              * NodeList textElems =
    211              * xslDoc.getElementsByTagNameNS(GSXML.XSL_NAMESPACE, "text"); for
    212              * (int i = 0; i < textElems.getLength(); i++) {
    213              * System.err.println("\n\n" + i + "\n\n" +
    214              * GSXML.xmlNodeToString(textElems.item(i))); }
    215              */
    216 
    217234            boolean found = false;
    218235            if (nameToSave != null && nameToSave.length() != 0)
     
    225242                        try
    226243                        {
    227                             System.err.println(xml);
    228244                            Element newTemplate = (Element) converter.getDOM("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"" + GSXML.XSL_NAMESPACE + "\" xmlns:java=\"" + GSXML.JAVA_NAMESPACE + "\" xmlns:util=\"" + GSXML.UTIL_NAMESPACE + "\" xmlns:gsf=\"" + GSXML.GSF_NAMESPACE + "\">" + xml + "</xsl:stylesheet>", "UTF-8").getDocumentElement().getElementsByTagNameNS(fullNamespace, nodeName).item(0);
    229245                            template.getParentNode().replaceChild(xslDoc.importNode(newTemplate, true), template);
Note: See TracChangeset for help on using the changeset viewer.