Uploaded image for project: 'JBoss BRMS Platform'
  1. JBoss BRMS Platform
  2. RHBRMS-2449

XStream marshaller can't be configured with NameCoder

This issue belongs to an archived project. You can view it, but you can't modify it. Learn more

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Done
    • Icon: Major Major
    • 6.4.0
    • 6.3.0.GA
    • Kie-Server
    • None
    • ER2

      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)
              rhn-support-agiertli Anton Giertli
              Archiver:
              rhn-support-ceverson Clark Everson
              Karel Suta Karel Suta
              Karel Suta Karel Suta

                Created:
                Updated:
                Resolved:
                Archived: