You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by jb...@apache.org on 2002/05/08 11:53:33 UTC

cvs commit: xml-xindice/java/src/org/apache/xindice/xml TextWriter.java

jbates      02/05/08 02:53:33

  Modified:    java/src/org/apache/xindice/core/data Value.java
               java/src/org/apache/xindice/core/indexer IndexPattern.java
               java/src/org/apache/xindice/server Kernel.java
               java/src/org/apache/xindice/util ByteBuffer.java MD5.java
               java/src/org/apache/xindice/xml TextWriter.java
  Log:
  Start of UTF-8 fix.
  
  Revision  Changes    Path
  1.2       +19 -3     xml-xindice/java/src/org/apache/xindice/core/data/Value.java
  
  Index: Value.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/core/data/Value.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Value.java	6 Dec 2001 21:00:11 -0000	1.1
  +++ Value.java	8 May 2002 09:53:33 -0000	1.2
  @@ -56,7 +56,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    *
  - * $Id: Value.java,v 1.1 2001/12/06 21:00:11 bradford Exp $
  + * $Id: Value.java,v 1.2 2002/05/08 09:53:33 jbates Exp $
    */
   
   import java.io.*;
  @@ -93,7 +93,17 @@
      }
   
      public Value(String data) {
  -      this(data.getBytes());
  +
  +       try {
  +           
  +           this.data = data.getBytes("utf-8");
  +           this.len = this.data.length;
  +           
  +       } catch (UnsupportedEncodingException e) {
  +           
  +           throw new IllegalStateException("Java doesn't support "
  +                + "UTF-8 encoding");
  +       }
      }
   
      /**
  @@ -143,7 +153,13 @@
      }
   
      public final String toString() {
  -      return new String(getData());
  +       try {
  +           
  +           return new String(getData(), "utf-8");
  +       } catch (UnsupportedEncodingException e) {
  +           
  +           throw new IllegalStateException("Java doesn't seem to support UTF-8!");
  +       }
      }
   
      public int hashCode() {
  
  
  
  1.2       +5 -5      xml-xindice/java/src/org/apache/xindice/core/indexer/IndexPattern.java
  
  Index: IndexPattern.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/core/indexer/IndexPattern.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- IndexPattern.java	6 Dec 2001 21:00:12 -0000	1.1
  +++ IndexPattern.java	8 May 2002 09:53:33 -0000	1.2
  @@ -56,7 +56,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    *
  - * $Id: IndexPattern.java,v 1.1 2001/12/06 21:00:12 bradford Exp $
  + * $Id: IndexPattern.java,v 1.2 2002/05/08 09:53:33 jbates Exp $
    */
   
   import org.apache.xindice.core.filer.*;
  @@ -210,27 +210,27 @@
            short len = dis.readShort();
            byte[] name = new byte[len];
            dis.read(name);
  -         elemName = new String(name);
  +         elemName = new String(name, "utf-8");
         }
         attrID = dis.readShort();
         if ( attrID == PATTERN_NAME ) {
            short len = dis.readShort();
            byte[] name = new byte[len];
            dis.read(name);
  -         attrName = new String(name);
  +         attrName = new String(name, "utf-8");
         }
      }
   
      public void write(DataOutputStream dos) throws IOException {
         dos.writeShort(elemID);
         if ( elemID == PATTERN_NAME ) {
  -         byte[] name = elemName.getBytes();
  +         byte[] name = elemName.getBytes("utf-8");
            dos.writeShort(name.length);
            dos.write(name);
         }
         dos.writeShort(attrID);
         if ( attrID == PATTERN_NAME ) {
  -         byte[] name = attrName.getBytes();
  +         byte[] name = attrName.getBytes("utf-8");
            dos.writeShort(name.length);
            dos.write(name);
         }
  
  
  
  1.3       +1 -146    xml-xindice/java/src/org/apache/xindice/server/Kernel.java
  
  Index: Kernel.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/server/Kernel.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Kernel.java	26 Feb 2002 07:10:09 -0000	1.2
  +++ Kernel.java	8 May 2002 09:53:33 -0000	1.3
  @@ -56,7 +56,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    *
  - * $Id: Kernel.java,v 1.2 2002/02/26 07:10:09 kstaken Exp $
  + * $Id: Kernel.java,v 1.3 2002/05/08 09:53:33 jbates Exp $
    */
    
   import org.apache.xindice.util.*;
  @@ -528,151 +528,6 @@
      }
      
      
  -   /**
  -    * TextWriter takes a Document, DocumentFragment, or Element and streams it
  -    * as text to an output source (or a String) -- Stolen from org.apache.xindice.xml
  -    */
  -   
  -   public final class TextWriter {
  -      private Node node = null;
  -   
  -      public TextWriter(Node node) throws DOMException {
  -         this.node = node;
  -      }
  -   
  -      private void writeNode(Writer writer, Node node) throws IOException {
  -         short type = node.getNodeType();
  -         switch ( type ) {
  -   
  -            case Node.DOCUMENT_NODE: {
  -               writer.write("<?xml version=\"1.0\"?>\n");
  -               writeChildren(writer, node);
  -               break;
  -            }
  -   
  -            case Node.DOCUMENT_FRAGMENT_NODE: {
  -               writeChildren(writer, node);
  -               break;
  -            }
   
  -            case Node.ELEMENT_NODE: {
  -               Element e = (Element)node;
  -               String n = e.getTagName();
  -   
  -               writer.write('<');
  -               writer.write(n);
  -   
  -               NamedNodeMap a = e.getAttributes();
  -               int size = a.getLength();
  -               for ( int i = 0; i < size; i++ ) {
  -                  Attr att = (Attr)a.item(i);
  -                  writer.write(' ');
  -                  writeNode(writer, att);
  -               }
  -   
  -               if ( e.hasChildNodes() ) {
  -                  writer.write('>');
  -                  writeChildren(writer, node);
  -                  writer.write("</");
  -                  writer.write(n);
  -                  writer.write('>');
  -               }
  -               else
  -                  writer.write(" />");
  -               break;
  -            }
  -   
  -            case Node.ATTRIBUTE_NODE:
  -               Attr a = (Attr)node;
  -               writer.write(a.getName());
  -               writer.write("=\"");
  -               writeEscapedText(writer, a.getValue());
  -               writer.write("\"");
  -               break;
  -   
  -   
  -            case Node.PROCESSING_INSTRUCTION_NODE: {
  -               ProcessingInstruction pi = (ProcessingInstruction)node;
  -               writer.write("<?");
  -               writer.write(pi.getTarget());
  -               writer.write(" ");
  -               writer.write(pi.getData());
  -               writer.write("?>\n");
  -               break;
  -            }
  -   
  -            case Node.TEXT_NODE: {
  -               writeEscapedText(writer, node.getNodeValue());
  -               break;
  -            }
  -   
  -            case Node.CDATA_SECTION_NODE: {
  -               writer.write("<![CDATA[");
  -               writer.write(node.getNodeValue());
  -               writer.write("]]>");
  -               break;
  -            }
  -   
  -            case Node.COMMENT_NODE: {
  -               writer.write("<!--");
  -               writer.write(node.getNodeValue());
  -               writer.write("-->");
  -               break;
  -            }
  -         }
  -      }
  -   
  -      private void writeChildren(Writer writer, Node node) throws IOException {
  -         NodeList l = node.getChildNodes();
  -         int size = l.getLength();
  -         for ( int i = 0; i < size; i++ )
  -            writeNode(writer, l.item(i));
  -      }
  -   
  -      private void writeEscapedText(Writer writer, String text) throws IOException {
  -         char[] value = text.toCharArray();
  -         String outval = null;
  -         int start = 0;
  -         int len = 0;
  -         for ( int i = 0; i < value.length; i++ ) {
  -            switch ( value[i] ) {
  -               case '&'  : outval = "&amp;";  break;
  -               case '\'' : outval = "&apos;"; break;
  -               case '\"' : outval = "&quot;"; break;
  -               case '<'  : outval = "&lt;";   break;
  -               case '>'  : outval = "&gt;";   break;
  -               default   : len++;             break;
  -            }
  -   
  -            if ( outval != null ) {
  -               if ( len > 0 )
  -                  writer.write(value, start, len);
  -               writer.write(outval);
  -               start = i+1;
  -               len = 0;
  -               outval = null;
  -            }
  -         }
  -         if ( len > 0 )
  -            writer.write(value, start, len);
  -      }
  -      
  -      /**
  -       * write writes the node to the OutputStream as text.
  -       *
  -       * @param output The OutputStream to write to
  -       */
  -      public void write(OutputStream output) throws IOException {
  -         try {
  -            OutputStreamWriter o = new OutputStreamWriter(output);
  -            BufferedWriter buf = new BufferedWriter(o, 4096);
  -            writeNode(buf, node);
  -            buf.flush();
  -         }
  -         catch ( Exception e ) {
  -             org.apache.xindice.Debug.printStackTrace(e);
  -         }
  -      }
  -   }
   }
   
  
  
  
  1.2       +72 -54    xml-xindice/java/src/org/apache/xindice/util/ByteBuffer.java
  
  Index: ByteBuffer.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/util/ByteBuffer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ByteBuffer.java	6 Dec 2001 19:33:57 -0000	1.1
  +++ ByteBuffer.java	8 May 2002 09:53:33 -0000	1.2
  @@ -56,7 +56,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    *
  - * $Id: ByteBuffer.java,v 1.1 2001/12/06 19:33:57 bradford Exp $
  + * $Id: ByteBuffer.java,v 1.2 2002/05/08 09:53:33 jbates Exp $
    */
   
   import java.io.*;
  @@ -87,12 +87,15 @@
      }
   
      public ByteBuffer(String value) {
  -      length = value.length();
  -      buffer = new byte[length];
  -      char[] temp = value.toCharArray();
  -      length = temp.length;
  -      for ( int i = 0; i < length; i++ )
  -         buffer[i] = (byte)temp[i];
  +      
  +       try {
  +           
  +           buffer = value.getBytes("utf-8");
  +           length = buffer.length;
  +       } catch (UnsupportedEncodingException e) {
  +           
  +           throw new IllegalStateException("Java doesn't seem to support UTF-8");
  +       }
      }
   
      public ByteBuffer(StringBuffer value) {
  @@ -161,21 +164,23 @@
      }
   
      public void append(char[] value) {
  -      if ( locked )
  -         return;
  -      checkCapacity(length+value.length);
  -      int len = value.length;
  -      for ( int i = 0; i < len; i++ )
  -         buffer[length+i] = (byte)value[i];
  -      length += value.length;
  +     
  +       append(new String(value));
      }
   
      public void append(String value) {
  -      append(value.toCharArray());
  +      
  +       try {
  +           
  +           append(value.getBytes("utf-8"));
  +       } catch (UnsupportedEncodingException e) {
  +           
  +           throw new IllegalStateException("Java doesn't seem to know UTF-8...");
  +       }   
      }
   
      public void append(StringBuffer value) {
  -      append(value.toString().toCharArray());
  +      append(value.toString());
      }
   
      public void append(ByteBuffer value) {
  @@ -193,8 +198,7 @@
      public void append(char value) {
         if ( locked )
            return;
  -      checkCapacity(length+1);
  -      buffer[length++] = (byte)value;
  +      append(new char[] { value });
      }
   
      public void append(byte value) {
  @@ -240,24 +244,23 @@
      public void insert(char[] value, int pos) {
         if ( locked )
            return;
  -      if ( pos < 0 || pos >= length )
  -         throw new IndexOutOfBoundsException();
  -      checkCapacity(length+value.length);
  -      byte[] temp = new byte[length-pos];
  -      System.arraycopy(buffer, pos, temp, 0, temp.length);
  -      System.arraycopy(temp, 0, buffer, pos+value.length, temp.length);
  -      int len = value.length;
  -      for ( int i = 0; i < len; i++ )
  -         buffer[pos+i] = (byte)value[i];
  -      length += value.length;
  +
  +      insert(new String(value), pos);
      }
   
      public void insert(String value, int pos) {
  -      insert(value.toCharArray(), pos);
  +       try {
  +           
  +           insert(value.getBytes("utf-8"), pos);
  +       } catch (UnsupportedEncodingException e) {
  +           
  +           throw new IllegalStateException("Java doesn't seem to support UTF-8");
  +       }
      }
   
      public void insert(StringBuffer value, int pos) {
  -      insert(value.toString().toCharArray(), pos);
  +       
  +       insert(value.toString(), pos);
      }
   
      public void insert(ByteBuffer value, int pos) {
  @@ -269,16 +272,8 @@
      }
   
      public void insert(char value, int pos) {
  -      if ( locked )
  -         return;
  -      if ( pos < 0 || pos >= length )
  -         throw new IndexOutOfBoundsException();
  -      checkCapacity(length+1);
  -      byte[] temp = new byte[length-pos];
  -      System.arraycopy(buffer, pos, temp, 0, temp.length);
  -      System.arraycopy(temp, 0, buffer, pos+1, temp.length);
  -      buffer[pos] = (byte)value;
  -      length++;
  +
  +       insert(new char[] {value}, pos);
      }
   
      public void insert(byte value, int pos) {
  @@ -331,53 +326,76 @@
      }
   
      public void replace(String value, int pos, int width) {
  -      replace(value.getBytes(), pos, width);
  +       try {
  +           
  +           replace(value.getBytes("utf-8"), pos, width);
  +       } catch (UnsupportedEncodingException e) {
  +           
  +           throw new IllegalStateException("Java doesn't seem to know UTF-8");
  +       }
      }
   
      public void replace(StringBuffer value, int pos, int width) {
  -      replace(value.toString().getBytes(), pos, width);
  +
  +       replace(value.toString(), pos, width);
      }
   
      public void replace(ByteBuffer value, int pos, int width) {
  -      replace(value.toByteArray(), pos, width);
  +       
  +       replace(value.toByteArray(), pos, width);
      }
   
      public void replace(boolean value, int pos, int width) {
  -      replace(String.valueOf(value), pos, width);
  +       
  +       replace(String.valueOf(value), pos, width);
      }
   
      public void replace(char value, int pos, int width) {
  -      replace(String.valueOf(value), pos, width);
  +       
  +       replace(String.valueOf(value), pos, width);
      }
   
      public void replace(double value, int pos, int width) {
  -      replace(String.valueOf(value), pos, width);
  +       
  +       replace(String.valueOf(value), pos, width);
      }
   
      public void replace(float value, int pos, int width) {
  -      replace(String.valueOf(value), pos, width);
  +       
  +       replace(String.valueOf(value), pos, width);
      }
   
      public void replace(int value, int pos, int width) {
  -      replace(String.valueOf(value), pos, width);
  +       
  +       replace(String.valueOf(value), pos, width);
      }
   
      public void replace(long value, int pos, int width) {
  -      replace(String.valueOf(value), pos, width);
  +       
  +       replace(String.valueOf(value), pos, width);
      }
   
      public void replace(Object value, int pos, int width) {
  -      replace(String.valueOf(value), pos, width);
  +       
  +       replace(String.valueOf(value), pos, width);
      }
   
      public byte[] toByteArray() {
  -      byte[] value = new byte[length];
  -      System.arraycopy(buffer, 0, value, 0, length);
  -      return value;
  +      
  +       byte[] value = new byte[length];
  +       System.arraycopy(buffer, 0, value, 0, length);
  +       return value;
      }
   
      public String toString() {
  -      return new String(buffer, 0, length);
  +      
  +       try {
  +           
  +           return new String(buffer, "utf-8");
  +       } catch (UnsupportedEncodingException e) {
  +           
  +           throw new IllegalStateException("Java doesn't seem to support UTF-8");
  +       }
      }
   
      public int length() {
  
  
  
  1.2       +2 -2      xml-xindice/java/src/org/apache/xindice/util/MD5.java
  
  Index: MD5.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/util/MD5.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MD5.java	6 Dec 2001 19:33:58 -0000	1.1
  +++ MD5.java	8 May 2002 09:53:33 -0000	1.2
  @@ -56,7 +56,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    *
  - * $Id: MD5.java,v 1.1 2001/12/06 19:33:58 bradford Exp $
  + * $Id: MD5.java,v 1.2 2002/05/08 09:53:33 jbates Exp $
    */
   
   import java.security.*;
  @@ -71,7 +71,7 @@
   
         try {
            md = MessageDigest.getInstance("MD5");
  -         md.update(pswd.getBytes());
  +         md.update(pswd.getBytes("utf-8"));
   
            byte[] digest = md.digest();
   
  
  
  
  1.3       +2 -2      xml-xindice/java/src/org/apache/xindice/xml/TextWriter.java
  
  Index: TextWriter.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/xml/TextWriter.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TextWriter.java	2 Apr 2002 22:57:06 -0000	1.2
  +++ TextWriter.java	8 May 2002 09:53:33 -0000	1.3
  @@ -56,7 +56,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    *
  - * $Id: TextWriter.java,v 1.2 2002/04/02 22:57:06 kstaken Exp $
  + * $Id: TextWriter.java,v 1.3 2002/05/08 09:53:33 jbates Exp $
    */
   
   import org.w3c.dom.*;
  @@ -302,7 +302,7 @@
               
         try {
            write(output);       
  -         return output.toString();
  +         return new String(output.toByteArray(), "utf-8");
         }
         catch ( Exception e ) {
            org.apache.xindice.Debug.printStackTrace(e);