本文整理汇总了Java中javax.xml.bind.annotation.XmlType.name方法的典型用法代码示例。如果您正苦于以下问题:Java XmlType.name方法的具体用法?Java XmlType.name怎么用?Java XmlType.name使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.bind.annotation.XmlType
的用法示例。
在下文中一共展示了XmlType.name方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
示例2: 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);
}
}
示例3: 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());
}
示例4: 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;
}
示例5: 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);
}
示例6: 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());
}
示例7: 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);
}
}
}
示例8: 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);
}
}
示例9: getType
import javax.xml.bind.annotation.XmlType; //导入方法依赖的package包/类
private static String getType(Class<? extends ClientResource<?>> clientObjectClass) {
String clientResourceType = null;
XmlRootElement xmlRootElement = clientObjectClass.getAnnotation(XmlRootElement.class);
if (xmlRootElement != null && !"##default".equals(xmlRootElement.name()))
clientResourceType = xmlRootElement.name();
else {
XmlType xmlType = clientObjectClass.getAnnotation(XmlType.class);
if (xmlType != null && !"##default".equals(xmlType.name()))
clientResourceType = xmlType.name();
}
if (clientResourceType == null) {
String classSimpleName = clientObjectClass.getSimpleName();
clientResourceType = classSimpleName.replaceFirst("^.", classSimpleName.substring(0, 1).toLowerCase());
}
return clientResourceType;
}
示例10: determineTypeForClassUncached
import javax.xml.bind.annotation.XmlType; //导入方法依赖的package包/类
private QName determineTypeForClassUncached(Class<?> 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());
}
示例11: addObjectFactoryClass
import javax.xml.bind.annotation.XmlType; //导入方法依赖的package包/类
private void addObjectFactoryClass(Map<String, RmType> target, Class<?> rmClass, RmType child) {
XmlType xmlType = rmClass.getAnnotation(XmlType.class);
if (xmlType != null || rmClass == RmObject.class) {
String rmType = xmlType != null ? xmlType.name() : RmTypes.RM_OBJECT;
RmType node = target.get(rmType);
if (node == null) {
node = new RmType(rmType);
rmTypeNameClasses.put(rmType, rmClass);
target.put(rmType, node);
}
if (child != null && !node.getChildren().contains(child)) {
node.addChild(child);
}
addObjectFactoryClass(target, rmClass.getSuperclass(), node);
}
}
示例12: getTypeIdName
import javax.xml.bind.annotation.XmlType; //导入方法依赖的package包/类
/**
* Get the id for the specified type.
*
* @param type The type.
* @return The type id.
*/
public static String getTypeIdName(Class<?> type) {
String ns = "";
if (type.getPackage() != null && type.getPackage().isAnnotationPresent(XmlSchema.class)) {
ns = type.getPackage().getAnnotation(XmlSchema.class).namespace();
}
String name = Introspector.decapitalize(type.getSimpleName());
if (type.isAnnotationPresent(XmlType.class)) {
XmlType typeMeta = type.getAnnotation(XmlType.class);
if (!"##default".equals(typeMeta.name())) {
name = typeMeta.name();
}
if (!"##default".equals(typeMeta.namespace())) {
ns = typeMeta.namespace();
}
}
return ns + name;
}
示例13: parseTypeName
import javax.xml.bind.annotation.XmlType; //导入方法依赖的package包/类
/**
* Parses a (potentially-null) {@link XmlType} annotation on a class
* and determine the actual value.
*
* @param clazz
* The class on which the XmlType annotation is checked.
* @param t
* The {@link XmlType} annotation on the clazz. This value
* is taken as a parameter to improve the performance for the case where
* 't' is pre-computed.
*/
protected final QName parseTypeName(ClassDeclT clazz, XmlType t) {
String nsUri="##default";
String local="##default";
if(t!=null) {
nsUri = t.namespace();
local = t.name();
}
if(local.length()==0)
return null; // anonymous
if(local.equals("##default"))
// if defaulted ...
local = NameConverter.standard.toVariableName(nav().getClassShortName(clazz));
if(nsUri.equals("##default")) {
// if defaulted ...
XmlSchema xs = reader().getPackageAnnotation(XmlSchema.class,clazz,this);
if(xs!=null)
nsUri = xs.namespace();
else {
nsUri = builder.defaultNsUri;
}
}
return new QName(nsUri.intern(),local.intern());
}
示例14: getClass
import javax.xml.bind.annotation.XmlType; //导入方法依赖的package包/类
/**
* 获取数据的类型
*
* @param name
* 标记的名称
* @return
* @throws ClassNotFoundException
* @throws IOException
* @throws TransformException
*/
protected Class<?> getClass(String name) throws ClassNotFoundException, IOException, TransformException {
String simpleName = name;
if (simpleName.startsWith("http")) {
simpleName = simpleName.substring(simpleName.lastIndexOf("/") + 1);
}
// 遍历加载器可识别类名称,找到可能的
for (String className : this.getClassLoader().getClassNames()) {
if (className.toLowerCase().endsWith(simpleName)) {
Class<?> type = this.getClassLoader().findClass(className);
if (type != null) {
XmlType xmlType = type.getAnnotation(XmlType.class);
if (xmlType != null) {
String xmlName = type.getSimpleName();
if (!xmlType.name().equals("##default")) {
xmlName = xmlType.name();
}
if (!xmlType.namespace().equals("##default")) {
if (xmlType.namespace().equals("/")) {
xmlName = xmlType.namespace() + xmlName;
} else {
xmlName = xmlType.namespace() + "/" + xmlName;
}
}
if (xmlName.equalsIgnoreCase(name)) {
return type;
}
}
}
}
}
throw new ClassNotFoundException(name);
}
示例15: findTypeNameUncached
import javax.xml.bind.annotation.XmlType; //导入方法依赖的package包/类
private QName findTypeNameUncached(Field field, Class contentClass, String schemaNamespace) {
if (field != null) {
XmlSchemaType xmlSchemaType = field.getAnnotation(XmlSchemaType.class);
if (xmlSchemaType != null) {
return new QName(xmlSchemaType.namespace(), xmlSchemaType.name());
}
}
QName typeName = XsdTypeMapper.getJavaToXsdMapping(contentClass);
if (typeName != null) {
return typeName;
}
// TODO the following code is similar to determineTypeForClass
XmlType xmlType = (XmlType) contentClass.getAnnotation(XmlType.class);
if (xmlType != null) {
String propTypeLocalPart = xmlType.name();
String propTypeNamespace = xmlType.namespace();
if (propTypeNamespace.equals(BeanMarshaller.DEFAULT_PLACEHOLDER)) {
PrismSchema schema = prismContext.getSchemaRegistry().findSchemaByCompileTimeClass(contentClass);
if (schema != null && schema.getNamespace() != null) {
propTypeNamespace = schema.getNamespace(); // should be non-null for properly initialized schemas
} else {
// schemaNamespace is only a poor indicator of required namespace (consider e.g. having c:UserType in apit:ObjectListType)
// so we use it only if we couldn't find anything else
propTypeNamespace = schemaNamespace;
}
}
return new QName(propTypeNamespace, propTypeLocalPart);
}
return null;
}