|
Axis 2 example source code file (XMLRootElementUtil.java)
The Axis 2 XMLRootElementUtil.java source code/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.axis2.jaxws.utility; import org.apache.axis2.java.security.AccessController; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlEnumValue; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlSchema; import javax.xml.namespace.QName; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringTokenizer; /** * */ public class XMLRootElementUtil { private static final Log log = LogFactory.getLog(XMLRootElementUtil.class); /** Constructor is intentionally private. This class only provides static utility methods */ private XMLRootElementUtil() { } /** * @param clazz * @return namespace of root element qname or null if this is not object does not represent a * root element */ public static QName getXmlRootElementQNameFromObject(Object obj) { // A JAXBElement stores its name if (obj instanceof JAXBElement) { return ((JAXBElement)obj).getName(); } Class clazz = (obj instanceof java.lang.Class) ? (Class)obj : obj.getClass(); return getXmlRootElementQName(clazz); } /** * @param clazz * @return namespace of root element qname or null if this is not object does not represent a * root element */ public static QName getXmlRootElementQName(Class clazz) { // See if the object represents a root element XmlRootElement root = (XmlRootElement)clazz.getAnnotation(XmlRootElement.class); if (root == null) { return null; } String name = root.name(); String namespace = root.namespace(); // The name may need to be defaulted if (name == null || name.length() == 0 || name.equals("##default")) { name = getSimpleName(clazz.getCanonicalName()); } // The namespace may need to be defaulted if (namespace == null || namespace.length() == 0 || namespace.equals("##default")) { Package pkg = clazz.getPackage(); XmlSchema schema = (XmlSchema)pkg.getAnnotation(XmlSchema.class); if (schema != null) { namespace = schema.namespace(); } else { namespace = ""; } } return new QName(namespace, name); } /** * @param clazz * @return namespace of root element qname or null if this is not object does not represent a root element */ public static String getEnumValue(Enum myEnum){ Field f; String value; try { f = myEnum.getClass().getField(myEnum.name()); f.setAccessible(true); XmlEnumValue xev = f.getAnnotation(XmlEnumValue.class); if (xev == null){ value = f.getName(); } else { value = xev.value(); } } catch (SecurityException e) { value = null; } catch (NoSuchFieldException e) { value = null; } return value; } /** * utility method to get the last token in a "."-delimited package+classname string * * @return */ private static String getSimpleName(String in) { if (in == null || in.length() == 0) { return in; } String out = null; StringTokenizer tokenizer = new StringTokenizer(in, "."); if (tokenizer.countTokens() == 0) out = in; else { while (tokenizer.hasMoreTokens()) { out = tokenizer.nextToken(); } } return out; } /** * The JAXBClass has a set of bean properties each represented by a PropertyDescriptor Each of * the fields of the class has an associated xml name. The method returns a map where the key is * the xml name and value is the PropertyDescriptor * * @param jaxbClass * @return map */ public static Map<String, PropertyDescriptorPlus> createPropertyDescriptorMap(Class jaxbClass) throws NoSuchFieldException, IntrospectionException { if (log.isDebugEnabled()) { log.debug("Get the PropertyDescriptor[] for " + jaxbClass); } PropertyDescriptor[] pds = Introspector.getBeanInfo(jaxbClass).getPropertyDescriptors(); Map<String, PropertyDescriptorPlus> map = new HashMap Other Axis 2 examples (source code examples)Here is a short list of links related to this Axis 2 XMLRootElementUtil.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.