I have an architecture where some of my objects have associated jelly scripts which are an XML representation of the object,
I call a method |'writeAsXML(File)'| and it gets written as XML to the file. Essentially, the jelly logic that's embedded in my
java code borrows heavily from the core:file code, but I resisted using the core:file tag for a couple of reasons.
1) By not using the core:file tag, my jelly scripts end up looking more like the XML they produce.
2) My own java code can have hooks into logging and reporting etc.
3) I have some custom tags which allow for the above actions to happen recursively (when jellified objects contain other jellified objects)
and having that code together makes it easier to maintain.
4) This does pretty much the same thing as castor's marshall behavior except I have a lot more control of the final output and it's more
WYSIWYG like.
I've looked closely at the source for core:file and can't see what I might have done differently. But in reading the explanation above,
perhaps I've answered my own question because I have a little more going on behind the scenes than core:file, but I don't modify XMLOutput
in any way after creating it, other than simply passing it around.
Here's an example of one of my 'root' jelly scripts. The wr:embed object calls a similar script for each composed object in a Component-like pattern.
<?xml version="1.0" encoding="UTF-8"?>
<j:jelly trim="true" xmlns:j="jelly:core" xmlns:x="jelly:xml"
xmlns:wr="jelly:com.wrycan.common.jelly.tags.wrycan.WrycanTagLibrary">
<x:element name="book">
<x:attribute name="xmlns">http://xml.classwell.com/clg</x:attribute>
<x:attribute name="xmlns:clg">http://xml.classwell.com/clg</x:attribute>
<x:attribute name="xmlns:xsi">http://www.w3.org/2001/XMLSchema-instance</x:attribute>
<x:attribute name="xsi:schemaLocation">${this.getSchemaLocation()}</x:attribute>
<x:attribute name="id">${this.getId()}</x:attribute>
<meta>
<wr:embed object="${this.getMetaSystem()}" />
<wr:embed object="${this.getMetaContent()}" />
<wr:embed object="${this.getMetaEducation()}" />
</meta>
<bookName>${this.getBookName()}</bookName>
<j:forEach items="${this.getSectionsList()}" var="section">
<wr:embed object="${section}" />
</j:forEach>
</x:element>
</j:jelly>
Christopher Farnham Senior Consultant at Wrycan, Inc.
[EMAIL PROTECTED] http://www.wrycan.com
Paul Libbrecht wrote:
Christopher,
Doesn't the core:file tag do this already ?
Paul
Christopher W. Farnham wrote:
Is there an elegant way to make sure the XML declaration is included when writing to XMOutput?
I have a factory method a la the core FileTag where I was calling |outputFormat.setSuppressDeclaration(false);
|This didn't work (there was no declaration at the top of my XML files). Finally I ended up hacking
my factory method to do this:
| OutputFormat format = new OutputFormat();
//we can use this line if we want results pretty printed
//format = OutputFormat.createPrettyPrint();
if (xmlDeclaration) {
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>".toCharArray());
}
format.setEncoding( "UTF8" );
format.setSuppressDeclaration(true);
final XMLWriter xmlWriter = new XMLWriter(writer, format);
xmlWriter.setEscapeText(false);
XMLOutput returnOutput = new XMLOutput() { public void close() throws IOException { xmlWriter.close(); } }; returnOutput.setContentHandler(xmlWriter); returnOutput.setLexicalHandler(xmlWriter); return returnOutput;
|This, of course, is not the best way to do things, but it works. Did I miss something?
Thanks,
Christopher Farnham Senior Consultant at Wrycan, Inc.
[EMAIL PROTECTED] http://www.wrycan.com
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
