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

Last change on this file since 24395 was 20139, checked in by oranfry, 15 years ago

protect against null pointer exception

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