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