You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by ba...@locus.apache.org on 2000/03/29 08:45:35 UTC

cvs commit: xml-cocoon/src/org/apache/cocoon/processor/sql ColumnFormatter.java SQLProcessor.java

balld       00/03/28 22:45:35

  Modified:    src/org/apache/cocoon/processor/sql ColumnFormatter.java
                        SQLProcessor.java
  Log:
  Added code that should check for byte and char arrays and construct strings
  from them more intelligently than toString() does.
  
  Revision  Changes    Path
  1.6       +80 -69    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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ColumnFormatter.java	2000/02/13 18:29:32	1.5
  +++ ColumnFormatter.java	2000/03/29 06:45:35	1.6
  @@ -65,80 +65,91 @@
    */
   class ColumnFormatter {
   
  -	protected Properties formats_by_name = new Properties();
  -	protected Properties formats_by_type = new Properties();
  +    protected Properties formats_by_name = new Properties();
  +    protected Properties formats_by_type = new Properties();
   
  -	protected ColumnFormatter(Element query_element) {
  -		NodeList children = query_element.getElementsByTagName("column");
  -		Element ary[] = new Element[children.getLength()];
  -		for (int i=0; i<ary.length; i++) {
  -			ary[i] = (Element)children.item(i);
  -		}
  -		for (int i=0; i<ary.length; i++) {
  -			Element element = ary[i];
  -			String name = element.getAttribute("name");
  -			String type = element.getAttribute("type");
  -			String format = element.getAttribute("format");
  -			putFormat(name,type,format);
  -		}
  -	}
  +    protected ColumnFormatter(Element query_element) {
  +        NodeList children = query_element.getElementsByTagName("column");
  +        Element ary[] = new Element[children.getLength()];
  +        for (int i=0; i<ary.length; i++) {
  +            ary[i] = (Element)children.item(i);
  +        }
  +        for (int i=0; i<ary.length; i++) {
  +            Element element = ary[i];
  +            String name = element.getAttribute("name");
  +            String type = element.getAttribute("type");
  +            String format = element.getAttribute("format");
  +            putFormat(name,type,format);
  +        }
  +    }
   
  -	protected void putFormat(String name, String type, String format) {
  -		if (format == null || format.equals("")) {
  -			return;
  -		}
  -		if (name == null || name.equals("") && (type != null && !type.equals(""))) {
  -			formats_by_type.put(type,format);
  -		} else {
  -			formats_by_name.put(name,format);
  -		}
  -	}
  +    protected void putFormat(String name, String type, String format) {
  +        if (format == null || format.equals("")) {
  +            return;
  +        }
  +        if (name == null || name.equals("") && (type != null && !type.equals(""))) {
  +            formats_by_type.put(type,format);
  +        } else {
  +            formats_by_name.put(name,format);
  +        }
  +    }
   
  -	protected String getFormat(Column column) {
  -		String format = formats_by_name.getProperty(column.name);
  -		if (format == null) {
  -			format = formats_by_type.getProperty(column.type);
  -		}
  -		return format;
  -	}
  +    protected String getFormat(Column column) {
  +        String format = formats_by_name.getProperty(column.name);
  +        if (format == null) {
  +            format = formats_by_type.getProperty(column.type);
  +        }
  +        return format;
  +    }
   
  -	protected void addColumnNode(Document document, Element parent, Column column, Object value, int i) throws SQLException {
  -		String format = getFormat(column);
  -		if (format != null) {
  -			if (column.type.equals("timestamp") || column.type.equals("time") || column.type.equals("date") || column.type.equals("datetime")) {
  -                                if (value instanceof java.util.Date) {
  -				    SimpleDateFormat date_format = new SimpleDateFormat(format);
  -				    parent.appendChild(document.createTextNode(date_format.format((java.util.Date)value)));
  -                                }
  -                                else {
  -            	                    //We can't format this object as a Date 'cos it isn't one!
  -    	                            //Fall back to simple String format
  -				    parent.appendChild(document.createTextNode(value.toString()));
  -                                }
  -				return;
  -			} else if (column.type.equals("varchar") || column.type.equals("text")) {
  -				if (format.equals("br")) {
  -					StringBuffer sb = new StringBuffer();
  -					StringCharacterIterator iter = new StringCharacterIterator(value.toString());
  -					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;
  -				}
  -			}
  +    protected void addColumnNode(Document document, Element parent, Column column, Object value, int i) throws SQLException {
  +        String format = getFormat(column);
  +        if (format != null) {
  +            if (column.type.equals("timestamp") || column.type.equals("time") || column.type.equals("date") || column.type.equals("datetime")) {
  +                if (value instanceof java.util.Date) {
  +                    SimpleDateFormat date_format = new SimpleDateFormat(format);
  +                    parent.appendChild(document.createTextNode(date_format.format((java.util.Date)value)));
  +                }
  +                else {
  +                    //We can't format this object as a Date 'cos it isn't one!
  +                    //Fall back to simple String format
  +                    parent.appendChild(document.createTextNode(value.toString()));
  +                }
  +                return;
  +            } else if (column.type.equals("varchar") || column.type.equals("text")) {
  +				String strvalue = getStringValue(value);
  +                if (format.equals("br")) {
  +                    StringBuffer sb = new StringBuffer();
  +                    StringCharacterIterator iter = new StringCharacterIterator(strvalue);
  +                    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;
  +                }
  +            }
  +        }
  +        parent.appendChild(document.createTextNode(getStringValue(value)));
  +    }
  +
  +	protected static String getStringValue(Object value) {
  +		if (value instanceof byte[]) {
  +			return new String((byte[])value);
  +		} else if (value instanceof char[]) {
  +			return new String((char[])value);
  +		} else {
  +			return value.toString();
   		}
  -                parent.appendChild(document.createTextNode(value.toString()));
   	}
   
   }
  
  
  
  1.11      +3 -3      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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- SQLProcessor.java	2000/02/13 18:29:32	1.10
  +++ SQLProcessor.java	2000/03/29 06:45:35	1.11
  @@ -62,7 +62,7 @@
    * A processor that performs SQL database queries.
    *
    * @author <a href="mailto:balld@webslingerZ.com">Donald Ball</a>
  - * @version $Revision: 1.10 $ $Date: 2000/02/13 18:29:32 $
  + * @version $Revision: 1.11 $ $Date: 2000/03/29 06:45:35 $
    */
   
   public class SQLProcessor extends AbstractActor implements Processor, Status {
  @@ -271,8 +271,8 @@
                   		Object value = rs.getObject(i+1);
                           if (create_row_elements && create_id_attribute
                   		  && id_attribute_column_index == i) {
  -                            row_element.setAttribute(id_attribute, value.toString());
  -                            continue;
  +                            row_element.setAttribute(id_attribute,ColumnFormatter.getStringValue(value));
  +							continue;
                           }
                           if (value == null && null_mode == OMIT_NULLS) continue;
                           column_element = Utils.createElement(document,namespace,columns[i].name);