Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4121039

Improper handling of special HTML characters (characters starting with &)

XMLWordPrintable

    • swing1.1
    • x86
    • windows_nt

      ==========================================================================
      carlos.lucasius@canada 1998-03-19:
      Bug reported by Corel (licensee) for JFC1.1 using JDK1.1.4 on WINNT4.0.
      Corel: "Very critical bug for our product - requires immediate attention."

      We have fixed this in your code already. Please get them sync in to your
      source code too.

      First in the file HTMLWriter.java I added a line to the method writeChar to
      output a semi-colon after we output a special character. I marked modified
      code with: "// !!!"

      Original code snippit in writeChar(Writer w, char c):

      if ((c == '\n') || (c == '\t') || (c == '\r'))
          w.write(c);
      else if ((c < ' ') || (c > 127)) {
          w.write("&#");
          w.write(String.valueOf((int)c));
      }

      Modified code snippit:

      if ((c == '\n') || (c == '\t') || (c == '\r'))
          w.write(c);
      else if ((c < ' ') || (c > 127)) {
          w.write("&#");
          w.write(String.valueOf((int)c));
          w.write(";"); // !!!
      }

      I then had to modify 'xlateSpecialChars' in the file HTMLDocument.java. I had to change an index check so that the method didn't exit if
      the ampersand was in the
      first position. I also had to store the string returned from 'replaceSpecialChar' so it could be returned.

      private String xlateSpecialChars(String str) {
         String result = str; // !!!
          int index = 0;

          // Look for the '&' character which designates special html chars.
          index = result.indexOf('&'); // !!!
          int nLength = result.length(); // !!!
          while ((index < nLength) && (index >= 0)) { // !!!
              // The special character description ends with ';' so find the
              // beginning and ending indexes of the special character.
              int jindex = 0;
              jindex = result.indexOf(';', index); // !!!
              if (jindex < 0) {
                  return(null);
              }
              // If a '#' character follows '&' then the special character is
              // being represented by an ascii numeric code. Replace the
              // code with the special character itself and return the string.
              if (result.charAt(index+1) == '#') { // !!!
                  String sstr = result.substring(index+2, jindex); // !!!
                  int num = Integer.valueOf(sstr).intValue();
                  result = replaceSpecialChar(result, index, jindex, (char)num);// !!!
              }
              // Otherwise, look up the character name (e.g. &quot) in the
              // SpecialCharTable. Replace the code with the special
              // character itself and return the string.
              else {
                  String sstr = result.substring(index+1, jindex); // !!!
                  char lookup = ' ';
                  boolean found = false;
                  try {
                      lookup = specTable.getSymbol(sstr);
                      found = true;
                  } catch (HTMLException e) {
                      ;
                  }
                  if (found) {
                      result = replaceSpecialChar(result, index,jindex, lookup);// !!!
                  }
              }
              index = result.indexOf('&', index + 1); // !!!
          }
          return(result);
      }

            tprinzing Tim Prinzing (Inactive)
            clucasius Carlos Lucasius (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: