[jdom-interest] [Fwd: Custom JDOM Output Nightmare]

Bradley S. Huffman hip at cs.okstate.edu
Mon Sep 20 19:34:00 PDT 2004


> I am processing XHTML and I need to be able to control what empty tags
> expand and which don't on a tag name basis.  No problem I figure, I'll
> just subclass XMLOutputter add my new bit of functionality and away I
> go.  Hmmm, well I really ought to add the formatting info to the Format
> class. OK I'll just subclass that too. Ooops, Format has only a private
> constructor...no subclassing.  Why wouldn't you allow someone to extent
> Format?  It seems like a natural thing to want to do.  Well OK, I'll
> just add the info to XMLOutputter.  It's not as well encapsulated, but
> what the hey.  Ooops, XMLOutputter.printElement() depends on a bunch of
> private methods.  That certainly makes subclassing a pain!  Darn.  OK,
> well I guess I will just copy and repackage XMLOutputter and then patch
> the copy.  Darn, XMLOutputter directly accesses Format members, (bad
> form), which have package level access.  Shoot!  OK, I guess I will copy
> and repackage Format as well.  Oh, and just for good measure,
> XMLOutputter also uses NamespaceStack, which of course, is a package
> access class.  Copy and repackage as well.
> 
> By the time I had figured all the above out, my 5 minute patch had
> turned into a 2 hour stressful headache.  I just couldn't believe that
> JDOM, which claims to be making my life easier would really make it this
> hard to create slightly customized output.  Please tell me that I am all
> screwed up and that there really is a much easier way.  I know I could
> have just hacked the JDOM source directly, but I just couldn't bring
> myself to do something that evil and it would surely have bit me the
> next time I went to update my version of JDOM.

Would subclassing XMLOutputter and doing something like this work?

void printElement(Writer out, Element element, ...) {
    boolean expandingEmptyElements = currentFormat.getExpandEmptyElements();

    if (shouldExpand(element)) {
        currentFormat.setExpandEmptyElements(true);
    }
    else {
        currentFormat.setExpandEmptyElements(false);
    }

    currentFormat.setExpandEmptyElements(expandingEmptyElements);
}

private boolean shouldExpand(Element e) {
    // Determine if element should be expanded if empty
}

Brad


More information about the jdom-interest mailing list