Uploaded image for project: 'Bugzilla for Java'
  1. Bugzilla for Java
  2. BFJ-19

UTF-8 Cleanup of XML response

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Normal
    • Resolution: Fixed
    • Affects Version/s: 1.4.0
    • Fix Version/s: 2.0.0
    • Labels:
      None

      Description

      Reported by Deepak:

      HttpBugzillaSession, lines 579 and following should change to:

      HttpBugzillaSession.java
      // This class itself will take care of the elements
                                      xmlReader.setContentHandler(this);
       
                                      StringWriter writer = new StringWriter();
                                      IOUtils.copy(xmlStream, writer, "UTF-8");
                                      String xmlString = writer.toString();
                                      String escapedXML = stripNonValidXMLCharacters(xmlString);
       
                                      xmlReader.parse(new InputSource(new StringReader(escapedXML)));
                                      if (getLog().isTraceEnabled())
                                              getLog().trace("XML file completed");
                              } catch (IOException e) {
                                      getLog().error("Error while retrieving Bugzilla XML response:",
                                                      e);
                              } catch (ParserConfigurationException e) {
                                      getLog().error("SAXParser not configured:", e);
                              } catch (SAXException e) {
                                      if (e.getMessage().indexOf("invalid XML character") >= 0) {
                                              getLog().error(
                                                              "The XML file received contains illegal characters. This is not a B4J error but a Bugzilla problem. Sorry",
                                                              e);
                                      } else {
                                              getLog().error(
                                                              "Error while parsing Bugzilla XML response:", e);
                                      }
                              }
                      }
       
                      /**
                       * This method cleans all the invalid characters in the XML and makes it
                       * conform to XML 1.0 standard
                       *
                       * @param in
                       *            The XML as String whose in-valid characters we want to
                       *            remove.
                       * @return The XML as String, stripped of all invalid characters
                       */
                      private String stripNonValidXMLCharacters(String in) {
                              StringBuffer out = new StringBuffer(); // Used to hold the output.
                              char current; // Used to reference the current character.
       
                              if (in == null || ("".equals(in)))
                                      return ""; // vacancy test.
                              for (int i = 0; i < in.length(); i++) {
                                      current = in.charAt(i); // NOTE: No IndexOutOfBoundsException
                                                                                      // caught here; it should not happen.
                                      if ((current == 0x9) || (current == 0xA) || (current == 0xD)
                                                      || ((current >= 0x20) && (current <= 0xD7FF))
                                                      || ((current >= 0xE000) && (current <= 0xFFFD))
                                                      || ((current >= 0x10000) && (current <= 0x10FFFF)))
                                              out.append(current);
                              }
                              return out.toString();
                      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              ralph Ralph Schuster
              Reporter:
              ralph Ralph Schuster
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: