blob: d930087fc817f72b49109ef80df6e43b016ef61f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
/*******************************************************************************
* Copyright (c) 2010 David Green and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* David Green - initial API and implementation
*******************************************************************************/
package org.eclipse.mylyn.wikitext.mediawiki;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.common.io.Resources;
/**
* compute the contents of a template based on
*
* @author dgreen
* @since 3.0
*/
public class WikiTemplateResolver extends TemplateResolver {
private String wikiBaseUrl;
public String getWikiBaseUrl() {
return wikiBaseUrl;
}
public void setWikiBaseUrl(String wikiBaseUrl) {
this.wikiBaseUrl = wikiBaseUrl;
}
@Override
public Template resolveTemplate(String templateName) {
if (wikiBaseUrl != null) {
int indexOf = templateName.indexOf(':');
if (indexOf == 0) {
templateName = templateName.substring(1);
} else if (indexOf == -1) {
templateName = "Template:" + templateName; //$NON-NLS-1$
}
URL url = computeRawUrl(templateName);
if (url != null) {
try {
String content = readContent(url);
Template template = new Template();
String basicName = templateName.toLowerCase().startsWith("template:") //$NON-NLS-1$
? templateName.substring(templateName.lastIndexOf(':') + 1)
: templateName;
template.setName(basicName);
template.setTemplateMarkup(content);
return template;
} catch (final IOException e) {
final String message = MessageFormat.format("Cannot read from {0}: {1}", url, e.getMessage()); //$NON-NLS-1$
Logger.getLogger(WikiTemplateResolver.class.getName()).log(Level.WARNING, message, e);
}
}
}
return null;
}
protected String readContent(URL url) throws IOException {
return Resources.toString(url, StandardCharsets.UTF_8);
}
private URL computeRawUrl(String path) {
try {
String qualifiedUrl = wikiBaseUrl;
if (!qualifiedUrl.endsWith("/")) { //$NON-NLS-1$
qualifiedUrl += "/"; //$NON-NLS-1$
}
qualifiedUrl += "index.php?title=" + URLEncoder.encode(path, "UTF-8") + "&action=raw"; //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
return new URL(qualifiedUrl);
} catch (IOException e) {
Logger.getLogger(WikiTemplateResolver.class.getName()).log(Level.WARNING,
MessageFormat.format("Cannot compute raw URL for {0}: {1}", path, e.getMessage()), e); //$NON-NLS-1$
return null;
}
}
}
|