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 | import org.w3c.dom.Text;
|
---|
26 |
|
---|
27 | import java.util.HashMap;
|
---|
28 | import java.util.Vector;
|
---|
29 | import java.util.ArrayList;
|
---|
30 |
|
---|
31 | /** various functions for manipulating Greenstone xslt */
|
---|
32 | public class GSXSLT
|
---|
33 | {
|
---|
34 | /**
|
---|
35 | * takes a stylesheet Document, and adds in any child nodes from extra_xsl
|
---|
36 | * named templates overwrite any existing one, while match templates are
|
---|
37 | * just added to the end of teh stylesheet
|
---|
38 | */
|
---|
39 | public static void mergeStylesheets(Document main_xsl, Element extra_xsl)
|
---|
40 | {
|
---|
41 | Element main = main_xsl.getDocumentElement();
|
---|
42 | NodeList children = extra_xsl.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "template");
|
---|
43 | for (int i = 0; i < children.getLength(); i++)
|
---|
44 | {
|
---|
45 | Node node = children.item(i);
|
---|
46 | String name = ((Element) node).getAttribute("name");
|
---|
47 | if (!name.equals(""))
|
---|
48 | {
|
---|
49 | Element old_template = GSXML.getNamedElement(main, "xsl:template", "name", name);
|
---|
50 | if (old_template != null)
|
---|
51 | {
|
---|
52 | main.removeChild(old_template);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | main.appendChild(main_xsl.importNode(node, true));
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * takes any import or include nodes, and creates absolute path names for
|
---|
62 | * the files
|
---|
63 | */
|
---|
64 | public static void absoluteIncludePaths(Document stylesheet, String gsdl3_home, String site_name, String collection, String interface_name, ArrayList base_interfaces)
|
---|
65 | {
|
---|
66 |
|
---|
67 | Element base_node = stylesheet.getDocumentElement();
|
---|
68 | if (base_node == null)
|
---|
69 | {
|
---|
70 | return;
|
---|
71 | }
|
---|
72 | Node child = base_node.getFirstChild();
|
---|
73 | while (child != null)
|
---|
74 | {
|
---|
75 | String name = child.getNodeName();
|
---|
76 | if (name.equals("xsl:import") || name.equals("xsl:include"))
|
---|
77 | {
|
---|
78 | ((Element) child).setAttribute("href", GSFile.stylesheetFile(gsdl3_home, site_name, collection, interface_name, base_interfaces, ((Element) child).getAttribute("href")));
|
---|
79 | }
|
---|
80 | child = child.getNextSibling();
|
---|
81 | }
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * looks through a stylesheet for <xxx:template match='template_name'>
|
---|
87 | * inside this template it looks for any <xxx:value-of
|
---|
88 | * select='metadataList/metadata[@name=yyy]> elements, and extracts the
|
---|
89 | * metadata names into a Vector
|
---|
90 | */
|
---|
91 | public static Vector extractWantedMetadata(Document stylesheet, String template_name)
|
---|
92 | {
|
---|
93 |
|
---|
94 | Vector metadata = new Vector();
|
---|
95 | Element base_node = stylesheet.getDocumentElement();
|
---|
96 | NodeList templates = base_node.getElementsByTagNameNS("*", "template");
|
---|
97 | for (int i = 0; i < templates.getLength(); i++)
|
---|
98 | {
|
---|
99 | Element template = (Element) templates.item(i);
|
---|
100 | String match_name = template.getAttribute("match");
|
---|
101 | if (!match_name.equals(template_name))
|
---|
102 | {
|
---|
103 | continue; // we're only looking for specific templates
|
---|
104 | }
|
---|
105 | String mode = template.getAttribute("mode");
|
---|
106 | if (!mode.equals(""))
|
---|
107 | {
|
---|
108 | continue; // we only want ones without modes - these are processing ones, not display ones
|
---|
109 | }
|
---|
110 | // we have one that we want to look through
|
---|
111 | NodeList values = template.getElementsByTagNameNS("*", "value-of");
|
---|
112 | for (int v = 0; v < values.getLength(); v++)
|
---|
113 | {
|
---|
114 | String select = ((Element) values.item(v)).getAttribute("select");
|
---|
115 | if (select.startsWith("metadataList/metadata[@name="))
|
---|
116 | {
|
---|
117 | String[] bits = select.split("'|\"");
|
---|
118 | // there should be two quotes in teh string, therefore 3 items, and the second one is teh one we want
|
---|
119 | String name = bits[1];
|
---|
120 | metadata.add(name);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 | return metadata;
|
---|
125 | }
|
---|
126 |
|
---|
127 | }
|
---|