org.milyn.javabean.dynamic.Descriptor Maven / Gradle / Ivy
The newest version!
/*
* Milyn - Copyright (C) 2006 - 2010
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License (version 2.1) as published by the Free Software
* Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU Lesser General Public License for more details:
* http://www.gnu.org/licenses/lgpl.txt
*/
package org.milyn.javabean.dynamic;
import org.milyn.Smooks;
import org.milyn.SmooksException;
import org.milyn.assertion.AssertArgument;
import org.milyn.cdr.SmooksConfigurationException;
import org.milyn.cdr.SmooksResourceConfiguration;
import org.milyn.cdr.SmooksResourceConfigurationList;
import org.milyn.cdr.XMLConfigDigester;
import org.milyn.cdr.xpath.SelectorStep;
import org.milyn.javabean.dynamic.ext.BeanWriterFactory;
import org.milyn.javabean.dynamic.resolvers.AbstractResolver;
import org.milyn.javabean.dynamic.resolvers.DefaultBindingConfigResolver;
import org.milyn.javabean.dynamic.resolvers.DefaultSchemaResolver;
import org.milyn.javabean.dynamic.serialize.BeanWriter;
import org.milyn.util.ClassUtil;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
/**
* Model Descriptor.
*
* @author [email protected]
*/
public class Descriptor {
public static final String DESCRIPTOR_NAMESPACE_POSTFIX = ".namespace";
public static final String DESCRIPTOR_SCHEMA_LOCATION_POSTFIX = ".schemaLocation";
public static final String DESCRIPTOR_BINDING_CONFIG_LOCATION_POSTFIX = ".bindingConfigLocation";
public static final String DESCRIPTOR_ORDER_POSTFIX = ".order";
private Smooks smooks;
private Schema schema;
private ClassLoader classloader = Descriptor.class.getClassLoader();
public Descriptor(List descriptors) throws SAXException, IOException {
AssertArgument.isNotNullAndNotEmpty(descriptors, "descriptors");
intialize(descriptors, new DefaultSchemaResolver(descriptors), new DefaultBindingConfigResolver(descriptors));
}
public Descriptor(String descriptorPath) throws SAXException, IOException {
AssertArgument.isNotNullAndNotEmpty(descriptorPath, "descriptorPath");
List descriptors = loadDescriptors(descriptorPath, getClass().getClassLoader());
intialize(descriptors, new DefaultSchemaResolver(descriptors), new DefaultBindingConfigResolver(descriptors));
}
public Descriptor(String descriptorPath, EntityResolver schemaResolver, EntityResolver bindingResolver, ClassLoader classloader) throws SAXException, IOException {
AssertArgument.isNotNullAndNotEmpty(descriptorPath, "descriptorPath");
AssertArgument.isNotNull(bindingResolver, "bindingResolver");
AssertArgument.isNotNull(classloader, "classloader");
this.classloader = classloader;
List descriptors = loadDescriptors(descriptorPath, classloader);
intialize(descriptors, schemaResolver, bindingResolver);
}
public Descriptor(List descriptors, EntityResolver schemaResolver, EntityResolver bindingResolver, ClassLoader classloader) throws SAXException, IOException {
AssertArgument.isNotNullAndNotEmpty(descriptors, "descriptors");
AssertArgument.isNotNull(bindingResolver, "bindingResolver");
AssertArgument.isNotNull(classloader, "classloader");
this.classloader = classloader;
intialize(descriptors, schemaResolver, bindingResolver);
}
public Smooks getSmooks() {
return smooks;
}
public Schema getSchema() {
return schema;
}
public Map, Map> getBeanWriters() {
return BeanWriterFactory.getBeanWriters(smooks.getApplicationContext());
}
public static List loadDescriptors(String descriptorPath, ClassLoader classLoader) {
List descriptorFiles = new ArrayList();
try {
List resources = ClassUtil.getResources(descriptorPath, classLoader);
if(resources.isEmpty()) {
throw new IllegalStateException("Failed to locate any model descriptor file by the name '" + descriptorPath + "' on the classpath.");
}
for(URL resource : resources) {
InputStream resStream = resource.openStream();
descriptorFiles.add(loadDescriptor(resStream));
}
} catch (IOException e) {
throw new IllegalStateException("Unexpected IO Exception when reading Dynamic Namespace Descriptor files from classpath.", e);
}
return descriptorFiles;
}
public static Properties loadDescriptor(InputStream descriptorStream) throws IOException {
AssertArgument.isNotNull(descriptorStream, "descriptorStream");
try {
Properties descriptor = new Properties();
descriptor.load(descriptorStream);
return descriptor;
} finally {
descriptorStream.close();
}
}
private void intialize(List descriptors, EntityResolver schemaResolver, EntityResolver bindingResolver) throws SAXException, IOException {
if(schemaResolver instanceof AbstractResolver) {
if(((AbstractResolver)schemaResolver).getClassLoader() != classloader) {
throw new SmooksException("Schema EntityResolver '" + schemaResolver.getClass().getName() + "' not using the same ClassLoader as this Descriptor instance.");
}
}
if(bindingResolver instanceof AbstractResolver) {
if(((AbstractResolver)bindingResolver).getClassLoader() != classloader) {
throw new SmooksException("Binding EntityResolver '" + bindingResolver.getClass().getName() + "' not using the same ClassLoader as this Descriptor instance.");
}
}
if(schemaResolver != null) {
this.schema = newSchemaInstance(descriptors, schemaResolver);
}
this.smooks = newSmooksInstance(descriptors, bindingResolver);
}
private Schema newSchemaInstance(List descriptors, EntityResolver schemaResolver) throws SAXException, IOException {
List