Hello!

I'm using Apache CXF to implement some WebServices at server side. I have
to implement a WebService that returns a string (Holder<String>) with some
values separated by tab character. Apache CXF encodes character tab as a
tab, but our client (that can not be change...) doesn't accept it and only
read tabs encoded as (&#9;).

So I tried to simply make a replaceAll on the string to change \t for &#9;
, but an escapeHandler on Marshaller changes it to &amp;#9; .

Then I tried to create a customCharacterEscapeHandler and set in the
marshall com.sun.xml.bind.marshaller.CharacterEscapeHandler property.

<jaxws:endpoint
  id="wsContainer"
  implementor="com.xxxxxx.xxxxxx.xxxx.webServices.impl.EOSWebServiceImpl"
  address="/ws" >

  <jaxws:dataBinding>
    <bean class="org.apache.cxf.jaxb.JAXBDataBinding">
        <property name="marshallerProperties">
         <map>
         <entry key="jaxb.encoding" value="UTF-8"/>   <!-- XML ENCODED Ej:
CR= &#9;-->
         <entry key="com.sun.xml.bind.marshaller.CharacterEscapeHandler"
value-ref="customCharacterEscapeHandler"/>
         </map>
        </property>
      </bean>
      </jaxws:dataBinding>

</jaxws:endpoint>


And my customCharacterEscapeHandler is:


public CustomCharacterEscapeHandler(String charsetName) {
        this.encoder = Charset.forName(charsetName).newEncoder();
    }

    public void escape(char[] ch, int start, int length, boolean isAttVal,
Writer out) throws IOException {
        int limit = start+length;
        for (int i = start; i < limit; i++) {
            switch (ch[i]) {
            case SoapUtils.SOAP_FIELD_SEP:  //Tab Character
                out.write("&#9;");
                break;
            case SoapUtils.SOAP_RSEP: // CR Character
                out.write("&#13;");
                break;
            case '&':
                out.write("&amp;");
                break;
            case '<':
                out.write("&lt;");
                break;
            case '>':
                out.write("&gt;");
                break;
            case '\"':
                if (isAttVal) {
                    out.write("&quot;");
                } else {
                    out.write('\"');
                }
                break;
            default:
                if( encoder.canEncode(ch[i]) ) {
                    out.write(ch[i]);
                } else {
                    out.write("&#");
                    out.write(Integer.toString(ch[i]));
                    out.write(';');
                }
            }
        }
    }


This should work but it doesn't, because this escapeHandler replace
correctly the tab character, but some other escapeHandler is running after
this and replaces again de '&' character, so I have &amp;#9; again ...


What should I do to get in the client a tab encoded as &#9; ??


PS: I'm using Apache CXF 2.5.2

Reply via email to