Project

General

Profile

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

    
11
import java.io.File;
12
import java.io.FileOutputStream;
13
import java.io.IOException;
14
import java.io.InputStream;
15
import java.io.OutputStream;
16
import java.io.StringReader;
17
import java.io.StringWriter;
18
import java.net.URL;
19

    
20
import javax.xml.parsers.DocumentBuilder;
21
import javax.xml.parsers.DocumentBuilderFactory;
22
import javax.xml.transform.Source;
23
import javax.xml.transform.Transformer;
24
import javax.xml.transform.TransformerException;
25
import javax.xml.transform.TransformerFactory;
26
import javax.xml.transform.dom.DOMSource;
27
import javax.xml.transform.stream.StreamResult;
28
import javax.xml.transform.stream.StreamSource;
29

    
30
import org.apache.log4j.Logger;
31
import org.jdom.Document;
32
import org.jdom.JDOMException;
33
import org.jdom.input.SAXBuilder;
34
import org.jdom.transform.JDOMSource;
35

    
36
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
37
import eu.etaxonomy.cdm.print.PublishConfigurator;
38
import eu.etaxonomy.cdm.print.out.PublishOutputModuleBase;
39

    
40

    
41
/**
42
 * @author l.morris, s.buers
43
 \* @since Sep 11, 2013
44
 * 
45
 */
46
public class MediawikiOutputModule extends PublishOutputModuleBase {
47

    
48
	private static final Logger logger = Logger
49
			.getLogger(MediawikiOutputModule.class);
50

    
51
	public static String STYLESHEET_RESOURCE_DEFAULT = "src/main/resources/stylesheets/mediawiki/multipages.xsl";
52
	
53
	
54
	// default wiki - exportparameter
55
	public String prefix="";
56
	public String sourceUrl="";
57
	public String username="CDM Mediawiki Exporter"; 
58
	
59
	public MediawikiOutputModule(String mediaWikiPagePrefix, String sourceUrl){
60
		super();
61
		this.sourceUrl = sourceUrl;
62
		this.prefix=mediaWikiPagePrefix+":";
63
	}
64
	
65
	
66
	public MediawikiOutputModule(String sourceUrl) {
67
		super();
68
		this.sourceUrl = sourceUrl;
69
	}
70

    
71

    
72
	public void setPrefix(String prefix) {
73
		this.prefix = prefix;
74
	}
75

    
76

    
77
	public void setUsername(String username) {
78
		this.username = username;
79
	}
80

    
81

    
82
	public void output(Document document, File exportFolder,
83
			IProgressMonitor progressMonitor) {
84

    
85
		
86
		super.output(document, exportFolder, progressMonitor);
87
		
88
		
89
		
90
		try {
91

    
92
			// URL xslURL = new
93
			// URL("file:///C:/Users/l.morris/workspace/cdmlib/cdmlib-print/src/main/resources/stylesheets/mediawiki/multipages.xsl");
94
			URL xslURL = (new java.io.File(STYLESHEET_RESOURCE_DEFAULT))
95
					.toURI().toURL();
96

    
97
			String xslID = xslURL.toString();
98

    
99
			// Setup output
100
			String filePath = getFilePath(exportFolder);
101
			OutputStream out = new FileOutputStream(filePath);
102
			out = new java.io.BufferedOutputStream(out);
103

    
104
			// validate namespace
105
			
106
//			System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
107
//		             "net.sf.saxon.om.DocumentBuilderFactoryImpl");
108
			DocumentBuilderFactory factory = DocumentBuilderFactory
109
					.newInstance();
110
			factory.setNamespaceAware(true);
111
			//			factory.setValidating(true);
112
			DocumentBuilder builder = factory.newDocumentBuilder();
113

    
114
			org.w3c.dom.Document namespDocument = builder.parse(new File(
115
					STYLESHEET_RESOURCE_DEFAULT));
116

    
117

    
118
			// get StreamSource out of namespDocument
119

    
120
			DOMSource domSource = new DOMSource(namespDocument);
121
			StringWriter xmlAsWriter = new StringWriter();
122
			StreamResult domResult = new StreamResult(xmlAsWriter);
123
			TransformerFactory.newInstance().newTransformer()
124
			.transform(domSource, domResult);
125
			StringReader xmlReader = new StringReader(xmlAsWriter.toString());
126
			StreamSource inputSource = new StreamSource(xmlReader);
127

    
128
			try {
129

    
130
				// Setup XSLT
131
				TransformerFactory tfactory = new net.sf.saxon.TransformerFactoryImpl();
132
				Transformer transformer = tfactory.newTransformer(inputSource);// (xslt));
133

    
134
				// new
135
				// DOMSource source = new DOMSource(builder.parse(document));
136

    
137
//				 JDOMBuilder jdomBuilder= new JDOMBuilder();
138
//				 Document doc = domBuilder.build(document);
139

    
140
				JDOMSource source = new JDOMSource(document);
141
				Source xmlSource = new DOMSource();
142

    
143
				// Setup input for XSLT transformation
144
				// JDOMSource source = new JDOMSource(namespDocument);
145

    
146
				// Resulting SAX events (the generated FO) must be piped through
147
				// to FOP
148
				// Result result = new SAXResult(fop.getDefaultHandler());
149
				// Result result = new SAXResult();
150
				StreamResult result = new StreamResult(out);
151

    
152
				// Run XSLT transformation 
153
				transformer.setParameter("prefix", prefix);
154
				transformer.setParameter("username", username);
155
				transformer.setParameter("cdm-url", sourceUrl);
156
				transformer.transform(source, result);
157

    
158
				// get the wikitext of the generated file
159
				// use fr_jussieu_snv_lis.wiki.bot.WikiBot to upload the file
160
				// and images
161

    
162
			} finally {
163
				out.close();
164
//				logger.info("Mediawiki XML file created: " + filePath);
165
			}
166

    
167
		} catch (TransformerException te) {
168

    
169
			logger.error(te.getLocalizedMessage());
170
			logger.error(te.getCause());
171
			logger.error("TransformerException. Could not generate document",
172
					te);
173

    
174
		} catch (Exception e) {
175
			logger.error("Could not generate document", e);
176
		}
177

    
178
	}
179

    
180
	/**
181
	 * @return
182
	 */
183
	private static Document useExternalXMLSource() {
184
		SAXBuilder testbuilder = new SAXBuilder();
185
		  File xmlFile = new File("/home/sybille/development/mediawiki/kick_off/ericaceae_source.xml");
186
		  Document document1=null;
187
		try {
188
			document1 = (Document) testbuilder.build(xmlFile);
189
		} catch (JDOMException e1) {
190
			// TODO Auto-generated catch block
191
			e1.printStackTrace();
192
		} catch (IOException e1) {
193
			// TODO Auto-generated catch block
194
			e1.printStackTrace();
195
		}
196
		return document1;
197
	}
198

    
199

    
200
	/*
201
	 * (non-Javadoc)
202
	 * 
203
	 * @see
204
	 * eu.etaxonomy.cdm.print.out.IPublishOutputModule#getOutputFileSuffix()
205
	 */
206
	@Override
207
	public String getOutputFileSuffix() {
208
		return "xml";
209
	}
210

    
211
	public InputStream getDefaultXsltInputStream() {
212
		return MediawikiOutputModule.class
213
				.getResourceAsStream(STYLESHEET_RESOURCE_DEFAULT);
214
	}
215

    
216
	public static void main(String[] args) {
217

    
218
		MediawikiOutputModule outputModule = new MediawikiOutputModule("");
219
		PublishConfigurator configurator = PublishConfigurator
220
				.NewRemoteInstance();
221
		configurator.setExportFolder(new File("src/main/resources/tmp"));
222
		// try out my own xml:
223
				
224
		Document document = useExternalXMLSource();
225

    
226
		outputModule.output(document, configurator.getExportFolder(),
227
				configurator.getProgressMonitor());
228

    
229
	}
230

    
231
	// like this or change modifier in abstract superclass?
232
	public String generateFilenameWithDate(String name) {
233
		return super.generateFilenameWithDate(name);
234
		
235
	}
236

    
237
}
(2-2/5)