[ http://jira.codehaus.org/browse/DISPL-24?page=comments#action_42124 ] 

Steve McLeod commented on DISPL-24:
-----------------------------------

I have implemented an export class which formats the XML as desired. A column's 
name is converted to an XML tag as follows:
1. Make the first letter of each word uppercase, all other letters lowercase
2. Strip all white space, including between words.

So a column called "Person ID" becomes "PersonId"

I call it EnhancedXmlView and the source code for the class follows:

/**
 * Licensed under the Artistic License; you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://displaytag.sourceforge.net/license.html
 *
 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */
package org.displaytag.export;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.displaytag.model.*;

import javax.servlet.jsp.JspException;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;


/**
 * Export view for xml exporting. Column names are used to make tag names.
 *
 * @author Steve McLeod
 */
public class EnhancedXmlView implements TextExportView {
    /**
     * logger.
     */
    private Log log = LogFactory.getLog(this.getClass());

    /**
     * TableModel to render.
     */
    private TableModel model;

    /**
     * export full list?
     */
    private boolean exportFull;

    /**
     * decorate export?
     */
    private boolean decorated;
    private List headerTitles = new ArrayList();


    /**
     * @see ExportView#setParameters(org.displaytag.model.TableModel, boolean, 
boolean, boolean)
     */
    public void setParameters(TableModel tableModel, boolean exportFullList, 
boolean includeHeader,
                              boolean decorateValues) {
        this.model = tableModel;
        this.exportFull = exportFullList;
        this.decorated = decorateValues;
    }

    /**
     * @see org.displaytag.export.BaseExportView#getRowStart()
     */
    protected String getRowStart() {
        return "<row>\n"; //$NON-NLS-1$
    }

    /**
     * @see org.displaytag.export.BaseExportView#getRowEnd()
     */
    protected String getRowEnd() {
        return "</row>\n"; //$NON-NLS-1$
    }

    /**
     * @see org.displaytag.export.BaseExportView#getCellStart()
     */
    protected String getCellStart(String columnName) {
        return "<" + columnName + ">"; //$NON-NLS-1$
    }

    /**
     * @see org.displaytag.export.BaseExportView#getCellEnd()
     */
    protected String getCellEnd(String columnName) {
        return "</" + columnName + ">\n"; //$NON-NLS-1$
    }

    /**
     * @see org.displaytag.export.BaseExportView#getDocumentStart()
     */
    protected String getDocumentStart() {
        return "<?xml version=\"1.0\"?>\n<table>\n"; //$NON-NLS-1$
    }

    /**
     * @see org.displaytag.export.BaseExportView#getDocumentEnd()
     */
    protected String getDocumentEnd() {
        return "</table>\n"; //$NON-NLS-1$
    }

    /**
     * @see org.displaytag.export.BaseExportView#getAlwaysAppendCellEnd()
     */
    protected boolean getAlwaysAppendCellEnd() {
        return true;
    }

    /**
     * @see org.displaytag.export.BaseExportView#getAlwaysAppendRowEnd()
     */
    protected boolean getAlwaysAppendRowEnd() {
        return true;
    }

    /**
     * @see org.displaytag.export.ExportView#getMimeType()
     */
    public String getMimeType() {
        return "text/xml"; //$NON-NLS-1$
    }

    /**
     * @see 
org.displaytag.export.BaseExportView#escapeColumnValue(java.lang.Object)
     */
    protected String escapeColumnValue(Object value) {
        if (value != null) {
            return StringEscapeUtils.escapeXml(value.toString());
        }
        return null;
    }

    /**
     * @see TextExportView#doExport(java.io.Writer)
     */
    public void doExport(Writer out) throws IOException, JspException {
        if (log.isDebugEnabled()) {
            log.debug(getClass().getName());
        }

        final String DOCUMENT_START = getDocumentStart();
        final String DOCUMENT_END = getDocumentEnd();
        final String ROW_START = getRowStart();
        final String ROW_END = getRowEnd();
        final boolean ALWAYS_APPEND_CELL_END = getAlwaysAppendCellEnd();
        final boolean ALWAYS_APPEND_ROW_END = getAlwaysAppendRowEnd();

        buildHeaders();

        // document start
        write(out, DOCUMENT_START);

        // get the correct iterator (full or partial list according to the 
exportFull field)
        RowIterator rowIterator = this.model.getRowIterator(this.exportFull);

        // iterator on rows
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();

            if (this.model.getTableDecorator() != null) {

                String stringStartRow = 
this.model.getTableDecorator().startRow();
                write(out, stringStartRow);
            }

            // iterator on columns
            ColumnIterator columnIterator = 
row.getColumnIterator(this.model.getHeaderCellList());

            write(out, ROW_START);

            int columnNumber = 0;
            while (columnIterator.hasNext()) {
                Column column = columnIterator.nextColumn();
                String columnHeader = (String) headerTitles.get(columnNumber);
                String cellStart = getCellStart(columnHeader);
                String cellEnd = getCellEnd(columnHeader);

                // Get the value to be displayed for the column
                String value = 
escapeColumnValue(column.getValue(this.decorated));

                write(out, cellStart);

                write(out, value);

                if (ALWAYS_APPEND_CELL_END || columnIterator.hasNext()) {
                    write(out, cellEnd);
                }
                columnNumber++;
            }
            if (ALWAYS_APPEND_ROW_END || rowIterator.hasNext()) {
                write(out, ROW_END);
            }
        }

        // document end
        write(out, DOCUMENT_END);

    }

    /**
     * Write a String, checking for nulls value.
     *
     * @param out    output writer
     * @param string String to be written
     * @throws java.io.IOException thrown by out.write
     */
    private void write(Writer out, String string) throws IOException {
        if (string != null) {
            out.write(string);
        }
    }

    /**
     * @see TextExportView#outputPage()
     */
    public boolean outputPage() {
        return false;
    }

    /**
     * iterates through the headers once, placing the titles in an ArrayList for
     * quick'n'easy access as we iterate through each row
     */
    private void buildHeaders()
    {
        headerTitles.clear();
        Iterator iterator = this.model.getHeaderCellList().iterator();

        while (iterator.hasNext())
        {
            HeaderCell headerCell = (HeaderCell) iterator.next();
            String columnHeader = headerCell.getTitle();
            if (columnHeader == null)
            {
                columnHeader = headerCell.getBeanPropertyName();
            }

            if (columnHeader != null)
            {
                columnHeader = 
StringUtils.deleteWhitespace(WordUtils.capitalizeFully(columnHeader));
                columnHeader = escapeColumnValue(columnHeader);
            }

            headerTitles.add(columnHeader);

        }

    }

}


Make sure you also change org.displaytag.properties.TableTag.properties as 
follows:

replace the line that reads

export.xml.class=org.displaytag.export.XmlView

with this line

export.xml.class=org.displaytag.export.EnhancedXmlView


> Export using reflection
> -----------------------
>
>          Key: DISPL-24
>          URL: http://jira.codehaus.org/browse/DISPL-24
>      Project: DisplayTag
>         Type: Wish
>   Components: Export
>     Versions: 1.0 RC1
>     Reporter: fabrizio giustina
>     Assignee: fabrizio giustina
>     Priority: Minor
>      Fix For: 1.0 RC2

>
>
> ====
> imported from sf tracker
> id 1021473
> submitted by Mike Miller - mikemiller991
> http://sourceforge.net/tracker/index.php?func=detail&group_id=73068&atid=536613&aid=1021473
>  
> ====
> One thing I would like to see with the export
> functionality is the ability for it to do the following:
> In the XML export, instead of
> <table>
> <column></column>
> </table>
> Is it possible to have it do something like
> <tableName>
> <fieldName></fieldName>
> </tableName>
> Also another function might be to allow a user to export
> the whole collection using the List provided as the table
> source. I have a case in which some of my data from a
> collection is used to populate a 'tooltips' for other data
> in the collection. When I do an export..that
> data "screwily" (that even a word :) ) placed into the
> output.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira



-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
displaytag-devel mailing list
displaytag-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/displaytag-devel

Reply via email to