public void doTag()

in jelly-tags/fmt/src/main/java/org/apache/commons/jelly/tags/fmt/FormatDateTag.java [90:203]


    public void doTag(XMLOutput output) throws JellyTagException {

        if (scope != null && var == null) {
            throw new JellyTagException(
            "If 'scope' is specified, 'var' must be defined for this tag" );
        }

        Object valueInput = null;
        if (this.value != null) {
            valueInput = this.value.evaluate(context);
        }

        Date date = null;
        if (valueInput != null && valueInput instanceof Date) {
            date = (Date) valueInput;
        }

        if (date == null && var != null) {
            if (scope != null) {
                context.removeVariable(var, scope);
            }
            else {
                context.removeVariable(var);
            }
        }

        etype = DATE;
        if (this.type != null) {
            etype = (String) this.type.evaluate(context);
        }

        edateStyle = DEFAULT;
        if (this.dateStyle != null) {
            edateStyle = (String) this.dateStyle.evaluate(context);
        }

        etimeStyle = DEFAULT;
        if (this.timeStyle != null) {
            etimeStyle = (String) this.timeStyle.evaluate(context);
        }

        String epattern = null;
        if (this.pattern != null) {
            epattern = (String) this.pattern.evaluate(context);
        }

        Object etimeZone = null;
        if (this.timeZone != null) {
            etimeZone = this.timeZone.evaluate(context);
        }

        // Create formatter
        Locale locale = SetLocaleTag.getFormattingLocale(
            context,
            this,
            true,
            DateFormat.getAvailableLocales());

        String formatted = null;
        if (locale != null) {
            DateFormat formatter = createFormatter(locale);

            // Apply pattern, if present
            if (pattern != null) {
                try {
                    ((SimpleDateFormat) formatter).applyPattern(epattern);
                } catch (ClassCastException cce) {
                    formatter = new SimpleDateFormat(epattern, locale);
                }
            }

            // Set time zone
            TimeZone tz = null;
            if ((etimeZone instanceof String)
            && ((String) etimeZone).isEmpty()) {
                etimeZone = null;
            }
            if (etimeZone != null) {
                if (etimeZone instanceof String) {
                    tz = TimeZone.getTimeZone((String) etimeZone);
                } else if (etimeZone instanceof TimeZone) {
                    tz = (TimeZone) etimeZone;
                } else {
                    throw new JellyTagException("Bad time zone");
                }
            } else {
                tz = TimeZoneTag.getTimeZone(context, this);
            }
            if (tz != null) {
                formatter.setTimeZone(tz);
            }
            formatted = formatter.format(date);
        } else {
            // no formatting locale available, use Date.toString()
            formatted = date.toString();
        }

        if (var != null) {
            if (scope != null) {
                context.setVariable(var, scope, formatted);
            }
            else {
                context.setVariable(var, formatted);
            }
        }
        else {
            try {
                // write the formatted
                output.write(formatted);
            } catch (SAXException e) {
                throw new JellyTagException("could not write formatted text",e);
            }
        }
    }