Difference between revisions of "EclipseLink/Release/2.4.0/JAXB RI Extensions/Character Escape Handler"

From Eclipsepedia

Jump to: navigation, search
Line 37: Line 37:
 
m.setProperty(MarshallerProperties.CHARACTER_ESCAPE_HANDLER, new MyHandler());
 
m.setProperty(MarshallerProperties.CHARACTER_ESCAPE_HANDLER, new MyHandler());
 
...
 
...
</source>
 
</div>
 
 
The '''CharacterEscapeHandler''' cannot be null.  If you need to re-set the '''CharacterEscapeHandler''' back to the EclipseLink default, use the following handler class:
 
 
<div style="width:850px">
 
<source lang="java">
 
m.setProperty(org.eclipse.persistence.jaxb.JAXBMarshaller.CHARACTER_ESCAPE_HANDLER,
 
  new org.eclipse.persistence.internal.oxm.record.DefaultCharacterEscapeHandler());
 
 
</source>
 
</source>
 
</div>
 
</div>

Revision as of 15:00, 5 June 2012

Design Documentation: Character Escape Handler

ER 370589

In the current JAXB RI, developed by Sun, there is a series of "proprietary" JAXB extensions which provide advanced functionality outside of the JAXB specification (these extension classes and properties reside in the com.sun.xml.bind package).

The CharacterEscapeHandler interface provided in the Sun JAXB implementation allows users to write their own character escaping code, to be used when marshalling.

This document will outline the design for an EclipseLink equivalent to this extension.


Behaviour

If a custom CharacterEscapeHandler is set, then that class will be responsible for all character escaping during marshal operations.

Custom escape handling is not supported when marshalling to the following targets:

  • javax.xml.stream.XMLStreamWriter
  • javax.xml.stream.XMLEventWriter
  • org.xml.sax.ContentHandler
  • org.w3c.dom.Node


Configuration

Your custom handler should be an implementation of the org.eclipse.persistence.oxm.CharacterEscapeHandler interface.

A custom CharacterEscapeHandler can be specified on a Marshaller using the setProperty() method:

...
JAXBContext ctx = ...
Marshaller m = ctx.createMarshaller();
m.setProperty(MarshallerProperties.CHARACTER_ESCAPE_HANDLER, new MyHandler());
...

Note: EclipseLink also supports Sun's CharacterEscapeHandler property names:

m.setProperty("com.sun.xml.bind.marshaller.CharacterEscapeHandler", new MyHandler());
m.setProperty("com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler", new MyHandler());


Appendix A - Example CharacterEscapeHandler

package example;
 
import java.io.IOException;
import java.io.Writer;
 
import org.eclipse.persistence.oxm.CharacterEscapeHandler;
 
public class MyHandler implements CharacterEscapeHandler {
 
    /**
     * Escape characters inside the buffer and send the output to the Writer.
     * 
     * @exception IOException
     *                In an error condition, IOException can be thrown to stop
     *                the marshalling process.
     */
    public void escape(char[] buf, int start, int len, boolean isAttValue, Writer out) throws IOException {
        for (int i = start; i < start + len; i++) {
            char ch = buf[i];
 
            if (ch == '&') {
                out.write("&amp;");
                continue;
            }
 
            if (ch == '"' && isAttValue) {
                out.write("&quot;");
                continue;
            }
            if (ch == '\'' && isAttValue) {
                out.write("&apos;");
                continue;
            }
 
            // you should handle other characters like < or >
            // ...
 
            if (ch > 0x7F) {
                // escape everything above ASCII to &#xXXXX;
                out.write("&#x");
                out.write(Integer.toHexString(ch));
                out.write(";");
                continue;
            }
 
            // otherwise print normally
            out.write(ch);
        }
    }
}