Coverage Report - org.apache.tapestry.json.CDL
 
Classes in this File Line Coverage Branch Coverage Complexity
CDL
0%
0/67
0%
0/53
4.727
 
 1  
 package org.apache.tapestry.json;
 2  
 
 3  
 /*
 4  
  Copyright (c) 2002 JSON.org
 5  
 
 6  
  Permission is hereby granted, free of charge, to any person obtaining a copy
 7  
  of this software and associated documentation files (the "Software"), to deal
 8  
  in the Software without restriction, including without limitation the rights
 9  
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10  
  copies of the Software, and to permit persons to whom the Software is
 11  
  furnished to do so, subject to the following conditions:
 12  
 
 13  
  The above copyright notice and this permission notice shall be included in all
 14  
  copies or substantial portions of the Software.
 15  
 
 16  
  The Software shall be used for Good, not Evil.
 17  
 
 18  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 24  
  SOFTWARE.
 25  
  */
 26  
 
 27  
 import java.text.ParseException;
 28  
 
 29  
 /**
 30  
  * This provides static methods to convert comma delimited text into a
 31  
  * JSONArray, and to covert a JSONArray into comma delimited text. Comma
 32  
  * delimited text is a very popular format for data interchange. It is
 33  
  * understood by most database, spreadsheet, and organizer programs.
 34  
  * <p>
 35  
  * Each row of text represents a row in a table or a data record. Each row ends
 36  
  * with a NEWLINE character. Each row contains one or more values. Values are
 37  
  * separated by commas. A value can contain any character except for comma,
 38  
  * unless is is wrapped in single quotes or double quotes.
 39  
  * <p>
 40  
  * The first row usually contains the names of the columns.
 41  
  * <p>
 42  
  * A comma delimited list can be converted into a JSONArray of JSONObjects. The
 43  
  * names for the elements in the JSONObjects can be taken from the names in the
 44  
  * first row.
 45  
  */
 46  
 public final class CDL
 47  
 {
 48  
 
 49  
     /* defeat instantiation */
 50  
     private CDL()
 51  0
     {
 52  0
     }
 53  
     
 54  
     /**
 55  
      * Get the next value. The value can be wrapped in quotes. The value can be
 56  
      * empty.
 57  
      * 
 58  
      * @param x
 59  
      *            A JSONTokener of the source text.
 60  
      * @return The value string, or null if empty.
 61  
      * @throws java.text.ParseException
 62  
      *             if the quoted string is badly formed.
 63  
      */
 64  
     private static String getValue(JSONTokener x)
 65  
         throws java.text.ParseException
 66  
     {
 67  
         char c;
 68  
         do
 69  
         {
 70  0
             c = x.next();
 71  0
         } while(c <= ' ' && c != 0);
 72  0
         switch(c)
 73  
         {
 74  
         case 0:
 75  0
             return null;
 76  
         case '"':
 77  
         case '\'':
 78  0
             return x.nextString(c);
 79  
         case ',':
 80  0
             x.back();
 81  0
             return "";
 82  
         default:
 83  0
             x.back();
 84  0
             return x.nextTo(',');
 85  
         }
 86  
     }
 87  
 
 88  
     /**
 89  
      * Produce a JSONArray of strings from a row of comma delimited values.
 90  
      * 
 91  
      * @param x
 92  
      *            A JSONTokener of the source text.
 93  
      * @return A JSONArray of strings.
 94  
      * @throws ParseException
 95  
      */
 96  
     public static JSONArray rowToJSONArray(JSONTokener x)
 97  
         throws ParseException
 98  
     {
 99  0
         JSONArray ja = new JSONArray();
 100  
         while(true)
 101  
         {
 102  0
             String value = getValue(x);
 103  0
             if (value == null) { return null; }
 104  0
             ja.put(value);
 105  
             while(true)
 106  
             {
 107  0
                 char c = x.next();
 108  0
                 if (c == ',')
 109  
                 {
 110  0
                     break;
 111  
                 }
 112  0
                 if (c != ' ')
 113  
                 {
 114  0
                     if (c == '\n' || c == '\r' || c == 0) { return ja; }
 115  0
                     throw x.syntaxError("Bad character '" + c + "' (" + (int) c
 116  
                             + ").");
 117  
                 }
 118  0
             }
 119  0
         }
 120  
     }
 121  
 
 122  
     /**
 123  
      * Produce a JSONObject from a row of comma delimited text, using a parallel
 124  
      * JSONArray of strings to provides the names of the elements.
 125  
      * 
 126  
      * @param names
 127  
      *            A JSONArray of names. This is commonly obtained from the first
 128  
      *            row of a comma delimited text file using the rowToJSONArray
 129  
      *            method.
 130  
      * @param x
 131  
      *            A JSONTokener of the source text.
 132  
      * @return A JSONObject combining the names and values.
 133  
      * @throws ParseException
 134  
      */
 135  
     public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x)
 136  
         throws ParseException
 137  
     {
 138  0
         JSONArray ja = rowToJSONArray(x);
 139  0
         return ja != null ? ja.toJSONObject(names) : null;
 140  
     }
 141  
 
 142  
     /**
 143  
      * Produce a JSONArray of JSONObjects from a comma delimited text string,
 144  
      * using the first row as a source of names.
 145  
      * 
 146  
      * @param string
 147  
      *            The comma delimited text.
 148  
      * @return A JSONArray of JSONObjects.
 149  
      * @throws ParseException
 150  
      */
 151  
     public static JSONArray toJSONArray(String string)
 152  
         throws ParseException
 153  
     {
 154  0
         return toJSONArray(new JSONTokener(string));
 155  
     }
 156  
 
 157  
     /**
 158  
      * Produce a JSONArray of JSONObjects from a comma delimited text string,
 159  
      * using the first row as a source of names.
 160  
      * 
 161  
      * @param x
 162  
      *            The JSONTokener containing the comma delimited text.
 163  
      * @return A JSONArray of JSONObjects.
 164  
      * @throws ParseException
 165  
      */
 166  
     public static JSONArray toJSONArray(JSONTokener x)
 167  
         throws ParseException
 168  
     {
 169  0
         return toJSONArray(rowToJSONArray(x), x);
 170  
     }
 171  
 
 172  
     /**
 173  
      * Produce a JSONArray of JSONObjects from a comma delimited text string
 174  
      * using a supplied JSONArray as the source of element names.
 175  
      * 
 176  
      * @param names
 177  
      *            A JSONArray of strings.
 178  
      * @param string
 179  
      *            The comma delimited text.
 180  
      * @return A JSONArray of JSONObjects.
 181  
      * @throws ParseException
 182  
      */
 183  
     public static JSONArray toJSONArray(JSONArray names, String string)
 184  
         throws ParseException
 185  
     {
 186  0
         return toJSONArray(names, new JSONTokener(string));
 187  
     }
 188  
 
 189  
     /**
 190  
      * Produce a JSONArray of JSONObjects from a comma delimited text string
 191  
      * using a supplied JSONArray as the source of element names.
 192  
      * 
 193  
      * @param names
 194  
      *            A JSONArray of strings.
 195  
      * @param x
 196  
      *            A JSONTokener of the source text.
 197  
      * @return A JSONArray of JSONObjects.
 198  
      * @throws java.text.ParseException
 199  
      */
 200  
     public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
 201  
         throws java.text.ParseException
 202  
     {
 203  0
         if (names == null || names.length() == 0) { return null; }
 204  0
         JSONArray ja = new JSONArray();
 205  
         while(true)
 206  
         {
 207  0
             JSONObject jo = rowToJSONObject(names, x);
 208  0
             if (jo == null)
 209  
             {
 210  0
                 break;
 211  
             }
 212  0
             ja.put(jo);
 213  0
         }
 214  0
         if (ja.length() == 0) { return null; }
 215  0
         return ja;
 216  
     }
 217  
 
 218  
     /**
 219  
      * Produce a comma delimited text row from a JSONArray. Values containing
 220  
      * the comma character will be quoted.
 221  
      * 
 222  
      * @param ja
 223  
      *            A JSONArray of strings.
 224  
      * @return A string ending in NEWLINE.
 225  
      */
 226  
     public static String rowToString(JSONArray ja)
 227  
     {
 228  0
         StringBuffer sb = new StringBuffer();
 229  0
         for(int i = 0; i < ja.length(); i += 1)
 230  
         {
 231  0
             if (i > 0)
 232  
             {
 233  0
                 sb.append(',');
 234  
             }
 235  0
             Object o = ja.opt(i);
 236  0
             if (o != null)
 237  
             {
 238  0
                 String s = o.toString();
 239  0
                 if (s.indexOf(',') >= 0)
 240  
                 {
 241  0
                     if (s.indexOf('"') >= 0)
 242  
                     {
 243  0
                         sb.append('\'');
 244  0
                         sb.append(s);
 245  0
                         sb.append('\'');
 246  
                     }
 247  
                     else
 248  
                     {
 249  0
                         sb.append('"');
 250  0
                         sb.append(s);
 251  0
                         sb.append('"');
 252  
                     }
 253  
                 }
 254  
                 else
 255  
                 {
 256  0
                     sb.append(s);
 257  
                 }
 258  
             }
 259  
         }
 260  0
         sb.append('\n');
 261  0
         return sb.toString();
 262  
 
 263  
     }
 264  
 
 265  
     /**
 266  
      * Produce a comma delimited text from a JSONArray of JSONObjects. The first
 267  
      * row will be a list of names obtained by inspecting the first JSONObject.
 268  
      * 
 269  
      * @param ja
 270  
      *            A JSONArray of JSONObjects.
 271  
      * @return A comma delimited text.
 272  
      */
 273  
     public static String toString(JSONArray ja)
 274  
     {
 275  0
         JSONObject jo = ja.optJSONObject(0);
 276  0
         if (jo != null)
 277  
         {
 278  0
             JSONArray names = jo.names();
 279  0
             if (names != null) { return rowToString(names)
 280  
                     + toString(names, ja); }
 281  
         }
 282  0
         return null;
 283  
     }
 284  
 
 285  
     /**
 286  
      * Produce a comma delimited text from a JSONArray of JSONObjects using a
 287  
      * provided list of names. The list of names is not included in the output.
 288  
      * 
 289  
      * @param names
 290  
      *            A JSONArray of strings.
 291  
      * @param ja
 292  
      *            A JSONArray of JSONObjects.
 293  
      * @return A comma delimited text.
 294  
      */
 295  
     public static String toString(JSONArray names, JSONArray ja)
 296  
     {
 297  0
         if (names == null || names.length() == 0) { return null; }
 298  0
         StringBuffer sb = new StringBuffer();
 299  0
         for(int i = 0; i < ja.length(); i += 1)
 300  
         {
 301  0
             JSONObject jo = ja.optJSONObject(i);
 302  0
             if (jo != null)
 303  
             {
 304  0
                 sb.append(rowToString(jo.toJSONArray(names)));
 305  
             }
 306  
         }
 307  0
         return sb.toString();
 308  
     }
 309  
 }