예제 #1
0
  public void serializeInto(
      @NotNull Object o, @NotNull Element element, @NotNull SerializationFilter filter) {
    for (Binding binding : myPropertyBindings.keySet()) {
      Accessor accessor = myPropertyBindings.get(binding);
      if (!filter.accepts(accessor, o)) {
        continue;
      }

      // todo: optimize. Cache it.
      Property property = accessor.getAnnotation(Property.class);
      if (property != null && property.filter() != SerializationFilter.class) {
        try {
          if (!ReflectionUtil.newInstance(property.filter()).accepts(accessor, o)) {
            continue;
          }
        } catch (RuntimeException e) {
          throw new XmlSerializationException(e);
        }
      }

      Object node = binding.serialize(o, element, filter);
      if (node != null) {
        if (node instanceof org.jdom.Attribute) {
          element.setAttribute((org.jdom.Attribute) node);
        } else {
          JDOMUtil.addContent(element, node);
        }
      }
    }
  }
예제 #2
0
  public Object deserializeInto(@NotNull Object result, @NotNull Element element) {
    Set<Binding> bindings = myPropertyBindings.keySet();
    MultiMap<Binding, Object> data = MultiMap.createSmartList();
    nextNode:
    for (Object child : ContainerUtil.concat(element.getContent(), element.getAttributes())) {
      if (XmlSerializerImpl.isIgnoredNode(child)) {
        continue;
      }

      for (Binding binding : bindings) {
        if (binding.isBoundTo(child)) {
          data.putValue(binding, child);
          continue nextNode;
        }
      }

      final String message = "Format error: no binding for " + child + " inside " + this;
      LOG.debug(message);
      Logger.getInstance(myBeanClass.getName()).debug(message);
      Logger.getInstance("#" + myBeanClass.getName()).debug(message);
    }

    for (Binding binding : data.keySet()) {
      binding.deserialize(result, ArrayUtil.toObjectArray(data.get(binding)));
    }

    return result;
  }
예제 #3
0
  private static Binding _createBinding(@NotNull Accessor accessor) {
    Binding binding = XmlSerializerImpl.getTypeBinding(accessor.getGenericType(), accessor);
    if (binding instanceof JDOMElementBinding) {
      return binding;
    }

    Attribute attribute = accessor.getAnnotation(Attribute.class);
    if (attribute != null) {
      return new AttributeBinding(accessor, attribute);
    }

    Tag tag = accessor.getAnnotation(Tag.class);
    if (tag != null && !tag.value().isEmpty()) {
      return new TagBinding(accessor, tag);
    }

    Text text = accessor.getAnnotation(Text.class);
    if (text != null) {
      return new TextBinding(accessor);
    }

    boolean surroundWithTag = true;
    Property property = accessor.getAnnotation(Property.class);
    if (property != null) {
      surroundWithTag = property.surroundWithTag();
    }

    if (!surroundWithTag) {
      if (!Element.class.isAssignableFrom(binding.getBoundNodeType())) {
        throw new XmlSerializationException(
            "Text-serializable properties can't be serialized without surrounding tags: "
                + accessor);
      }
      return new AccessorBindingWrapper(accessor, binding);
    }

    return new OptionTagBinding(accessor, accessor.getAnnotation(OptionTag.class));
  }
예제 #4
0
 private static Binding createBindingByAccessor(@NotNull Accessor accessor) {
   final Binding binding = _createBinding(accessor);
   binding.init();
   return binding;
 }