public Object readFromStream() {
   reader.moveDown();
   Class type = HierarchicalStreams.readClassType(reader, mapper);
   Object streamItem = context.convertAnother(remoteConfig, type);
   reader.moveUp();
   return streamItem;
 }
Пример #2
0
  /**
   * Unmarshal all the elements to their field values if the fields have the {@link Auto} annotation
   * defined.
   *
   * @param reader - The reader where the elements can be read.
   * @param o - The object for which the value of fields need to be populated.
   */
  private static void autoUnmarshalEligible(HierarchicalStreamReader reader, Object o) {
    try {
      String nodeName = reader.getNodeName();
      Class c = o.getClass();
      Field f = null;
      try {
        f = c.getDeclaredField(nodeName);
      } catch (NoSuchFieldException e) {
        UNMARSHALL_ERROR_COUNTER.increment();
      }
      if (f == null) {
        return;
      }
      Annotation annotation = f.getAnnotation(Auto.class);
      if (annotation == null) {
        return;
      }
      f.setAccessible(true);

      String value = reader.getValue();
      Class returnClass = f.getType();
      if (value != null) {
        if (!String.class.equals(returnClass)) {

          Method method = returnClass.getDeclaredMethod("valueOf", java.lang.String.class);
          Object valueObject = method.invoke(returnClass, value);
          f.set(o, valueObject);
        } else {
          f.set(o, value);
        }
      }
    } catch (Throwable th) {
      logger.error("Error in unmarshalling the object:", th);
    }
  }
 private Class determineType(
     HierarchicalStreamReader reader,
     boolean validField,
     Object result,
     String fieldName,
     Class definedInCls) {
   String classAttribute = reader.getAttribute(mapper.aliasForAttribute("class"));
   Class fieldType = reflectionProvider.getFieldType(result, fieldName, definedInCls);
   if (classAttribute != null) {
     Class specifiedType = mapper.realClass(classAttribute);
     if (fieldType.isAssignableFrom(specifiedType))
       // make sure that the specified type in XML is compatible with the field type.
       // this allows the code to evolve in more flexible way.
       return specifiedType;
   }
   if (!validField) {
     Class itemType = mapper.getItemTypeForItemFieldName(result.getClass(), fieldName);
     if (itemType != null) {
       return itemType;
     } else {
       return mapper.realClass(reader.getNodeName());
     }
   } else {
     return mapper.defaultImplementationOf(fieldType);
   }
 }
Пример #4
0
 public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
   HierarchicalStreamReader reader = createHierarchicalStreamReader(exchange, stream);
   try {
     return getXStream(exchange.getContext()).unmarshal(reader);
   } finally {
     reader.close();
   }
 }
 /**
  * Utility method for reading an item of a collection. It was created because the {@code readItem}
  * method from the parent class does not move the reader ({@code
  * HierarchicalStreamReader#moveDown()} and{@code HierarchicalStreamReader#moveUp()})
  */
 @SuppressWarnings("unchecked")
 protected <T> T readChildItem(
     HierarchicalStreamReader reader, UnmarshallingContext context, Object current) {
   reader.moveDown();
   T o = (T) readItem(reader, context, current);
   reader.moveUp();
   return o;
 }
 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
   String nullAttribute = reader.getAttribute("null");
   if (nullAttribute != null && nullAttribute.equals("true")) {
     return new Character('\0');
   } else {
     return new Character(reader.getValue().charAt(0));
   }
 }
Пример #7
0
 private Map<String, String> readNode(HierarchicalStreamReader reader) {
   Map<String, String> values = new HashMap<String, String>();
   while (reader.hasMoreChildren()) {
     reader.moveDown();
     values.put(reader.getNodeName(), reader.getValue());
     reader.moveUp();
   }
   return values;
 }
Пример #8
0
 @Override
 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
   if (reader.getNodeName().equals(Constants.TAG_TRUE)) return new TrueXML();
   else if (reader.getNodeName().equals(Constants.TAG_FALSE)) return new FalseXML();
   else {
     System.out.println("Boolean Unmarshal unexpectd result");
     return null;
   }
 }
 @Override
 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
   Map<String, String> map = new HashMap<String, String>();
   while (reader.hasMoreChildren()) {
     reader.moveDown();
     map.put(reader.getNodeName(), reader.getValue());
     reader.moveUp();
   }
   return map;
 }
 @SuppressWarnings("unchecked")
 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
   List list = new ArrayList();
   while (reader.hasMoreChildren()) {
     reader.moveDown();
     list.add(context.convertAnother(list, type));
     reader.moveUp();
   }
   return list;
 }
Пример #11
0
 @Override
 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
   List<String> names = new ArrayList<String>();
   while (reader.hasMoreChildren()) {
     reader.moveDown();
     names.add(reader.getAttribute(Constants.ATTRIBUTE_PLAYER_NAME));
     reader.moveUp();
   }
   return new PlayersXML(names);
 }
Пример #12
0
 @Override
 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
   List<Tile> tiles = new ArrayList<Tile>();
   while (reader.hasMoreChildren()) {
     reader.moveDown();
     TileXML tileXML = (TileXML) context.convertAnother(null, TileXML.class);
     tiles.add(tileXML.getTile());
     reader.moveUp();
   }
   return new RerackXML(tiles);
 }
Пример #13
0
 private List<Tile> parsePlayerHand(
     HierarchicalStreamReader reader, UnmarshallingContext context) {
   List<Tile> tiles = new ArrayList<Tile>();
   while (reader.hasMoreChildren()) {
     reader.moveDown();
     TileXML tileXML = (TileXML) context.convertAnother(null, TileXML.class);
     tiles.add(tileXML.getTile());
     reader.moveUp();
   }
   return tiles;
 }
 @Override
 protected void unmarshalEntry(
     HierarchicalStreamReader reader, UnmarshallingContext context, Map map) {
   String key = reader.getNodeName();
   if (key.equals(getEntryNodeName())) key = reader.getAttribute("key");
   if (key == null) {
     super.unmarshalEntry(reader, context, map);
   } else {
     unmarshalStringKey(reader, context, map, key);
   }
 }
Пример #15
0
 protected void populateMap(
     final HierarchicalStreamReader reader,
     final UnmarshallingContext context,
     final Map<?, ?> map,
     final Map<?, ?> target) {
   while (reader.hasMoreChildren()) {
     reader.moveDown();
     putCurrentEntryIntoMap(reader, context, map, target);
     reader.moveUp();
   }
 }
Пример #16
0
 @Override
 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
   String name = reader.getAttribute(Constants.ATTRIBUTE_RESULT_NAME);
   List<Placement> placements = new ArrayList<Placement>();
   while (reader.hasMoreChildren()) {
     reader.moveDown();
     PlacementXML placementXML = (PlacementXML) context.convertAnother(null, PlacementXML.class);
     placements.add(placementXML.getPlacement());
     reader.moveUp();
   }
   return new ResultXML(name, placements);
 }
Пример #17
0
  public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    Murmurs murmurs = new Murmurs();

    while (reader.hasMoreChildren()) {
      reader.moveDown();
      Murmur project = (Murmur) context.convertAnother(murmurs, Murmur.class);
      murmurs.add(project);
      reader.moveUp();
    }

    return murmurs;
  }
Пример #18
0
 public Object unmarshal(HierarchicalStreamReader reader, final UnmarshallingContext context) {
   if (reader.hasMoreChildren()) {
     /* old format where <range> elements are nested like
            <range>
              <start>1337</start>
              <end>1479</end>
            </range>
     */
     return new RangeSet((List<Range>) (collectionConv.unmarshal(reader, context)));
   } else {
     return RangeSet.fromString(reader.getValue(), true);
   }
 }
Пример #19
0
  public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {

    Object obj = context.currentObject();
    if (obj == null) {
      try {
        obj = context.getRequiredType().newInstance();
      } catch (Exception e) {
        throw new ObjectAccessException(
            "Cannot construct " + context.getRequiredType().getName(), e);
      }
    }

    Iterator attNames = reader.getAttributeNames();
    while (attNames.hasNext()) {
      String attName = (String) attNames.next();
      if (attName.equals(textName)) {
        continue;
      }

      try {
        Field field = obj.getClass().getDeclaredField(mapper.realMember(obj.getClass(), attName));
        if (!field.isAccessible()) {
          field.setAccessible(true);
        }
        String v = reader.getAttribute(attName);
        if (null == v || "".equals(v)) {
          continue;
        }
        Class fieldType = field.getType();
        Constructor strnum = fieldType.getDeclaredConstructor(String.class);
        field.set(obj, strnum.newInstance(v));
      } catch (Exception e) {
        e.printStackTrace();
        throw new ObjectAccessException("Cannot construct " + obj.getClass(), e);
      }
    }
    String value = reader.getValue();
    if (null != value && !"".equals(value)) {
      try {
        Field field = obj.getClass().getDeclaredField(mapper.realMember(obj.getClass(), textName));
        if (!field.isAccessible()) {
          field.setAccessible(true);
        }
        field.set(obj, value);
      } catch (Exception e) {
        e.printStackTrace();
        throw new ObjectAccessException("Cannot construct " + obj.getClass(), e);
      }
    }
    return obj;
  }
 @Override
 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext uc) {
   String code = reader.getAttribute(CURRENCY_CODE_ATTRIBUTE_NAME);
   Currency currency = new Currency(code);
   String exchangeRateString = reader.getAttribute(CURRENCY_EXCHANGE_RATE_ATTRIBUTE_NAME);
   // seems to return null if attribute isn't present, improvement
   // of docs requested as
   // https://github.com/x-stream/xstream/issues/47
   if (exchangeRateString != null) {
     double exchangeRate = Double.valueOf(exchangeRateString);
     currency.setExchangeRate(exchangeRate);
   }
   return currency;
 }
Пример #21
0
    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {

      if (Constants.TAG_NULL_TILE.equals(reader.getNodeName())) {
        Tile tile = new NullTile();
        return tile;
      } else {
        String c0 = reader.getAttribute(Constants.ATTRIBUTE_TILE_C0);
        String c1 = reader.getAttribute(Constants.ATTRIBUTE_TILE_C1);

        Tile tile = new Tile(Color.valueOf(c0), Color.valueOf(c1));
        return new TileXML(tile);
      }
    }
Пример #22
0
  public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    NotePad frame = new NotePad();

    reader.moveDown();

    String fieldText = (String) context.convertAnother(frame, String.class);
    frame.textField.setText(Marshaler.newLinesToJava(fieldText));
    reader.moveUp();
    reader.moveDown();
    String areaText = (String) context.convertAnother(frame, String.class);
    frame.textArea.setText(Marshaler.newLinesToJava(areaText));
    reader.moveUp();

    return frame;
  }
 @Override
 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
   // TODO Auto-generated method stub
   GregorianCalendar calendar = new GregorianCalendar();
   calendar.setTime(new Date(Long.parseLong(reader.getValue())));
   return calendar;
 }
Пример #24
0
    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {

      String players = reader.getAttribute(Constants.ATTRIBUTE_BOARD_PLAYERS);
      int numberOfPlayers = new Integer(players).intValue();

      List<Placement> currentPlacements = new ArrayList<Placement>();
      while (reader.hasMoreChildren()) {
        reader.moveDown();
        PlacementXML placementXML = (PlacementXML) context.convertAnother(null, PlacementXML.class);
        currentPlacements.add(placementXML.getPlacement());
        reader.moveUp();
      }

      return new BoardXML(numberOfPlayers, currentPlacements);
    }
Пример #25
0
  public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {

    Resposta3098LoteMensagem enq = new Resposta3098LoteMensagem();
    Resposta3098OcorrenciaMensagem resp = new Resposta3098OcorrenciaMensagem();
    Resposta3098EnquadramentoMensagem enqMensagem = new Resposta3098EnquadramentoMensagem();
    enqMensagem.setCodigoEnquadramento(new Long(reader.getValue()));
    List<Resposta3098EnquadramentoMensagem> listEnqMensagem =
        new ArrayList<Resposta3098EnquadramentoMensagem>();
    listEnqMensagem.add(enqMensagem);
    resp.setEnquadramentos(listEnqMensagem);
    List<Resposta3098OcorrenciaMensagem> listResp = new ArrayList<Resposta3098OcorrenciaMensagem>();
    listResp.add(resp);
    enq.setOcorrencias(listResp);
    reader.moveUp();
    return enq;
  }
Пример #26
0
  protected void putCurrentEntryIntoMap(
      final HierarchicalStreamReader reader,
      final UnmarshallingContext context,
      final Map<?, ?> map,
      final Map<?, ?> target) {
    reader.moveDown();
    final Object key = readItem(reader, context, map);
    reader.moveUp();

    reader.moveDown();
    final Object value = readItem(reader, context, map);
    reader.moveUp();

    @SuppressWarnings("unchecked")
    final Map<Object, Object> targetMap = (Map<Object, Object>) target;
    targetMap.put(key, value);
  }
 private Class determineType(HierarchicalStreamReader reader, Object result, String fieldName) {
   String classAttribute = reader.getAttribute(classAttributeIdentifier);
   if (classAttribute != null) {
     return classMapper.realClass(classAttribute);
   } else {
     return classMapper.defaultImplementationOf(beanProvider.getPropertyType(result, fieldName));
   }
 }
  protected void unmarshallAttributes(
      Object current, HierarchicalStreamReader reader, UnmarshallingContext context) {

    while (reader.hasMoreChildren()) {
      Attribute attribute = readChildItem(reader, context, current);
      addAttribute(current, attribute);
    }
  }
Пример #29
0
    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks
     * .xstream.io.HierarchicalStreamReader,
     * com.thoughtworks.xstream.converters.UnmarshallingContext)
     */
    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
      Application app = new Application();

      while (reader.hasMoreChildren()) {
        reader.moveDown();

        String nodeName = reader.getNodeName();

        if (ELEM_NAME.equals(nodeName)) {
          app.setName(reader.getValue());
        } else if (NODE_INSTANCE.equals(nodeName)) {
          app.addInstance((InstanceInfo) context.convertAnother(app, InstanceInfo.class));
        }
        reader.moveUp();
      }
      return app;
    }
Пример #30
0
    private Map<String, String> unmarshalMap(
        HierarchicalStreamReader reader, UnmarshallingContext context) {

      Map<String, String> map = Collections.emptyMap();

      while (reader.hasMoreChildren()) {
        if (map == Collections.EMPTY_MAP) {
          map = new HashMap<String, String>();
        }
        reader.moveDown();
        String key = reader.getNodeName();
        String value = reader.getValue();
        reader.moveUp();

        map.put(StringCache.intern(key), value);
      }
      return map;
    }