Java XML QName Get getElementQName(Class type)

Here you can find the source of getElementQName(Class type)

Description

get Element Q Name

License

Mozilla Public License

Declaration

private static <T> QName getElementQName(Class<T> type) 

Method Source Code

//package com.java2s;
/*/*from w  w  w .j  a  v a  2  s .  c  o m*/
 * The contents of this file are subject to the Mozilla Public 
 * License Version 1.1 (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.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an 
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express 
 * or implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 *
 * The Original Code is OIOSAML Java Service Provider.
 * 
 * The Initial Developer of the Original Code is Trifork A/S. Portions 
 * created by Trifork A/S are Copyright (C) 2008 Danish National IT 
 * and Telecom Agency (http://www.itst.dk). All Rights Reserved.
 * 
 * Contributor(s):
 *   Joakim Recht <jre@trifork.com>
 *   Rolf Njor Jensen <rolf@trifork.com>
 *
 */

import java.lang.reflect.Field;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.xml.namespace.QName;

public class Main {
    private static final Map<Class<?>, QName> elementCache = new ConcurrentHashMap<Class<?>, QName>();

    private static <T> QName getElementQName(Class<T> type) {
        if (elementCache.containsKey(type))
            return elementCache.get(type);

        try {
            Field typeField;
            try {
                typeField = type.getDeclaredField("DEFAULT_ELEMENT_NAME");
            } catch (NoSuchFieldException ex) {
                typeField = type.getDeclaredField("ELEMENT_NAME");
            }

            QName objectQName = (QName) typeField.get(null);
            elementCache.put(type, objectQName);
            return objectQName;
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. getElementQName(Element element)
  2. getElementQName(Element xml)
  3. getElementQName(final Element el)
  4. getElements(Node context, QName qname)