pagecontextimpl.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,055 行 · 第 1/4 页

JAVA
2,055
字号
    String acceptLanguage = getCauchoRequest().getHeader("Accept-Language");    if (acceptLanguage != null) {      Enumeration e = getCauchoRequest().getLocales();      if (e != null && e.hasMoreElements())	locale = (Locale) e.nextElement();    }    localeObj = Config.find(this, Config.FMT_FALLBACK_LOCALE);    if (localeObj instanceof Locale)      return (Locale) localeObj;    else if (localeObj instanceof String)      return getLocale((String) localeObj, null);    else      return null;  }  public static Locale getLocale(String value, String variant)  {    Locale locale = null;    int len = value.length();        CharBuffer cb = new CharBuffer();    int i = 0;    char ch = 0;    for (; i < len && (ch = value.charAt(i)) != '_' && ch != '-'; i++)      cb.append(ch);    String language = cb.toString();    if (ch == '_' || ch == '-') {      cb.clear();      for (i++; i < len && (ch = value.charAt(i)) != '_' && ch != '-'; i++)        cb.append(ch);      String country = cb.toString();      if (variant != null && ! variant.equals(""))	return new Locale(language, country, variant);      else	return new Locale(language, country);    }    else if (variant != null && ! variant.equals(""))      return new Locale(language, "", variant);    else      return new Locale(language, "");  }  public static void printBody(BodyContentImpl body, boolean isEscaped)    throws IOException  {    JspWriter out = body.getEnclosingWriter();    CharBuffer string = body.getCharBuffer();    char []cBuf = string.getBuffer();    int length = string.length() - 1;    for (; length >= 0; length--) {      char ch = cBuf[length];      if (ch != ' ' && ch != '\n' && ch != '\t' && ch != '\r')        break;    }    length++;    int i;    for (i = 0; i < length; i++) {      char ch = cBuf[i];      if (ch != ' ' && ch != '\n' && ch != '\t' && ch != '\r')        break;    }    if (! isEscaped) {      out.write(cBuf, i, length - i);      return;    }    for (; i < length; i++) {      char ch = cBuf[i];      switch (ch) {      case '<':        out.write("&lt;");        break;      case '>':        out.write("&gt;");        break;      case '&':        out.write("&amp;");        break;      case '\'':        out.write("&#039;");        break;      case '"':        out.write("&#034;");        break;      default:        out.write((char) ch);        break;      }    }  }  /**   * Evaluates the fragment, returing the string value.   */  public String invoke(JspFragment fragment)    throws JspException, IOException  {    if (fragment == null)      return "";        BodyContentImpl body = (BodyContentImpl) pushBody();    try {      fragment.invoke(body);      return body.getString();    } finally {      popBody();      body.release();    }  }  /**   * Evaluates the fragment, returing the string value.   */  public String invokeTrim(JspFragment fragment)    throws JspException, IOException  {    if (fragment == null)      return "";        BodyContentImpl body = (BodyContentImpl) pushBody();    try {      fragment.invoke(body);      return body.getTrimString();    } finally {      popBody();      body.release();    }  }  /**   * Evaluates the fragment, returing a reader   */  public Reader invokeReader(JspFragment fragment)    throws JspException, IOException  {    if (fragment == null)      return null;        BodyContentImpl body = (BodyContentImpl) pushBody();    try {      fragment.invoke(body);      return body.getReader();    } finally {      popBody();      body.release();    }  }  /**   * Parses a boolean value.   */  static public boolean toBoolean(String value)  {    if (value == null)      return false;    else if (value.equalsIgnoreCase("true"))      return true;    else if (value.equalsIgnoreCase("false"))      return false;    else      return value.trim().equalsIgnoreCase("true");  }  /**   * Set/Remove a page attribute.   */  public void defaultSetOrRemove(String var, Object value)  {    if (value != null)      putAttribute(var, value);    else      removeAttribute(var);  }  /**   * Set/Remove a page attribute.   */  public void pageSetOrRemove(String var, Object value)  {    if (value != null)      _attributes.put(var, value);    else      _attributes.remove(var);  }  /**   * Set/Remove a request attribute.   */  public void requestSetOrRemove(String var, Object value)  {    if (value != null)      getCauchoRequest().setAttribute(var, value);    else      getCauchoRequest().removeAttribute(var);  }    /**   * Set/Remove a session attribute.   */  public void sessionSetOrRemove(String var, Object value)  {    if (value != null)      getSession().setAttribute(var, value);    else      getSession().removeAttribute(var);  }  /**   * Set/Remove an webApp attribute.   */  public void applicationSetOrRemove(String var, Object value)  {    if (value != null)      getApplication().setAttribute(var, value);    else      getApplication().removeAttribute(var);  }  /**   * Returns true if the EL ignores exceptions   */  public boolean isIgnoreException()  {    JspPropertyGroup jsp = getApplication().getJsp();    return (jsp == null || jsp.isIgnoreELException());  }  /**   * Returns the XPath variable environment corresponding to this page   */  public VarEnv getVarEnv()  {    if (_varEnv == null)      _varEnv = new PageVarEnv();    return _varEnv;  }  /**   * Returns the XPath node environment corresponding to this page   */  public Node getNodeEnv()  {    return _nodeEnv;  }  /**   * Returns the XPath node environment corresponding to this page   */  public void setNodeEnv(Node node)  {    _nodeEnv = node;  }  /**   * Creates an expression.   */  public ValueExpression createExpr(ValueExpression expr,                                    String exprString,                                    Class type)  {    if (_variableMapper == null || _variableMapper.isConstant())      return expr;    return JspUtil.createValueExpression(getELContext(), type, exprString);  }    /**   * Finds an attribute in any of the scopes from page to webApp.   *   * @param name the attribute name.   *   * @return the attribute value   */  public Object resolveVariable(String name)    throws javax.el.ELException  {    Object value = findAttribute(name);    if (value != null)      return value;    if (_elContext == null)      _elContext = EL.getEnvironment();    return _elContext.getELResolver().getValue(_elContext, name, null);  }  /**   * Returns the bundle manager.   */  private BundleManager getBundleManager()  {    if (_bundleManager == null)      _bundleManager = BundleManager.create();    return _bundleManager;  }  /**   * Represents the XPath environment for this page.   */  public class PageVarEnv extends VarEnv {    /**     * Returns the value corresponding to the name.     */    public Object getValue(String name)    {      Object value = findAttribute(name);      if (value != null)        return value;      int p = name.indexOf(':');      if (p < 0) {        // jsp/1m43, TCK        throw new javax.el.ELException(L.l("'{0}' is an unknown variable", name));      }      String prefix = name.substring(0, p);      String suffix = name.substring(p + 1);      if (prefix.equals("param"))        return getCauchoRequest().getParameter(suffix);      else if (prefix.equals("header"))        return ((HttpServletRequest) getCauchoRequest()).getHeader(suffix);      else if (prefix.equals("cookie")) {        Cookie cookie;        HttpServletRequest request = (HttpServletRequest) getCauchoRequest();        if (request instanceof CauchoRequest)          cookie = ((CauchoRequest) request).getCookie(suffix);        else          cookie = null;        if (cookie != null)          return cookie.getValue();        else          return null;      }      else if (prefix.equals("initParam"))        return getApplication().getInitParameter(suffix);      else if (prefix.equals("pageScope"))        return getAttribute(suffix);      else if (prefix.equals("requestScope"))        return getCauchoRequest().getAttribute(suffix);      else if (prefix.equals("sessionScope"))        return getSession().getAttribute(suffix);      else if (prefix.equals("applicationScope"))        return getApplication().getAttribute(suffix);      else        return null;                                      }  }    static class StringArrayEnum implements Enumeration  {    private int _index;    private String []_values;    StringArrayEnum(String []values)    {      _values = values;    }    public boolean hasMoreElements()    {      return _index < _values.length;    }        public Object nextElement()    {      return _values[_index++];    }  }  public class PageELContext extends ServletELContext {    public PageELContext()    {    }    public Object getContext(Class key)    {      if (key == JspContext.class)	return PageContextImpl.this;      else	return super.getContext(key);    }        public PageContextImpl getPageContext()    {      return PageContextImpl.this;    }    @Override    public ServletContext getApplication()    {      return PageContextImpl.this.getApplication();    }    @Override    public Object getApplicationScope()    {      return new PageContextAttributeMap(PageContextImpl.this,					 APPLICATION_SCOPE);    }    @Override    public HttpServletRequest getRequest()    {      return (HttpServletRequest) PageContextImpl.this.getRequest();    }    @Override    public Object getRequestScope()    {      return new PageContextAttributeMap(PageContextImpl.this,					 REQUEST_SCOPE);    }    @Override    public Object getSessionScope()    {      return new PageContextAttributeMap(PageContextImpl.this,					 SESSION_SCOPE);    }        public ELResolver getELResolver()    {      return _elResolver;    }    public javax.el.FunctionMapper getFunctionMapper()    {      return _functionMapper;    }    public javax.el.VariableMapper getVariableMapper()    {      return _variableMapper;    }  }  public class PageFunctionMapper extends javax.el.FunctionMapper {    public Method resolveFunction(String prefix, String localName)    {      if (_functionMap != null)	return _functionMap.get(prefix + ":" + localName);      else	return null;    }  }  public class PageVariableMapper extends ImplicitVariableMapper {    private HashMap<String,ValueExpression> _map;    final boolean isConstant()    {      return _map == null || _map.size() == 0;    }        public ValueExpression resolveVariable(String var)    {      if (_map != null) {        ValueExpression expr = _map.get(var);        if (expr != null)          return expr;      }            ValueExpression expr = super.resolveVariable(var);      return expr;    }    public ValueExpression setVariable(String var,				       ValueExpression expr)    {      // ValueExpression oldValue = getVariable(var);            if (_map == null) {        _map = new HashMap<String,ValueExpression>();      }      _map.put(var, expr);            return expr;    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?