/*
This library is part of octools
Copyright (c) 2000-2007 Valtech A/S (http://www.valtech.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
Alkacon OpenCms and the OpenCms logo are registered trademarks of
Alkacon Software GmbH in Germany, the USA and other countries
For further information about Alkacon Software GmbH, please see the
company website: http://www.alkacon.com
For further information about OpenCms, please see the
project website: http://www.opencms.org
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package dk.valtech.octools.jsp;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.flex.CmsFlexController;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.main.CmsException;
import org.opencms.staticexport.CmsLinkManager;
/**
* Tag that reads the contents of a file to either the given var-name or prints it to the content
* of the page.
*
* @author Stefan Uldum Grinsted (stefan.grinsted@valtech.dk)
*
*/
public class CmsJspReadFileTag extends ValtechTagSupport {
private static final long serialVersionUID = 1L;
private String uri;
private String var;
private String encoding = "UTF-8";
private int scope = PageContext.PAGE_SCOPE;
@Override
public int doStartTag() throws JspException {
return SKIP_BODY;
}
@Override
public int doEndTag() throws JspException {
try {
CmsJspActionElement cms = getCmsActionElement();
CmsFlexController controller = CmsFlexController.getController(cms.getRequest());
CmsObject cmso = cms.getCmsObject();
uri = CmsLinkManager.getAbsoluteUri(uri, controller.getCurrentRequest().getElementUri());
CmsFile file = cmso.readFile(uri);
String contents = new String(file.getContents(), encoding);
if (var != null) {
pageContext.setAttribute(var, contents, scope);
} else {
pageContext.getOut().write(contents);
}
} catch (UnsupportedEncodingException e) {
throw new JspException("Unknow encoding, " + encoding, e);
} catch (CmsException e) {
throw new JspException("Unable to read file '" + uri + "'", e);
} catch (IOException e) {
throw new JspException("Unable to write to the outputstream", e);
}
this.uri = null;
this.var = null;
this.encoding = "UTF-8";
this.scope = PageContext.PAGE_SCOPE;
return EVAL_PAGE;
}
@Override
public void release() {
encoding = "UTF-8";
var = null;
uri = null;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public void setScope(String scope) throws JspException {
this.scope = getScope(scope);
}
}