Java XML Namespace getNamespace(Class clazz, String namespace)

Here you can find the source of getNamespace(Class clazz, String namespace)

Description

get Namespace

License

Apache License

Declaration

private static String getNamespace(Class clazz, String namespace) 

Method Source Code

//package com.java2s;
/**//from  w ww . j a  v a2s .  c o  m
 *  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.
 */

import java.util.StringTokenizer;
import javax.jws.WebService;

import javax.xml.ws.WebServiceProvider;

public class Main {
    private static String getNamespace(Class clazz, String namespace) {
        if (namespace == null || namespace.trim().length() == 0) {
            Package pkg = clazz.getPackage();
            if (pkg == null) {
                return null;
            } else {
                return getNamespace(pkg.getName());
            }
        } else {
            return namespace.trim();
        }
    }

    private static String getNamespace(String packageName) {
        if (packageName == null || packageName.length() == 0) {
            return null;
        }
        StringTokenizer tokenizer = new StringTokenizer(packageName, ".");
        String[] tokens;
        if (tokenizer.countTokens() == 0) {
            tokens = new String[0];
        } else {
            tokens = new String[tokenizer.countTokens()];
            for (int i = tokenizer.countTokens() - 1; i >= 0; i--) {
                tokens[i] = tokenizer.nextToken();
            }
        }
        StringBuilder namespace = new StringBuilder("http://");
        String dot = "";
        for (int i = 0; i < tokens.length; i++) {
            if (i == 1) {
                dot = ".";
            }
            namespace.append(dot + tokens[i]);
        }
        namespace.append('/');
        return namespace.toString();
    }

    public static String getName(Class clazz) {
        WebService webService = (WebService) clazz.getAnnotation(WebService.class);
        if (webService == null) {
            WebServiceProvider webServiceProvider = (WebServiceProvider) clazz
                    .getAnnotation(WebServiceProvider.class);
            if (webServiceProvider == null) {
                throw new IllegalArgumentException("The " + clazz.getName() + " is not annotated");
            }
            return clazz.getSimpleName();
        } else {
            String sei = webService.endpointInterface();
            if (sei == null || sei.trim().length() == 0) {
                return getName(clazz, webService.name());
            } else {
                try {
                    Class seiClass = clazz.getClassLoader().loadClass(sei.trim());
                    return getNameFromSEI(seiClass);
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException("Unable to load SEI class: " + sei, e);
                }
            }
        }
    }

    private static String getName(Class clazz, String name) {
        if (name == null || name.trim().length() == 0) {
            return clazz.getSimpleName();
        } else {
            return name.trim();
        }
    }

    private static String getNameFromSEI(Class seiClass) {
        WebService webService = (WebService) seiClass.getAnnotation(WebService.class);
        if (webService == null) {
            throw new IllegalArgumentException("The " + seiClass.getName() + " is not annotated");
        }
        return getName(seiClass, webService.name());
    }
}

Related

  1. getBaseNamespace(InputStream owlStream)
  2. getBuilder(boolean ignoreComments, boolean validating, boolean ignoreContentWhitespace, boolean isNamespaceAware)
  3. getCarbonNamespace()
  4. getDefaultNamespaceURI(final XMLStreamWriter writer)
  5. getFactory(boolean validate, boolean namespaceAware)
  6. getNamespace(Class klass)
  7. getNamespace(final Package targetPackage)
  8. getNamespace(Map namespaces, String str, String defaultNamespace)
  9. getNamespaceContext()