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