Ignore:
Timestamp:
2012-11-14T15:37:35+13:00 (11 years ago)
Author:
sjm84
Message:

Phase one of commiting the improved debugging system

File:
1 edited

Legend:

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

    r26055 r26458  
    2323import java.util.Vector;
    2424
     25import org.apache.log4j.Logger;
    2526import org.greenstone.util.GlobalProperties;
    2627import org.w3c.dom.Document;
    2728import org.w3c.dom.Element;
     29import org.w3c.dom.NamedNodeMap;
    2830import org.w3c.dom.Node;
    2931import org.w3c.dom.NodeList;
    30 
    31 import org.apache.log4j.*;
    3232
    3333/** various functions for manipulating Greenstone xslt */
     
    5555        if (debug)
    5656        {
    57             System.err.println("ADDING DEBUG ELEMENTS WITH FILE NAME " + firstDocFileName);
    5857            insertDebugElements(main_xsl, firstDocFileName);
    5958        }
     
    188187        if (debug)
    189188        {
    190             System.err.println("ADDING DEBUG ELEMENTS WITH FILE NAME " + secondDocFileName);
    191189            insertDebugElements(main_xsl, secondDocFileName);
    192190        }
    193191    }
    194192
    195     protected static void insertDebugElements(Document doc, String fileName)
    196     {
    197         NodeList htmlTags = GSXML.getHTMLStructureElements(doc);
    198         System.err.println("HTML TAGS SIZE IS " + htmlTags.getLength());
    199         for (int i = 0; i < htmlTags.getLength(); i++)
    200         {
    201             Element current = (Element) htmlTags.item(i);
    202             if (current.getUserData("GSDEBUGFILENAME") == null)
    203             {
    204                 Element xslParent = (Element) current.getParentNode();
    205 
    206                 while (xslParent.getNamespaceURI() != GSXML.XSL_NAMESPACE && !xslParent.getNodeName().startsWith("xsl:"))
    207                 {
    208                     xslParent = (Element) xslParent.getParentNode();
    209                 }
    210 
    211                 System.err.println("ADDING FILE NAME " + fileName);
    212                 current.setUserData("GSDEBUGFILENAME", fileName, null);
    213                 current.setUserData("GSDEBUGXML", xslParent.cloneNode(true), null);
     193    protected static void insertDebugElements(Document doc, String filename)
     194    {
     195        NodeList xslTemplates = doc.getElementsByTagNameNS(GSXML.XSL_NAMESPACE, "template");
     196        NodeList gsfTemplates = doc.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "template");
     197
     198        for (int i = 0; i < xslTemplates.getLength() + gsfTemplates.getLength(); i++)
     199        {
     200            boolean gsf = (i >= xslTemplates.getLength());
     201            Element currentTemplate = (Element) (!gsf ? xslTemplates.item(i) : gsfTemplates.item(i - xslTemplates.getLength()));
     202
     203            NodeList childNodes = currentTemplate.getChildNodes();
     204            boolean debugInformationAlreadyExists = false;
     205            for (int j = 0; j < childNodes.getLength(); j++)
     206            {
     207                Node current = childNodes.item(j);
     208                if (current instanceof Element && ((Element) current).getNodeName().equals("debug") && (!((Element) current).getAttribute("nodename").startsWith("gsf:") || ((Element) current).getAttribute("nodename").equals("gsf:template")))
     209                {
     210                    debugInformationAlreadyExists = true;
     211                    break;
     212                }
     213            }
     214
     215            if (debugInformationAlreadyExists)
     216            {
     217                continue;
     218            }
     219
     220            Element debugElement = doc.createElement("debug");
     221            debugElement.setAttribute("filename", filename);
     222            debugElement.setAttribute("nodename", gsf ? "gsf:template" : "xsl:template");
     223            if (currentTemplate.getAttribute("match").length() > 0)
     224            {
     225                debugElement.setAttribute("match", currentTemplate.getAttribute("match"));
     226            }
     227            if (currentTemplate.getAttribute("name").length() > 0)
     228            {
     229                debugElement.setAttribute("name", currentTemplate.getAttribute("name"));
     230            }
     231
     232            Element currentDebugElement = (Element) debugElement.cloneNode(true);
     233
     234            if (childNodes.getLength() > 0)
     235            {
     236                int paramCount = 0;
     237                while (childNodes.getLength() > paramCount)
     238                {
     239                    Node currentNode = childNodes.item(paramCount);
     240                    if (currentNode instanceof Element)
     241                    {
     242                        if (((Element) currentNode).getNodeName().equals("xsl:param") || ((Element) currentNode).getNodeName().equals("xslt:param") || (((Element) currentNode).getNodeName().equals("param") && ((Element) currentNode).getNamespaceURI().equals(GSXML.XSL_NAMESPACE)))
     243                        {
     244                            paramCount++;
     245                        }
     246                        else
     247                        {
     248                            currentDebugElement.appendChild(currentNode);
     249                        }
     250                    }
     251                    else
     252                    {
     253                        currentDebugElement.appendChild(currentNode);
     254                    }
     255                }
     256                currentTemplate.appendChild(currentDebugElement);
    214257            }
    215258            else
    216259            {
    217                 System.err.println("ALREADY SET!");
    218             }
     260                currentTemplate.appendChild(currentDebugElement);
     261            }
     262
     263            Element textElement = doc.createElementNS(GSXML.XSL_NAMESPACE, "text");
     264            textElement.appendChild(doc.createTextNode(" "));
     265            currentDebugElement.appendChild(textElement);
    219266        }
    220267    }
     
    227274    public static void inlineImportAndIncludeFilesDebug(Document doc, String pathExtra, boolean debug, String docFileName, String site, String collection, String interface_name, ArrayList<String> base_interfaces)
    228275    {
    229         XMLConverter converter = new XMLConverter();
    230 
    231276        String path = (pathExtra == null) ? "" : pathExtra;
    232277
     
    238283            Element current = (Element) ((i < importList.getLength()) ? importList.item(i) : includeList.item(i - importList.getLength()));
    239284            String href = current.getAttribute("href");
    240             //String filePath = GSFile.interfaceHome(GlobalProperties.getGSDL3Home(), interface_name) + File.separator + "transform" + File.separator + path.replace("/", File.separator) + href.replace("/", File.separator);
    241             //String filePath = GSFile.stylesheetFile(GlobalProperties.getGSDL3Home(), site_name, collection, interface_name, base_interfaces,
    242285
    243286            try
     
    254297                //Do this recursively
    255298                inlineImportAndIncludeFilesDebug(inlineDoc, newPath, debug, "merged " + href/* filePath */, site, collection, interface_name, base_interfaces);
    256 
    257299                GSXSLT.mergeStylesheetsDebug(doc, inlineDoc.getDocumentElement(), false, debug, docFileName, /* filePath */"merged " + href);
    258300            }
     
    312354        }
    313355
     356        if (stylesheets.size() == 1 && debug)
     357        {
     358            insertDebugElements(finalDoc, stylesheets.get(0).getAbsolutePath());
     359        }
     360
    314361        return finalDoc;
    315362    }
    316363
    317     public static void modifyConfigFormatForDebug(Document doc, String fileName)
     364    public static void modifyConfigFormatForDebug(Document doc, String filename)
    318365    {
    319366        NodeList templateNodes = doc.getElementsByTagNameNS(GSXML.XSL_NAMESPACE, "template");
     
    323370        }
    324371
    325         String debugElementString = "";
    326         debugElementString += "<span class=\"configDebugSpan\" style=\"display:none;\">";
    327         debugElementString += "  \"filename\":\"" + fileName + "\",";
    328         debugElementString += "  \"xml\":\"<xsl:value-of select=\"util:xmlNodeToString(.)\"/>\""; //<xsl:copy><xsl:copy-of select=\"@*\"/></xsl:copy>
    329         debugElementString += "</span>";
    330 
    331         XMLConverter converter = new XMLConverter();
    332         Element debugElement = (Element) converter.getDOM("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xslt:stylesheet version=\"1.0\" xmlns:xsl=\"" + GSXML.XSL_NAMESPACE + "\" xmlns:xslt=\"output.xsl\" xmlns:gsf=\"" + GSXML.GSF_NAMESPACE + "\">" + debugElementString + "</xslt:stylesheet>").getDocumentElement().getFirstChild();
     372        Element debugElement = doc.createElement("debug");
     373        debugElement.setAttribute("filename", filename);
    333374
    334375        for (int i = 0; i < templateNodes.getLength(); i++)
    335376        {
    336377            Element currentTemplate = (Element) templateNodes.item(i);
    337             if (currentTemplate.getAttribute("match") != null && (currentTemplate.getAttribute("match").equals("gsf:metadata") || currentTemplate.getAttribute("match").equals("*") || currentTemplate.getAttribute("match").equals("format")))
     378            if (currentTemplate.getAttribute("match").equals("*") || currentTemplate.getAttribute("match").equals("format"))
    338379            {
    339380                continue;
    340381            }
    341382
     383            Element currentDebugElement = (Element) debugElement.cloneNode(true);
     384
     385            //The xsl that gets the name of current node (e.g. gsf:icon)
     386            Element nodeNameAttribute = doc.createElementNS(GSXML.XSL_NAMESPACE, "attribute");
     387            nodeNameAttribute.setAttribute(GSXML.NAME_ATT, "nodeName");
     388            Element nodeNameValue = doc.createElementNS(GSXML.XSL_NAMESPACE, "value-of");
     389            nodeNameValue.setAttribute("select", "name()");
     390            nodeNameAttribute.appendChild(nodeNameValue);
     391            currentDebugElement.appendChild(nodeNameAttribute);
     392
     393            //The xsl that copies the attributes of the current node
     394            Element attributeCopy = doc.createElementNS(GSXML.XSL_NAMESPACE, "copy-of");
     395            attributeCopy.setAttribute("select", "@*");
     396            currentDebugElement.appendChild(attributeCopy);
     397
    342398            if (currentTemplate.hasChildNodes())
    343399            {
    344                 currentTemplate.insertBefore(doc.importNode(debugElement.cloneNode(true), true), currentTemplate.getFirstChild());
     400                NodeList childNodes = currentTemplate.getChildNodes();
     401                while (childNodes.getLength() > 0)
     402                {
     403                    currentDebugElement.appendChild(childNodes.item(0));
     404                }
     405                currentTemplate.appendChild(currentDebugElement);
    345406            }
    346407            else
    347408            {
    348                 currentTemplate.appendChild(doc.importNode(debugElement.cloneNode(true), true));
     409                currentTemplate.appendChild(currentDebugElement);
    349410            }
    350411        }
     
    485546        }
    486547    }
     548
     549    public static void fixTables(Document doc)
     550    {
     551        NodeList debugElements = doc.getElementsByTagName("debug");
     552        for (int i = 0; i < debugElements.getLength(); i++)
     553        {
     554            Element currentElement = (Element) debugElements.item(i);
     555
     556            boolean hasChildElements = false;
     557            NodeList children = currentElement.getChildNodes();
     558            for (int j = 0; j < children.getLength(); j++)
     559            {
     560                Node current = children.item(j);
     561                if (current instanceof Element)
     562                {
     563                    hasChildElements = true;
     564                }
     565            }
     566
     567            if (hasChildElements && currentElement.getParentNode() != null && currentElement.getParentNode() instanceof Element)
     568            {
     569                Element parent = (Element) currentElement.getParentNode();
     570                if (parent.getNodeName().toLowerCase().equals("table") || parent.getNodeName().toLowerCase().equals("tr"))
     571                {
     572                    parent.setAttribute("debug", "true");
     573                    NamedNodeMap attributes = currentElement.getAttributes();
     574                    for (int j = 0; j < attributes.getLength(); j++)
     575                    {
     576                        Node currentAttribute = attributes.item(j);
     577                        String name = currentAttribute.getNodeName();
     578                        String value = currentAttribute.getNodeValue();
     579                        parent.setAttribute(name, value);
     580                    }
     581                }
     582            }
     583        }
     584    }
    487585}
Note: See TracChangeset for help on using the changeset viewer.