Package net.sourceforge.javautil.common.jaxb

Source Code of net.sourceforge.javautil.common.jaxb.JavaXMLContent

package net.sourceforge.javautil.common.jaxb;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;

import javax.xml.bind.JAXBException;

import net.sourceforge.javautil.common.ReflectionUtil;
import net.sourceforge.javautil.common.reflection.cache.IClassMemberWritableValue;

/**
* Handler for XML comments appended to a {@link JavaXMLBean} instance.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class JavaXMLContent implements IJavaXMLComment, IJavaXMLContent {

  protected final IClassMemberWritableValue member;
  protected boolean cumulative = true;

  public JavaXMLContent(IClassMemberWritableValue member) {
    this.member = member;
  }

  @Override public void appendComment(JavaXMLUnmarshallerContext context, char[] buffer, int offset, int length) throws JAXBException {
    this.handleContent(context, false, buffer, offset, length);
  }

  @Override public void startContent(JavaXMLUnmarshallerContext context) {}

  @Override public void handleContent(JavaXMLUnmarshallerContext context, boolean cdata, char[] buffer, int offset, int length) throws JAXBException {
    this.handleContentInternal(context, cdata, buffer, offset, length);
  }

  @Override public void endContent(JavaXMLUnmarshallerContext context) {}

  protected void handleContentInternal (JavaXMLUnmarshallerContext context, boolean cdata, char[] buffer, int offset, int length) throws JAXBException {
    Object instance = context.getCurrentElement().getParentContentInstance(context);
   
    Object value = member.getValue(instance);
    if (Collection.class.isAssignableFrom( member.getBaseType() )) {
      if (value == null) member.setValue(instance, value = new ArrayList());
     
      Class<?> real = ReflectionUtil.getConcreteGeneric(member.getGenericType(), 0);
      Object item = new String(buffer, offset, length);
     
      if (real != null && real != Object.class) {
        item = ReflectionUtil.coerce(real, item);
      }
     
      ((Collection)value).add(item);
    } else if (value instanceof Appendable) {
      try {
        ((Appendable)value).append(new String(buffer, offset, length));
      } catch (IOException e) {
        throw new JAXBException(e);
      }
    } else if (Writer.class.isAssignableFrom(member.getBaseType())) {
      try {
        ((Writer)value).write(buffer, offset, length);
      } catch (IOException e) {
        throw new JAXBException(e);
      }
    } else {
      String str = new String(buffer, offset, length);
     
      if (cumulative && value != null) {
        str = ReflectionUtil.coerce(String.class, value) + str;
      }
     
      member.setValue(instance, ReflectionUtil.coerce(member.getBaseType(), str));
    }
  }
 
}
TOP

Related Classes of net.sourceforge.javautil.common.jaxb.JavaXMLContent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.