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

📄 imgtag.java

📁 structs源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public void setUsemap(String usemap) {
        this.usemap = usemap;
    }

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

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

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

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

    public boolean isUseLocalEncoding() {
        return useLocalEncoding;
    }

    public void setUseLocalEncoding(boolean b) {
        useLocalEncoding = b;
    }

    // --------------------------------------------------------- Public Methods

    /**
     * Render the beginning of the IMG tag.
     *
     * @throws 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.
     *
     * @throws 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) {
            prepareAttribute(results, "src", response.encodeURL(srcurl));
        }

        prepareAttribute(results, "name", getImageName());
        prepareAttribute(results, "height", getHeight());
        prepareAttribute(results, "width", getWidth());
        prepareAttribute(results, "align", getAlign());
        prepareAttribute(results, "border", getBorder());
        prepareAttribute(results, "hspace", getHspace());
        prepareAttribute(results, "vspace", getVspace());
        prepareAttribute(results, "ismap", getIsmap());
        prepareAttribute(results, "usemap", getUsemap());
        results.append(prepareStyles());
        results.append(prepareEventHandlers());
        prepareOtherAttributes(results);
        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;
        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.
     *
     * @param config Our Moduleconfig
     * @return True if this is the default module or contextRelative is set
     */
    private boolean srcDefaultReference(ModuleConfig config) {
        return (config == null);
    }

    /**
     * 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.
     *
     * @throws 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();
            String pageValue = this.page;

            if (!srcDefaultReference(config)) {
                pageValue =
                    TagUtils.getInstance().pageURL(request, this.page, config);
            }

            return (request.getContextPath() + pageValue);
        }

        // 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();
            String pageValue =
                TagUtils.getInstance().message(pageContext, getBundle(),
                    getLocale(), this.pageKey);

            if (!srcDefaultReference(config)) {
                pageValue =
                    TagUtils.getInstance().pageURL(request, pageValue, config);
            }

            return (request.getContextPath() + pageValue);
        }

        if (this.action != null) {
            if ((this.src != null) || (this.srcKey != null)) {
                throwImgTagSrcException();
            }

            return TagUtils.getInstance().getActionMappingURL(action, module,
                pageContext, false);
        }

        // 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)
     * @throws 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 + -