[16869] | 1 | /*
|
---|
| 2 | * GSXSLT.java
|
---|
| 3 | * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
|
---|
| 4 | *
|
---|
| 5 | * This program is free software; you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program; if not, write to the Free Software
|
---|
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 18 | */
|
---|
[3450] | 19 | package org.greenstone.gsdl3.util;
|
---|
| 20 |
|
---|
[25656] | 21 | import java.io.File;
|
---|
| 22 | import java.util.ArrayList;
|
---|
| 23 | import java.util.Vector;
|
---|
| 24 |
|
---|
| 25 | import org.greenstone.util.GlobalProperties;
|
---|
| 26 | import org.w3c.dom.Document;
|
---|
| 27 | import org.w3c.dom.Element;
|
---|
[3450] | 28 | import org.w3c.dom.Node;
|
---|
| 29 | import org.w3c.dom.NodeList;
|
---|
| 30 |
|
---|
[24864] | 31 | /** various functions for manipulating Greenstone xslt */
|
---|
| 32 | public class GSXSLT
|
---|
| 33 | {
|
---|
[25656] | 34 | public static void mergeStylesheets(Document main_xsl, Element extra_xsl, boolean overwrite)
|
---|
| 35 | {
|
---|
| 36 | mergeStylesheetsDebug(main_xsl, extra_xsl, overwrite, false, null, null);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[24864] | 39 | /**
|
---|
| 40 | * takes a stylesheet Document, and adds in any child nodes from extra_xsl
|
---|
| 41 | * named templates overwrite any existing one, while match templates are
|
---|
[25820] | 42 | * just added to the end of the stylesheet
|
---|
[25913] | 43 | *
|
---|
| 44 | * elements are added in following order, and added to preserve original
|
---|
| 45 | * order with imported ones coming after existing ones import, include,
|
---|
| 46 | * output, variable, template
|
---|
[24864] | 47 | */
|
---|
[25656] | 48 | public static void mergeStylesheetsDebug(Document main_xsl, Element extra_xsl, boolean overwrite, boolean debug, String firstDocFileName, String secondDocFileName)
|
---|
[24864] | 49 | {
|
---|
[25656] | 50 | if (debug)
|
---|
| 51 | {
|
---|
| 52 | System.err.println("ADDING DEBUG ELEMENTS WITH FILE NAME " + firstDocFileName);
|
---|
| 53 | insertDebugElements(main_xsl, firstDocFileName);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[24864] | 56 | Element main = main_xsl.getDocumentElement();
|
---|
[25820] | 57 | Node insertion_point = null;
|
---|
[25913] | 58 | Element last_import = GSXML.getLastElementByTagNameNS(main, "http://www.w3.org/1999/XSL/Transform", "import");
|
---|
| 59 | if (last_import != null)
|
---|
| 60 | {
|
---|
| 61 | insertion_point = last_import.getNextSibling();
|
---|
| 62 | }
|
---|
| 63 | else
|
---|
| 64 | {
|
---|
| 65 | insertion_point = main.getFirstChild();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[25820] | 68 | // imports
|
---|
| 69 | NodeList children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "import");
|
---|
[25656] | 70 | for (int i = 0; i < children.getLength(); i++)
|
---|
| 71 | {
|
---|
[25913] | 72 | Element node = (Element) children.item(i);
|
---|
[25397] | 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
|
---|
[25820] | 75 | if (GSXML.getNamedElementNS(main, "http://www.w3.org/1999/XSL/Transform", "import", "href", node.getAttribute("href")) == null)
|
---|
[25656] | 76 | {
|
---|
| 77 | // Import statements should be the first children of an xsl:stylesheet element
|
---|
| 78 | // If firstchild is null, then this xsl:import element will be inserted at the "end"
|
---|
| 79 | // Although Node.insertBefore() will first remove identical nodes before inserting, we check
|
---|
| 80 | // only the href attribute to see if they're "identical" to any pre-existing <xsl:import>
|
---|
[25820] | 81 | //main.insertBefore(main_xsl.importNode(node, true), main.getFirstChild());
|
---|
[25913] | 82 | main.insertBefore(main_xsl.importNode(node, true), insertion_point);
|
---|
[25656] | 83 | }
|
---|
| 84 | }
|
---|
[25913] | 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 | {
|
---|
| 90 | insertion_point = last_include.getNextSibling();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[25820] | 93 | // includes
|
---|
[25397] | 94 | children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "include");
|
---|
[25656] | 95 | for (int i = 0; i < children.getLength(); i++)
|
---|
| 96 | {
|
---|
[25913] | 97 | Element node = (Element) children.item(i);
|
---|
[25397] | 98 | // If the new xsl:include element is identical (in terms of href attr value)
|
---|
| 99 | // to any in the merged document, don't copy it over
|
---|
| 100 | // Although Node.appendChild() will first remove identical nodes before appending, we check
|
---|
| 101 | // only the href attribute to see if they're "identical" to any pre-existing <xsl:include>
|
---|
[25913] | 102 | if (GSXML.getNamedElementNS(main, "http://www.w3.org/1999/XSL/Transform", "include", "href", node.getAttribute("href")) == null)
|
---|
[25656] | 103 | {
|
---|
[25913] | 104 | //main.appendChild(main_xsl.importNode(node, true));
|
---|
| 105 | main.insertBefore(main_xsl.importNode(node, true), insertion_point);
|
---|
[25397] | 106 | }
|
---|
[25820] | 107 | } // for each include
|
---|
[25389] | 108 |
|
---|
[25914] | 109 | if (main.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "output").getLength() == 0)
|
---|
[25913] | 110 | {
|
---|
[25914] | 111 | // outputs
|
---|
| 112 | children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "output");
|
---|
| 113 | for (int i = 0; i < children.getLength(); i++)
|
---|
| 114 | {
|
---|
| 115 | Element node = (Element) children.item(i);
|
---|
| 116 | // If the new xsl:output element is identical (in terms of the value for the method attr)
|
---|
| 117 | // to any in the merged document, don't copy it over
|
---|
| 118 |
|
---|
| 119 | main.insertBefore(main_xsl.importNode(node, true), insertion_point);
|
---|
| 120 | }
|
---|
[25913] | 121 | }
|
---|
| 122 |
|
---|
[25914] | 123 | // variables - only top level ones!!
|
---|
| 124 | // append to end of document
|
---|
| 125 | children = GSXML.getChildrenByTagNameNS(extra_xsl, "http://www.w3.org/1999/XSL/Transform", "variable");
|
---|
[25656] | 126 | for (int i = 0; i < children.getLength(); i++)
|
---|
| 127 | {
|
---|
[25913] | 128 | Element node = (Element) children.item(i);
|
---|
[25914] | 129 | // If the new xsl:import element is identical (in terms of href attr value)
|
---|
[25397] | 130 | // to any in the merged document, don't copy it over
|
---|
[25914] | 131 | if (GSXML.getNamedElementNS(main, "http://www.w3.org/1999/XSL/Transform", "variable", "name", node.getAttribute("name")) == null)
|
---|
[25656] | 132 | {
|
---|
[25914] | 133 | main.appendChild(main_xsl.importNode(node, true));
|
---|
[25820] | 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[25914] | 137 | // params - only top level ones!!
|
---|
[25820] | 138 | // append to end of document
|
---|
[25914] | 139 | children = GSXML.getChildrenByTagNameNS(extra_xsl, "http://www.w3.org/1999/XSL/Transform", "param");
|
---|
[25820] | 140 | for (int i = 0; i < children.getLength(); i++)
|
---|
| 141 | {
|
---|
[25913] | 142 | Element node = (Element) children.item(i);
|
---|
[25820] | 143 | // If the new xsl:import element is identical (in terms of href attr value)
|
---|
| 144 | // to any in the merged document, don't copy it over
|
---|
[25914] | 145 | if (GSXML.getNamedElementNS(main, "http://www.w3.org/1999/XSL/Transform", "param", "name", node.getAttribute("name")) == null)
|
---|
[25820] | 146 | {
|
---|
[25656] | 147 | main.appendChild(main_xsl.importNode(node, true));
|
---|
[25397] | 148 | }
|
---|
[25381] | 149 | }
|
---|
| 150 |
|
---|
[25820] | 151 | // templates
|
---|
| 152 | // append to end of document
|
---|
[25389] | 153 | children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "template");
|
---|
[24864] | 154 | for (int i = 0; i < children.getLength(); i++)
|
---|
| 155 | {
|
---|
[25913] | 156 | Element node = (Element) children.item(i);
|
---|
[25820] | 157 | // remove any previous occurrences of xsl:template with the same value for name or match
|
---|
[25913] | 158 | String template_match = node.getAttribute("match");
|
---|
| 159 | String template_name = node.getAttribute("name");
|
---|
[25924] | 160 | String template_mode = node.getAttribute("mode");
|
---|
[25656] | 161 |
|
---|
| 162 | if (overwrite)
|
---|
| 163 | {
|
---|
[25913] | 164 | // if we have a name attribute, remove any other similarly named template
|
---|
[25926] | 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});
|
---|
| 166 |
|
---|
[25913] | 167 | // now add our good template in
|
---|
| 168 | main.appendChild(main_xsl.importNode(node, true));
|
---|
[25656] | 169 | }
|
---|
[25913] | 170 | else
|
---|
[25656] | 171 | {
|
---|
[25913] | 172 | // if overwrite is false, then we only add in templates if they don't match something else.
|
---|
| 173 | // In this case (eg from expanding imported stylesheets)
|
---|
| 174 | // there can't be any duplicate named templates, so just look for matches
|
---|
| 175 | // we already have the one with highest import precedence (from the top most level) so don't add any more in
|
---|
[25926] | 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)
|
---|
[25913] | 177 | {
|
---|
| 178 | main.appendChild(main_xsl.importNode(node, true));
|
---|
| 179 | }
|
---|
[25656] | 180 | }
|
---|
[4704] | 181 | }
|
---|
[25656] | 182 |
|
---|
| 183 | if (debug)
|
---|
| 184 | {
|
---|
| 185 | System.err.println("ADDING DEBUG ELEMENTS WITH FILE NAME " + secondDocFileName);
|
---|
| 186 | insertDebugElements(main_xsl, secondDocFileName);
|
---|
| 187 | }
|
---|
[3450] | 188 | }
|
---|
| 189 |
|
---|
[25656] | 190 | protected static void insertDebugElements(Document doc, String fileName)
|
---|
| 191 | {
|
---|
| 192 | NodeList htmlTags = GSXML.getHTMLStructureElements(doc);
|
---|
| 193 | System.err.println("HTML TAGS SIZE IS " + htmlTags.getLength());
|
---|
| 194 | for (int i = 0; i < htmlTags.getLength(); i++)
|
---|
| 195 | {
|
---|
| 196 | Element current = (Element) htmlTags.item(i);
|
---|
| 197 | if (current.getUserData("GSDEBUGFILENAME") == null)
|
---|
| 198 | {
|
---|
| 199 | Element xslParent = (Element) current.getParentNode();
|
---|
[25381] | 200 |
|
---|
[25656] | 201 | while (xslParent.getNamespaceURI() != "http://www.w3.org/1999/XSL/Transform" && !xslParent.getNodeName().startsWith("xsl:"))
|
---|
| 202 | {
|
---|
| 203 | xslParent = (Element) xslParent.getParentNode();
|
---|
| 204 | }
|
---|
[25381] | 205 |
|
---|
[25656] | 206 | System.err.println("ADDING FILE NAME " + fileName);
|
---|
| 207 | current.setUserData("GSDEBUGFILENAME", fileName, null);
|
---|
| 208 | current.setUserData("GSDEBUGXML", xslParent.cloneNode(true), null);
|
---|
| 209 | }
|
---|
| 210 | else
|
---|
| 211 | {
|
---|
| 212 | System.err.println("ALREADY SET!");
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[25914] | 217 | public static void inlineImportAndIncludeFiles(Document doc, String pathExtra)
|
---|
[25656] | 218 | {
|
---|
[25914] | 219 | inlineImportAndIncludeFilesDebug(doc, pathExtra, false, null);
|
---|
[25656] | 220 | }
|
---|
| 221 |
|
---|
[25914] | 222 | public static void inlineImportAndIncludeFilesDebug(Document doc, String pathExtra, boolean debug, String docFileName)
|
---|
[25656] | 223 | {
|
---|
| 224 | XMLConverter converter = new XMLConverter();
|
---|
| 225 |
|
---|
[25914] | 226 | String path = (pathExtra == null) ? "" : pathExtra;
|
---|
[25656] | 227 |
|
---|
| 228 | NodeList importList = doc.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "import");
|
---|
| 229 | NodeList includeList = doc.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "include");
|
---|
| 230 |
|
---|
| 231 | for (int i = 0; i < importList.getLength() + includeList.getLength(); i++)
|
---|
| 232 | {
|
---|
| 233 | Element current = (Element) ((i < importList.getLength()) ? importList.item(i) : includeList.item(i - importList.getLength()));
|
---|
| 234 | String href = current.getAttribute("href");
|
---|
[25914] | 235 | String filePath = GSFile.interfaceHome(GlobalProperties.getGSDL3Home(), "default") + File.separator + "transform" + File.separator + path.replace("/", File.separator) + href.replace("/", File.separator);
|
---|
[25656] | 236 |
|
---|
| 237 | try
|
---|
| 238 | {
|
---|
[25914] | 239 | Document inlineDoc = converter.getDOM(new File(filePath), "UTF-8");
|
---|
[25656] | 240 |
|
---|
| 241 | String newPath = path;
|
---|
| 242 | int lastSepIndex = href.lastIndexOf("/");
|
---|
| 243 | if (lastSepIndex != -1)
|
---|
| 244 | {
|
---|
| 245 | newPath += href.substring(0, lastSepIndex + 1);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | //Do this recursively
|
---|
[25914] | 249 | inlineImportAndIncludeFilesDebug(inlineDoc, newPath, debug, filePath);
|
---|
| 250 |
|
---|
[25656] | 251 | GSXSLT.mergeStylesheetsDebug(doc, inlineDoc.getDocumentElement(), false, debug, docFileName, filePath);
|
---|
| 252 | }
|
---|
| 253 | catch (Exception ex)
|
---|
| 254 | {
|
---|
| 255 | ex.printStackTrace();
|
---|
| 256 | return;
|
---|
| 257 | }
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | while (importList.getLength() > 0)
|
---|
| 261 | {
|
---|
| 262 | Element importElem = (Element) importList.item(0);
|
---|
| 263 | importElem.getParentNode().removeChild(importElem);
|
---|
| 264 | }
|
---|
| 265 | while (includeList.getLength() > 0)
|
---|
| 266 | {
|
---|
| 267 | Element includeElem = (Element) includeList.item(0);
|
---|
| 268 | includeElem.getParentNode().removeChild(includeElem);
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | public static void modifyConfigFormatForDebug(Document doc, String fileName)
|
---|
| 273 | {
|
---|
| 274 | NodeList templateNodes = doc.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "template");
|
---|
| 275 | if (templateNodes.getLength() == 0)
|
---|
| 276 | {
|
---|
| 277 | templateNodes = doc.getElementsByTagName("xsl:template");
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | String debugElementString = "";
|
---|
| 281 | debugElementString += "<span class=\"configDebugSpan\" style=\"display:none;\">";
|
---|
| 282 | debugElementString += " \"filename\":\"" + fileName + "\",";
|
---|
| 283 | debugElementString += " \"xml\":\"<xsl:value-of select=\"util:xmlNodeToString(.)\"/>\""; //<xsl:copy><xsl:copy-of select=\"@*\"/></xsl:copy>
|
---|
| 284 | debugElementString += "</span>";
|
---|
| 285 |
|
---|
| 286 | XMLConverter converter = new XMLConverter();
|
---|
| 287 | Element debugElement = (Element) converter.getDOM("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xslt:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:xslt=\"output.xsl\" xmlns:gsf=\"http://www.greenstone.org/greenstone3/schema/ConfigFormat\">" + debugElementString + "</xslt:stylesheet>").getDocumentElement().getFirstChild();
|
---|
| 288 |
|
---|
| 289 | for (int i = 0; i < templateNodes.getLength(); i++)
|
---|
| 290 | {
|
---|
| 291 | Element currentTemplate = (Element) templateNodes.item(i);
|
---|
| 292 | if (currentTemplate.getAttribute("match") != null && (currentTemplate.getAttribute("match").equals("gsf:metadata") || currentTemplate.getAttribute("match").equals("*") || currentTemplate.getAttribute("match").equals("format")))
|
---|
| 293 | {
|
---|
| 294 | continue;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | if (currentTemplate.hasChildNodes())
|
---|
| 298 | {
|
---|
| 299 | currentTemplate.insertBefore(doc.importNode(debugElement.cloneNode(true), true), currentTemplate.getFirstChild());
|
---|
| 300 | }
|
---|
| 301 | else
|
---|
| 302 | {
|
---|
| 303 | currentTemplate.appendChild(doc.importNode(debugElement.cloneNode(true), true));
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[24864] | 308 | /**
|
---|
| 309 | * takes any import or include nodes, and creates absolute path names for
|
---|
| 310 | * the files
|
---|
| 311 | */
|
---|
[25635] | 312 | public static void absoluteIncludePaths(Document stylesheet, String gsdl3_home, String site_name, String collection, String interface_name, ArrayList<String> base_interfaces)
|
---|
[24864] | 313 | {
|
---|
| 314 | Element base_node = stylesheet.getDocumentElement();
|
---|
| 315 | if (base_node == null)
|
---|
| 316 | {
|
---|
| 317 | return;
|
---|
| 318 | }
|
---|
| 319 | Node child = base_node.getFirstChild();
|
---|
| 320 | while (child != null)
|
---|
| 321 | {
|
---|
| 322 | String name = child.getNodeName();
|
---|
| 323 | if (name.equals("xsl:import") || name.equals("xsl:include"))
|
---|
| 324 | {
|
---|
| 325 | ((Element) child).setAttribute("href", GSFile.stylesheetFile(gsdl3_home, site_name, collection, interface_name, base_interfaces, ((Element) child).getAttribute("href")));
|
---|
| 326 | }
|
---|
| 327 | child = child.getNextSibling();
|
---|
| 328 | }
|
---|
[20139] | 329 | }
|
---|
[4079] | 330 |
|
---|
[24864] | 331 | /**
|
---|
| 332 | * looks through a stylesheet for <xxx:template match='template_name'>
|
---|
| 333 | * inside this template it looks for any <xxx:value-of
|
---|
| 334 | * select='metadataList/metadata[@name=yyy]> elements, and extracts the
|
---|
| 335 | * metadata names into a Vector
|
---|
| 336 | */
|
---|
[25635] | 337 | public static Vector<String> extractWantedMetadata(Document stylesheet, String template_name)
|
---|
[24864] | 338 | {
|
---|
| 339 |
|
---|
[25635] | 340 | Vector<String> metadata = new Vector<String>();
|
---|
[24864] | 341 | Element base_node = stylesheet.getDocumentElement();
|
---|
| 342 | NodeList templates = base_node.getElementsByTagNameNS("*", "template");
|
---|
| 343 | for (int i = 0; i < templates.getLength(); i++)
|
---|
| 344 | {
|
---|
| 345 | Element template = (Element) templates.item(i);
|
---|
| 346 | String match_name = template.getAttribute("match");
|
---|
| 347 | if (!match_name.equals(template_name))
|
---|
| 348 | {
|
---|
| 349 | continue; // we're only looking for specific templates
|
---|
| 350 | }
|
---|
| 351 | String mode = template.getAttribute("mode");
|
---|
| 352 | if (!mode.equals(""))
|
---|
| 353 | {
|
---|
| 354 | continue; // we only want ones without modes - these are processing ones, not display ones
|
---|
| 355 | }
|
---|
| 356 | // we have one that we want to look through
|
---|
| 357 | NodeList values = template.getElementsByTagNameNS("*", "value-of");
|
---|
| 358 | for (int v = 0; v < values.getLength(); v++)
|
---|
| 359 | {
|
---|
| 360 | String select = ((Element) values.item(v)).getAttribute("select");
|
---|
| 361 | if (select.startsWith("metadataList/metadata[@name="))
|
---|
| 362 | {
|
---|
| 363 | String[] bits = select.split("'|\"");
|
---|
| 364 | // there should be two quotes in teh string, therefore 3 items, and the second one is teh one we want
|
---|
| 365 | String name = bits[1];
|
---|
| 366 | metadata.add(name);
|
---|
| 367 | }
|
---|
| 368 | }
|
---|
[4079] | 369 | }
|
---|
[24864] | 370 | return metadata;
|
---|
[4079] | 371 | }
|
---|
| 372 |
|
---|
[3450] | 373 | }
|
---|