[3235] | 1 | /*
|
---|
| 2 | * XMLTransformer.java
|
---|
| 3 | * Copyright (C) 2002 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 | */
|
---|
[3222] | 19 | package org.greenstone.gsdl3.util;
|
---|
| 20 |
|
---|
| 21 | // XML classes
|
---|
| 22 | import javax.xml.transform.Transformer;
|
---|
| 23 | import javax.xml.transform.TransformerFactory;
|
---|
| 24 | import javax.xml.transform.TransformerConfigurationException;
|
---|
| 25 | import javax.xml.transform.TransformerException;
|
---|
| 26 |
|
---|
| 27 | import javax.xml.transform.stream.StreamSource;
|
---|
| 28 | import javax.xml.transform.dom.DOMSource;
|
---|
| 29 | import javax.xml.transform.stream.StreamResult;
|
---|
[3572] | 30 | import javax.xml.transform.dom.DOMResult;
|
---|
[3222] | 31 |
|
---|
[3451] | 32 | import org.w3c.dom.Document;
|
---|
[3341] | 33 | import org.w3c.dom.Node;
|
---|
[3451] | 34 | import org.w3c.dom.NodeList;
|
---|
[3341] | 35 |
|
---|
[3222] | 36 | // other java classes
|
---|
| 37 | import java.io.StringReader;
|
---|
| 38 | import java.io.StringWriter;
|
---|
[3451] | 39 | import java.io.File;
|
---|
[4701] | 40 | import java.util.HashMap;
|
---|
| 41 | import java.util.Set;
|
---|
| 42 | import java.util.Map;
|
---|
| 43 | import java.util.Iterator;
|
---|
[3451] | 44 | /** XMLTransformer - utility class for greenstone
|
---|
[3222] | 45 | *
|
---|
[3451] | 46 | * transforms xml using xslt
|
---|
[3222] | 47 | *
|
---|
[3235] | 48 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
| 49 | * @version $Revision: 5142 $
|
---|
[3222] | 50 | */
|
---|
| 51 | public class XMLTransformer {
|
---|
[3451] | 52 |
|
---|
| 53 | /** The transformer factory we're using */
|
---|
[5142] | 54 | TransformerFactory t_factory=null;
|
---|
[3235] | 55 |
|
---|
| 56 | /**
|
---|
| 57 | * The no-arguments constructor.
|
---|
| 58 | *
|
---|
| 59 | * Any exceptions thrown are caught internally
|
---|
| 60 | *
|
---|
| 61 | * @return a new transformer
|
---|
| 62 | * @see javax.xml.transform.TransformerFactory
|
---|
| 63 | */
|
---|
[3222] | 64 | public XMLTransformer() {
|
---|
[3362] | 65 |
|
---|
[3451] | 66 | // make sure we are using the xalan transformer
|
---|
[3362] | 67 | System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.processor.TransformerFactoryImpl");
|
---|
[3222] | 68 | try {
|
---|
[5142] | 69 | this.t_factory = org.apache.xalan.processor.TransformerFactoryImpl.newInstance();
|
---|
[3222] | 70 |
|
---|
| 71 | } catch (Exception e) {
|
---|
[3941] | 72 | System.err.println("XMLTransformer() exception "+e.getMessage());
|
---|
[3222] | 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 |
|
---|
[3235] | 78 | /**
|
---|
| 79 | * Transform an XML document using a XSLT stylesheet
|
---|
| 80 | *
|
---|
[3451] | 81 | * @param stylesheet a filename for an XSLT stylesheet
|
---|
[3235] | 82 | * @param xml_in the XML to be transformed
|
---|
| 83 | * @return the transformed XML
|
---|
| 84 | */
|
---|
[3222] | 85 | public String transform(String stylesheet, String xml_in) {
|
---|
| 86 |
|
---|
| 87 | try {
|
---|
| 88 | // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
|
---|
[5142] | 89 | Transformer transformer = this.t_factory.newTransformer(new StreamSource(stylesheet));
|
---|
[3222] | 90 |
|
---|
| 91 | // Use the Transformer to transform an XML Source and send the output to a Result object.
|
---|
| 92 | StringWriter output = new StringWriter();
|
---|
| 93 |
|
---|
| 94 | transformer.transform(new StreamSource(new StringReader(xml_in)), new StreamResult(output));
|
---|
| 95 | return output.toString();
|
---|
| 96 | } catch (TransformerConfigurationException e) {
|
---|
[4701] | 97 | System.err.println("XMLTransformer: couldn't create transformer object: "+e.getMessageAndLocation());
|
---|
| 98 | System.err.println(e.getLocationAsString());
|
---|
[3222] | 99 | return "";
|
---|
| 100 | } catch (TransformerException e) {
|
---|
[3308] | 101 | System.err.println("XMLTransformer: couldn't transform the source: " + e.getMessage());
|
---|
[3222] | 102 | return "";
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
[3341] | 105 |
|
---|
[4701] | 106 | public String transformToString(Document stylesheet, Document source) {
|
---|
| 107 | return transformToString(stylesheet, source, null);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | public String transformToString(Document stylesheet, Document source, HashMap parameters) {
|
---|
| 111 |
|
---|
| 112 | try {
|
---|
[3341] | 113 | // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
|
---|
[5142] | 114 | Transformer transformer = this.t_factory.newTransformer(new DOMSource(stylesheet));
|
---|
[4701] | 115 | if (parameters != null) {
|
---|
| 116 | Set params = parameters.entrySet();
|
---|
| 117 | Iterator i = params.iterator();
|
---|
| 118 | while (i.hasNext()) {
|
---|
| 119 | Map.Entry m = (Map.Entry)i.next();
|
---|
| 120 | transformer.setParameter((String)m.getKey(), m.getValue());
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | //transformer.setParameter("page_lang", source.getDocumentElement().getAttribute(GSXML.LANG_ATT));
|
---|
| 124 |
|
---|
[3572] | 125 |
|
---|
[3341] | 126 | // Use the Transformer to transform an XML Source and send the output to a Result object.
|
---|
| 127 | StringWriter output = new StringWriter();
|
---|
[3572] | 128 |
|
---|
[4701] | 129 | transformer.transform(new DOMSource(source), new StreamResult(output));
|
---|
[3341] | 130 | return output.toString();
|
---|
| 131 | } catch (TransformerConfigurationException e) {
|
---|
[4701] | 132 | System.err.println("XMLTransformer: couldn't create transformer object: "+e.getMessageAndLocation());
|
---|
| 133 | System.err.println(e.getLocationAsString());
|
---|
[3451] | 134 | return "";
|
---|
| 135 | } catch (TransformerException e) {
|
---|
| 136 | System.err.println("XMLTransformer: couldn't transform the source: " + e.getMessage());
|
---|
| 137 | return "";
|
---|
| 138 | }
|
---|
[3572] | 139 | }
|
---|
[3451] | 140 |
|
---|
[4701] | 141 | public Node transform(Document stylesheet, Document source) {
|
---|
| 142 | return transform(stylesheet, source, null);
|
---|
| 143 | }
|
---|
[3451] | 144 |
|
---|
[4701] | 145 | public Node transform(Document stylesheet, Document source, HashMap parameters) {
|
---|
| 146 |
|
---|
[3451] | 147 | try {
|
---|
| 148 | // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
|
---|
[5142] | 149 | Transformer transformer = this.t_factory.newTransformer(new DOMSource(stylesheet));
|
---|
[4701] | 150 | if (parameters != null) {
|
---|
| 151 | Set params = parameters.entrySet();
|
---|
| 152 | Iterator i = params.iterator();
|
---|
| 153 | while (i.hasNext()) {
|
---|
| 154 | Map.Entry m = (Map.Entry)i.next();
|
---|
| 155 | transformer.setParameter((String)m.getKey(), m.getValue());
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
[3572] | 158 |
|
---|
| 159 | DOMResult result = new DOMResult();
|
---|
[4701] | 160 | transformer.transform(new DOMSource(source), result);
|
---|
[3572] | 161 | return result.getNode().getFirstChild();
|
---|
[3451] | 162 | } catch (TransformerConfigurationException e) {
|
---|
[4701] | 163 | System.err.println("XMLTransformer: couldn't create transformer object: "+e.getMessageAndLocation());
|
---|
| 164 | System.err.println(e.getLocationAsString());
|
---|
[3572] | 165 | return null;
|
---|
[3341] | 166 | } catch (TransformerException e) {
|
---|
| 167 | System.err.println("XMLTransformer: couldn't transform the source: " + e.getMessage());
|
---|
[3572] | 168 | return null;
|
---|
[3341] | 169 | }
|
---|
[4701] | 170 |
|
---|
| 171 |
|
---|
| 172 | }
|
---|
[5111] | 173 |
|
---|
| 174 | public Node transform(File stylesheet, File source) {
|
---|
| 175 |
|
---|
| 176 | try {
|
---|
[5142] | 177 | Transformer transformer = this.t_factory.newTransformer(new StreamSource(stylesheet));
|
---|
[5111] | 178 |
|
---|
| 179 | DOMResult result = new DOMResult();
|
---|
| 180 | transformer.transform(new StreamSource(source), result);
|
---|
| 181 | return result.getNode().getFirstChild();
|
---|
| 182 | } catch (TransformerConfigurationException e) {
|
---|
| 183 | System.err.println("XMLTransformer: couldn't create transformer object: "+e.getMessageAndLocation());
|
---|
| 184 | System.err.println(e.getLocationAsString());
|
---|
| 185 | return null;
|
---|
| 186 | } catch (TransformerException e) {
|
---|
| 187 | System.err.println("XMLTransformer: couldn't transform the source: " + e.getMessage());
|
---|
| 188 | return null;
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
[3222] | 191 | }
|
---|
| 192 |
|
---|
| 193 |
|
---|