Kawai Wiki

Compare Revisions

Ignore whitespace Rev 198 → Rev 199

/trunk/src/main/java/org/kawai/tag/kcode/ShowSvnSourceCode.java
New file
0,0 → 1,67
package org.kawai.tag.kcode;
 
import java.util.Map;
 
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
 
import org.mentawai.util.IOUtils;
 
/**
* KCode to load source code from a SVN url.
*
* @author Sergio Oliveira
*
*/
public class ShowSvnSourceCode implements KCode {
 
/**
* {@inheritDoc}
*/
@Override
public boolean hasBody() {
return false;
}
 
/**
* {@inheritDoc}
*/
@Override
public String getTag() {
return "svn";
}
 
/**
* {@inheritDoc}
*/
@Override
public String process(PageContext pageContext, Map<String, String> attrs, String html) throws JspException {
 
String url = attrs.get("url");
if (url == null) throw new JspException("Mandatory attribute missing: url");
String removeLicense = attrs.get("removeLicense");
boolean shouldRemoveLicense = true;
if (removeLicense != null && removeLicense.equals("false")) {
shouldRemoveLicense = false;
}
try {
String code = IOUtils.readURL(url);
code = Code.escapeAngleBrackets(code).trim();
// remove license
if (shouldRemoveLicense && code.startsWith("/*")) {
int end = code.indexOf("*/");
code = code.substring(end + 2);
code = "// License goes here...\n" + code;
}
return code;
} catch(Throwable e) {
e.printStackTrace();
return "Failed to load source code from svn: " + url;
}
 
}
 
}