Project

General

Profile

« Previous | Next » 

Revision 4671

Added by Jing Tao about 16 years ago

Add a new class which will transform eml201 and eml 200 to eml210.

View differences:

src/edu/ucsb/nceas/metacat/util/EMLVersionsTransformer.java
1
/**
2
 *  '$RCSfile: XSLTransform.java,v $'
3
 *  Copyright: 2003 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: jones $'
7
 *     '$Date: 2003/08/18 20:27:03 $'
8
 * '$Revision: 1.4 $'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 */
24

  
25
package edu.ucsb.nceas.metacat.util;
26

  
27
import java.io.*;
28
import java.util.Enumeration;
29
import java.util.Hashtable;
30

  
31
import org.apache.log4j.Logger;
32

  
33
import javax.xml.transform.TransformerFactory;
34
import javax.xml.transform.Transformer;
35
import javax.xml.transform.stream.StreamSource;
36
import javax.xml.transform.stream.StreamResult;
37
import javax.xml.transform.TransformerException;
38
import javax.xml.transform.TransformerConfigurationException;
39
import javax.xml.transform.URIResolver;
40

  
41

  
42

  
43
/**
44
 * A Class that transforms older eml version to newer eml version utitlizing XSL style sheets. 
45
 */
46
public class EMLVersionsTransformer {
47
	private static Logger logMetacat = Logger.getLogger(GeoserverUtil.class);
48

  
49
    /**
50
     * Public constructor because all methods are static and do not need 
51
     * an instance.
52
     */
53
    private EMLVersionsTransformer() 
54
    {
55
    }
56

  
57
    /*
58
     * Transform an XML document using an XSLT stylesheet to another format,
59
     * probably HTML or another XML document format.
60
     *
61
     * @param docString the document to be transformed
62
     * @param xslSystemId the system location of the stylesheet
63
     * @param pw the PrintWriter to which output is printed
64
     * @param params some parameters for inclusion to the transformation
65
     */
66
    private static void transform(String docString, String xslSystemId,
67
        Writer pw, Hashtable param) throws Exception
68
    {
69
        transform(new StringReader(docString), xslSystemId, pw, param);
70
    }
71

  
72
    /*
73
     * Transform an XML document using an XSLT stylesheet to another format,
74
     * probably HTML or another XML document format.
75
     *
76
     * @param doc the document to be transformed
77
     * @param xslSystemId the system location of the stylesheet
78
     * @param pw the PrintWriter to which output is printed
79
     * @param params some parameters for inclusion to the transformation
80
     */
81
    private static void transform(Reader doc, String xslSystemId,
82
        Writer pw, Hashtable param) throws Exception
83
    {
84
        
85
            StreamSource xslSource = 
86
                new StreamSource(xslSystemId);
87
            xslSource.setSystemId(xslSystemId);
88
            // Create a stylesheet from the system id that was found
89
            TransformerFactory tFactory = TransformerFactory.newInstance();
90
            Transformer transformer = tFactory.newTransformer(xslSource);
91

  
92
            // Set up parameters for transformation
93
            if ( param != null) {
94
                Enumeration en = param.keys();
95
                while (en.hasMoreElements()) {
96
                    String key =(String)en.nextElement();
97
                    String value = ((String)(param.get(key)));
98
                    transformer.setParameter(key, value);
99
                }
100
            }
101

  
102
            // Run the transform engine
103
            StreamSource ss = new StreamSource(doc);
104
            StreamResult sr = new StreamResult(pw);
105
            transformer.transform(ss, sr);
106
        
107
    }
108

  
109
    /*
110
     * Transform single eml201 doc to eml 210 doc
111
     */
112
    private static void transformEML201ToEML210(Reader reader, String xslfile, Writer writer, String packageid) throws Exception{    
113
        	Hashtable param = null;
114
            if (packageid != null)
115
            {
116
            	param = new Hashtable();
117
            	param.put("package-id", packageid);
118
            }
119
             EMLVersionsTransformer.transform(reader, xslfile, writer, param);
120
         
121
        }
122
  
123
}
124

  

Also available in: Unified diff