Uploaded image for project: 'jBPM'
  1. jBPM
  2. JBPM-5310

XStream marshaller can't be configured with NameCoder

XMLWordPrintable

    • NEW
    • NEW

      Kie-Server XStream marshaller can't be configured with NameCoder.

      Why is this an issue? One real example can be this:

      Imagine a java model including "_" characters in the attribute names.
      It means that the resultant XML would include these characters too.
      However, by default XStream uses "" char as an escape char, so it will replace this char with "_" (double underscore).

      This may be undesired from a business perspective, if a precise XML format is required and can't be changed.

      If you'd have a full XStream API at hand, this can be changed with plugging the NameCoder implementation.

      In Kie-Server there is no API which would allow to configure the NameCoder.

      See the code example below which demonstrates it in detail:

      import org.kie.server.api.marshalling.Marshaller;
      import org.kie.server.api.marshalling.MarshallerFactory;
      import org.kie.server.api.marshalling.MarshallingFormat;
      
      import com.thoughtworks.xstream.XStream;
      import com.thoughtworks.xstream.io.xml.DomDriver;
      import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
      
      public class RESTClient {
      
          public static void main(String args[]) {
      
      // 1) To avoid __ in the generated XML, we can configure XStream with NameCoder
               XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("_-", "_"))); 
               String xml = xstream.toXML(new Message("anton"));
               System.out.println("configured XSTREAM:\n"+xml);
      
      // 2) The original Kie-Server marshaller can't be configured with NameCoder, therefore, the generated XML will include double underscore "__" which may not be desired
               Marshaller marshaller = MarshallerFactory.getMarshaller(MarshallingFormat.XSTREAM, RESTClient.class.getClassLoader());
              String xml2 = marshaller.marshall(new Message("anton"));
              System.out.println("Unconfigured XSTREAM:\n"+xml2);
      }
      }
      
      class Message {
          private String name_another;
      
          public Message(String name) {
              this.name_another = name;
          }
      }
      

      The code above produces following output:

      configured XSTREAM:
      <org.redhat.gss.Message>
        <name_another>anton</name_another>
      </org.redhat.gss.Message>
      Unconfigured XSTREAM:
      <org.redhat.gss.Message>
        <name__another>anton</name__another>
      </org.redhat.gss.Message>
      

              swiderski.maciej Maciej Swiderski (Inactive)
              swiderski.maciej Maciej Swiderski (Inactive)
              Karel Suta Karel Suta
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: