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

Jools jools at jools.org
Wed Apr 11 11:52:30 PDT 2001


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;
    }



More information about the jdom-interest mailing list