1 10 package org.mmbase.bridge.jsp.taglib; 11 import org.mmbase.bridge.jsp.taglib.util.*; 12 import javax.servlet.jsp.JspTagException ; 13 import javax.servlet.http.*; 14 15 import org.mmbase.util.transformers.CharTransformer; 16 import org.mmbase.util.logging.Logger; 17 import org.mmbase.util.logging.Logging; 18 19 import java.util.*; 20 21 29 30 public class ImportTag extends ContextReferrerTag { 31 private static final Logger log = Logging.getLoggerInstance(ImportTag.class); 32 33 protected Attribute required = Attribute.NULL; 34 protected Attribute from = Attribute.NULL; 35 36 protected Attribute externid = Attribute.NULL; 37 private Attribute reset = Attribute.NULL; 38 39 private boolean found = false; 40 private String useId = null; 41 42 45 46 public void setExternid(String e) throws JspTagException { 47 externid = getAttribute(e); 48 } 49 50 55 public void setRequired(String b) throws JspTagException { 56 required = getAttribute(b); 57 } 58 59 60 65 public void setReset(String b) throws JspTagException { 66 reset = getAttribute(b); 67 } 68 69 72 73 public void setFrom(String s) throws JspTagException { 74 from = getAttribute(s); 75 } 76 77 78 public int doStartTag() throws JspTagException { 79 Object value = null; 80 helper.overrideWrite(false); 81 log.trace("dostarttag of import"); 82 83 if (getId() == null) { 84 log.trace("No id was given, using externid "); 85 useId = (String ) externid.getValue(this); 86 } else { 87 useId = getId(); 88 if (log.isDebugEnabled()) log.trace("An id was given (" + id + ")"); 89 } 90 91 92 if (externid != Attribute.NULL) { 93 94 boolean res = reset.getBoolean(this, false); 95 if (log.isDebugEnabled()) { 96 log.trace("Externid was given " + externid.getString(this)); 97 } 98 if (from.getString(this).equals("")) { 99 found = (getContextProvider().getContextContainer().findAndRegister(pageContext, externid.getString(this), useId, ! res) != null); 100 } else { 101 List fromsList = from.getList(this); 102 boolean searchThis = fromsList.contains("this") || fromsList.contains("THIS"); 103 104 if (searchThis) { 105 res = true; 106 } 107 Iterator froms = fromsList.iterator(); 108 ContextContainer cc = getContextProvider().getContextContainer(); 109 while (froms.hasNext() && ! found) { 110 int from = ContextContainer.stringToLocation((String ) froms.next()); 111 Object result = cc.find(pageContext, from, externid.getString(this)); 112 if (from == ContextContainer.LOCATION_THIS && result == null) { 113 result = cc.find(pageContext, from, useId); 114 } 115 if (result != null) { 116 if (! (from == ContextContainer.LOCATION_PARAMETERS || from == ContextContainer.LOCATION_MULTIPART)) { 117 helper.overrideNoImplicitList(); 118 } 119 120 cc.register(useId, result, ! res); 121 found = true; 122 break; 123 } 124 } 125 } 126 127 if (! found && required.getBoolean(this, false)) { 128 String fromString = from.getString(this).toLowerCase(); 129 if (fromString.equals("session") && ((HttpServletRequest) pageContext.getRequest()).getSession(false) == null) { 130 throw new JspTagException ("Required parameter '" + externid.getString(this) + "' not found in session, because there is no session"); 131 } 132 throw new JspTagException ("Required parameter '" + externid.getString(this) + "' not found " + (fromString.equals("") ? "anywhere" : ("in " + fromString))); 133 } 134 if (found) { 135 value = getObject(useId); 136 if (log.isDebugEnabled()) { 137 log.debug("found value for " + useId + " '" + value + "'"); 138 } 139 } 140 } 141 if (found) { 142 setValue(value, WriterHelper.NOIMPLICITLIST); 143 if (useId != null) { 144 ContextContainer cc = getContextProvider().getContextContainer(); 145 cc.reregister(useId, helper.getValue()); 146 } 147 return SKIP_BODY; 148 } else { 149 setValue(null); 150 return EVAL_BODY_BUFFERED; 151 } 152 153 } 154 155 159 protected void setValue(Object value, boolean noImplicitList) throws JspTagException { 160 value = getEscapedValue(value); 161 if (log.isDebugEnabled()) { 162 log.debug("Setting " + value + " " + (value == null ? "NULL" : "" + value.getClass())); 163 } 164 helper.setValue(value, noImplicitList); 165 } 166 169 protected void setValue(Object value) throws JspTagException { 170 setValue(value, WriterHelper.IMPLICITLIST); 171 } 172 173 174 public int doEndTag() throws JspTagException { 175 if (log.isDebugEnabled()) { 176 log.debug("endtag of import with id:" + id + " externid: " + externid.getString(this)); 177 } 178 if (externid != Attribute.NULL) { 179 if (! found ) { 180 if (log.isDebugEnabled()) log.debug("External Id " + externid.getString(this) + " not found"); 181 Object body = bodyContent != null ? bodyContent.getString() : ""; 183 if (! "".equals(body)) { if (log.isDebugEnabled()) { 185 log.debug("Found a default in the body (" + body + ")"); 186 } 187 setValue(body); 188 getContextProvider().getContextContainer().reregister(useId, helper.getValue()); 189 } else { 190 setValue(null); 192 getContextProvider().getContextContainer().reregister(useId, helper.getValue()); 193 } 194 } 195 } else { setValue(bodyContent != null ? bodyContent.getString() : ""); 197 if (useId != null) { 198 if (log.isDebugEnabled()) { 199 log.debug("Setting " + useId + " to " + helper.getValue()); 200 } 201 boolean res = reset.getBoolean(this, false); 202 204 getContextProvider().getContextContainer().register(useId, helper, !res); 205 206 } else { 207 if (helper.getJspvar() == null) { 208 found = false; useId = null; 210 throw new JspTagException ("Attributes externid, id and jspvar cannot be all missing"); 211 } 212 } 213 } 214 found = false; useId = null; 216 bodyContent = null; 217 helper.doEndTag(); 218 log.debug("end of importag"); 219 super.doEndTag(); 220 return EVAL_PAGE; 221 } 222 223 224 } 225 | Popular Tags |