You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by mr...@apache.org on 2004/03/04 20:27:13 UTC

cvs commit: xml-xerces/java/src/org/apache/xerces/impl/io MalformedByteSequenceException.java ASCIIReader.java UTF8Reader.java

mrglavas    2004/03/04 11:27:13

  Modified:    java/src/org/apache/xerces/impl/io ASCIIReader.java
                        UTF8Reader.java
  Added:       java/src/org/apache/xerces/impl/io
                        MalformedByteSequenceException.java
  Log:
  As a first step towards fixing Bug #27083 and #27422
  modify our internal readers so that they throw a new
  exception class which exposes the necessary fields
  required for generating localized error messages. This
  allows these exceptions to be caught where appropriate
  so that the error which occured may be reported to
  an error reporter.
  
  Revision  Changes    Path
  1.7       +8 -3      xml-xerces/java/src/org/apache/xerces/impl/io/ASCIIReader.java
  
  Index: ASCIIReader.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/impl/io/ASCIIReader.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ASCIIReader.java	24 Feb 2004 22:59:15 -0000	1.6
  +++ ASCIIReader.java	4 Mar 2004 19:27:13 -0000	1.7
  @@ -21,6 +21,7 @@
   import java.io.Reader;
   import java.util.Locale;
   import org.apache.xerces.util.MessageFormatter;
  +import org.apache.xerces.impl.msg.XMLMessageFormatter;
   
   /**
    * A simple ASCII byte reader. This is an optimized reader for reading
  @@ -111,7 +112,9 @@
       public int read() throws IOException {
           int b0 = fInputStream.read();
           if (b0 >= 0x80) {
  -            throw new IOException(fFormatter.formatMessage(fLocale, "InvalidASCII", new Object [] {Integer.toString(b0)}));
  +            throw new MalformedByteSequenceException(fFormatter, 
  +                fLocale, XMLMessageFormatter.XML_DOMAIN, 
  +                "InvalidASCII", new Object [] {Integer.toString(b0)});
           }
           return b0;
       } // read():int
  @@ -138,7 +141,9 @@
           for (int i = 0; i < count; i++) {
               int b0 = fBuffer[i];
               if (b0 < 0) {
  -                throw new IOException(fFormatter.formatMessage(fLocale, "InvalidASCII", new Object [] {Integer.toString(b0 & 0x0FF)}));
  +                throw new MalformedByteSequenceException(fFormatter,
  +                    fLocale, XMLMessageFormatter.XML_DOMAIN,
  +                    "InvalidASCII", new Object [] {Integer.toString(b0 & 0x0FF)});
               }
               ch[offset + i] = (char)b0;
           }
  
  
  
  1.10      +25 -21    xml-xerces/java/src/org/apache/xerces/impl/io/UTF8Reader.java
  
  Index: UTF8Reader.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/impl/io/UTF8Reader.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- UTF8Reader.java	24 Feb 2004 22:59:15 -0000	1.9
  +++ UTF8Reader.java	4 Mar 2004 19:27:13 -0000	1.10
  @@ -19,13 +19,14 @@
   import java.io.InputStream;
   import java.io.IOException;
   import java.io.Reader;
  -import java.io.UTFDataFormatException;
   
   import java.util.Locale;
   import org.apache.xerces.util.MessageFormatter;
   import org.apache.xerces.impl.msg.XMLMessageFormatter;
   
   /**
  + * <p>A UTF-8 reader.</p>
  + * 
    * @author Andy Clark, IBM
    *
    * @version $Id$
  @@ -93,7 +94,7 @@
       public UTF8Reader(InputStream inputStream, MessageFormatter messageFormatter,
               Locale locale) {
           this(inputStream, DEFAULT_BUFFER_SIZE, messageFormatter, locale);
  -    } // <init>(InputStream, MessageFormatter)
  +    } // <init>(InputStream, MessageFormatter, Locale)
   
       /**
        * Constructs a UTF-8 reader from the specified input stream,
  @@ -110,7 +111,7 @@
           fBuffer = new byte[size];
           fFormatter = messageFormatter;
           fLocale = locale;
  -    } // <init>(InputStream,int, MessageFormatter)
  +    } // <init>(InputStream, int, MessageFormatter, Locale)
   
       //
       // Reader methods
  @@ -654,33 +655,36 @@
   
       /** Throws an exception for expected byte. */
       private void expectedByte(int position, int count)
  -        throws UTFDataFormatException {
  +        throws MalformedByteSequenceException {
   
  -        String message = fFormatter.formatMessage(fLocale, "ExpectedByte",
  -                new Object[] {Integer.toString(position), Integer.toString(count)});
  -        throw new UTFDataFormatException(message);
  +        throw new MalformedByteSequenceException(fFormatter,
  +            fLocale,
  +            XMLMessageFormatter.XML_DOMAIN,
  +            "ExpectedByte",
  +            new Object[] {Integer.toString(position), Integer.toString(count)});
   
  -    } // expectedByte(int,int,int)
  +    } // expectedByte(int,int)
   
       /** Throws an exception for invalid byte. */
       private void invalidByte(int position, int count, int c)
  -        throws UTFDataFormatException {
  +        throws MalformedByteSequenceException {
   
  -        String message = fFormatter.formatMessage(fLocale, "InvalidByte",
  -                new Object [] {Integer.toString(position), Integer.toString(count)});
  -        throw new UTFDataFormatException(message);
  +        throw new MalformedByteSequenceException(fFormatter,
  +            fLocale,
  +            XMLMessageFormatter.XML_DOMAIN,
  +            "InvalidByte", 
  +            new Object [] {Integer.toString(position), Integer.toString(count)});
   
  -    } // invalidByte(int,int,int,int)
  +    } // invalidByte(int,int,int)
   
       /** Throws an exception for invalid surrogate bits. */
  -    private void invalidSurrogate(int uuuuu) throws UTFDataFormatException {
  -
  -        StringBuffer str = new StringBuffer();
  -        str.append("high surrogate bits in UTF-8 sequence must not exceed 0x10 but found 0x");
  +    private void invalidSurrogate(int uuuuu) throws MalformedByteSequenceException {
   
  -        String message = fFormatter.formatMessage(fLocale, "InvalidHighSurrogate",
  -                new Object[] {Integer.toHexString(uuuuu)});
  -        throw new UTFDataFormatException(message);
  +        throw new MalformedByteSequenceException(fFormatter,
  +            fLocale,
  +            XMLMessageFormatter.XML_DOMAIN,
  +            "InvalidHighSurrogate", 
  +            new Object[] {Integer.toHexString(uuuuu)});
   
       } // invalidSurrogate(int)
   
  
  
  
  1.1                  xml-xerces/java/src/org/apache/xerces/impl/io/MalformedByteSequenceException.java
  
  Index: MalformedByteSequenceException.java
  ===================================================================
  /*
   * Copyright 2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
   
  package org.apache.xerces.impl.io;
  
  import java.io.IOException;
  import java.util.Locale;
  import org.apache.xerces.util.MessageFormatter;
  
  /**
   * <p>Signals that a malformed byte sequence was detected
   * by a <code>java.io.Reader</code> that decodes bytes 
   * of a given encoding into characters.</p>
   *
   * @author Michael Glavassevich, IBM
   *
   * @version $Id: MalformedByteSequenceException.java,v 1.1 2004/03/04 19:27:13 mrglavas Exp $
   */
  public class MalformedByteSequenceException extends IOException {
  
      //
      // Data
      //
      
      /** message formatter **/
      private MessageFormatter fFormatter;
      
      /** locale for error message **/
      private Locale fLocale;
      
      /** error domain **/
      private String fDomain;
      
      /** key for the error message **/
      private String fKey;
      
      /** replacement arguements for the error message **/
      private Object[] fArguments;
      
      /** message text for this message, initially null **/
      private String fMessage;
      
      //
      // Constructors
      //
  
      /**
       * Constructs a MalformedByteSequenceException with the given
       * parameters which may be passed to an error reporter to 
       * generate a localized string for this exception.
       * 
       * @param formatter The MessageFormatter used for building the 
       *                  message text for this exception.
       * @param locale    The Locale for which messages are to be reported.
       * @param domain    The error domain.
       * @param key       The key of the error message.
       * @param arguments The replacement arguments for the error message,
       *                  if needed.
       */
      public MalformedByteSequenceException(MessageFormatter formatter,
          Locale locale, String domain, String key, Object[] arguments) {
          fFormatter = formatter;
          fLocale = locale;
          fDomain = domain;
          fKey = key;
          fArguments = arguments;
      } // <init>(MessageFormatter, Locale, String, String, Object[])
      
      //
      // Public methods
      //
      
      /**
       * <p>Returns the error domain of the error message.</p>
       * 
       * @return the error domain
       */
      public String getDomain () {
      	return fDomain;
      } // getDomain
      
      /**
       * <p>Returns the key of the error message.</p>
       * 
       * @return the error key of the error message
       */
      public String getKey () {
      	return fKey;
      } // getKey()
      
      /**
       * <p>Returns the replacement arguments for the error
       * message or <code>null</code> if none exist.</p>
       * 
       * @return the replacement arguments for the error message
       * or <code>null</code> if none exist
       */
      public Object[] getArguments () {
      	return fArguments;
      } // getArguments();
      
      /**
       * <p>Returns the localized message for this exception.</p>
       * 
       * @return the localized message for this exception.
       */
      public String getMessage() {
          if (fMessage == null) {
              fMessage = fFormatter.formatMessage(fLocale, fKey, fArguments);
              // The references to the message formatter and locale
              // aren't needed anymore so null them.
              fFormatter = null;
              fLocale = null;
          }
          return fMessage;
       } // getMessage()
       
  } // MalformedByteSequenceException
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org