source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXSLT.java@ 25389

Last change on this file since 25389 was 25389, checked in by ak19, 12 years ago

Merging XSLT stylesheets should also process xsl:import statements in the same way it does xsl:include statements.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
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 */
19package org.greenstone.gsdl3.util;
20
21import org.w3c.dom.Node;
22import org.w3c.dom.Element;
23import org.w3c.dom.NodeList;
24import org.w3c.dom.Document;
25
26import java.util.Vector;
27import java.util.ArrayList;
28
29/** various functions for manipulating Greenstone xslt */
30public class GSXSLT
31{
32 /**
33 * takes a stylesheet Document, and adds in any child nodes from extra_xsl
34 * named templates overwrite any existing one, while match templates are
35 * just added to the end of teh stylesheet
36 */
37 public static void mergeStylesheets(Document main_xsl, Element extra_xsl)
38 {
39 Element main = main_xsl.getDocumentElement();
40
41 NodeList children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "include");
42 for (int i = 0; i < children.getLength(); i++) {
43 Node node = children.item(i);
44 // remove any previous occurrences of xsl:include with the same href value
45 removeDuplicateElementsFrom(main, node, "xsl:include", "href");
46 main.appendChild(main_xsl.importNode(node, true));
47 }
48
49 children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "import");
50 for (int i = 0; i < children.getLength(); i++) {
51 Node node = children.item(i);
52 // remove any previous occurrences of xsl:output with the same method value
53 removeDuplicateElementsFrom(main, node, "xsl:import", "href");
54 main.appendChild(main_xsl.importNode(node, true));
55 }
56
57 children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "output");
58 for (int i = 0; i < children.getLength(); i++) {
59 Node node = children.item(i);
60 // remove any previous occurrences of xsl:output with the same method value
61 removeDuplicateElementsFrom(main, node, "xsl:output", "method");
62 main.appendChild(main_xsl.importNode(node, true));
63 }
64
65 children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "template");
66 for (int i = 0; i < children.getLength(); i++)
67 {
68 Node node = children.item(i);
69 // remove any previous occurrences of xsl:template with the same value for name
70 // or even the same value for match (should we use priorities for match?)
71 removeDuplicateElementsFrom(main, node, "xsl:template", "name");
72 removeDuplicateElementsFrom(main, node, "xsl:template", "match");
73 main.appendChild(main_xsl.importNode(node, true));
74 }
75 }
76
77 // In element main, tries to find if any previous occurrence of elements with template=templateName,
78 // and whose named attribute (attributeName) has the same value as the same attribute in node.
79 // If this is the case, such a previous occurrence is removed it from element main
80 public static void removeDuplicateElementsFrom(Element main, Node node, String templateName, String attrName) {
81 String attr = ((Element) node).getAttribute(attrName);
82 if (!attr.equals(""))
83 {
84 Element old_template = GSXML.getNamedElement(main, templateName, attrName, attr);
85 if (old_template != null)
86 {
87 main.removeChild(old_template);
88 }
89 }
90 }
91
92
93
94 /**
95 * takes any import or include nodes, and creates absolute path names for
96 * the files
97 */
98 public static void absoluteIncludePaths(Document stylesheet, String gsdl3_home, String site_name, String collection, String interface_name, ArrayList base_interfaces)
99 {
100
101 Element base_node = stylesheet.getDocumentElement();
102 if (base_node == null)
103 {
104 return;
105 }
106 Node child = base_node.getFirstChild();
107 while (child != null)
108 {
109 String name = child.getNodeName();
110 if (name.equals("xsl:import") || name.equals("xsl:include"))
111 {
112 ((Element) child).setAttribute("href", GSFile.stylesheetFile(gsdl3_home, site_name, collection, interface_name, base_interfaces, ((Element) child).getAttribute("href")));
113 }
114 child = child.getNextSibling();
115 }
116
117 }
118
119 /**
120 * looks through a stylesheet for <xxx:template match='template_name'>
121 * inside this template it looks for any <xxx:value-of
122 * select='metadataList/metadata[@name=yyy]> elements, and extracts the
123 * metadata names into a Vector
124 */
125 public static Vector extractWantedMetadata(Document stylesheet, String template_name)
126 {
127
128 Vector metadata = new Vector();
129 Element base_node = stylesheet.getDocumentElement();
130 NodeList templates = base_node.getElementsByTagNameNS("*", "template");
131 for (int i = 0; i < templates.getLength(); i++)
132 {
133 Element template = (Element) templates.item(i);
134 String match_name = template.getAttribute("match");
135 if (!match_name.equals(template_name))
136 {
137 continue; // we're only looking for specific templates
138 }
139 String mode = template.getAttribute("mode");
140 if (!mode.equals(""))
141 {
142 continue; // we only want ones without modes - these are processing ones, not display ones
143 }
144 // we have one that we want to look through
145 NodeList values = template.getElementsByTagNameNS("*", "value-of");
146 for (int v = 0; v < values.getLength(); v++)
147 {
148 String select = ((Element) values.item(v)).getAttribute("select");
149 if (select.startsWith("metadataList/metadata[@name="))
150 {
151 String[] bits = select.split("'|\"");
152 // there should be two quotes in teh string, therefore 3 items, and the second one is teh one we want
153 String name = bits[1];
154 metadata.add(name);
155 }
156 }
157 }
158 return metadata;
159 }
160
161}
Note: See TracBrowser for help on using the repository browser.