paulplee (4) [Avatar] Offline
#1
Hi,

After reading your excellent book I came away with the understanding that xdoclet can generate files (code or whatnot) based on tags (like your Factory Template in Ch12.2.2 for example). If I just want to copy a bunch of text into the generated files, I'll have to use a seperate merge file.

My question is: can I keep everything in the same source file, rather than using seperate merge files?

As a concrete example I'll spin off from the aforementioned Factory Template example.

Suppose that in addition to just creating a skeleton Factory class, I have a special function, widgetFoobar(). To me it seems that the best way to do it is to actually write the code for the method in the java file, then have the xdt file look for specially the tagged method, and copy the entire method to the generated file. That way we have one place for everything.

Unless I am mistaken (which I can be, knowing myself), the only way to do this in xdoclet is to use seperate merge files, which is not ideal in this case as the programmer now has to remember to maintain yet another file.

Is what I mentioned possible? If so how? I apologize beforehand if I am missing something glaringly simple - I am still overwhemed by xdoclet smilie

Thanks in advance,
Paul
orb (16) [Avatar] Offline
#2
Re: Can I 'merge' code using the source file instead of merge files?
Perhaps you can mark the code in the other class with a special marker. ("///BEGIN" and "//END", for example) Then you can use a script to extract the sections out and "generate" the merge file. I don't know that I would recommend this, but it would certainyl meet your needs.
orb (16) [Avatar] Offline
#3
Re: Can I 'merge' code using the source file instead of merge files?
I guess I should also say what I think a more appropriate solution is. In general, I would recommend having your generated classes extend some base class to get common functionality wherever possible. Where standard java re-use mechanism exist, it is certainly better to make use of them.
paulplee (4) [Avatar] Offline
#4
Re: Can I 'merge' code using the source file instead of merge files?
Hi Norman, thanks for replying so quickly!

Perhaps I should tell you what I am trying to do: I am tasked with desiging a code generation platform for programmers who have never heard of xdoclet, so my goal is to make the resulting platform require as little trainning as possible.

I understand the idea and benefit of putting the common methods into a parent class, but that's slightly different from my problem. What doesn't sit well with me are the special, one-time methods that the final classes have.

Of course it would be dumb to write the one-time methods into the generated shell, which is bound to be regenerated. So now the programmer will have to maintain 2 files: the .java file that has all the tags to create the skeleton code (including the methods), and a merge file to contain the meat of the methods.

Wouldn't that be conflicting the philosophy of "1 place for all info"?

As for the ///BEGIN ///END, I thought about that too. What I don't understand is: we already have <XDtMethod:forAllMethods/> and <XDtMethod:ifHasMethodTag/>. Can't we do something like this in the xdt file:

<XDtMethod:forAllMethods>
<XDtMethod:currentMethodContent />
</XDtMethod:forAllMethods>

This would be just like the ///BEGIN ///END solution you proposed but it would be more "legal", and this is what I am striving for for my fellow newbie programmers. Surely it is more natural to put the implementation code into a .java file with tags, then a seperate merge file?

I am sure all xdoclet users faces this issue everyday. What are some best-practice? Any pointers would be greatly appreciated!

Paul
orb (16) [Avatar] Offline
#5
Re: Can I 'merge' code using the source file instead of merge files?
XDoclet just doesn't work that way. You might want to consider contributing a tag that does that. Merge files are simple and they work well. They don't require you add code where it doesn't belong just to drive the generator. I'm not saying your requirements here are bad, but I am saying the merge files work well for most people. Most people need to generate code that varies each time and need the the merge files. (which are full fledged XDoclet templates) When they don't need that, Java provided re-use mechanisms usually work best.

But, I could be wrong. You might try pinging the XDoclet mailing list and seeing if someone there has run into anyone wanting this before.
paulplee (4) [Avatar] Offline
#6
Re: Can I 'merge' code using the source file instead of merge files?
I can definitely see the power of the merge files. Its just that as a newbie myself and thinking for other newbie users, for plain old java coding, I think ONE .java file is better than TWO in different directories (and they may not even be called *.java).

Thank you very much for taking your time in answering my questions though. Now I know I actually do understand the book and I now feel confident enough to brave the xdoclet mailing list!

I might actually end up contributing a tag as you suggested. I'll let you know here if that happens.

Thanks again!

Paul
habuma (279) [Avatar] Offline
#7
Re: Can I 'merge' code using the source file instead of merge files?
I just caught up on this thread and had a thought concerning the <XDtMethod:currentMethodContent /> tag that you proposed.

It's been awhile since I've written a custom tag, but if my memory serves correctly, the tag can only work with data given to it by XJavadoc, which (in the case of methods) includes the method signature and any doclet tags and attributes placed on the method. It does *not* include the method body itself.

Norm: Correct me if I'm wrong, but I don't think it will be possible to write a custom XDT tag that includes the current method's content.
orb (16) [Avatar] Offline
#8
Re: Can I 'merge' code using the source file instead of merge files?
Well, you might have to extend xjavadoc. I guessed that it might have some functionality for storing code because I'm told regular javadoc can output source code in the javadoc HTML type pages. But I don't know this. If it can't, you'd have to add that functionality. Either way, it's a lot of work, but the best solution is for him to jump into the code and see what he can do.
paulplee (4) [Avatar] Offline
#9
Re: Can I 'merge' code using the source file instead of merge files?
Hi Craig,

You are probably right. I have never written a custom tag for xdoclet before - hell I just picked up your book 2 weeks ago! smilie

Perhaps it can't be done without modifying xdoclet sources. I don't know. I am about to post to the xdoclet mailing lists to see if I am off or something.

But while I get you here Craig, what do you think of my question? Are merge files the best practice for generating specific implementations?

It's just that I sometimes work with less motivated programmers, and I know they'll grumble that I am further changing their way of life by breaking their java files into several places (.xdt, .java and merge). If too many programmers think code generation is more hassle than the old fashioned way, I won't get management buy in.

Thanks!

Paul