⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imgtag.java

📁 jakarta-struts-1.2.4-src
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * The vertical spacing around the image.
     */
    protected String vspace = null;

    public String getVspace() {
        return (this.vspace);
    }

    public void setVspace(String vspace) {
        this.vspace = vspace;
    }

    /**
     * The image width.
     */
    protected String width = null;

    public String getWidth() {
        return (this.width);
    }

    public void setWidth(String width) {
        this.width = width;
    }

	protected boolean useLocalEncoding = false;
    
    public boolean isUseLocalEncoding() {
	   return useLocalEncoding;
    }

    public void setUseLocalEncoding(boolean b) {
	   useLocalEncoding = b;
    }
   
   // --------------------------------------------------------- Public Methods

    /**
     * Render the beginning of the IMG tag.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doStartTag() throws JspException {

        // Evaluate the body of this tag
        return (EVAL_BODY_TAG);

    }

    /**
     * Render the end of the IMG tag.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doEndTag() throws JspException {

        // Generate the name definition or image element
        HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
        StringBuffer results = new StringBuffer("<img");
        String tmp = src();
        String srcurl = url(tmp);
        if (srcurl != null) {
            results.append(" src=\"");
            results.append(response.encodeURL(srcurl));
            results.append("\"");
        }
        String lowsrcurl = url(this.lowsrc);
        if (lowsrcurl != null) {
            results.append(" lowsrc=\"");
            results.append(response.encodeURL(lowsrcurl));
            results.append("\"");
        }
        if (imageName != null) {
            results.append(" name=\"");
            results.append(imageName);
            results.append("\"");
        }
        if (height != null) {
            results.append(" height=\"");
            results.append(height);
            results.append("\"");
        }
        if (width != null) {
            results.append(" width=\"");
            results.append(width);
            results.append("\"");
        }
        if (align != null) {
            results.append(" align=\"");
            results.append(align);
            results.append("\"");
        }
        if (border != null) {
            results.append(" border=\"");
            results.append(border);
            results.append("\"");
        }
        if (hspace != null) {
            results.append(" hspace=\"");
            results.append(hspace);
            results.append("\"");
        }
        if (vspace != null) {
            results.append(" vspace=\"");
            results.append(vspace);
            results.append("\"");
        }
        if (ismap != null) {
            results.append(" ismap=\"");
            results.append(ismap);
            results.append("\"");
        }
        if (usemap != null) {
            results.append(" usemap=\"");
            results.append(usemap);
            results.append("\"");
        }
        results.append(prepareStyles());
        results.append(prepareEventHandlers());
        results.append(getElementClose());

        TagUtils.getInstance().write(pageContext, results.toString());

        return (EVAL_PAGE);

    }

    /**
     * Release any acquired resources.
     */
    public void release() {

        super.release();

        border = null;
        height = null;
        hspace = null;
        imageName = null;
        ismap = null;
        lowsrc = null;
        name = null;
        page = null;
        pageKey = null;
        action = null;
        paramId = null;
        paramName = null;
        paramProperty = null;
        paramScope = null;
        property = null;
        scope = null;
        src = null;
        srcKey = null;
        usemap = null;
        vspace = null;
        width = null;

    }

    // ------------------------------------------------------ Protected Methods

    /**
     * Convenience method to throw a "imgTag.src" exception.
     * @throws JspException
     */
    private void throwImgTagSrcException() throws JspException {
        JspException e = new JspException(messages.getMessage("imgTag.src"));
        TagUtils.getInstance().saveException(pageContext, e);
        throw e;
    }

    /**
     * Convenience method to test whether this is the default module
     * or if contestRelative has been set.
     * @param config Our Moduleconfig
     * @return True if this is the default module or contextRelative is set
     */
    private boolean srcDefaultReference(ModuleConfig config) {
       return (config == null || isContextRelativeSet());
    }

    /**
     * Return the base source URL that will be rendered in the <code>src</code>
     * property for this generated element, or <code>null</code> if there is
     * no such URL.
     *
     * @exception JspException if an error occurs
     */
    protected String src() throws JspException {

        // Deal with a direct context-relative page that has been specified
        if (this.page != null) {
            if ((this.src != null) || (this.srcKey != null) || (this.pageKey != null)) {
                throwImgTagSrcException();
            }
            ModuleConfig config = 
                ModuleUtils.getInstance().getModuleConfig(
                        this.module,
                        (HttpServletRequest) pageContext.getRequest(),
                        pageContext.getServletContext());                
            
            HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
            if (srcDefaultReference(config)) {
                return (request.getContextPath() + this.page);
            } else {
                return (request.getContextPath() + config.getPrefix() + this.page);
            }
        }

        // Deal with an indirect context-relative page that has been specified
        if (this.pageKey != null) {
            if ((this.src != null) || (this.srcKey != null)) {
                throwImgTagSrcException();
            }
            ModuleConfig config =
                ModuleUtils.getInstance().getModuleConfig(
                        this.module,
                        (HttpServletRequest) pageContext.getRequest(),
                        pageContext.getServletContext());
            
            HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
            if (srcDefaultReference(config)) {
                return (
                    request.getContextPath()
                        + TagUtils.getInstance().message(
                            pageContext,
                            getBundle(),
                            getLocale(),
                            this.pageKey));
            } else {
                return (
                    request.getContextPath()
                        + config.getPrefix()
                        + TagUtils.getInstance().message(
                            pageContext,
                            getBundle(),
                            getLocale(),
                            this.pageKey));
            }
        }

        if (this.action != null) {
            if ((this.src != null) || (this.srcKey != null)) {
                throwImgTagSrcException();
            }
            return TagUtils.getInstance().getActionMappingURL(action, module, pageContext, isContextRelativeSet());
        }

        // Deal with an absolute source that has been specified
        if (this.src != null) {
            if (this.srcKey != null) {
                throwImgTagSrcException();
            }
            return (this.src);
        }

        // Deal with an indirect source that has been specified
        if (this.srcKey == null) {
            throwImgTagSrcException();
        }
        
        return TagUtils.getInstance().message(
            pageContext,
            getBundle(),
            getLocale(),
            this.srcKey);

    }

    /**
     * Return the specified src URL, modified as necessary with optional
     * request parameters.
     *
     * @param url The URL to be modified (or null if this url will not be used)
     *
     * @exception JspException if an error occurs preparing the URL
     */
    protected String url(String url) throws JspException {

        if (url == null) {
            return (url);
        }

		String charEncoding = "UTF-8";
		if(useLocalEncoding){
			charEncoding = pageContext.getResponse().getCharacterEncoding();
		}
		
        // Start with an unadorned URL as specified
        StringBuffer src = new StringBuffer(url);

        // Append a single-parameter name and value, if requested
        if ((paramId != null) && (paramName != null)) {
            if (src.toString().indexOf('?') < 0) {
                src.append('?');
            } else {
                src.append("&amp;");
            }
            src.append(paramId);
            src.append('=');
            Object value = TagUtils.getInstance().lookup(pageContext, paramName, paramProperty, paramScope);
            if (value != null)
                src.append(TagUtils.getInstance().encodeURL(value.toString(), charEncoding));
        }

        // Just return the URL if there is no bean to look up
        if ((property != null) && (name == null)) {
            JspException e = new JspException(messages.getMessage("getter.name"));
            TagUtils.getInstance().saveException(pageContext, e);
            throw e;
        }

        if (name == null) {
            return (src.toString());
        }

        // Look up the map we will be using
        Object mapObject = TagUtils.getInstance().lookup(pageContext, name, property, scope);
        Map map = null;
        try {
            map = (Map) mapObject;
        } catch (ClassCastException e) {
            TagUtils.getInstance().saveException(pageContext, e);
            throw new JspException(messages.getMessage("imgTag.type"));
        }

        // Append the required query parameters
        boolean question = (src.toString().indexOf("?") >= 0);
        Iterator keys = map.keySet().iterator();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            Object value = map.get(key);
            if (value == null) {
                if (question) {
                    src.append("&amp;");
                } else {
                    src.append('?');
                    question = true;
                }
                src.append(key);
                src.append('=');
                // Interpret null as "no value specified"
            } else if (value instanceof String[]) {
                String values[] = (String[]) value;
                for (int i = 0; i < values.length; i++) {
                    if (question) {
                        src.append("&amp;");
                    } else {
                        src.append('?');
                        question = true;
                    }
                    src.append(key);
                    src.append('=');
                    src.append(TagUtils.getInstance().encodeURL(values[i], charEncoding));
                }
            } else {

                if (question) {
                    src.append("&amp;");
                } else {
                    src.append('?');
                    question = true;
                }
                src.append(key);
                src.append('=');
                src.append(TagUtils.getInstance().encodeURL(value.toString(), charEncoding));
            }
        }

        // Return the final result
        return (src.toString());

    }

}

⌨️ 快捷键说明

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