Changeset 26558 for main/trunk


Ignore:
Timestamp:
2012-12-05T15:15:50+13:00 (11 years ago)
Author:
sjm84
Message:

Fixing the problem where multiple debug elements are between a table and a tr tag or between a tr tag and a td tag

File:
1 edited

Legend:

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

    r26472 r26558  
    2121import java.io.File;
    2222import java.util.ArrayList;
     23import java.util.HashMap;
    2324import java.util.Vector;
    2425
     
    204205    }
    205206
    206     protected static void insertDebugElements(Document doc, String filename)
     207    public static void insertDebugElements(Document doc, String filename)
    207208    {
    208209        NodeList xslTemplates = doc.getElementsByTagNameNS(GSXML.XSL_NAMESPACE, "template");
     
    234235            debugElement.setAttribute("filename", filename);
    235236            debugElement.setAttribute("nodename", gsf ? "gsf:template" : "xsl:template");
     237
    236238            if (currentTemplate.getAttribute("match").length() > 0)
    237239            {
     
    242244                debugElement.setAttribute("name", currentTemplate.getAttribute("name"));
    243245            }
    244 
    245             Element currentDebugElement = (Element) debugElement.cloneNode(true);
    246246
    247247            if (childNodes.getLength() > 0)
     
    259259                        else
    260260                        {
    261                             currentDebugElement.appendChild(currentNode);
     261                            debugElement.appendChild(currentNode);
    262262                        }
    263263                    }
    264264                    else
    265265                    {
    266                         currentDebugElement.appendChild(currentNode);
     266                        debugElement.appendChild(currentNode);
    267267                    }
    268268                }
    269                 currentTemplate.appendChild(currentDebugElement);
     269                currentTemplate.appendChild(debugElement);
    270270            }
    271271            else
    272272            {
    273                 currentTemplate.appendChild(currentDebugElement);
     273                currentTemplate.appendChild(debugElement);
    274274            }
    275275
    276276            Element textElement = doc.createElementNS(GSXML.XSL_NAMESPACE, "text");
    277277            textElement.appendChild(doc.createTextNode(" "));
    278             currentDebugElement.appendChild(textElement);
     278            debugElement.appendChild(textElement);
    279279        }
    280280    }
     
    373373
    374374        return finalDoc;
    375     }
    376 
    377     public static void modifyConfigFormatForDebug(Document doc, String filename)
    378     {
    379         NodeList templateNodes = doc.getElementsByTagNameNS(GSXML.XSL_NAMESPACE, "template");
    380         if (templateNodes.getLength() == 0)
    381         {
    382             templateNodes = doc.getElementsByTagName("xsl:template");
    383         }
    384 
    385         Element debugElement = doc.createElement("debug");
    386         debugElement.setAttribute("filename", filename);
    387 
    388         for (int i = 0; i < templateNodes.getLength(); i++)
    389         {
    390             Element currentTemplate = (Element) templateNodes.item(i);
    391             if (currentTemplate.getAttribute("match").equals("*") || currentTemplate.getAttribute("match").equals("format"))
    392             {
    393                 continue;
    394             }
    395 
    396             Element currentDebugElement = (Element) debugElement.cloneNode(true);
    397 
    398             //The xsl that gets the name of current node (e.g. gsf:icon)
    399             Element nodeNameAttribute = doc.createElementNS(GSXML.XSL_NAMESPACE, "attribute");
    400             nodeNameAttribute.setAttribute(GSXML.NAME_ATT, "nodeName");
    401             Element nodeNameValue = doc.createElementNS(GSXML.XSL_NAMESPACE, "value-of");
    402             nodeNameValue.setAttribute("select", "name()");
    403             nodeNameAttribute.appendChild(nodeNameValue);
    404             currentDebugElement.appendChild(nodeNameAttribute);
    405 
    406             //The xsl that copies the attributes of the current node
    407             Element attributeCopy = doc.createElementNS(GSXML.XSL_NAMESPACE, "copy-of");
    408             attributeCopy.setAttribute("select", "@*");
    409             currentDebugElement.appendChild(attributeCopy);
    410 
    411             if (currentTemplate.hasChildNodes())
    412             {
    413                 NodeList childNodes = currentTemplate.getChildNodes();
    414                 while (childNodes.getLength() > 0)
    415                 {
    416                     currentDebugElement.appendChild(childNodes.item(0));
    417                 }
    418                 currentTemplate.appendChild(currentDebugElement);
    419             }
    420             else
    421             {
    422                 currentTemplate.appendChild(currentDebugElement);
    423             }
    424         }
    425375    }
    426376
     
    563513    {
    564514        NodeList debugElements = doc.getElementsByTagName("debug");
     515
     516        HashMap<Element, ArrayList<Element>> tracker = new HashMap<Element, ArrayList<Element>>();
    565517        for (int i = 0; i < debugElements.getLength(); i++)
    566518        {
     
    580532            if (hasChildElements && currentElement.getParentNode() != null && currentElement.getParentNode() instanceof Element)
    581533            {
    582                 Element parent = (Element) currentElement.getParentNode();
     534                Element parent = findNonDebugParent(currentElement);
     535                if (parent == null)
     536                {
     537                    continue;
     538                }
     539
    583540                if (parent.getNodeName().toLowerCase().equals("table") || parent.getNodeName().toLowerCase().equals("tr"))
    584541                {
    585                     parent.setAttribute("debug", "true");
    586                     NamedNodeMap attributes = currentElement.getAttributes();
    587                     for (int j = 0; j < attributes.getLength(); j++)
     542                    if (tracker.get(parent) == null)
    588543                    {
    589                         Node currentAttribute = attributes.item(j);
    590                         String name = currentAttribute.getNodeName();
    591                         String value = currentAttribute.getNodeValue();
    592                         parent.setAttribute(name, value);
     544                        ArrayList<Element> debugElems = new ArrayList<Element>();
     545                        debugElems.add(currentElement);
     546                        tracker.put(parent, debugElems);
    593547                    }
    594                 }
    595             }
    596         }
     548                    else
     549                    {
     550                        ArrayList<Element> debugElems = tracker.get(parent);
     551                        debugElems.add(currentElement);
     552                    }
     553                }
     554            }
     555        }
     556
     557        for (Element tableElem : tracker.keySet())
     558        {
     559            ArrayList<Element> debugElems = tracker.get(tableElem);
     560            ArrayList<String> attrNames = new ArrayList<String>();
     561
     562            for (Element debugElem : debugElems)
     563            {
     564                NamedNodeMap attributes = debugElem.getAttributes();
     565                for (int i = 0; i < attributes.getLength(); i++)
     566                {
     567                    attrNames.add(attributes.item(i).getNodeName());
     568                }
     569            }
     570
     571            for (String name : attrNames)
     572            {
     573                String attrValueString = "[";
     574                for (int i = debugElems.size() - 1; i >= 0; i--)
     575                {
     576                    Element current = debugElems.get(i);
     577                    attrValueString += "\'" + current.getAttribute(name).replace("\\", "\\\\").replace("'", "\\'") + "\'";
     578                    if (i != 0)
     579                    {
     580                        attrValueString += ",";
     581                    }
     582                }
     583                attrValueString += "]";
     584
     585                tableElem.setAttribute(name, attrValueString);
     586            }
     587            tableElem.setAttribute("debug", "true");
     588            tableElem.setAttribute("debugSize", "" + debugElems.size());
     589        }
     590    }
     591
     592    private static Element findNonDebugParent(Element elem)
     593    {
     594        Node parent = elem.getParentNode();
     595        while (parent instanceof Element && parent.getNodeName().equals("debug"))
     596        {
     597            parent = parent.getParentNode();
     598        }
     599
     600        if (parent instanceof Element)
     601        {
     602            return (Element) parent;
     603        }
     604        return null;
    597605    }
    598606}
Note: See TracChangeset for help on using the changeset viewer.