Project

General

Profile

Download (2.76 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.cdm.print;
12

    
13
import java.io.IOException;
14
import java.io.InputStream;
15
import java.net.URL;
16

    
17
import javax.xml.transform.Templates;
18
import javax.xml.transform.Transformer;
19
import javax.xml.transform.TransformerException;
20
import javax.xml.transform.TransformerFactory;
21
import javax.xml.transform.stream.StreamSource;
22

    
23
import org.apache.log4j.Logger;
24
import org.jdom.Document;
25
import org.jdom.transform.JDOMResult;
26
import org.jdom.transform.JDOMSource;
27
import org.jdom.transform.XSLTransformException;
28

    
29
/**
30
 * Perform XSL transformations.
31
 * 
32
 * Note: This class provides access to the JAXP XSL transformer currently active. 
33
 * 
34
 * @author n.hoffmann
35
 * @created Apr 20, 2010
36
 * @version 1.0
37
 */
38
public class Transformator {
39

    
40
	private static final Logger logger = Logger
41
				.getLogger(Transformator.class);
42
	private Transformer transformer;
43
	
44

    
45
	public Transformator(InputStream stylesheet) throws XSLTransformException{
46
		if(stylesheet == null){
47
			throw new IllegalArgumentException("Stylsheet may not be null");
48
		}
49
		
50
		try {
51
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
52
            Templates templates = transformerFactory.newTemplates(new StreamSource(stylesheet));
53
            transformer = templates.newTransformer();
54
        }
55
        catch (TransformerException e) {
56
            throw new XSLTransformException("Could not construct XSLTransformer", e);
57
			
58
        }
59
	}
60
	
61
	
62
	public Transformator(URL stylesheet) throws XSLTransformException, IOException {
63
		this(stylesheet.openStream());
64
	}
65
	
66
    /**
67
     * Transforms the given document to an output document.
68
     *
69
     * @param  inputDoc            input document
70
     * @param  resolver			   entity resolver for the input document
71
     * @return                     transformed output document
72
     * @throws XSLTransformException       if there's a problem in the transformation
73
     */
74
    public Document transform(org.jdom.Document inputDocument) throws XSLTransformException {
75
    	JDOMSource source = new JDOMSource(inputDocument);
76
    	JDOMResult result = new JDOMResult();
77
        try {
78
        	logger.trace("Transforming input document: " + inputDocument);
79
        	
80
            transformer.transform(source, result);
81
            Document resultDocument = result.getDocument();
82
 
83
        	resultDocument.getContent();
84
        	return resultDocument;         
85
        }
86
    	catch (TransformerException e) {
87
    		logger.error(e);
88
            throw new XSLTransformException("Could not perform transformation", e);
89
        }
90
    }
91
}
(7-7/10)