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