public void doTag()

in jelly-tags/fmt/src/main/java/org/apache/commons/jelly/tags/fmt/MessageTag.java [68:165]


    public void doTag(XMLOutput output) throws JellyTagException {

        Object keyInput = null;
        if (this.key != null) {
            keyInput = this.key.evaluate(context);
            // process <param> sub tags
            invokeBody(output);
        }
        else {
            // get key from body
            keyInput = getBodyText();
        }

        if ((keyInput == null) || keyInput.equals("")) {
            try {
                output.write("??????");
            } catch (SAXException e) {
                throw new JellyTagException(e);
            }
            return;
        }

        Object bundleInput = null;
        if (this.bundle != null) {
            bundleInput = this.bundle.evaluate(context);
        }
        if (bundleInput != null && bundleInput instanceof LocalizationContext) {
            locCtxt = (LocalizationContext) bundleInput;
        }

        String prefix = null;
        if (locCtxt == null) {
            Tag t = findAncestorWithClass(this, BundleTag.class);
            if (t != null) {
                // use resource bundle from parent <bundle> tag
                BundleTag parent = (BundleTag) t;
                locCtxt = parent.getLocalizationContext();
                prefix = parent.getPrefixAsString();
            } else {
                locCtxt = BundleTag.getLocalizationContext(context);
            }
        } else {
            // localization context taken from 'bundle' attribute
            if (locCtxt.getLocale() != null) {
                // TODO
                // SetLocaleSupport.setResponseLocale(pageContext,
                // locCtxt.getLocale());
            }
        }

        String message = UNDEFINED_KEY + keyInput + UNDEFINED_KEY;
        if (locCtxt != null) {
            ResourceBundle bundle = locCtxt.getResourceBundle();
            if (bundle != null) {
                try {
                    // prepend 'prefix' attribute from parent bundle
                    if (prefix != null) {
                        keyInput = prefix + keyInput;
                    }
                    message = bundle.getString(keyInput.toString());
                    // Perform parametric replacement if required
                    if (!params.isEmpty()) {
                        Object[] messageArgs = params.toArray();
                        MessageFormat formatter = new MessageFormat("");
                        if (locCtxt.getLocale() != null) {
                            formatter.setLocale(locCtxt.getLocale());
                        }
                        formatter.applyPattern(message);
                        message = formatter.format(messageArgs);
                    }
                } catch (MissingResourceException mre) {
                    message = UNDEFINED_KEY + keyInput + UNDEFINED_KEY;
                }
            }
        }

        if (scope != null) {
            if (var != null) {
                context.setVariable(var, scope, message);
            }
            else {
                throw new JellyTagException( "If 'scope' is specified, 'var' must be defined for this tag" );
            }
        }
        else {
            if (var != null) {
                context.setVariable(var, message);
            }
            else {
                // write the message
                try {
                    output.write(message);
                } catch (SAXException e) {
                    throw new JellyTagException(e);
                }
            }
        }
    }