org.chromium.sdk/src/org/chromium/sdk/internal/protocolparser/dynamicimpl/FieldLoader.java
changeset 2 e4420d2515f1
equal deleted inserted replaced
1:ef76fc2ac88c 2:e4420d2515f1
       
     1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
       
     2 // Use of this source code is governed by a BSD-style license that can be
       
     3 // found in the LICENSE file.
       
     4 
       
     5 package org.chromium.sdk.internal.protocolparser.dynamicimpl;
       
     6 
       
     7 import org.chromium.sdk.internal.protocolparser.JsonProtocolParseException;
       
     8 
       
     9 /**
       
    10  * This classs is responsible for parsing field values and saving them in {@link ObjectData}
       
    11  * for future use.
       
    12  */
       
    13 class FieldLoader {
       
    14   private final String fieldName;
       
    15   private final int fieldPosInArray;
       
    16   private final SlowParser<?> slowParser;
       
    17   private final boolean isOptional;
       
    18 
       
    19   FieldLoader(int fieldPosInArray, String fieldName, SlowParser<?> slowParser, boolean isOptional) {
       
    20     this.fieldName = fieldName;
       
    21     this.fieldPosInArray = fieldPosInArray;
       
    22     this.slowParser = slowParser;
       
    23     this.isOptional = isOptional;
       
    24   }
       
    25 
       
    26   public String getFieldName() {
       
    27     return fieldName;
       
    28   }
       
    29 
       
    30   public void parse(boolean hasValue, Object value, ObjectData objectData)
       
    31       throws JsonProtocolParseException {
       
    32     if (hasValue) {
       
    33       try {
       
    34         objectData.getFieldArray()[fieldPosInArray] = slowParser.parseValue(value, objectData);
       
    35       } catch (JsonProtocolParseException e) {
       
    36         throw new JsonProtocolParseException("Failed to parse field " + getFieldName(), e);
       
    37       }
       
    38     } else {
       
    39       if (!isOptional) {
       
    40         throw new JsonProtocolParseException("Field is not optional: " + getFieldName());
       
    41       }
       
    42     }
       
    43   }
       
    44 }