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