[jdom-interest] Bug in JDOMException And Some Filters to take away the pain ):)

Teemu Tingander Teemu.Tingander at tecnomen.fi
Thu Oct 3 06:46:55 PDT 2002


There seems to be annoying stacktrace bug in JDOMException

Some where in the file JDOMException.java ...

    /**
     * <p>
     * This prints the stack trace of the <code>Exception</code> to the
given
     *   PrintStream. If there is a root cause, the stack trace of the root
     *   <code>Exception</code> is printed right after.
     * </p>
     */
    public void printStackTrace(PrintStream s) {
        // Print the stack trace for this exception.
        super.printStackTrace(s);

        Throwable parent = this;
        Throwable child;

        // Print the stack trace for each nested exception.
        while((child = getNestedException(parent)) != null) {
            if (child != null) {
                System.err.print("Caused by: ");
			<--- BLAH BLAH BLAH -->
            }
        }
    }

    /**
     * <p>
     * This prints the stack trace of the <code>Exception</code> to the
given
     *   PrintWriter. If there is a root cause, the stack trace of the root
     *   <code>Exception</code> is printed right after.
     * </p>
     */
    public void printStackTrace(PrintWriter w) {
        // Print the stack trace for this exception.
        super.printStackTrace(w);

        Throwable parent = this;
        Throwable child;

        // Print the stack trace for each nested exception.
        while((child = getNestedException(parent)) != null) {
            if (child != null) {
                System.err.print("Caused by: ");
			<--- BLAH BLAH BLAH -->
            }
        }
    }

In PrintWriter version that obviously should be w.print("Caused by:") and in
PrintStream version s.print("Caused by:"); 

Easy fix anyway, but i thing that Java 1.4 with its chained exceptions
should be used to handle this correctly. (  but if 1.3 is used .. ?? );

OK And then some Filters (These supposed to be here a long long time ago but
better now the never ..).. The implementations for canAdd and canRemove are
somowhet fuzzy, so if someone can tell me what they supposed to do and they
dont do that please let me know.

AndFilter.java --->

package com.tecnomen.jdom.filter;

import java.util.Iterator;
import java.util.LinkedList;

import org.jdom.filter.Filter;

/**
 *  Copyright (C) 2001, 002 Tecnomen Oyj & Teemu Tingander.
 *  All rights reserved.
 * 
 * 	This code is provided as a contribution to JDOM
project(www.jdom.org) 
 *
 *	@author	Teemu Tingander (teemu.tingander at tecnomen.fi)
 */
public class AndFilter implements Filter {

	private LinkedList filters = new LinkedList();			
			
	public AndFilter(){
	}		

	public AndFilter(Filter f1){
		this.filters.add(f1);		
	}		
	
	public AndFilter(Filter f1, Filter f2){
		this.filters.add(f1);	
		this.filters.add(f2);
	}
	
	
	public AndFilter and(Filter filter){
		this.filters.add(filter);
		return this;
	}

	/**
	 * @see Filter#canAdd(Object)
	 */
	public boolean canAdd(Object obj) {
		Filter e = null;
		Iterator i = filters.iterator();
		while (i.hasNext()) {
			e = (Filter) i.next();
			if ( ! e.canAdd(obj) ) return false;
		}
		return true;
	}

	/**
	 * @see Filter#canRemove(Object)
	 */
	public boolean canRemove(Object obj) {
		Filter e = null;
		Iterator i = filters.iterator();
		while (i.hasNext()) {
			e = (Filter) i.next();
			if ( ! e.canRemove(obj) ) return false;
		}
		return true;
	}

	/**
	 * @see Filter#matches(Object)
	 */
	public boolean matches(Object obj) {
		Filter e = null;
		Iterator i = filters.iterator();
		while (i.hasNext()) {
			e = (Filter) i.next();
			if ( ! e.matches(obj) ) return false;
		}
		return true;
	}

}

AttributeFilter.java -->

package com.tecnomen.jdom.filter;

import org.jdom.Attribute;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.filter.Filter;

/**
 *  Copyright (C) 2001, 2002 Tecnomen Oyj & Teemu Tingander.
 *  All rights reserved.
 * 
 * 	This code is provided as a contribution to JDOM
project(www.jdom.org) 
 *
 *	@author	Teemu Tingander (teemu.tingander at tecnomen.fi)
 */
public class AttributeFilter implements Filter {

	/** The attribute name */
	protected String name;

	/** The attribute name */
	protected String value;

	/** The attribute namespace */
	protected Namespace namespace;

	/**
	 * <p>
	 * Filter out the Elements with the named attribute name in any
Namespace.
	 * </p>
	 * @param name   The name of the Attribute.
	 */
	public AttributeFilter(String name) {
		this.name = name;
	}

	/**
	 * <p>
	 * Filter out the Elements with the named attribute name in any
Namespace, 
	 * While Attribute is having given value.
	 * </p>
	 * @param name   The name  of the Attribute.
	 * @param name   The value of the Attribute.
	 */
	public AttributeFilter(String name, String value) {
		this.name = name;
		this.value = value;
	}

	/**
	 * <p>
	 * Filter out the Elements with the supplied name and Namespace.
	 * While Attribute is having given value.
	 * </p>
	 *
	 * @param name   The name of the Attribute.
	 * @param namespace The namespace the Element lives in.
	 */
	public AttributeFilter(String name, Namespace namespace, String
value) {
		this.name = name;
		this.value = value;
		this.namespace = namespace;
	}

	/**
	  * <p>
	  * Filter out the Elements with the supplied name and Namespace.
	  * </p>
	  *
	  * @param name   The name of the Attribute.
	  * @param namespace The namespace the Element lives in.
	  */
	public AttributeFilter(String name, Namespace namespace) {
		this.name = name;
		this.namespace = namespace;
	}

	/**
	 * @see Filter#canAdd(Object)
	 */
	public boolean canAdd(Object obj) {
		return (obj instanceof Element);
	}

	/**
	 * @see Filter#canRemove(Object)
	 */
	public boolean canRemove(Object obj) {
		return (obj instanceof Element);
	}

	/**
	 * @see Filter#matches(Object)
	 */
	public boolean matches(Object obj) {
		if (obj instanceof Element) {
			Element element = (Element) obj;

			
			Attribute a =  ( (namespace == null ) ?
element.getAttribute(name) : element.getAttribute(name, namespace));
			if ( a != null ){
				if (value == null)
					return true;
				else
					return value.equals(a.getValue());

			}
		}
		return false;
	}
}


ContentFilter.java -->

package com.tecnomen.jdom.filter;

import org.jdom.Element;
import org.jdom.filter.Filter;

/**
 *  Copyright (C) 2001, 2002 Tecnomen Oyj & Teemu Tingander.
 *  All rights reserved.
 * 
 * 	This code is provided as a contribution to JDOM
project(www.jdom.org) 
 *
 *	@author	Teemu Tingander (teemu.tingander at tecnomen.fi)
 */
public class ContentFilter implements Filter {

	private Filter filter = null;

	public ContentFilter(Filter filter){
		this.filter = filter;
	}
	
	/**
	 * @see Filter#canAdd(Object)
	 */
	public boolean canAdd(Object obj) {
		return (obj instanceof Element);
	}

	/**
	 * @see Filter#canRemove(Object)
	 */
	public boolean canRemove(Object obj) {
		return (obj instanceof Element);
	}

	/**
	 * @see Filter#matches(Object)
	 */
	public boolean matches(Object obj) {
		if (obj instanceof Element) {
			Element element = (Element) obj;

			return (! element.getContent(filter).isEmpty() );

		}		
		return false;
	}

}

NotFilter.java -->

package com.tecnomen.jdom.filter;

import org.jdom.filter.Filter;

/**
 *  Copyright (C) 2001, 2002 Tecnomen Oyj & Teemu Tingander.
 *  All rights reserved.
 * 
 * 	This code is provided as a contribution to JDOM
project(www.jdom.org) 
 *
 *	@author	Teemu Tingander (teemu.tingander at tecnomen.fi)
 */
public final class NotFilter implements Filter {

	final Filter filter;

	public NotFilter(Filter filter){
		this.filter = filter;		
	}

	/**
	 * @see Filter#canAdd(Object)
	 */
	public boolean canAdd(Object obj) {
		return filter.canAdd(obj);
	}

	/**
	 * @see Filter#canRemove(Object)
	 */
	public boolean canRemove(Object obj) {
		return filter.canRemove(obj);
	}

	/**
	 * @see Filter#matches(Object)
	 */
	public boolean matches(Object obj) {
		return ! filter.matches(obj);
	}

}

OrFilter -->

package com.tecnomen.jdom.filter;

import java.util.Iterator;
import java.util.LinkedList;

import org.jdom.filter.Filter;

/**
 *  Copyright (C) 2001, 2002 Tecnomen Oyj & Teemu Tingander.
 *  All rights reserved.
 * 
 * 	This code is provided as a contribution to JDOM
project(www.jdom.org) 
 *
 *	@author	Teemu Tingander (teemu.tingander at tecnomen.fi)
 */
public class OrFilter implements Filter {

	private LinkedList filters = new LinkedList();			

	public OrFilter(){
	}		

	public OrFilter(Filter f1){
		this.filters.add(f1);		
	}		

	public OrFilter(Filter f1, Filter f2){
		this.filters.add(f1);	
		this.filters.add(f2);
	}	
	
			
	public OrFilter or(Filter filter){
		this.filters.add(filter);
		return this;
	}
	
	/**
	 * @see Filter#canAdd(Object)
	 */
	public boolean canAdd(Object obj) {
		Filter e = null;
		Iterator i = filters.iterator();
		while (i.hasNext()) {
			e = (Filter) i.next();
			if ( e.canAdd(obj) ) return true;
		}
		return false;
	}

	/**
	 * @see Filter#canRemove(Object)
	 */
	public boolean canRemove(Object obj) {
		Filter e = null;
		Iterator i = filters.iterator();
		while (i.hasNext()) {
			e = (Filter) i.next();
			if ( e.canRemove(obj) ) return true;
		}
		return false;
	}

	/**
	 * @see Filter#matches(Object)
	 */
	public boolean matches(Object obj) {
		Filter e = null;
		Iterator i = filters.iterator();
		while (i.hasNext()) {
			e = (Filter) i.next();
			if ( e.matches(obj) ) return true;
		}
		return false;
	}

}

Cheers, hopefully these will be helpful.. They should be quite self
explaning and commentin , but is quiestins raise just ask ( i´ll try to
answer )
	- Teemu




More information about the jdom-interest mailing list