Java XML JAXB Context getJAXBContext(Class clazz)

Here you can find the source of getJAXBContext(Class clazz)

Description

get JAXB Context

License

Apache License

Declaration

public static JAXBContext getJAXBContext(Class<?> clazz) throws JAXBException 

Method Source Code


//package com.java2s;
/*//from   w  w w .  j a  va  2 s  .  com
 * JBoss, Home of Professional Open Source
 *
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed 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 javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import java.util.HashMap;

public class Main {
    private static HashMap<String, JAXBContext> jaxbContextHash = new HashMap<String, JAXBContext>();

    public static JAXBContext getJAXBContext(String path) throws JAXBException {
        JAXBContext jx = jaxbContextHash.get(path);
        if (jx == null) {
            jx = JAXBContext.newInstance(path);
            jaxbContextHash.put(path, jx);
        }
        return jx;
    }

    public static JAXBContext getJAXBContext(String... paths) throws JAXBException {
        int len = paths.length;
        if (len == 0)
            return getJAXBContext(paths[0]);

        StringBuilder builder = new StringBuilder();
        for (String path : paths) {
            builder.append(path).append(":");
        }

        String finalPath = builder.toString();

        JAXBContext jx = jaxbContextHash.get(finalPath);
        if (jx == null) {
            jx = JAXBContext.newInstance(finalPath);
            jaxbContextHash.put(finalPath, jx);
        }
        return jx;
    }

    public static JAXBContext getJAXBContext(Class<?> clazz) throws JAXBException {
        String clazzName = clazz.getName();

        JAXBContext jx = jaxbContextHash.get(clazzName);
        if (jx == null) {
            jx = JAXBContext.newInstance(clazz);
            jaxbContextHash.put(clazzName, jx);
        }
        return jx;
    }
}

Related

  1. getContextPath(Class... classes)
  2. getCustomContext()
  3. getCustomIdentityJAXBContext()
  4. getJaxbContext(Class... classes)
  5. getJaxbContext(Class... classTypes)
  6. getJAXBContext(Class clazz)
  7. getJaxbContext(Class clazz)
  8. getJAXBContext(Class clazz)
  9. getJAXBContext(Class objclass)