source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/TransformingLibrary.java@ 31771

Last change on this file since 31771 was 26494, checked in by ak19, 11 years ago
  1. Adding new Library class TransformingLibrary.java that takes a GS3 o=xml input file and an XSLT file (like o=skinandlibdoc) and applies the XSLT to the XML to produce the final GS3 html page. 2. Needed a custom transform() method in XMLTransformer to get this to work as the XML and XSLT files can be located anywhere, but the XSLT import statements that the input XSLT file makes should be resolved to absolute paths despite specifying only relative paths. 3. Updated Library2.java to ensure GSDL3SRCHOME is set before it is run, in order to set GSDL3HOME correctly.
File size: 3.8 KB
Line 
1/*
2 * TransformingLibrary.java
3 * Copyright (C) 2012 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;
20
21import org.greenstone.gsdl3.util.GSXML;
22import org.greenstone.gsdl3.util.XMLTransformer;
23import org.greenstone.util.GlobalProperties;
24
25import org.w3c.dom.Document;
26import org.w3c.dom.Element;
27import org.w3c.dom.Node;
28
29
30import java.io.File;
31
32
33/**
34 * A program to take an GS3 xml file and an XSLT file from the command line and return html
35 * @author ak19
36 */
37final public class TransformingLibrary
38{
39 // GS3> java -classpath "web/WEB-INF/lib/*":"lib/jni/*":"web/WEB-INF/classes/" org.greenstone.gsdl3.TransformingLibrary localsite default C1.xml CL1.xslt 2>&1 | less
40
41 /**
42 * On Linux, run as:
43 * GS3> java -classpath "web/WEB-INF/lib/*":"lib/jni/*":"web/WEB-INF/classes/" org.greenstone.gsdl3.TransformingLibrary localsite default inputXML inputXSLT
44 * where inputXML is generated by o=xml and inputXSLT is generated by o=skinandlibdoc
45 *
46 * Relative paths in xsl:imports made by the input xslt file were solved as described in
47 * @see http://stackoverflow.com/questions/3699860/resolving-relative-paths-when-loading-xslt-files
48 *
49 * For how to include all jars in a folder into the classpath to run a java program, see
50 * @see http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath
51 * @see http://stackoverflow.com/questions/6780678/run-class-in-jar-file
52 */
53 public static void main(String args[])
54 {
55 String usageMsg = "Usage: TransformingLibrary <site name> <interface name> <xml file> <xslt file>";
56
57 if (System.getenv("GSDL3SRCHOME") == null) {
58 System.out.println("Before calling this script, run: source gs3-setup.sh\n" + usageMsg);
59 System.exit(1);
60 }
61 // force GlobalProperties to default GSDL3HOME to GSDL3SRCHOME/web if not already set
62 // need GSDL3HOME set for the XMLTransformer.transformer(XSLTfile, XMLfile, interfaceName, DocType)
63 GlobalProperties.loadGlobalProperties("");
64
65
66 if (args.length < 3 || args.length > 4) {
67 System.out.println(usageMsg);
68 System.exit(1);
69 }
70
71 File xmlFile = new File(args[2]);
72 if (!xmlFile.exists()) {
73 System.out.println("File " + args[2] + " does not exist");
74 System.out.println(usageMsg);
75 System.exit(1);
76 }
77
78 File xsltFile = null;
79 if(args.length == 4) {
80 xsltFile = new File(args[3]);
81 if (!xsltFile.exists()) {
82 System.out.println("XSLT file " + args[3] + " does not exist");
83 System.out.println(usageMsg);
84 System.exit(1);
85 }
86 }
87
88
89 String siteName = args[0];
90 String interfaceName = args[1];
91
92 XMLTransformer transformer = new XMLTransformer();
93 // Can now call the custom transform() method to do the transformation while
94 // also dealing with relative paths in <xsl:import>s in the input XSLT file
95 Node resultNode = transformer.transform(xsltFile, xmlFile, interfaceName, null);
96 Element resultElement = resultNode.getOwnerDocument().getDocumentElement();
97 System.out.println("*********************");
98 System.out.println(GSXML.elementToString(resultElement, true));
99
100 }
101}
Note: See TracBrowser for help on using the repository browser.