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

org.jpmml.model.ExtensionUtil Maven / Gradle / Ivy

There is a newer version: 1.6.5
Show newest version
/*
 * Copyright (c) 2013 KNIME.com AG, Zurich, Switzerland
 */
package org.jpmml.model;

import java.util.*;

import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;

import org.dmg.pmml.*;

import org.w3c.dom.Node;

public class ExtensionUtil {

	private ExtensionUtil(){
	}

	static
	public  E getExtension(Extension extension, Class clazz) throws JAXBException {
		JAXBContext context = JAXBUtil.getContext();

		Unmarshaller unmarshaller = context.createUnmarshaller();

		return getExtension(extension, clazz, unmarshaller);
	}

	static
	public  E getExtension(Extension extension, Class clazz, Unmarshaller unmarshaller) throws JAXBException {
		XmlRootElement rootElement = clazz.getAnnotation(XmlRootElement.class);
		if(rootElement == null){
			throw new IllegalArgumentException();
		}

		String name = rootElement.name();

		List objects = extension.getContent();
		for(Object object : objects){

			if(object instanceof Node){
				Node node = (Node)object;

				if((name).equals(node.getLocalName())){
					Source source = new DOMSource(node);

					return clazz.cast(JAXBUtil.unmarshal(unmarshaller, source));
				}
			}
		}

		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy