本文整理匯總了Java中javax.xml.bind.annotation.XmlType類的典型用法代碼示例。如果您正苦於以下問題:Java XmlType類的具體用法?Java XmlType怎麽用?Java XmlType使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
XmlType類屬於javax.xml.bind.annotation包,在下文中一共展示了XmlType類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addsCorrectAnnotations
import javax.xml.bind.annotation.XmlType; //導入依賴的package包/類
/**
* PageBuilder can add correct annotations.
* @throws Exception If there is some problem inside
*/
@Test
public void addsCorrectAnnotations() throws Exception {
final String xsl = "/some/path/test.xsl";
final Object page = new PageBuilder()
.stylesheet(xsl)
.build(PageBuilderTest.BarPage.class);
MatcherAssert.assertThat(
page.getClass().getAnnotation(XmlType.class).name(),
Matchers.equalTo(
"com.rexsl.page.PageBuilderTest$BarPage$somepathtestxsl"
)
);
MatcherAssert.assertThat(
page.getClass().getAnnotation(Stylesheet.class).value(),
Matchers.equalTo(xsl)
);
}
示例2: canRead
import javax.xml.bind.annotation.XmlType; //導入依賴的package包/類
/**
* {@inheritDoc}
* <p>Jaxb2CollectionHttpMessageConverter can read a generic
* {@link Collection} where the generic type is a JAXB type annotated with
* {@link XmlRootElement} or {@link XmlType}.
*/
@Override
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
if (!(type instanceof ParameterizedType)) {
return false;
}
ParameterizedType parameterizedType = (ParameterizedType) type;
if (!(parameterizedType.getRawType() instanceof Class)) {
return false;
}
Class<?> rawType = (Class<?>) parameterizedType.getRawType();
if (!(Collection.class.isAssignableFrom(rawType))) {
return false;
}
if (parameterizedType.getActualTypeArguments().length != 1) {
return false;
}
Type typeArgument = parameterizedType.getActualTypeArguments()[0];
if (!(typeArgument instanceof Class)) {
return false;
}
Class<?> typeArgumentClass = (Class<?>) typeArgument;
return (typeArgumentClass.isAnnotationPresent(XmlRootElement.class) ||
typeArgumentClass.isAnnotationPresent(XmlType.class)) && canRead(mediaType);
}
示例3: marshal
import javax.xml.bind.annotation.XmlType; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public static String marshal(Object obj) {
StringWriter stringWriter = new StringWriter();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
XmlRootElement xmlRootAnnotation = obj.getClass().getAnnotation(XmlRootElement.class);
System.out.println(xmlRootAnnotation);
if (xmlRootAnnotation == null) {
XmlType xmlTypeAnnotation = obj.getClass().getAnnotation(XmlType.class);
QName qname = new QName("", xmlTypeAnnotation.name());
JAXBElement<Object> jaxbElement = new JAXBElement<Object>(qname, (Class<Object>) obj.getClass(), null, obj);
jaxbMarshaller.marshal(jaxbElement, stringWriter);
} else {
jaxbMarshaller.marshal(obj, stringWriter);
}
} catch (Exception e) {
e.printStackTrace();
}
return stringWriter.toString();
}
示例4: writeTo
import javax.xml.bind.annotation.XmlType; //導入依賴的package包/類
@Override
@SuppressWarnings("unchecked")
public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException, WebApplicationException {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
for (Annotation annotation : annotations) {
if (annotation instanceof XsiSchemaLocation) {
XsiSchemaLocation schemaAnnotation = (XsiSchemaLocation) annotation;
String schemaLocation = schemaAnnotation.schemaLocation();
jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation);
}
}
XmlType xmlTypeAnnotation = object.getClass().getAnnotation(XmlType.class);
QName qname = new QName("", xmlTypeAnnotation.name());
StringWriter stringWriter = new StringWriter();
JAXBElement<Object> jaxbElement = new JAXBElement<Object>(qname, (Class<Object>) object.getClass(), null, object);
jaxbMarshaller.marshal(jaxbElement, stringWriter);
entityStream.write(stringWriter.toString().getBytes());
} catch (Exception e) {
throw new WebApplicationException(e);
}
}
示例5: determineNamespaceUncached
import javax.xml.bind.annotation.XmlType; //導入依賴的package包/類
private String determineNamespaceUncached(Class<? extends Object> beanClass) {
XmlType xmlType = beanClass.getAnnotation(XmlType.class);
if (xmlType == null) {
return null;
}
String namespace = xmlType.namespace();
if (BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) {
XmlSchema xmlSchema = beanClass.getPackage().getAnnotation(XmlSchema.class);
namespace = xmlSchema.namespace();
}
if (StringUtils.isBlank(namespace) || BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) {
return null;
}
return namespace;
}
示例6: determineTypeForClassUncached
import javax.xml.bind.annotation.XmlType; //導入依賴的package包/類
private QName determineTypeForClassUncached(Class<? extends Object> beanClass) {
XmlType xmlType = beanClass.getAnnotation(XmlType.class);
if (xmlType == null) {
return null;
}
String namespace = xmlType.namespace();
if (BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) {
XmlSchema xmlSchema = beanClass.getPackage().getAnnotation(XmlSchema.class);
namespace = xmlSchema.namespace();
}
if (StringUtils.isBlank(namespace) || BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) {
return null;
}
return new QName(namespace, xmlType.name());
}
示例7: getShortName
import javax.xml.bind.annotation.XmlType; //導入依賴的package包/類
/**
* Returns a short name for this node which can be useful for ID generation or referring to related resources like images
*
* @return defaults to "node" but derived nodes should overload this to provide a unique name
*/
@Override
public String getShortName() {
if (shortName == null) {
XmlRootElement root = getClass().getAnnotation(XmlRootElement.class);
if (root != null) {
shortName = root.name();
}
if (shortName == null) {
XmlType type = getClass().getAnnotation(XmlType.class);
if (type != null) {
shortName = type.name();
}
}
}
return shortName;
}
示例8: findQNameForSoapActionOrType
import javax.xml.bind.annotation.XmlType; //導入依賴的package包/類
/**
* @return determine element name by using the XmlType.name() of the type to
* be marshalled and the XmlSchema.namespace() of the package-info
*/
public QName findQNameForSoapActionOrType(String soapAction, Class<?> type) {
XmlType xmlType = type.getAnnotation(XmlType.class);
if (xmlType == null || xmlType.name() == null) {
throw new RuntimeException("The type " + type.getName() + " needs to have an XmlType annotation with name");
}
String nameSpace = xmlType.namespace();
if ("##default".equals(nameSpace)) {
XmlSchema xmlSchema = type.getPackage().getAnnotation(XmlSchema.class);
if (xmlSchema != null) {
nameSpace = xmlSchema.namespace();
}
}
// prefer name from the XmlType, and fallback to XmlRootElement
String localName = xmlType.name();
if (ObjectHelper.isEmpty(localName)) {
XmlRootElement root = type.getAnnotation(XmlRootElement.class);
if (root != null) {
localName = root.name();
}
}
return new QName(nameSpace, localName);
}
示例9: getTagName
import javax.xml.bind.annotation.XmlType; //導入依賴的package包/類
public static String getTagName(Class<?> handled) {
if (TAG_NAMES.containsKey(handled)) {
return TAG_NAMES.get(handled);
}
XmlType xmlType = handled.getAnnotation(XmlType.class);
if (xmlType != null && xmlType.name() != null && xmlType.name().trim().length() > 0) {
TAG_NAMES.put(handled, xmlType.name());
return xmlType.name();
} else {
XmlRootElement xmlRoot = handled.getAnnotation(XmlRootElement.class);
if (xmlRoot != null && xmlRoot.name() != null && xmlRoot.name().trim().length() > 0) {
TAG_NAMES.put(handled, xmlRoot.name());
return xmlRoot.name();
}
}
throw new IllegalArgumentException("XML name not found for " + handled.getName());
}
示例10: convertToStub
import javax.xml.bind.annotation.XmlType; //導入依賴的package包/類
public static Stub convertToStub(JavaLanguageVariable variable, Stub parentStub, StubTypeTreeRepository typeTreeRepository) {
Stub stub = new Stub();
stub.setParentStubRelation(parentStub);
stub.setStubName(variable.getVariableName());
stub.setRequired(variable.isRequired());
stub.setHeader(variable.isHeader());
stub.setMultiOccurs(variable.isMultiOccurs());
stub.setType(variable.getType());
if (variable.getType().isAnnotationPresent(XmlType.class) && !variable.getType().isEnum() && !stub.isSameTypeWithSomeAncestor()) {
convertFieldsToChildStubs(stub, variable.getType(), typeTreeRepository);
}
return stub;
}
示例11: getFieldsIncludingAncestorTypes
import javax.xml.bind.annotation.XmlType; //導入依賴的package包/類
private static List<Field> getFieldsIncludingAncestorTypes(Class<?> type) {
List<Field> allFields = new ArrayList<Field>();
while (true) {
Field[] fieldsArray = type.getDeclaredFields();
List<Field> fields = new ArrayList<Field>();
if (fieldsArray != null) {
for (Field f : fieldsArray) {
fields.add(f);
}
}
allFields.addAll(0, fields);
type = type.getSuperclass();
if (type == null) {
break;
}
if (!type.isAnnotationPresent(XmlType.class)) {
break;
}
}
return allFields;
}
示例12: validateCollection
import javax.xml.bind.annotation.XmlType; //導入依賴的package包/類
private void validateCollection(Field f, Object obj) throws IllegalArgumentException, IllegalAccessException {
XmlType xtype = obj.getClass().getAnnotation(XmlType.class);
if (xtype == null) {
return;
}
String elementName = xtype.name();
logger.debug(String.format("validating %s->%s", elementName, f.getName()));
Collection l = (Collection) f.get(obj);
XmlElement eat = f.getAnnotation(XmlElement.class);
if (eat != null && (eat.required() && (l == null || l.isEmpty()))) {
throw new IllegalArgumentException(String.format("field[%s] of element[%s] is mandatory, cannot be missed", f.getName(), elementName));
}
XmlAttribute aat = f.getAnnotation(XmlAttribute.class);
if (aat != null && (aat.required() && (l == null || l.isEmpty()))) {
throw new IllegalArgumentException(String.format("field[%s] of element[%s] is mandatory, cannot be missed", aat.name(), elementName));
}
if (l != null) {
Object val = l.iterator().next();
if (val != null) {
validateObject(val);
}
}
}
示例13: validateField
import javax.xml.bind.annotation.XmlType; //導入依賴的package包/類
private void validateField(Field f, Object obj) throws IllegalArgumentException, IllegalAccessException {
XmlType xtype = obj.getClass().getAnnotation(XmlType.class);
if (xtype == null) {
return;
}
Object val = f.get(obj);
String elementName = xtype.name();
logger.debug(String.format("validating %s->%s", elementName, f.getName()));
XmlElement eat = f.getAnnotation(XmlElement.class);
if (eat != null && eat.required() && val == null) {
throw new IllegalArgumentException(String.format("field[%s] of element[%s] is mandatory, cannot be missed", f.getName(), elementName));
}
XmlAttribute aat = f.getAnnotation(XmlAttribute.class);
if (aat != null && aat.required() && val == null) {
throw new IllegalArgumentException(String.format("field[%s] of element[%s] is mandatory, cannot be missed", aat.name(), elementName));
}
if (val != null) {
validateObject(val);
}
}
示例14: getPropertyOrder
import javax.xml.bind.annotation.XmlType; //導入依賴的package包/類
private static int getPropertyOrder(final Map<String, Integer> propMap, final Class<?> beanClass, final int offset) {
int index = offset;
if (beanClass.getSuperclass() != null) {
index = getPropertyOrder(propMap, beanClass.getSuperclass(), offset);
}
final XmlType xmlTypeAnnotation = beanClass.getAnnotation(XmlType.class);
if (xmlTypeAnnotation != null && xmlTypeAnnotation.propOrder() != null && !xmlTypeAnnotation.propOrder()[0].isEmpty()) {
for (final String propName : xmlTypeAnnotation.propOrder()) {
propMap.put(propName, index++);
}
} else {
try {
final BeanInfo beanInfo = Introspector.getBeanInfo(beanClass, beanClass.getSuperclass());
for (final PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
propMap.put(propertyDescriptor.getName(), index++);
}
} catch (final IntrospectionException e) {
throw new JXPathException(e);
}
}
return index;
}
示例15: writeOut
import javax.xml.bind.annotation.XmlType; //導入依賴的package包/類
public <T> void writeOut(T node, Class<T> declaredType) throws JAXBException {
try {
if (this.context == null) {
init();
}
Marshaller marshaller = this.context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, SCHEMA_LOCATION);
marshaller.marshal(new JAXBElement(
new QName(declaredType.getDeclaredAnnotation(XmlType.class).name()), declaredType, node),
new File(xmlPath));
} catch (JAXBException ex) {
getLogger(FlowProcessor.class.getName()).log(Level.SEVERE,
"Fail to write configuration down to config file[{0}] due to error : \n{1}",
new Object[]{this.xmlPath, ex});
throw ex;
}
}