Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

Line 24: Line 24:
 
Custom escape handling is not supported when marshalling to the following targets:
 
Custom escape handling is not supported when marshalling to the following targets:
  
    * javax.xml.stream.XMLStreamWriter
+
* javax.xml.stream.XMLStreamWriter
    * javax.xml.stream.XMLEventWriter
+
* javax.xml.stream.XMLEventWriter
    * org.xml.sax.ContentHandler
+
* org.xml.sax.ContentHandler
    * org.w3c.dom.Node
+
* org.w3c.dom.Node
 +
 
  
 
= Configuration =  
 
= Configuration =  

Revision as of 12:43, 22 February 2012

Design Documentation: CharacterEscapeHandler

ER 370589

In the current JAXB RI, developed by Sun, there are a series of "proprietary" JAXB extensions that are available to provide advanced JAXB functionality outside of the JAXB spec (these extension classes 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.


Requirements

  • Provide an interface that the user can implement, and use this implementation when escaping characters during marshal.
  • Provide drop-in-replacement support, so that users already using the Sun implementation will not need to change their code when switching to EclipseLink.


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 addProperty() method:

...
JAXBContext ctx = ...
Marshaller m = ctx.createMarshaller();
m.setProperty(org.eclipse.persistence.jaxb.JAXBMarshaller.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 EclipseLinkCustomCharacterEscapeHandler implements CharacterEscapeHandler {
 
    /**
     * Escape characters inside the buffer and send the output to the Writer.
     * 
     * @exception IOException
     *                if something goes wrong, 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];
 
            // you are supposed to do the standard XML character escapes
            // like & ... &amp; < ... &lt; etc
 
            if (ch == '&') {
                out.write("&amp;");
                continue;
            }
 
            if (ch == '"' && isAttValue) {
                // isAttValue is set to true when the marshaller is processing
                // attribute values. Inside attribute values, there are more
                // things you need to escape, usually.
                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);
        }
    }
}


Document History

Date Author Version Description & Notes
120222 Rick Barkhouse 1.00 : First draft

Copyright © Eclipse Foundation, Inc. All Rights Reserved.