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.
Difference between revisions of "EclipseLink/Release/2.4.0/JAXB RI Extensions/Character Escape Handler"
m (EclipseLink/Development/370589 moved to EclipseLink/Release/2.4.0/JAXB RI Extensions/Character Escape Handler) |
|||
(13 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<div style="margin:5px;float:right;border:1px solid #000000;padding:5px">__TOC__</div> | <div style="margin:5px;float:right;border:1px solid #000000;padding:5px">__TOC__</div> | ||
− | = | + | = Character Escape Handler = |
− | + | 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). | |
− | + | ||
− | In the current JAXB RI, developed by Sun, there | + | |
The '''CharacterEscapeHandler''' interface provided in the Sun JAXB implementation allows users to write their own character escaping code, to be used when marshalling. | The '''CharacterEscapeHandler''' interface provided in the Sun JAXB implementation allows users to write their own character escaping code, to be used when marshalling. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Line 24: | Line 14: | ||
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.XMLEventWriter | |
− | + | * org.xml.sax.ContentHandler | |
− | + | * org.w3c.dom.Node | |
+ | |||
= Configuration = | = Configuration = | ||
Line 33: | Line 24: | ||
Your custom handler should be an implementation of the '''org.eclipse.persistence.oxm.CharacterEscapeHandler''' interface. | 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 <tt> | + | A custom '''CharacterEscapeHandler''' can be specified on a '''Marshaller''' using the <tt>setProperty()</tt> method: |
<div style="width:850px"> | <div style="width:850px"> | ||
Line 40: | Line 31: | ||
JAXBContext ctx = ... | JAXBContext ctx = ... | ||
Marshaller m = ctx.createMarshaller(); | Marshaller m = ctx.createMarshaller(); | ||
− | m.setProperty( | + | m.setProperty(MarshallerProperties.CHARACTER_ESCAPE_HANDLER, new MyHandler()); |
... | ... | ||
</source> | </source> | ||
Line 57: | Line 48: | ||
= Appendix A - Example CharacterEscapeHandler = | = Appendix A - Example CharacterEscapeHandler = | ||
− | <div style="width: | + | <div style="width:900px"> |
<source lang="java"> | <source lang="java"> | ||
package example; | package example; | ||
Line 66: | Line 57: | ||
import org.eclipse.persistence.oxm.CharacterEscapeHandler; | import org.eclipse.persistence.oxm.CharacterEscapeHandler; | ||
− | public class | + | public class MyHandler implements CharacterEscapeHandler { |
/** | /** | ||
Line 72: | Line 63: | ||
* | * | ||
* @exception IOException | * @exception IOException | ||
− | * | + | * In an error condition, IOException can be thrown to stop |
* the marshalling process. | * the marshalling process. | ||
*/ | */ | ||
Line 78: | Line 69: | ||
for (int i = start; i < start + len; i++) { | for (int i = start; i < start + len; i++) { | ||
char ch = buf[i]; | char ch = buf[i]; | ||
− | |||
− | |||
− | |||
if (ch == '&') { | if (ch == '&') { | ||
Line 88: | Line 76: | ||
if (ch == '"' && isAttValue) { | if (ch == '"' && isAttValue) { | ||
− | |||
− | |||
− | |||
out.write("""); | out.write("""); | ||
continue; | continue; | ||
Line 100: | Line 85: | ||
// you should handle other characters like < or > | // you should handle other characters like < or > | ||
+ | // ... | ||
if (ch > 0x7F) { | if (ch > 0x7F) { | ||
Line 116: | Line 102: | ||
</source> | </source> | ||
</div> | </div> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Latest revision as of 11:36, 18 June 2012
Character Escape Handler
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.
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("&"); continue; } if (ch == '"' && isAttValue) { out.write("""); continue; } if (ch == '\'' && isAttValue) { out.write("'"); 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); } } }