<commit_before>/*
 * Copyright 2001-2008 The Apache Software Foundation.
 * 
 * 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.
 *
 */
package org.apache.juddi.jaxb;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Element;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class JAXBMarshaller {
	private static Log logger = LogFactory.getLog(JAXBMarshaller.class);
	
	public static final String PACKAGE_UDDIAPI = "org.uddi.api_v3";
	public static final String PACKAGE_SUBSCRIPTION = "org.uddi.sub_v3";
	public static final String PACKAGE_JUDDIAPI = "org.apache.juddi.api_v3";
	public static final String PACKAGE_JUDDI = "org.apache.juddi";

	private static final Map<String, JAXBContext> JAXBContexts = new HashMap<String, JAXBContext>();
	static {
		try {
			JAXBContexts.put(PACKAGE_UDDIAPI, JAXBContext.newInstance(PACKAGE_UDDIAPI));
			JAXBContexts.put(PACKAGE_SUBSCRIPTION, JAXBContext.newInstance(PACKAGE_SUBSCRIPTION));
			JAXBContexts.put(PACKAGE_JUDDIAPI, JAXBContext.newInstance(PACKAGE_JUDDIAPI));
			JAXBContexts.put(PACKAGE_JUDDI, JAXBContext.newInstance(PACKAGE_JUDDI));
		} catch (JAXBException e) {
			logger.error("Initialization of JAXBMarshaller failed:" + e, e);
			throw new ExceptionInInitializerError(e);
		}
	}
	
	@SuppressWarnings("rawtypes")
	public static Object unmarshallFromInputStream(InputStream inputStream, String thePackage) throws JAXBException {
		Object obj = null;
		if (inputStream != null) {
			JAXBContext jc = JAXBContexts.get(thePackage);
			Unmarshaller unmarshaller = jc.createUnmarshaller();
			obj = ((JAXBElement)unmarshaller.unmarshal(inputStream)).getValue();
		}
		else
			logger.error("A null input stream was provided");

		return obj;
		
	}
	
	public static Object unmarshallFromFileResource(String fileName, String thePackage) throws JAXBException, IOException {
		Object obj = null;
		URL url = Thread.currentThread().getContextClassLoader().getResource(fileName);
		if (url==null) {
			logger.error("Could not find resource: " + fileName);
		} else {
			InputStream resourceStream =url.openStream();
	
			obj = unmarshallFromInputStream(resourceStream, thePackage);
		}
		return obj;
	}

	public static Object unmarshallFromString(String rawObject, String thePackage) throws JAXBException {
		Object obj = null;
		if (rawObject != null && rawObject.length() > 0) {
			ByteArrayInputStream bais = new ByteArrayInputStream(rawObject.getBytes());
			obj = unmarshallFromInputStream(bais, thePackage);
		}
		else
			logger.error("The raw object provided is null or empty");
		return obj;
	}

	public static String marshallToString(Object object, String thePackage) throws JAXBException {
		String rawObject = null;

		JAXBContext jc = JAXBContexts.get(thePackage);
		Marshaller marshaller = jc.createMarshaller();
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		marshaller.marshal(object, baos);
		rawObject = baos.toString();
		
		return rawObject;
	}

	public static Element marshallToElement(Object object, String thePackage, Element element) throws JAXBException {
		
		JAXBContext jc = JAXBContexts.get(thePackage);
		Marshaller marshaller = jc.createMarshaller();
		marshaller.marshal(object, element);	
		return element;
	}
	
	public static Object unmarshallFromElement(Element element, String thePackage) throws JAXBException {
		JAXBContext jc = JAXBContexts.get(thePackage);
		Unmarshaller unmarshaller = jc.createUnmarshaller();
		@SuppressWarnings("rawtypes")
		Object obj = ((JAXBElement) unmarshaller.unmarshal(element)).getValue();
		return obj;
	}
	
}<commit_msg>JUDDI-472 lazy loading the JAXBContexts<commit_after>/*
 * Copyright 2001-2008 The Apache Software Foundation.
 * 
 * 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.
 *
 */
package org.apache.juddi.jaxb;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Element;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class JAXBMarshaller {
	private static Log logger = LogFactory.getLog(JAXBMarshaller.class);
	
	public static final String PACKAGE_UDDIAPI = "org.uddi.api_v3";
	public static final String PACKAGE_SUBSCRIPTION = "org.uddi.sub_v3";
	public static final String PACKAGE_JUDDIAPI = "org.apache.juddi.api_v3";
	public static final String PACKAGE_JUDDI = "org.apache.juddi";
	private static final Map<String, JAXBContext> JAXBContexts = new HashMap<String, JAXBContext>();

	private static JAXBContext getContext(String packageName) {
		if (!JAXBContexts.containsKey(packageName)) {
			try {
				JAXBContexts.put(packageName, JAXBContext.newInstance(packageName));
			} catch (JAXBException e) {
				logger.error("Initialization of JAXBMarshaller failed:" + e, e);
				throw new ExceptionInInitializerError(e);
			}
		}
		
		return JAXBContexts.get(packageName);
	}
	
	@SuppressWarnings("rawtypes")
	public static Object unmarshallFromInputStream(InputStream inputStream, String thePackage) throws JAXBException {
		Object obj = null;
		if (inputStream != null) {
			JAXBContext jc = getContext(thePackage);
			Unmarshaller unmarshaller = jc.createUnmarshaller();
			obj = ((JAXBElement)unmarshaller.unmarshal(inputStream)).getValue();
		}
		else
			logger.error("A null input stream was provided");

		return obj;
		
	}
	
	public static Object unmarshallFromFileResource(String fileName, String thePackage) throws JAXBException, IOException {
		Object obj = null;
		URL url = Thread.currentThread().getContextClassLoader().getResource(fileName);
		if (url==null) {
			logger.error("Could not find resource: " + fileName);
		} else {
			InputStream resourceStream =url.openStream();
	
			obj = unmarshallFromInputStream(resourceStream, thePackage);
		}
		return obj;
	}

	public static Object unmarshallFromString(String rawObject, String thePackage) throws JAXBException {
		Object obj = null;
		if (rawObject != null && rawObject.length() > 0) {
			ByteArrayInputStream bais = new ByteArrayInputStream(rawObject.getBytes());
			obj = unmarshallFromInputStream(bais, thePackage);
		}
		else
			logger.error("The raw object provided is null or empty");
		return obj;
	}

	public static String marshallToString(Object object, String thePackage) {
		String rawObject = null;

		try {
			JAXBContext jc = getContext(thePackage);
			Marshaller marshaller = jc.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			marshaller.marshal(object, baos);
			rawObject = baos.toString();
		} catch (JAXBException e) {
			logger.error(e.getMessage(),e);
		}
		
		return rawObject;
	}

	public static Element marshallToElement(Object object, String thePackage, Element element) throws JAXBException {
		
		JAXBContext jc = getContext(thePackage);
		Marshaller marshaller = jc.createMarshaller();
		marshaller.marshal(object, element);	
		return element;
	}
	
	public static Object unmarshallFromElement(Element element, String thePackage) throws JAXBException {
		JAXBContext jc = getContext(thePackage);
		Unmarshaller unmarshaller = jc.createUnmarshaller();
		@SuppressWarnings("rawtypes")
		Object obj = ((JAXBElement) unmarshaller.unmarshal(element)).getValue();
		return obj;
	}
	
}
{'fraction_non_alphanumeric': 0.09832341806381828, 'fraction_numerical': 0.00411032990805841, 'mean_word_length': 5.043137254901961, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7058886', 'n_tokens_mistral': 3024, 'n_tokens_neox': 2830, 'n_words': 725}