类javax.xml.bind.annotation.adapters.XmlAdapter源码实例Demo

下面列出了怎么用javax.xml.bind.annotation.adapters.XmlAdapter的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: hottub   文件: TypeUseImpl.java
public JExpression createConstant(Outline outline, XmlString lexical) {
    if(isCollection())  return null;

    if(adapter==null)     return coreType.createConstant(outline, lexical);

    // [RESULT] new Adapter().unmarshal(CONSTANT);
    JExpression cons = coreType.createConstant(outline, lexical);
    Class<? extends XmlAdapter> atype = adapter.getAdapterIfKnown();

    // try to run the adapter now rather than later.
    if(cons instanceof JStringLiteral && atype!=null) {
        JStringLiteral scons = (JStringLiteral) cons;
        XmlAdapter a = ClassFactory.create(atype);
        try {
            Object value = a.unmarshal(scons.str);
            if(value instanceof String) {
                return JExpr.lit((String)value);
            }
        } catch (Exception e) {
            // assume that we can't eagerly bind this
        }
    }

    return JExpr._new(adapter.getAdapterClass(outline)).invoke("unmarshal").arg(cons);
}
 
源代码2 项目: openjdk-jdk8u-backup   文件: TypeUseImpl.java
public JExpression createConstant(Outline outline, XmlString lexical) {
    if(isCollection())  return null;

    if(adapter==null)     return coreType.createConstant(outline, lexical);

    // [RESULT] new Adapter().unmarshal(CONSTANT);
    JExpression cons = coreType.createConstant(outline, lexical);
    Class<? extends XmlAdapter> atype = adapter.getAdapterIfKnown();

    // try to run the adapter now rather than later.
    if(cons instanceof JStringLiteral && atype!=null) {
        JStringLiteral scons = (JStringLiteral) cons;
        XmlAdapter a = ClassFactory.create(atype);
        try {
            Object value = a.unmarshal(scons.str);
            if(value instanceof String) {
                return JExpr.lit((String)value);
            }
        } catch (Exception e) {
            // assume that we can't eagerly bind this
        }
    }

    return JExpr._new(adapter.getAdapterClass(outline)).invoke("unmarshal").arg(cons);
}
 
源代码3 项目: openjdk-8   文件: BIConversion.java
public TypeUse getTypeUse(XSSimpleType owner) {
    if(typeUse!=null)
        return typeUse;

    JCodeModel cm = getCodeModel();

    JDefinedClass a;
    try {
        a = cm._class(adapter);
        a.hide();   // we assume this is given by the user
        a._extends(cm.ref(XmlAdapter.class).narrow(String.class).narrow(
                cm.ref(type)));
    } catch (JClassAlreadyExistsException e) {
        a = e.getExistingClass();
    }

    // TODO: it's not correct to say that it adapts from String,
    // but OTOH I don't think we can compute that.
    typeUse = TypeUseFactory.adapt(
            CBuiltinLeafInfo.STRING,
            new CAdapter(a));

    return typeUse;
}
 
源代码4 项目: springlets   文件: SpringletsDataJaxb.java
/**
 * Marshals each of the elements of the given {@link Iterable} using the given {@link XmlAdapter}.
 *
 * @param source
 * @param adapter must not be {@literal null}.
 * @return
 * @throws Exception
 */
public static <T, S> List<S> marshal(Iterable<T> source, XmlAdapter<S, T> adapter) {

  Assert.notNull(adapter, "[Assertion failed] - this argument is required; it must not be null");

  if (source == null) {
    return Collections.emptyList();
  }

  List<S> result = new ArrayList<S>();

  for (T element : source) {
    try {
      result.add(adapter.marshal(element));
    } catch (Exception ex) {
      throw new IllegalStateException(ex);
    }
  }

  return result;
}
 
源代码5 项目: openjdk-jdk8u   文件: TypeUseImpl.java
public JExpression createConstant(Outline outline, XmlString lexical) {
    if(isCollection())  return null;

    if(adapter==null)     return coreType.createConstant(outline, lexical);

    // [RESULT] new Adapter().unmarshal(CONSTANT);
    JExpression cons = coreType.createConstant(outline, lexical);
    Class<? extends XmlAdapter> atype = adapter.getAdapterIfKnown();

    // try to run the adapter now rather than later.
    if(cons instanceof JStringLiteral && atype!=null) {
        JStringLiteral scons = (JStringLiteral) cons;
        XmlAdapter a = ClassFactory.create(atype);
        try {
            Object value = a.unmarshal(scons.str);
            if(value instanceof String) {
                return JExpr.lit((String)value);
            }
        } catch (Exception e) {
            // assume that we can't eagerly bind this
        }
    }

    return JExpr._new(adapter.getAdapterClass(outline)).invoke("unmarshal").arg(cons);
}
 
源代码6 项目: TencentKona-8   文件: PropertyInfoImpl.java
/**
 * Checks if the given adapter is applicable to the declared property type.
 */
private boolean isApplicable(XmlJavaTypeAdapter jta, T declaredType ) {
    if(jta==null)   return false;

    T type = reader().getClassValue(jta,"type");
    if(nav().isSameType(declaredType, type))
        return true;    // for types explicitly marked in XmlJavaTypeAdapter.type()

    T ad = reader().getClassValue(jta,"value");
    T ba = nav().getBaseClass(ad, nav().asDecl(XmlAdapter.class));
    if(!nav().isParameterizedType(ba))
        return true;   // can't check type applicability. assume Object, which means applicable to any.
    T inMemType = nav().getTypeArgument(ba, 1);

    return nav().isSubClassOf(declaredType,inMemType);
}
 
源代码7 项目: openjdk-jdk9   文件: BIConversion.java
public TypeUse getTypeUse(XSSimpleType owner) {
    if(typeUse!=null)
        return typeUse;

    JCodeModel cm = getCodeModel();

    JDefinedClass a;
    try {
        a = cm._class(adapter);
        a.hide();   // we assume this is given by the user
        a._extends(cm.ref(XmlAdapter.class).narrow(String.class).narrow(
                cm.ref(type)));
    } catch (JClassAlreadyExistsException e) {
        a = e.getExistingClass();
    }

    // TODO: it's not correct to say that it adapts from String,
    // but OTOH I don't think we can compute that.
    typeUse = TypeUseFactory.adapt(
            CBuiltinLeafInfo.STRING,
            new CAdapter(a));

    return typeUse;
}
 
源代码8 项目: cxf   文件: JAXBUtils.java
@SuppressWarnings("unchecked")
public static Object useAdapter(Object obj,
                                XmlJavaTypeAdapter typeAdapter,
                                boolean marshal,
                                Object defaultValue) {
    if (typeAdapter != null) {
        try {
            @SuppressWarnings("rawtypes")
            XmlAdapter xmlAdapter = typeAdapter.value().newInstance();
            if (marshal) {
                return xmlAdapter.marshal(obj);
            }
            return xmlAdapter.unmarshal(obj);
        } catch (Exception ex) {
            LOG.log(Level.INFO, "(un)marshalling failed, using defaultValue", ex);
        }
    }
    return defaultValue;
}
 
源代码9 项目: cxf   文件: JAXBDataBindingTest.java
@Test
public void testConfiguredXmlAdapter() throws Exception {
    Language dutch = new Language("nl_NL", "Dutch");
    Language americanEnglish = new Language("en_US", "Americanish");

    Person person = new Person(dutch);
    JAXBDataBinding binding = new JAXBDataBinding(Person.class, Language.class);
    binding.setConfiguredXmlAdapters(Arrays.<XmlAdapter<?, ?>>asList(new LanguageAdapter(dutch, americanEnglish)));
    DataWriter<OutputStream> writer = binding.createWriter(OutputStream.class);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    writer.write(person, baos);
    String output = baos.toString();
    String xml = "<person motherTongue=\"nl_NL\"/>";

    assertEquals(xml, output);

    DataReader<XMLStreamReader> reader = binding.createReader(XMLStreamReader.class);
    Person read = (Person) reader.read(XMLInputFactory.newFactory().createXMLStreamReader(new StringReader(xml)));

    assertEquals(dutch, read.getMotherTongue());

}
 
源代码10 项目: TencentKona-8   文件: CAdapter.java
static NClass getRef( final Class<? extends XmlAdapter> adapter, boolean copy ) {
    if(copy) {
        // TODO: this is a hack. the code generation should be defered until
        // the backend. (right now constant generation happens in the front-end)
        return new EagerNClass(adapter) {
            @Override
            public JClass toType(Outline o, Aspect aspect) {
                return o.addRuntime(adapter);
            }
            public String fullName() {
                // TODO: implement this method later
                throw new UnsupportedOperationException();
            }
        };
    } else {
        return NavigatorImpl.theInstance.ref(adapter);
    }
}
 
源代码11 项目: openjdk-jdk9   文件: TypeUseImpl.java
public JExpression createConstant(Outline outline, XmlString lexical) {
    if(isCollection())  return null;

    if(adapter==null)     return coreType.createConstant(outline, lexical);

    // [RESULT] new Adapter().unmarshal(CONSTANT);
    JExpression cons = coreType.createConstant(outline, lexical);
    Class<? extends XmlAdapter> atype = adapter.getAdapterIfKnown();

    // try to run the adapter now rather than later.
    if(cons instanceof JStringLiteral && atype!=null) {
        JStringLiteral scons = (JStringLiteral) cons;
        XmlAdapter a = ClassFactory.create(atype);
        try {
            Object value = a.unmarshal(scons.str);
            if(value instanceof String) {
                return JExpr.lit((String)value);
            }
        } catch (Exception e) {
            // assume that we can't eagerly bind this
        }
    }

    return JExpr._new(adapter.getAdapterClass(outline)).invoke("unmarshal").arg(cons);
}
 
源代码12 项目: jdk8u60   文件: PropertyInfoImpl.java
/**
 * Checks if the given adapter is applicable to the declared property type.
 */
private boolean isApplicable(XmlJavaTypeAdapter jta, T declaredType ) {
    if(jta==null)   return false;

    T type = reader().getClassValue(jta,"type");
    if(nav().isSameType(declaredType, type))
        return true;    // for types explicitly marked in XmlJavaTypeAdapter.type()

    T ad = reader().getClassValue(jta,"value");
    T ba = nav().getBaseClass(ad, nav().asDecl(XmlAdapter.class));
    if(!nav().isParameterizedType(ba))
        return true;   // can't check type applicability. assume Object, which means applicable to any.
    T inMemType = nav().getTypeArgument(ba, 1);

    return nav().isSubClassOf(declaredType,inMemType);
}
 
源代码13 项目: openjdk-8   文件: AdaptedAccessor.java
public OnWireValueT get(BeanT bean) throws AccessorException {
    InMemValueT v = core.get(bean);

    XmlAdapter<OnWireValueT,InMemValueT> a = getAdapter();
    try {
        return a.marshal(v);
    } catch (Exception e) {
        throw new AccessorException(e);
    }
}
 
源代码14 项目: hyperjaxb3   文件: XmlAdapterUtils.java
public static <ValueType, BoundType> JAXBElement<BoundType> marshallJAXBElement(
		Class<? extends XmlAdapter<BoundType, ValueType>> xmlAdapterClass,
		Class<BoundType> declaredType, QName name, Class<?> scope, ValueType v) {
	try {
		if (v == null) {
			return null;
		} else {
			final XmlAdapter<BoundType, ValueType> xmlAdapter = getXmlAdapter(xmlAdapterClass);
			return new JAXBElement<BoundType>(name, declaredType, scope,
					xmlAdapter.marshal(v));
		}
	} catch (Exception ex) {
		throw new RuntimeException(ex);
	}
}
 
源代码15 项目: hyperjaxb3   文件: XmlAdapterUtils.java
public static <ValueType, BoundType> ValueType unmarshallJAXBElement(
		Class<? extends XmlAdapter<BoundType, ValueType>> xmlAdapterClass,
		JAXBElement<? extends BoundType> v) {
	try {
		if (v == null) {
			return null;
		} else {
			final XmlAdapter<BoundType, ValueType> xmlAdapter = getXmlAdapter(xmlAdapterClass);
			return xmlAdapter.unmarshal(v.getValue());
		}
	} catch (Exception ex) {
		throw new RuntimeException(ex);
	}
}
 
源代码16 项目: openjdk-8-source   文件: BridgeAdapter.java
private @NotNull InMemory adaptU(Unmarshaller _u, OnWire v) throws JAXBException {
    UnmarshallerImpl u = (UnmarshallerImpl) _u;
    XmlAdapter<OnWire,InMemory> a = u.coordinator.getAdapter(adapter);
    u.coordinator.pushCoordinator();
    try {
        return a.unmarshal(v);
    } catch (Exception e) {
        throw new UnmarshalException(e);
    } finally {
        u.coordinator.popCoordinator();
    }
}
 
源代码17 项目: jackson-modules-base   文件: AdapterConverter.java
@SuppressWarnings("unchecked")
public AdapterConverter(XmlAdapter<?,?> adapter,
        JavaType inType, JavaType outType, boolean ser)
{
    _adapter = (XmlAdapter<Object,Object>) adapter;
    _inputType = inType;
    _targetType = outType;
    _forSerialization = ser;
}
 
源代码18 项目: hyperjaxb3   文件: DefaultTypeUse.java
public JExpression createConstant(Outline outline, XmlString lexical) {
	if (isCollection())
		return null;

	if (adapter == null)
		return coreType.createConstant(outline, lexical);

	// [RESULT] new Adapter().unmarshal(CONSTANT);
	JExpression cons = coreType.createConstant(outline, lexical);
	@SuppressWarnings("unchecked")
	Class<? extends XmlAdapter<?, ?>> atype = (Class<? extends XmlAdapter<?, ?>>) adapter
			.getAdapterIfKnown();

	// try to run the adapter now rather than later.
	if (cons instanceof JStringLiteral && atype != null) {
		JStringLiteral scons = (JStringLiteral) cons;
		@SuppressWarnings("unchecked")
		XmlAdapter<Object, String> a = (XmlAdapter<Object, String>) ClassFactory
				.create(atype);
		try {
			Object value = a.unmarshal(scons.str);
			if (value instanceof String) {
				return JExpr.lit((String) value);
			}
		} catch (Exception e) {
			// assume that we can't eagerly bind this
		}
	}

	return JExpr._new(adapter.getAdapterClass(outline)).invoke("unmarshal")
			.arg(cons);
}
 
源代码19 项目: hottub   文件: AdaptedLister.java
AdaptedLister(
    Lister<BeanT,PropT,InMemItemT,PackT> core,
    Class<? extends XmlAdapter<OnWireItemT,InMemItemT>> adapter) {

    this.core = core;
    this.adapter = adapter;
}
 
源代码20 项目: TencentKona-8   文件: BridgeAdapter.java
private OnWire _adaptM(XMLSerializer serializer, InMemory v) throws MarshalException {
    XmlAdapter<OnWire,InMemory> a = serializer.getAdapter(adapter);
    try {
        return a.marshal(v);
    } catch (Exception e) {
        serializer.handleError(e,v,null);
        throw new MarshalException(e);
    }
}
 
源代码21 项目: openjdk-jdk9   文件: MarshallerImpl.java
@Override
public <A extends XmlAdapter> A getAdapter(Class<A> type) {
    if(type==null)
        throw new IllegalArgumentException();
    if(serializer.containsAdapter(type))
        // so as not to create a new instance when this method is called
        return serializer.getAdapter(type);
    else
        return null;
}
 
源代码22 项目: TencentKona-8   文件: AdaptedAccessor.java
public OnWireValueT get(BeanT bean) throws AccessorException {
    InMemValueT v = core.get(bean);

    XmlAdapter<OnWireValueT,InMemValueT> a = getAdapter();
    try {
        return a.marshal(v);
    } catch (Exception e) {
        throw new AccessorException(e);
    }
}
 
源代码23 项目: TencentKona-8   文件: AdaptedAccessor.java
private XmlAdapter<OnWireValueT, InMemValueT> getAdapter() {
    Coordinator coordinator = Coordinator._getInstance();
    if(coordinator!=null)
        return coordinator.getAdapter(adapter);
    else {
        synchronized(this) {
            if(staticAdapter==null)
                staticAdapter = ClassFactory.create(adapter);
        }
        return staticAdapter;
    }
}
 
源代码24 项目: TencentKona-8   文件: MarshallerImpl.java
@Override
public <A extends XmlAdapter> A getAdapter(Class<A> type) {
    if(type==null)
        throw new IllegalArgumentException();
    if(serializer.containsAdapter(type))
        // so as not to create a new instance when this method is called
        return serializer.getAdapter(type);
    else
        return null;
}
 
源代码25 项目: openjdk-8   文件: Coordinator.java
/**
 * Gets the instance of the adapter.
 *
 * @return
 *      always non-null.
 */
public final <T extends XmlAdapter> T getAdapter(Class<T> key) {
    T v = key.cast(adapters.get(key));
    if(v==null) {
        v = ClassFactory.create(key);
        putAdapter(key,v);
    }
    return v;
}
 
源代码26 项目: openjdk-jdk8u   文件: UnmarshallerImpl.java
@Override
public <A extends XmlAdapter> A getAdapter(Class<A> type) {
    if(type==null) {
        throw new IllegalArgumentException();
    }
    if(coordinator.containsAdapter(type)) {
        return coordinator.getAdapter(type);
    } else {
        return null;
    }
}
 
源代码27 项目: openjdk-jdk8u-backup   文件: Coordinator.java
/**
 * Gets the instance of the adapter.
 *
 * @return
 *      always non-null.
 */
public final <T extends XmlAdapter> T getAdapter(Class<T> key) {
    T v = key.cast(adapters.get(key));
    if(v==null) {
        v = ClassFactory.create(key);
        putAdapter(key,v);
    }
    return v;
}
 
源代码28 项目: openjdk-jdk8u   文件: BridgeAdapter.java
private @NotNull InMemory adaptU(Unmarshaller _u, OnWire v) throws JAXBException {
    UnmarshallerImpl u = (UnmarshallerImpl) _u;
    XmlAdapter<OnWire,InMemory> a = u.coordinator.getAdapter(adapter);
    u.coordinator.pushCoordinator();
    try {
        return a.unmarshal(v);
    } catch (Exception e) {
        throw new UnmarshalException(e);
    } finally {
        u.coordinator.popCoordinator();
    }
}
 
源代码29 项目: openjdk-jdk9   文件: UnmarshallerImpl.java
@Override
public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) {
    if (type==null) {
        throw new IllegalArgumentException();
    }
    coordinator.putAdapter(type,adapter);
}
 
源代码30 项目: openjdk-jdk8u   文件: AdaptedAccessor.java
public void set(BeanT bean, OnWireValueT o) throws AccessorException {
    XmlAdapter<OnWireValueT, InMemValueT> a = getAdapter();
    try {
        core.set(bean, (o == null ? null : a.unmarshal(o)));
    } catch (Exception e) {
        throw new AccessorException(e);
    }
}
 
 同包方法