@Override
  public void begin(String namespace, String name, Attributes attrs) throws Exception {
    Object top = digester.peek();
    Class<?> topclass = top.getClass();

    // iterate over the attributes, setting public fields where applicable
    for (int i = 0; i < attrs.getLength(); i++) {
      String lname = attrs.getLocalName(i);
      if (StringUtil.isBlank(lname)) {
        lname = attrs.getQName(i);
      }

      // look for a public field with this lname
      Field field = null;
      try {
        field = topclass.getField(lname);
      } catch (NoSuchFieldException nsfe) {
        if (_warnNonFields) {
          digester
              .getLogger()
              .warn("Skipping property '" + lname + "' for which there is no field.");
        }
        continue;
      }

      // convert the value into the appropriate object type
      String valstr = attrs.getValue(i);
      FieldParser parser = null;
      Object value;

      // look for a custom field parser
      if ((_parsers != null) && ((parser = _parsers.get(lname)) != null)) {
        value = parser.parse(valstr);
      } else {
        // otherwise use the value marshaller to parse the property based on the type of
        // the target object field
        value = ValueMarshaller.unmarshal(field.getType(), valstr);
      }

      if (digester.getLogger().isDebugEnabled()) {
        digester.getLogger().debug("  Setting property '" + lname + "' to '" + valstr + "'");
      }

      // and finally set the field
      field.set(top, value);
    }
  }
Example #2
0
 /** If input is null, then an empty map is returned. */
 public static <K, V> Map<K, V> parse(
     @Nullable String input, Converter<K> keyConverter, Converter<V> valueConverter) {
   Map<K, V> map = Maps.newLinkedHashMap();
   if (input != null) {
     FieldParser reader = new FieldParser(input);
     boolean end = false;
     while (!end) {
       String key = reader.nextKey();
       if (key == null) {
         end = true;
       } else {
         String val = StringUtils.defaultString(reader.nextVal(), "");
         map.put(keyConverter.parse(key), valueConverter.parse(val));
       }
     }
   }
   return map;
 }
Example #3
0
  private static <F extends Field> F parse(FieldParser parser, String fieldName, String fieldBody) {
    String rawStr = MimeUtil.fold(fieldName + ": " + fieldBody, 0);
    ByteSequence raw = ContentUtil.encode(rawStr);

    Field field = parser.parse(fieldName, fieldBody, raw);

    @SuppressWarnings("unchecked")
    F f = (F) field;
    return f;
  }
  public List<FieldParser.TypeParsingResult> parseFields(
      TypeElement elm, FieldParser fieldParser, GlobalParsingContext context) {
    final Optional<Strategy> strategy = aptUtils.getAnnotationOnClass(elm, Strategy.class);
    final InternalNamingStrategy namingStrategy =
        inferNamingStrategy(strategy, context.namingStrategy);

    final EntityParsingContext entityContext =
        new EntityParsingContext(elm, TypeName.get(aptUtils.erasure(elm)), namingStrategy, context);

    return extractCandidateFields(elm, context.fieldFilter)
        .map(x -> fieldParser.parse(x, entityContext))
        .collect(toList());
  }
  String decodeAllCodes(StringBuilder buff, int initialPosition)
      throws NotFoundException, FormatException {
    int currentPosition = initialPosition;
    String remaining = null;
    do {
      DecodedInformation info = this.decodeGeneralPurposeField(currentPosition, remaining);
      String parsedFields = FieldParser.parseFieldsInGeneralPurpose(info.getNewString());
      if (parsedFields != null) {
        buff.append(parsedFields);
      }
      if (info.isRemaining()) {
        remaining = String.valueOf(info.getRemainingValue());
      } else {
        remaining = null;
      }

      if (currentPosition == info.getNewPosition()) { // No step forward!
        break;
      }
      currentPosition = info.getNewPosition();
    } while (true);

    return buff.toString();
  }
 private static void checkFields(String expected) throws NotFoundException {
   String field = expected.replace("(", "").replace(")", "");
   String actual = FieldParser.parseFieldsInGeneralPurpose(field);
   assertEquals(expected, actual);
 }