[jdom-interest] non-deterministic behaviour [eg]

Steven D. Keens skeens at planetfred.com
Wed Apr 11 13:23:36 PDT 2001


There's another solution, prior to clearing the content
you can iterate through the new list of mixed content testing
each object.  Then you throw the exception only if an
object is of the wrong type.  If all of the new mixed content
is OK then you can go about adding them into the Element's
content (but now you don't have to test their types anymore).
Obviously, this will slow things down because you have to
iterate through the new mixed content twice but it will reduce
the memory footprint.  The question here is what's more important
memory or speed?

One benefit of the pretest is that the Element is always
left in a proper state.

Another question that comes to mind with the pretest is
that you may not have to add the new mixed content to the
Element's content.  You could just replace the Element's
content with the new mixed content.

Here's an implementation:

public Element setMixedContent( List mixedContent ) {

    if ( ! isAllValidContent( mixedContent ) {
        throw new IllegalAddException(
            "An Element may directly contain only objects of type " +
            "String, Element, Comment, CDATA, Entity, and " +
            "ProcessingInstruction: " + obj.getClass().getName() +
            " is not allowed" );
    }

    if ( content != null ) {
        content.clear();
    }

    // Can this be done, replacing the content wholesale with mixedContent?
    content = mixedContent;

    //  If not then this commented code would replace it the above line.
    //  for (Iterator i = mixedContent.iterator(); i.hasNext(); ) {
    //      Object obj = i.next();
    //      content.add( obj );
    //  }

    return this;
}

public boolean isAllValidContent( List mixedContent ) {
    for (Iterator i = mixedContent.iterator(); i.hasNext(); ) {
        Object obj = i.next();
        if ( !( obj instanceof Element ||
		obj instanceof String ||
		obj instanceof Comment ||
		obj instanceof ProcessingInstruction ||
		obj instanceof CDATA ||
		obj instanceof Entity ) )
	{
	    return false;
	}
    }
    return true;
}


>-----Original Message-----
>From: jdom-interest-admin at jdom.org
>[mailto:jdom-interest-admin at jdom.org]On Behalf Of Jools
>Sent: Wednesday, April 11, 2001 14:53
>To: jdom-interest at jdom.org
>Subject: [jdom-interest] non-deterministic behaviour [eg]
>
>
>
>Hi all
>
>I'd like a little feedback as to what state we 
>shoukd leave a JDOM object, should an update operation 
>partially fail.
>
>Below is the code for setMixedContent. I have a 
>little concern with the exception thrown at the bottom
>of the for(;;) loop, please take a look;
>
>My concern is this;
>
>When setMixedContent is called we take it for granted
>that we will successfully complete the operation and
>the Element will contain all the right data, however
>should the operation fail the original data will have
>been lost as we clear the list at the start of the
>operation.
>
>My worry is that this is non-deterministic behaviour
>as an Element could end up in a bit of mess should 
>the above senario take place.
>
>Would it be better to just ignore Objects which are
>not JDOM objects ? Or do we need to keep a copy
>of the original data before we commit to the new
>data, should some of it be non-JDOM classes ?
>
>--Jools 
>
><-- CODE -->
>
>    public Element setMixedContent(List mixedContent) {
>        if (content != null) {
>            content.clear();
>        } else {
>            content = new LinkedList();
>        }
>
>        if (mixedContent == null) {
>            return this;
>        }
>
>        for (Iterator i = mixedContent.iterator(); i.hasNext(); ) {
>            Object obj = i.next();
>            if (obj instanceof Element) {
>                addContent((Element)obj);
>            }
>            else if (obj instanceof String) {
>                addContent((String)obj);
>            }
>            else if (obj instanceof Comment) {
>                addContent((Comment)obj);
>            }
>            else if (obj instanceof ProcessingInstruction) {
>                addContent((ProcessingInstruction)obj);
>            }
>            else if (obj instanceof CDATA) {
>                addContent((CDATA)obj);
>            }
>            else if (obj instanceof Entity) {
>                addContent((Entity)obj);
>            }
>            else {
>-->                throw new IllegalAddException(
>                    "An Element may directly contain only objects of
>type " +
>                    "String, Element, Comment, CDATA, Entity, and " + 
>                    "ProcessingInstruction: " + obj.getClass().getName()
>+
>                    " is not allowed");
>            }
>        }
>
>        return this;
>    }
>_______________________________________________
>To control your jdom-interest membership:
>http://lists.denveronline.net/mailman/options/jdom-interest/youradd
r at yourhost.com




More information about the jdom-interest mailing list