Coverage Report - org.apache.tapestry.util.text.LocalizedPropertiesLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
LocalizedPropertiesLoader
0%
0/73
0%
0/39
5.25
LocalizedPropertiesLoader$1
N/A
N/A
5.25
LocalizedPropertiesLoader$IgnoreCharacterException
0%
0/1
N/A
5.25
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.util.text;
 16  
 
 17  
 import java.io.BufferedReader;
 18  
 import java.io.IOException;
 19  
 import java.io.InputStream;
 20  
 import java.io.InputStreamReader;
 21  
 import java.io.Reader;
 22  
 import java.io.UnsupportedEncodingException;
 23  
 import java.util.Map;
 24  
 
 25  
 /**
 26  
  * An object that loads a properties file from the provided input stream or
 27  
  * reader. This class reads the property file exactly like java.util.Properties,
 28  
  * except that it also allows the files to use an encoding other than ISO-8859-1
 29  
  * and all non-ASCII characters are read correctly using the given encoding. In
 30  
  * short, non-latin characters no longer need to be quoted using native2ascii.
 31  
  * 
 32  
  * @author mb
 33  
  * @since 4.0
 34  
  */
 35  
 public class LocalizedPropertiesLoader
 36  
 {
 37  
 
 38  
     private static final String HEX_DIGITS = "0123456789ABCDEF";
 39  
 
 40  0
     private static final ICharacterMatcher WHITESPACE = new WhitespaceMatcher(
 41  
             false);
 42  0
     private static final ICharacterMatcher LINE_SEPARATOR = new AsciiCharacterMatcher(
 43  
             "\n\r");
 44  0
     private static final ICharacterMatcher NOT_LINE_SEPARATOR = new InverseMatcher(
 45  
             LINE_SEPARATOR);
 46  0
     private static final ICharacterMatcher KEY_VALUE_SEPARATOR = new AsciiCharacterMatcher(
 47  
             "=:");
 48  0
     private static final ICharacterMatcher SEPARATOR = new AsciiCharacterMatcher(
 49  
             "=:\r\n");
 50  0
     private static final ICharacterMatcher COMMENT = new AsciiCharacterMatcher(
 51  
             "#!");
 52  0
     private static final ICharacterMatcher WHITESPACE_OR_SEPARATOR = new CompoundMatcher(
 53  
             new ICharacterMatcher[] { WHITESPACE, SEPARATOR });
 54  
 
 55  
     private ExtendedReader _extendedReader;
 56  
 
 57  
     /**
 58  
      * Creates a new loader that will load the properties from the given input
 59  
      * stream using the default character encoding.
 60  
      * 
 61  
      * @param ins
 62  
      *            the input stream to load the properties from
 63  
      */
 64  
     public LocalizedPropertiesLoader(InputStream ins)
 65  
     {
 66  0
         this(new InputStreamReader(ins));
 67  0
     }
 68  
 
 69  
     /**
 70  
      * Creates a new loader that will load the properties from the given input
 71  
      * stream using the provided character encoding.
 72  
      * 
 73  
      * @param ins
 74  
      *            the input stream to load the properties from
 75  
      * @param encoding
 76  
      *            the character encoding the be used when reading from the
 77  
      *            stream
 78  
      * @throws UnsupportedEncodingException
 79  
      *             if the name of the encoding cannot be recognized
 80  
      */
 81  
     public LocalizedPropertiesLoader(InputStream ins, String encoding)
 82  
         throws UnsupportedEncodingException
 83  
     {
 84  0
         this(new InputStreamReader(ins, encoding));
 85  0
     }
 86  
 
 87  
     /**
 88  
      * Creates a new loader that will load the properties from the given reader.
 89  
      * 
 90  
      * @param reader
 91  
      *            the Reader to load the properties from
 92  
      */
 93  
     public LocalizedPropertiesLoader(Reader reader)
 94  0
     {
 95  0
         _extendedReader = new ExtendedReader(new BufferedReader(reader));
 96  0
     }
 97  
 
 98  
     /**
 99  
      * Read the properties from the provided stream and store them into the
 100  
      * given map.
 101  
      * 
 102  
      * @param properties
 103  
      *            the map where the properties will be stored
 104  
      * @throws IOException
 105  
      *             if an error occurs
 106  
      */
 107  
     public void load(Map properties)
 108  
         throws IOException
 109  
     {
 110  0
         while(!isAtEndOfStream())
 111  
         {
 112  
             // we are at the beginning of a line.
 113  
             // check whether it is a comment and if it is, skip it
 114  0
             int nextChar = _extendedReader.peek();
 115  0
             if (COMMENT.matches((char) nextChar))
 116  
             {
 117  0
                 _extendedReader.skipCharacters(NOT_LINE_SEPARATOR);
 118  0
                 continue;
 119  
             }
 120  
 
 121  0
             _extendedReader.skipCharacters(WHITESPACE);
 122  0
             if (!isAtEndOfLine())
 123  
             {
 124  
                 // this line does not consist only of whitespace. the next word
 125  
                 // is the key
 126  0
                 String key = readQuotedLine(WHITESPACE_OR_SEPARATOR);
 127  0
                 _extendedReader.skipCharacters(WHITESPACE);
 128  
 
 129  
                 // if the next char is a key-value separator, read it and skip
 130  
                 // the following spaces
 131  0
                 nextChar = _extendedReader.peek();
 132  0
                 if (nextChar > 0
 133  
                         && KEY_VALUE_SEPARATOR.matches((char) nextChar))
 134  
                 {
 135  0
                     _extendedReader.read();
 136  0
                     _extendedReader.skipCharacters(WHITESPACE);
 137  
                 }
 138  
 
 139  
                 // finally, read the value
 140  0
                 String value = readQuotedLine(LINE_SEPARATOR);
 141  
 
 142  0
                 properties.put(key, value);
 143  
             }
 144  0
             _extendedReader.skipCharacters(LINE_SEPARATOR);
 145  0
         }
 146  0
     }
 147  
 
 148  
     private boolean isAtEndOfStream()
 149  
         throws IOException
 150  
     {
 151  0
         int nextChar = _extendedReader.peek();
 152  0
         return (nextChar < 0);
 153  
     }
 154  
 
 155  
     private boolean isAtEndOfLine()
 156  
         throws IOException
 157  
     {
 158  0
         int nextChar = _extendedReader.peek();
 159  0
         if (nextChar < 0) return true;
 160  0
         return LINE_SEPARATOR.matches((char) nextChar);
 161  
     }
 162  
 
 163  
     private String readQuotedLine(ICharacterMatcher terminators)
 164  
         throws IOException
 165  
     {
 166  0
         StringBuffer buf = new StringBuffer();
 167  
 
 168  
         while(true)
 169  
         {
 170  
             // see what the next char is
 171  0
             int nextChar = _extendedReader.peek();
 172  
 
 173  
             // if at end of stream or the char is one of the terminators, stop
 174  0
             if (nextChar < 0 || terminators.matches((char) nextChar)) break;
 175  
 
 176  
             try
 177  
             {
 178  
                 // read the char (and possibly unquote it)
 179  0
                 char ch = readQuotedChar();
 180  0
                 buf.append(ch);
 181  
             }
 182  0
             catch (IgnoreCharacterException e)
 183  
             {
 184  
                 // simply ignore -- no character was read
 185  0
             }
 186  0
         }
 187  
 
 188  0
         return buf.toString();
 189  
     }
 190  
 
 191  
     private char readQuotedChar()
 192  
         throws IOException, IgnoreCharacterException
 193  
     {
 194  0
         int nextChar = _extendedReader.read();
 195  0
         if (nextChar < 0) throw new IgnoreCharacterException();
 196  0
         char ch = (char) nextChar;
 197  
 
 198  
         // if the char is not the quotation char, simply return it
 199  0
         if (ch != '\\') return ch;
 200  
 
 201  
         // the character is a quotation character. unquote it
 202  0
         nextChar = _extendedReader.read();
 203  
 
 204  
         // if at the end of the stream, stop
 205  0
         if (nextChar < 0) throw new IgnoreCharacterException();
 206  
 
 207  0
         ch = (char) nextChar;
 208  0
         switch(ch)
 209  
         {
 210  
         case 'u':
 211  0
             char res = 0;
 212  0
             for(int i = 0; i < 4; i++)
 213  
             {
 214  0
                 nextChar = _extendedReader.read();
 215  0
                 if (nextChar < 0)
 216  0
                     throw new IllegalArgumentException(
 217  
                             "Malformed \\uxxxx encoding.");
 218  0
                 char digitChar = (char) nextChar;
 219  0
                 int digit = HEX_DIGITS
 220  
                         .indexOf(Character.toUpperCase(digitChar));
 221  0
                 if (digit < 0)
 222  0
                     throw new IllegalArgumentException(
 223  
                             "Malformed \\uxxxx encoding.");
 224  0
                 res = (char) (res * 16 + digit);
 225  
             }
 226  0
             return res;
 227  
 
 228  
         case '\r':
 229  
             // if the next char is \n, read it and fall through
 230  0
             nextChar = _extendedReader.peek();
 231  0
             if (nextChar == '\n') _extendedReader.read();
 232  
         case '\n':
 233  0
             _extendedReader.skipCharacters(WHITESPACE);
 234  0
             throw new IgnoreCharacterException();
 235  
 
 236  
         case 't':
 237  0
             return '\t';
 238  
         case 'n':
 239  0
             return '\n';
 240  
         case 'r':
 241  0
             return '\r';
 242  
         default:
 243  0
             return ch;
 244  
         }
 245  
     }
 246  
 
 247  
     /**
 248  
      * 
 249  
      * @author unknown
 250  
      */
 251  0
     private static class IgnoreCharacterException extends Exception
 252  
     {
 253  
 
 254  
         private static final long serialVersionUID = 8366308710256427596L;
 255  
     }
 256  
 }