2 Replies Latest reply: Aug 19, 2009 5:30 AM by 843810 RSS

    Mixed Arabic/Latin text confuson in a single String

    843810
      Hi all,

      I hope someone knows the answer to this issue!

      I've got an application in Java which calls another app via a stored procedure and callable statement, passing various parameters across. One of these parameters is a buffer containing transaction information.

      Now we're working on an Arabic language proof of concept, and when I create this buffer containing both Arabic, Latin and numeric data, the order of the string is messed up.

      E.g.

      The data may be (in order):

      I
      AED
      17
      شارة النقاط
      1.7
      101010
      Test seventeen
      17D

      This appears in the buffer (with some padding) as:
      IAED17شارة النقاط 1.7 101010Test seventeen 17D

      As you can see, the order has changed - the numeric fields appear to have been included with the Arabic text.

      This causes the API we're calling to break, as you can imagine - expected fields are not appearing in the right place in the buffer.

      The string for the buffer is being built as follows:

      StringBuffer paramBuffer = new StringBuffer(BUFFER_INITIAL_CAPACITY);
      while (iterator.hasNext()) {
      String fieldName = iterator.next().toString();
      // Get Object using field name from Vector.
      FieldApi field = (FieldApi)fields.get(fieldName);

      tempCtr = tempCtr + field.getLength();
      if (field.getLength() == 0) {
      break; // We have reached end of the fields
      }

      //Add each formatted field to param.
      paramBuffer.append(field.getString());
      }

      Now, I suspect that I could get around this by constructing a byte array and using a setBytes method on my callableStatement. However, the set up of my JDBC connection means that I can't use setBytes (translateBinary is set to true, giving a data type mismatch exception), and this set up is something I can't change.

      I'm sure this is a problem with the fact that Arabic languages are right to left rather than left to right, and the String I create attempts to format it in this manner (and gets it a little confused when a numeral is next to Arabic text). Is there a way I can tell the String to leave things exactly as they're added, rather than attempting to format them?

      Any help would be appreciated! :-)

      Cheers,

      Dan

      P.S. I'm aware that what is displayed and what is actually there may not be the same, but when I inspect the contents of the buffer in the stored procedure, the order is indeed wrong.
        • 1. Re: Mixed Arabic/Latin text confuson in a single String
          800025
          Hi Dan,

          Some time ago a faced a similar problem whenn preparing text for display. I solved it by wrapping the text in a html table, thus forcing the elements to remain in the exact order I wnated them in. Something as follows:
          import java.awt.BorderLayout;
          
          import javax.swing.JFrame;
          import javax.swing.JLabel;
          import javax.swing.SwingUtilities;
          
          public class MixedArabic {
          
              public static void main(String[] args) {
               SwingUtilities.invokeLater(new Runnable() {
          
                   @Override
                   public void run() {
                    new MixedArabic().createGUI();
                   }
               });
              }
          
              private String[] parts = new String[] { "AED", "17",
                   "\u0634\u0627\u0631\u0629 \u0627\u0644\u0646\u0642\u0627\u0637",
                   "1.7", "101010", "Test seventeen", "17D" };
          
              private void createGUI() {
               JFrame frame = new JFrame();
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.add(new JLabel(createString(parts, false)),
                    BorderLayout.PAGE_START);
               frame.add(new JLabel(createString(parts, true)), BorderLayout.PAGE_END);
               frame.pack();
               frame.setLocationRelativeTo(null);
               frame.setVisible(true);
              }
          
              private String createString(String[] parts, boolean formatted) {
               StringBuilder sb = new StringBuilder();
               if (formatted) {
                   sb.append("<html><table><tr>");
               }
               for (String part : parts) {
                   sb.append(formatted ? "<td>" : ' ');
                   sb.append(part);
                   sb.append(formatted ? "</td>" : ' ');
               }
               if (formatted) {
                   sb.append("</tr></table></html>");
               }
               return sb.toString();
              }
          
          }
          Piet
          • 2. Re: Mixed Arabic/Latin text confuson in a single String
            843810
            Hi Piet,

            Thanks for your reply. Your solution works as the table prevents Arabic characters running into the Latin numerals.

            However, my form and display work correctly - my error is not at this point. It's when I take the data from the form and construct a buffer from it that I have problems - at this point I have no characters between the sections of data.

            I've been in discussion with IBM regarding this issue, and they suspect there may be a problem with their JDBC driver - even when the String displays correctly before I submit it via JDBC, it's messed up on the other side of the connection.

            However, thanks for your input - if I can work out how to assign Duke stars, I'll send some your way to thank you for your response.

            Kind regards,

            Dan