All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jvnet.hyperjaxb3.hibernate.customizations.impl.DefaultCustomizationStrategy Maven / Gradle / Ivy

The newest version!
package org.jvnet.hyperjaxb3.hibernate.customizations.impl;

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 javax.xml.namespace.QName;
import javax.xml.transform.dom.DOMResult;

import org.jvnet.hyperjaxb3.hibernate.customizations.ClassType;
import org.jvnet.hyperjaxb3.hibernate.customizations.ComplexCollectionPropertyType;
import org.jvnet.hyperjaxb3.hibernate.customizations.ComplexSinglePropertyType;
import org.jvnet.hyperjaxb3.hibernate.customizations.ComponentType;
import org.jvnet.hyperjaxb3.hibernate.customizations.CompositeElementType;
import org.jvnet.hyperjaxb3.hibernate.customizations.CustomizationStrategy;
import org.jvnet.hyperjaxb3.hibernate.customizations.FieldItemType;
import org.jvnet.hyperjaxb3.hibernate.customizations.IdType;
import org.jvnet.hyperjaxb3.hibernate.customizations.SimpleCollectionPropertyType;
import org.jvnet.hyperjaxb3.hibernate.customizations.SimpleSinglePropertyType;
import org.jvnet.hyperjaxb3.hibernate.customizations.VersionType;
import org.jvnet.hyperjaxb3.hibernate.customizations.WildcardCollectionPropertyType;
import org.jvnet.hyperjaxb3.hibernate.customizations.WildcardSinglePropertyType;
import org.jvnet.jaxb2_commons.util.CustomizationUtils;
import org.w3c.dom.Document;

import com.sun.tools.xjc.model.CCustomizations;
import com.sun.tools.xjc.model.CPluginCustomization;
import com.sun.tools.xjc.outline.ClassOutline;
import com.sun.tools.xjc.outline.FieldOutline;

public class DefaultCustomizationStrategy implements CustomizationStrategy {

	private static final JAXBContext CONTEXT;
	static {
		try {
			CONTEXT = JAXBContext.newInstance(Constants.CONTEXT_PATH,
					IdType.class.getClassLoader());
		} catch (JAXBException jaxbex) {
			throw new ExceptionInInitializerError(jaxbex);
		}
	}

	private Object getCustomization(FieldOutline fieldOutline, QName name) {
		final CCustomizations customizations = CustomizationUtils
				.getCustomizations(fieldOutline);
		return getCustomization(customizations, name);
	}

	private Object getCustomization(ClassOutline classOutline, QName name) {
		final CCustomizations customizations = CustomizationUtils
				.getCustomizations(classOutline);
		return getCustomization(customizations, name);
	}

	private Object getCustomization(final CCustomizations customizations,
			QName name) {
		final CPluginCustomization customization = customizations.find(name
				.getNamespaceURI(), name.getLocalPart());
		if (customization != null)
			customization.markAsAcknowledged();
		final Object object = unmarshallCustomization(customization);
		if (object instanceof JAXBElement) {
			return ((JAXBElement) object).getValue();

		} else {
			return object;
		}
	}

	public org.w3c.dom.Element getCustomizationElement(Object object) {
		if (object == null) {
			return null;
		} else {
			try {
				final Marshaller marshaller = CONTEXT.createMarshaller();
				final DOMResult result = new DOMResult();
				marshaller.marshal(object, result);
				final Document document = (Document) result.getNode();
				return document.getDocumentElement();
			} catch (JAXBException jaxbex) {
				// todo
				return null;
			}
		}
	}

	/**
	 * @param customization
	 * @return
	 */
	private Object unmarshallCustomization(
			final CPluginCustomization customization) {
		if (customization == null) {
			return null;
		} else {
			try {
				final Unmarshaller unmarshaller = CONTEXT.createUnmarshaller();
				final Object object = unmarshaller
						.unmarshal(customization.element);
				return object;
			} catch (JAXBException jaxbex) {
				// todo
				return null;
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jvnet.hyperjaxb3.hibernate.customizations.impl.CustomizationStrategy#getClazz(com.sun.tools.xjc.model.CCustomizable)
	 */
	public ClassType getClazz(ClassOutline classOutline) {
		final ClassType cclass = (ClassType) getCustomization(classOutline,
				Constants.CLASS);
		if (cclass != null) {
			return cclass;
		} else {
			return new ClassType();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jvnet.hyperjaxb3.hibernate.customizations.impl.CustomizationStrategy#getCompositeElement(com.sun.tools.xjc.model.CCustomizable)
	 */
	public CompositeElementType getCompositeElement(ClassOutline item) {
		final CompositeElementType ccompositeElement = (CompositeElementType) getCustomization(
				item, Constants.COMPOSITE_ELEMENT);
		return ccompositeElement;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jvnet.hyperjaxb3.hibernate.customizations.impl.CustomizationStrategy#getComponent(com.sun.tools.xjc.model.CCustomizable)
	 */
	public ComponentType getComponent(ClassOutline classOutline) {
		final ComponentType ccomponent = (ComponentType) getCustomization(
				classOutline, Constants.COMPONENT);
		return ccomponent;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jvnet.hyperjaxb3.hibernate.customizations.impl.CustomizationStrategy#getSimpleSingleProperty(com.sun.tools.xjc.model.CCustomizable)
	 */
	public SimpleSinglePropertyType getSimpleSingleProperty(FieldOutline item) {
		final SimpleSinglePropertyType cproperty = (SimpleSinglePropertyType) getCustomization(
				item, Constants.SIMPLE_SINGLE_PROPERTY);
		if (cproperty != null) {
			return cproperty;
		} else {
			return new SimpleSinglePropertyType();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jvnet.hyperjaxb3.hibernate.customizations.impl.CustomizationStrategy#getComplexSingleProperty(com.sun.tools.xjc.model.CCustomizable)
	 */
	public ComplexSinglePropertyType getComplexSingleProperty(FieldOutline item) {
		final ComplexSinglePropertyType cproperty = (ComplexSinglePropertyType) getCustomization(
				item, Constants.COMPLEX_SINGLE_PROPERTY);
		if (cproperty != null) {
			return cproperty;
		} else {
			return new ComplexSinglePropertyType();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jvnet.hyperjaxb3.hibernate.customizations.impl.CustomizationStrategy#getWildcardSingleProperty(com.sun.tools.xjc.model.CCustomizable)
	 */
	public WildcardSinglePropertyType getWildcardSingleProperty(
			FieldOutline item) {
		final WildcardSinglePropertyType cproperty = (WildcardSinglePropertyType) getCustomization(
				item, Constants.WILDCARD_SINGLE_PROPERTY);
		if (cproperty != null) {
			return cproperty;
		} else {
			return new WildcardSinglePropertyType();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jvnet.hyperjaxb3.hibernate.customizations.impl.CustomizationStrategy#getSimpleCollectionProperty(com.sun.tools.xjc.model.CCustomizable)
	 */
	public SimpleCollectionPropertyType getSimpleCollectionProperty(
			FieldOutline item) {
		final SimpleCollectionPropertyType cproperty = (SimpleCollectionPropertyType) getCustomization(
				item, Constants.SIMPLE_COLLECTION_PROPERTY);
		if (cproperty != null) {
			return cproperty;
		} else {
			return new SimpleCollectionPropertyType();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jvnet.hyperjaxb3.hibernate.customizations.impl.CustomizationStrategy#getId(com.sun.tools.xjc.model.CCustomizable)
	 */
	public IdType getId(ClassOutline item) {
		return (IdType) getCustomization(item, Constants.ID);
	}

	public IdType getId(FieldOutline item) {
		return (IdType) getCustomization(item, Constants.ID);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jvnet.hyperjaxb3.hibernate.customizations.impl.CustomizationStrategy#getVersion(com.sun.tools.xjc.model.CCustomizable)
	 */
	public VersionType getVersion(ClassOutline item) {
		return (VersionType) getCustomization(item, Constants.VERSION);
	}

	public VersionType getVersion(FieldOutline item) {
		return (VersionType) getCustomization(item, Constants.VERSION);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jvnet.hyperjaxb3.hibernate.customizations.impl.CustomizationStrategy#getComplexCollectionProperty(com.sun.tools.xjc.model.CCustomizable)
	 */
	public ComplexCollectionPropertyType getComplexCollectionProperty(
			FieldOutline item) {
		final ComplexCollectionPropertyType cproperty = (ComplexCollectionPropertyType) getCustomization(
				item, Constants.COMPLEX_COLLECTION_PROPERTY);
		if (cproperty != null) {
			return cproperty;
		} else {
			return new ComplexCollectionPropertyType();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jvnet.hyperjaxb3.hibernate.customizations.impl.CustomizationStrategy#getWildcardCollectionProperty(com.sun.tools.xjc.model.CCustomizable)
	 */
	public WildcardCollectionPropertyType getWildcardCollectionProperty(
			FieldOutline item) {
		final WildcardCollectionPropertyType cproperty = (WildcardCollectionPropertyType) getCustomization(
				item, Constants.WILDCARD_COLLECTION_PROPERTY);
		if (cproperty != null) {
			return cproperty;
		} else {
			return new WildcardCollectionPropertyType();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jvnet.hyperjaxb3.hibernate.customizations.impl.CustomizationStrategy#getFieldItem(com.sun.tools.xjc.model.CCustomizable)
	 */
	public FieldItemType getFieldItem(FieldOutline item) {
		return (FieldItemType) getCustomization(item, Constants.FIELD_ITEM);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jvnet.hyperjaxb3.hibernate.customizations.impl.MappingCreatingStrategy#isIgnored(com.sun.tools.xjc.outline.FieldOutline)
	 */
	public boolean isIgnored(FieldOutline item) {
		return getCustomization(item, Constants.IGNORED) != null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jvnet.hyperjaxb3.hibernate.customizations.impl.MappingCreatingStrategy#isIgnored(com.sun.tools.xjc.outline.ClassOutline)
	 */
	public boolean isIgnored(ClassOutline item) {
		return getCustomization(item, Constants.IGNORED) != null
				|| (item.getSuperClass() != null && isIgnored(item
						.getSuperClass()));
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy