[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: 5663 $
|
---|
[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 | * @see javax.xml.transform.TransformerFactory
|
---|
| 62 | */
|
---|
[3222] | 63 | public XMLTransformer() {
|
---|
[3362] | 64 |
|
---|
[3451] | 65 | // make sure we are using the xalan transformer
|
---|
[3362] | 66 | System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.processor.TransformerFactoryImpl");
|
---|
[3222] | 67 | try {
|
---|
[5142] | 68 | this.t_factory = org.apache.xalan.processor.TransformerFactoryImpl.newInstance();
|
---|
[3222] | 69 |
|
---|
| 70 | } catch (Exception e) {
|
---|
[3941] | 71 | System.err.println("XMLTransformer() exception "+e.getMessage());
|
---|
[3222] | 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 |
|
---|
[3235] | 77 | /**
|
---|
| 78 | * Transform an XML document using a XSLT stylesheet
|
---|
| 79 | *
|
---|
[3451] | 80 | * @param stylesheet a filename for an XSLT stylesheet
|
---|
[3235] | 81 | * @param xml_in the XML to be transformed
|
---|
| 82 | * @return the transformed XML
|
---|
| 83 | */
|
---|
[3222] | 84 | public String transform(String stylesheet, String xml_in) {
|
---|
| 85 |
|
---|
| 86 | try {
|
---|
| 87 | // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
|
---|
[5142] | 88 | Transformer transformer = this.t_factory.newTransformer(new StreamSource(stylesheet));
|
---|
[3222] | 89 |
|
---|
| 90 | // Use the Transformer to transform an XML Source and send the output to a Result object.
|
---|
| 91 | StringWriter output = new StringWriter();
|
---|
| 92 |
|
---|
| 93 | transformer.transform(new StreamSource(new StringReader(xml_in)), new StreamResult(output));
|
---|
| 94 | return output.toString();
|
---|
| 95 | } catch (TransformerConfigurationException e) {
|
---|
[4701] | 96 | System.err.println("XMLTransformer: couldn't create transformer object: "+e.getMessageAndLocation());
|
---|
| 97 | System.err.println(e.getLocationAsString());
|
---|
[3222] | 98 | return "";
|
---|
| 99 | } catch (TransformerException e) {
|
---|
[3308] | 100 | System.err.println("XMLTransformer: couldn't transform the source: " + e.getMessage());
|
---|
[3222] | 101 | return "";
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
[3341] | 104 |
|
---|
[4701] | 105 | public String transformToString(Document stylesheet, Document source) {
|
---|
| 106 | return transformToString(stylesheet, source, null);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | public String transformToString(Document stylesheet, Document source, HashMap parameters) {
|
---|
| 110 |
|
---|
| 111 | try {
|
---|
[3341] | 112 | // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
|
---|
[5142] | 113 | Transformer transformer = this.t_factory.newTransformer(new DOMSource(stylesheet));
|
---|
[4701] | 114 | if (parameters != null) {
|
---|
| 115 | Set params = parameters.entrySet();
|
---|
| 116 | Iterator i = params.iterator();
|
---|
| 117 | while (i.hasNext()) {
|
---|
| 118 | Map.Entry m = (Map.Entry)i.next();
|
---|
| 119 | transformer.setParameter((String)m.getKey(), m.getValue());
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | //transformer.setParameter("page_lang", source.getDocumentElement().getAttribute(GSXML.LANG_ATT));
|
---|
| 123 |
|
---|
[3572] | 124 |
|
---|
[3341] | 125 | // Use the Transformer to transform an XML Source and send the output to a Result object.
|
---|
| 126 | StringWriter output = new StringWriter();
|
---|
[3572] | 127 |
|
---|
[4701] | 128 | transformer.transform(new DOMSource(source), new StreamResult(output));
|
---|
[3341] | 129 | return output.toString();
|
---|
| 130 | } catch (TransformerConfigurationException e) {
|
---|
[4701] | 131 | System.err.println("XMLTransformer: couldn't create transformer object: "+e.getMessageAndLocation());
|
---|
| 132 | System.err.println(e.getLocationAsString());
|
---|
[3451] | 133 | return "";
|
---|
| 134 | } catch (TransformerException e) {
|
---|
| 135 | System.err.println("XMLTransformer: couldn't transform the source: " + e.getMessage());
|
---|
| 136 | return "";
|
---|
| 137 | }
|
---|
[3572] | 138 | }
|
---|
[3451] | 139 |
|
---|
[4701] | 140 | public Node transform(Document stylesheet, Document source) {
|
---|
| 141 | return transform(stylesheet, source, null);
|
---|
| 142 | }
|
---|
[3451] | 143 |
|
---|
[4701] | 144 | public Node transform(Document stylesheet, Document source, HashMap parameters) {
|
---|
| 145 |
|
---|
[3451] | 146 | try {
|
---|
| 147 | // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
|
---|
[5142] | 148 | Transformer transformer = this.t_factory.newTransformer(new DOMSource(stylesheet));
|
---|
[4701] | 149 | if (parameters != null) {
|
---|
| 150 | Set params = parameters.entrySet();
|
---|
| 151 | Iterator i = params.iterator();
|
---|
| 152 | while (i.hasNext()) {
|
---|
| 153 | Map.Entry m = (Map.Entry)i.next();
|
---|
| 154 | transformer.setParameter((String)m.getKey(), m.getValue());
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
[3572] | 157 |
|
---|
| 158 | DOMResult result = new DOMResult();
|
---|
[4701] | 159 | transformer.transform(new DOMSource(source), result);
|
---|
[3572] | 160 | return result.getNode().getFirstChild();
|
---|
[3451] | 161 | } catch (TransformerConfigurationException e) {
|
---|
[4701] | 162 | System.err.println("XMLTransformer: couldn't create transformer object: "+e.getMessageAndLocation());
|
---|
| 163 | System.err.println(e.getLocationAsString());
|
---|
[3572] | 164 | return null;
|
---|
[3341] | 165 | } catch (TransformerException e) {
|
---|
| 166 | System.err.println("XMLTransformer: couldn't transform the source: " + e.getMessage());
|
---|
[3572] | 167 | return null;
|
---|
[3341] | 168 | }
|
---|
[4701] | 169 |
|
---|
| 170 |
|
---|
| 171 | }
|
---|
[5111] | 172 |
|
---|
| 173 | public Node transform(File stylesheet, File source) {
|
---|
| 174 |
|
---|
| 175 | try {
|
---|
[5142] | 176 | Transformer transformer = this.t_factory.newTransformer(new StreamSource(stylesheet));
|
---|
[5111] | 177 |
|
---|
| 178 | DOMResult result = new DOMResult();
|
---|
| 179 | transformer.transform(new StreamSource(source), result);
|
---|
| 180 | return result.getNode().getFirstChild();
|
---|
| 181 | } catch (TransformerConfigurationException e) {
|
---|
| 182 | System.err.println("XMLTransformer: couldn't create transformer object: "+e.getMessageAndLocation());
|
---|
| 183 | System.err.println(e.getLocationAsString());
|
---|
| 184 | return null;
|
---|
| 185 | } catch (TransformerException e) {
|
---|
| 186 | System.err.println("XMLTransformer: couldn't transform the source: " + e.getMessage());
|
---|
| 187 | return null;
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
[3222] | 190 | }
|
---|
| 191 |
|
---|
| 192 |
|
---|