Project

General

Profile

Download (6.82 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.logging.log4j.LogManager;
31
import org.apache.logging.log4j.Logger;
32
import org.jdom.Document;
33
import org.jdom.JDOMException;
34
import org.jdom.input.SAXBuilder;
35
import org.jdom.transform.JDOMSource;
36

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

    
41

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

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

    
50
	public static String STYLESHEET_RESOURCE_DEFAULT = "src/main/resources/stylesheets/mediawiki/multipages.xsl";
51

    
52
	// default wiki - exportparameter
53
	public String prefix="";
54
	public String sourceUrl="";
55
	public String username="CDM Mediawiki Exporter";
56

    
57
	public MediawikiOutputModule(String mediaWikiPagePrefix, String sourceUrl){
58
		super();
59
		this.sourceUrl = sourceUrl;
60
		this.prefix=mediaWikiPagePrefix+":";
61
	}
62

    
63

    
64
	public MediawikiOutputModule(String sourceUrl) {
65
		super();
66
		this.sourceUrl = sourceUrl;
67
	}
68

    
69

    
70
	public void setPrefix(String prefix) {
71
		this.prefix = prefix;
72
	}
73

    
74

    
75
	public void setUsername(String username) {
76
		this.username = username;
77
	}
78

    
79

    
80
	@Override
81
    public void output(Document document, File exportFolder,
82
			IProgressMonitor progressMonitor) {
83

    
84

    
85
		super.output(document, exportFolder, progressMonitor);
86

    
87

    
88

    
89
		try {
90

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

    
96
			String xslID = xslURL.toString();
97

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

    
103
			// validate namespace
104

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

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

    
116

    
117
			// get StreamSource out of namespDocument
118

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

    
127
			try {
128

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

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

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

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

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

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

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

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

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

    
166
		} catch (TransformerException te) {
167

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

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

    
177
	}
178

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

    
198

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

    
210
	@Override
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
	@Override
233
    public String generateFilenameWithDate(String name) {
234
		return super.generateFilenameWithDate(name);
235

    
236
	}
237

    
238
}
(2-2/5)