Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 950 → Rev 961

/hostadmiral/trunk/src/ak/hostadmiral/core/taglib/LocaleOptionsTag.java
0,0 → 1,49
// based on jakarta struts taglib
package ak.hostadmiral.core.taglib;
 
import java.util.Locale;
import javax.servlet.jsp.JspException;
 
import org.apache.struts.taglib.html.Constants;
import org.apache.struts.taglib.html.SelectTag;
import org.apache.struts.taglib.html.OptionsTag;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.ResponseUtils;
 
import ak.hostadmiral.core.Locales;
import ak.hostadmiral.core.model.User;
 
public class LocaleOptionsTag extends OptionsTag
{
protected static MessageResources coreMessages =
MessageResources.getMessageResources("ak.hostadmiral.core.CoreResources");
 
public int doEndTag() throws JspException
{
SelectTag selectTag = (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY);
if (selectTag == null) {
throw new JspException(messages.getMessage("optionsTag.select"));
}
 
User user = (User)pageContext.getSession().getAttribute("user");
if(user == null) throw new JspException("no user found");
Locale userLocale = user.getLocale();
 
StringBuffer sb = new StringBuffer();
Locale[] locales = Locales.getLocales();
for(int i = 0; i < locales.length; i++) {
Locale locale = locales[i];
String stringValue = locale.toString();
String label = locale.getDisplayLanguage(userLocale);
String country = locale.getDisplayCountry(userLocale);
 
if(country != null && !country.equals(""))
label += " / " + country; // FIXME: move the slash to JSP?
 
addOption(sb, stringValue, label, selectTag.isMatched(stringValue));
}
 
ResponseUtils.write(pageContext, sb.toString());
return EVAL_PAGE;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/taglib/CountryTag.java
0,0 → 1,29
package ak.hostadmiral.core.taglib;
 
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
 
import ak.hostadmiral.core.model.User;
 
public class CountryTag
extends TagSupport
{
public int doStartTag()
throws JspException
{
User user = (User)pageContext.getSession().getAttribute("user");
 
if(user == null)
throw new JspException("no user found");
 
try {
pageContext.getOut().print(user.getLocale().getDisplayCountry(user.getLocale()));
}
catch(IOException ex) {
throw new JspException("Cannot write out: " + ex.getMessage());
}
 
return SKIP_BODY;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/taglib/LanguageTag.java
0,0 → 1,29
package ak.hostadmiral.core.taglib;
 
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
 
import ak.hostadmiral.core.model.User;
 
public class LanguageTag
extends TagSupport
{
public int doStartTag()
throws JspException
{
User user = (User)pageContext.getSession().getAttribute("user");
 
if(user == null)
throw new JspException("no user found");
 
try {
pageContext.getOut().print(user.getLocale().getDisplayLanguage(user.getLocale()));
}
catch(IOException ex) {
throw new JspException("Cannot write out: " + ex.getMessage());
}
 
return SKIP_BODY;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/taglib/ViewableTag.java
0,0 → 1,13
package ak.hostadmiral.core.taglib;
 
import javax.servlet.jsp.JspException;
 
public class ViewableTag
extends RightTagBase
{
protected boolean condition()
throws JspException
{
return object.viewableBy(user);
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/taglib/NotViewableTag.java
0,0 → 1,13
package ak.hostadmiral.core.taglib;
 
import javax.servlet.jsp.JspException;
 
public class NotViewableTag
extends RightTagBase
{
protected boolean condition()
throws JspException
{
return !object.viewableBy(user);
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/taglib/RightsTag.java
0,0 → 1,15
package ak.hostadmiral.core.taglib;
 
import javax.servlet.jsp.JspException;
 
import org.apache.struts.util.RequestUtils;
 
public class RightsTag
extends MethodTagBase
{
protected boolean condition(boolean value)
throws JspException
{
return value;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/taglib/ModelObjectOptionsTag.java
0,0 → 1,71
// based on jakarta struts taglib
package ak.hostadmiral.core.taglib;
 
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
 
import javax.servlet.jsp.JspException;
 
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.taglib.html.Constants;
import org.apache.struts.taglib.html.SelectTag;
import org.apache.struts.taglib.html.OptionsTag;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.ResponseUtils;
 
import ak.hostadmiral.core.model.ModelObject;
 
public class ModelObjectOptionsTag extends OptionsTag
{
protected static MessageResources coreMessages =
MessageResources.getMessageResources("ak.hostadmiral.core.CoreResources");
 
public int doEndTag() throws JspException
{
SelectTag selectTag = (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY);
if (selectTag == null) {
throw new JspException(messages.getMessage("optionsTag.select"));
}
StringBuffer sb = new StringBuffer();
 
if (collection != null) {
Iterator collIterator = getIterator(collection, null);
while (collIterator.hasNext()) {
Object bean = collIterator.next();
Object value = null;
 
if(!(bean instanceof ModelObject))
throw new JspException("Not a ModelObject");
 
ModelObject model = (ModelObject)bean;
 
try {
value = PropertyUtils.getProperty(bean, property);
if (value == null) {
value = "";
}
} catch (IllegalAccessException e) {
throw new JspException(
messages.getMessage("getter.access", property, collection));
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
throw new JspException(
messages.getMessage("getter.result", property, t.toString()));
} catch (NoSuchMethodException e) {
throw new JspException(
messages.getMessage("getter.method", property, collection));
}
 
String identKey = model.getIdentKey();
Object[] identParams = model.getIdentParams();
String label = coreMessages.getMessage(identKey, identParams);
String stringValue = value.toString();
 
addOption(sb, stringValue, label, selectTag.isMatched(stringValue));
}
}
 
ResponseUtils.write(pageContext, sb.toString());
return EVAL_PAGE;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/taglib/DeleteableTag.java
0,0 → 1,13
package ak.hostadmiral.core.taglib;
 
import javax.servlet.jsp.JspException;
 
public class DeleteableTag
extends RightTagBase
{
protected boolean condition()
throws JspException
{
return object.deleteableBy(user);
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/taglib/NotDeleteableTag.java
0,0 → 1,13
package ak.hostadmiral.core.taglib;
 
import javax.servlet.jsp.JspException;
 
public class NotDeleteableTag
extends RightTagBase
{
protected boolean condition()
throws JspException
{
return !object.deleteableBy(user);
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/taglib/RightTagBase.java
0,0 → 1,64
package ak.hostadmiral.core.taglib;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
 
import org.apache.struts.util.RequestUtils;
 
import ak.hostadmiral.core.model.User;
import ak.hostadmiral.core.model.ModelObject;
 
public abstract class RightTagBase
extends TagSupport
{
protected User user;
protected ModelObject object;
 
protected String name;
 
public String getName()
{
return name;
}
 
public void setName(String name)
{
this.name = name;
}
 
public void release()
{
super.release();
name = null;
user = null;
}
 
public int doStartTag()
throws JspException
{
user = (User)RequestUtils.lookup(pageContext, "user", "session");
 
Object obj = RequestUtils.lookup(pageContext, name, null);
if(obj == null)
throw new JspException(name + " is null");
if(!(obj instanceof ModelObject))
throw new JspException(name + " must be a ModelObject, but is " + obj.getClass());
 
object = (ModelObject)obj;
 
if(condition())
return EVAL_BODY_INCLUDE;
else
return SKIP_BODY;
}
 
public int doEndTag()
throws JspException
{
return EVAL_PAGE;
}
 
protected abstract boolean condition()
throws JspException;
}
/hostadmiral/trunk/src/ak/hostadmiral/core/taglib/EditableTag.java
0,0 → 1,13
package ak.hostadmiral.core.taglib;
 
import javax.servlet.jsp.JspException;
 
public class EditableTag
extends RightTagBase
{
protected boolean condition()
throws JspException
{
return object.editableBy(user);
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/taglib/NotEditableTag.java
0,0 → 1,13
package ak.hostadmiral.core.taglib;
 
import javax.servlet.jsp.JspException;
 
public class NotEditableTag
extends RightTagBase
{
protected boolean condition()
throws JspException
{
return !object.editableBy(user);
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/taglib/WriteTag.java
0,0 → 1,50
// based on struts write tag
package ak.hostadmiral.core.taglib;
 
import javax.servlet.jsp.JspException;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;
 
public class WriteTag
extends org.apache.struts.taglib.bean.WriteTag
{
protected String defValue;
 
public String getDefault()
{
return defValue;
}
 
public void setDefault(String defValue)
{
this.defValue = defValue;
}
 
public int doStartTag()
throws JspException
{
Object value;
 
if(ignore && RequestUtils.lookup(pageContext, name, scope) == null)
value = null;
else
value = RequestUtils.lookup(pageContext, name, property, scope);
 
if(value == null) value = defValue;
 
if(value != null) {
if(filter)
ResponseUtils.write(pageContext, ResponseUtils.filter(formatValue(value)));
else
ResponseUtils.write(pageContext, formatValue(value));
}
 
return SKIP_BODY;
}
 
public void release()
{
super.release();
defValue = null;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/taglib/MethodTagBase.java
0,0 → 1,63
package ak.hostadmiral.core.taglib;
 
import java.lang.reflect.Method;
import javax.servlet.jsp.JspException;
 
import ak.hostadmiral.core.model.User;
import org.apache.struts.util.RequestUtils;
 
public abstract class MethodTagBase
extends RightTagBase
{
protected String method;
 
public String getMethod()
{
return method;
}
 
public void setMethod(String method)
{
this.method = method;
}
 
public void release()
{
super.release();
method = null;
}
 
protected boolean condition()
throws JspException
{
Method m;
Object value;
 
// find method
try {
m = object.getClass().getMethod(method, new Class[] { User.class } );
}
catch(NoSuchMethodException ex) {
throw new JspException("Method " + method
+ " with parameter of type user not found");
}
 
// invoke it
try {
value = m.invoke(object, new Object[] { user } );
}
catch(Exception ex) {
throw new JspException("Cannot call " + method + ": " + ex.getMessage());
}
 
// check value type
if(!(value instanceof Boolean))
throw new JspException("Return type of method " + method
+ " must be java.lang.Boolean");
 
return condition(((Boolean)value).booleanValue());
}
 
protected abstract boolean condition(boolean value)
throws JspException;
}
/hostadmiral/trunk/src/ak/hostadmiral/core/taglib/NoRightsTag.java
0,0 → 1,15
package ak.hostadmiral.core.taglib;
 
import javax.servlet.jsp.JspException;
 
import org.apache.struts.util.RequestUtils;
 
public class NoRightsTag
extends MethodTagBase
{
protected boolean condition(boolean value)
throws JspException
{
return !value;
}
}