cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ba...@locus.apache.org
Subject cvs commit: xml-cocoon/src/org/apache/cocoon/processor/sql ColumnFormatter.java SQLProcessor.java Utils.java
Date Wed, 08 Dec 1999 07:37:09 GMT
balld       99/12/07 23:37:09

  Modified:    src/org/apache/cocoon/processor/sql ColumnFormatter.java
                        SQLProcessor.java Utils.java
  Log:
  Moved a few things around in ColumnFormatter so that it's possible for columns to be formatted
as more than simply a text node (e.g. embedded HTML). Also added a little formatter that can
transform \n into <br> for text and varchar columns.
  
  Revision  Changes    Path
  1.2       +84 -12    xml-cocoon/src/org/apache/cocoon/processor/sql/ColumnFormatter.java
  
  Index: ColumnFormatter.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon/src/org/apache/cocoon/processor/sql/ColumnFormatter.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ColumnFormatter.java	1999/12/07 09:20:59	1.1
  +++ ColumnFormatter.java	1999/12/08 07:37:08	1.2
  @@ -1,3 +1,52 @@
  +/*
  + ============================================================================
  +                   The Apache Software License, Version 1.1
  + ============================================================================
  + 
  +    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
  + 
  + Redistribution and use in source and binary forms, with or without modifica-
  + tion, are permitted provided that the following conditions are met:
  + 
  + 1. Redistributions of  source code must  retain the above copyright  notice,
  +    this list of conditions and the following disclaimer.
  + 
  + 2. Redistributions in binary form must reproduce the above copyright notice,
  +    this list of conditions and the following disclaimer in the documentation
  +    and/or other materials provided with the distribution.
  + 
  + 3. The end-user documentation included with the redistribution, if any, must
  +    include  the following  acknowledgment:  "This product includes  software
  +    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
  +    Alternately, this  acknowledgment may  appear in the software itself,  if
  +    and wherever such third-party acknowledgments normally appear.
  + 
  + 4. The names "Cocoon" and  "Apache Software Foundation"  must not be used to
  +    endorse  or promote  products derived  from this  software without  prior
  +    written permission. For written permission, please contact
  +    apache@apache.org.
  + 
  + 5. Products  derived from this software may not  be called "Apache", nor may
  +    "Apache" appear  in their name,  without prior written permission  of the
  +    Apache Software Foundation.
  + 
  + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  + FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
  + APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
  + INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
  + DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
  + OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
  + ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
  + (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
  + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  + 
  + This software  consists of voluntary contributions made  by many individuals
  + on  behalf of the Apache Software  Foundation and was  originally created by
  + Stefano Mazzocchi  <stefano@apache.org>. For more  information on the Apache 
  + Software Foundation, please see <http://www.apache.org/>.
  + */
  +
   package org.apache.cocoon.processor.sql;
   
   import org.w3c.dom.*;
  @@ -5,6 +54,15 @@
   import java.text.*;
   import java.util.*;
   
  +/**
  + * This class allows certain columns to be formatted specially. If it gets
  + * too complex, it should probably be broken apart into a manager and a
  + * set of specific formatting classes (e.g. one for dates, one to transform
  + * text into an HTML tree according to certain rules, etc.)
  + *
  + * @author <a href="balld@webslingerz.com">Donald A. Ball Jr.</a>
  + * @version does this really change magically? let's see.
  + */
   class ColumnFormatter {
   
   	protected Properties formats_by_name = new Properties();
  @@ -26,21 +84,17 @@
   	}
   
   	protected void putFormat(String name, String type, String format) {
  -		System.err.println("TRYING TO ADD FORMAT FOR NAME: "+name+" TYPE: "+type);
   		if (format == null || format.equals("")) {
   			return;
   		}
   		if (name == null || name.equals("") && (type != null && !type.equals("")))
{
  -			System.err.println("ADDING TYPE: "+type);
   			formats_by_type.put(type,format);
   		} else {
  -			System.err.println("ADDING NAME: "+name);
   			formats_by_name.put(name,format);
   		}
   	}
   
   	protected String getFormat(Column column) {
  -		System.err.println("GETTING FORMAT FOR "+column.name+" "+column.type);
   		String format = formats_by_name.getProperty(column.name);
   		if (format == null) {
   			format = formats_by_type.getProperty(column.type);
  @@ -48,19 +102,37 @@
   		return format;
   	}
   
  -	protected String formatColumn(Column column, ResultSet rs, int i) throws SQLException
{
  +	protected void addColumnNode(Document document, Element parent, Column column, ResultSet
rs, int i) throws SQLException {
   		String format = getFormat(column);
   		String value;
   		if (format != null) {
  -			if (column.type.equals("timestamp") ||
  -				column.type.equals("time") ||
  -				column.type.equals("date") ||
  -				column.type.equals("datetime")) {
  -					SimpleDateFormat date_format = new SimpleDateFormat(format);
  -					return date_format.format(rs.getDate(i));
  +			if (column.type.equals("timestamp") || column.type.equals("time") || column.type.equals("date")
|| column.type.equals("datetime")) {
  +				SimpleDateFormat date_format = new SimpleDateFormat(format);
  +				parent.appendChild(document.createTextNode(date_format.format(rs.getDate(i))));
  +				return;
  +			} else if (column.type.equals("varchar") || column.type.equals("text")) {
  +				if (format.equals("br")) {
  +					StringBuffer sb = new StringBuffer();
  +					StringCharacterIterator iter = new StringCharacterIterator(rs.getString(i));
  +					for (char c = iter.first(); c != iter.DONE; c = iter.next()) {
  +						if (c == '\n') {
  +							if (sb.length() > 0) {
  +								parent.appendChild(document.createTextNode(sb.toString()));
  +								sb.setLength(0);
  +							}
  +							parent.appendChild(document.createElement("br"));
  +						} else {
  +							sb.append(c);
  +						}
  +					}
  +					if (sb.length() > 0) {
  +						parent.appendChild(document.createTextNode(sb.toString()));
  +					}
  +					return;
  +				}
   			}
   		}
  -		return rs.getString(i);
  +		parent.appendChild(document.createTextNode(rs.getString(i)));
   	}
   
   }
  
  
  
  1.7       +3 -16     xml-cocoon/src/org/apache/cocoon/processor/sql/SQLProcessor.java
  
  Index: SQLProcessor.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon/src/org/apache/cocoon/processor/sql/SQLProcessor.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- SQLProcessor.java	1999/12/07 09:20:59	1.6
  +++ SQLProcessor.java	1999/12/08 07:37:08	1.7
  @@ -62,7 +62,7 @@
    * A processor that performs SQL database queries.
    *
    * @author <a href="mailto:balld@webslingerZ.com">Donald Ball</a>
  - * @version $Revision: 1.6 $ $Date: 1999/12/07 09:20:59 $
  + * @version $Revision: 1.7 $ $Date: 1999/12/08 07:37:08 $
    */
   
   public class SQLProcessor extends AbstractActor implements Processor, Status {
  @@ -250,7 +250,6 @@
                   Element column_element;
                   Node row_node = results_node;
                   Element row_element = null;
  -                String value;
                   int count = 0;
                   if (skip_rows > 0) {
                       while (rs.next()) {
  @@ -269,19 +268,7 @@
                       }
   
                       for (int i=0; i<columns.length; i++) {
  -						value = formatter.formatColumn(columns[i],rs,i+1);
  -						/*
  -						switch(columns[i].type) {
  -							case Types.TIMESTAMP:
  -								if (timestamp_format != null)
  -									value = timestamp_format.format(rs.getDate(i+1));
  -								else
  -                        			value = rs.getString(i+1);
  -								break;
  -							default:
  -								value = rs.getString(i+1);
  -						}
  -						*/
  +						String value = rs.getString(i+1);
                           if (create_row_elements && create_id_attribute &&
id_attribute_column_index == i) {
                               row_element.setAttribute(id_attribute,value);
                               continue;
  @@ -292,7 +279,7 @@
                               column_element.setAttribute("NULL","YES");
                               column_element.appendChild(document.createTextNode(""));
                           } else {
  -                            column_element.appendChild(document.createTextNode(value));
  +							formatter.addColumnNode(document,column_element,columns[i],rs,i+1);
                           }
                           row_node.appendChild(column_element);
                       }
  
  
  
  1.6       +1 -2      xml-cocoon/src/org/apache/cocoon/processor/sql/Utils.java
  
  Index: Utils.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon/src/org/apache/cocoon/processor/sql/Utils.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Utils.java	1999/12/03 08:42:29	1.5
  +++ Utils.java	1999/12/08 07:37:09	1.6
  @@ -59,7 +59,7 @@
    * Utility methods for this processor.
    *
    * @author <a href="mailto:balld@webslingerZ.com">Donald Ball</a>
  - * @version $Revision: 1.5 $ $Date: 1999/12/03 08:42:29 $
  + * @version $Revision: 1.6 $ $Date: 1999/12/08 07:37:09 $
    */
   
   public class Utils {
  @@ -173,6 +173,5 @@
   		}
   		return sb.toString();
   	}
  -
   
   }
  
  
  

Mime
View raw message