package net.sourceforge.javautil.common.jaxb;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Marshaller.Listener;
import javax.xml.bind.PropertyException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.attachment.AttachmentMarshaller;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Result;
import javax.xml.validation.Schema;
import net.sourceforge.javautil.common.io.IInputSource;
import net.sourceforge.javautil.common.jaxb.xml.XMLReader;
import net.sourceforge.javautil.common.jaxb.xml.writer.XMLWriter;
import net.sourceforge.javautil.common.jaxb.xml.writer.XMLWriterContext;
import org.w3c.dom.Node;
import org.xml.sax.ContentHandler;
/**
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class JavaXML {
public static JavaXML newInstance (Class<?>... types) throws JAXBException {
JavaXMLBeanInitializationContext context = new JavaXMLBeanInitializationContext(new StandardJAXBMapper());
for (Class<?> type : types) {
context.resolve(type);
}
return new JavaXML(context.types);
}
private static final JavaXMLDocument DOCUMENT = new JavaXMLDocument();
protected final Map<Class<?>, JavaXMLBean> elements;
protected final Map<String, JavaXMLBean> nameMap = new HashMap<String, JavaXMLBean>();
protected final Map<String, JavaXMLBean> rootMap = new HashMap<String, JavaXMLBean>();
private JavaXML(Map<Class<?>, JavaXMLBean> elements) {
this.elements = elements;
for (JavaXMLBean bean : elements.values()) {
String id = !"".equals( bean.getNamespace() ) ? bean.getNamespace() + ":" + bean.getName() : bean.getName();
if (bean.isRoot()) {
rootMap.put(id, bean);
}
nameMap.put(id, bean);
}
}
public JavaXMLBeanInstance getInstance (JavaXMLUnmarshallerContext context, String namespace, String name) {
String id = namespace != null && !"".equals(namespace) ? namespace + ":" + name : name;
JavaXMLBean bean = nameMap.get(id);
if (bean == null) {
throw new IllegalArgumentException("Unrecognized XML bean: " + id);
}
return new JavaXMLBeanInstance(context, bean);
}
public JavaXMLBean getTemplate (Object instance) {
return this.elements.get(instance.getClass());
}
public <T> T unmarshal (IInputSource input) throws JAXBException, IOException {
return (T) XMLReader.parse(input, DOCUMENT, new JavaXMLUnmarshallerContext(this)).getResult();
}
public <O extends OutputStream> O marshal (Object instance, O output) throws JAXBException {
try {
Writer writer = new OutputStreamWriter(output);
new JavaXMLBeanInstance(instance, getTemplate(instance)).accept(
XMLWriter.DOCUMENT_WRITER,
new XMLWriterContext(writer),
new JavaXMLMarshallerContext(this)
);
writer.flush();
return output;
} catch (IOException e) {
throw new JAXBException(e);
}
}
}