package com.sun.tools.internal.xjc.reader.dtd.bindinfo;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.validation.ValidatorHandler;
import com.sun.codemodel.internal.ClassType;
import com.sun.codemodel.internal.JClass;
import com.sun.codemodel.internal.JClassAlreadyExistsException;
import com.sun.codemodel.internal.JCodeModel;
import com.sun.codemodel.internal.JDefinedClass;
import com.sun.codemodel.internal.JPackage;
import com.sun.istack.internal.SAXParseException2;
import com.sun.tools.internal.xjc.AbortException;
import com.sun.tools.internal.xjc.ErrorReceiver;
import com.sun.tools.internal.xjc.SchemaCache;
import com.sun.tools.internal.xjc.model.CCustomizations;
import com.sun.tools.internal.xjc.model.CPluginCustomization;
import com.sun.tools.internal.xjc.model.Model;
import com.sun.tools.internal.xjc.reader.Const;
import com.sun.tools.internal.xjc.util.CodeModelClassFactory;
import com.sun.tools.internal.xjc.util.ErrorReceiverFilter;
import com.sun.tools.internal.xjc.util.ForkContentHandler;
import com.sun.xml.internal.bind.v2.util.XmlFactory;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
public class BindInfo
{
protected final ErrorReceiver errorReceiver;
final Model model;
private final String defaultPackage;
public BindInfo(Model model, InputSource source, ErrorReceiver _errorReceiver) throws AbortException {
this( model, parse(model,source,_errorReceiver), _errorReceiver);
}
public BindInfo(Model model, Document _dom, ErrorReceiver _errorReceiver) {
this.model = model;
this.dom = _dom.getDocumentElement();
this.codeModel = model.codeModel;
this.errorReceiver = _errorReceiver;
this.classFactory = new CodeModelClassFactory(_errorReceiver);
this.defaultPackage = model.options.defaultPackage;
model.getCustomizations().addAll(getGlobalCustomizations());
for( Element ele : DOMUtil.getChildElements(dom,"element")) {
BIElement e = new BIElement(this,ele);
elements.put(e.name(),e);
}
BIUserConversion.addBuiltinConversions(this,conversions);
for( Element cnv : DOMUtil.getChildElements(dom,"conversion")) {
BIConversion c = new BIUserConversion(this,cnv);
conversions.put(c.name(),c);
}
for( Element en : DOMUtil.getChildElements(dom,"enumeration")) {
BIConversion c = BIEnumeration.create( en, this );
conversions.put(c.name(),c);
}
for( Element itf : DOMUtil.getChildElements(dom,"interface")) {
BIInterface c = new BIInterface(itf);
interfaces.put(c.name(),c);
}
}
final JCodeModel codeModel;
final CodeModelClassFactory classFactory;
private final Element dom;
private final Map<String,BIConversion> conversions = new HashMap<String,BIConversion>();
private final Map<String,BIElement> elements = new HashMap<String,BIElement>();
private final Map<String,BIInterface> interfaces = new HashMap<String,BIInterface>();
private static final String XJC_NS = Const.XJC_EXTENSION_URI;
public Long getSerialVersionUID() {
Element serial = DOMUtil.getElement(dom,XJC_NS,"serializable");
if(serial==null) return null;
String v = DOMUtil.getAttribute(serial,"uid");
if(v==null) v="1";
return new Long(v);
}
public JClass getSuperClass() {
Element sc = DOMUtil.getElement(dom,XJC_NS,"superClass");
if (sc == null) return null;
JDefinedClass c;
try {
String v = DOMUtil.getAttribute(sc,"name");
if(v==null) return null;
c = codeModel._class(v);
c.hide();
} catch (JClassAlreadyExistsException e) {
c = e.getExistingClass();
}
return c;
}
public JClass getSuperInterface() {
Element sc = DOMUtil.getElement(dom,XJC_NS,"superInterface");
if (sc == null) return null;
String name = DOMUtil.getAttribute(sc,"name");
if (name == null) return null;
JDefinedClass c;
try {
c = codeModel._class(name, ClassType.INTERFACE);
c.hide();
} catch (JClassAlreadyExistsException e) {
c = e.getExistingClass();
}
return c;
}
public JPackage getTargetPackage() {
if(model.options.defaultPackage!=null)
return codeModel._package(model.options.defaultPackage);
String p;
if( defaultPackage!=null )
p = defaultPackage;
else
p = getOption("package", "");
return codeModel._package(p);
}
public BIConversion conversion(String name) {
BIConversion r = conversions.get(name);
if (r == null)
throw new AssertionError("undefined conversion name: this should be checked by the validator before we read it");
return r;
}
public BIElement element( String name ) {
return elements.get(name);
}
public Collection<BIElement> elements() {
return elements.values();
}
public Collection<BIInterface> interfaces() {
return interfaces.values();
}
private CCustomizations getGlobalCustomizations() {
CCustomizations r=null;
for( Element e : DOMUtil.getChildElements(dom) ) {
if(!model.options.pluginURIs.contains(e.getNamespaceURI()))
continue;
if(r==null)
r = new CCustomizations();
r.add(new CPluginCustomization(e, DOMLocator.getLocationInfo(e)));
}
if(r==null) r = CCustomizations.EMPTY;
return new CCustomizations(r);
}
private String getOption(String attName, String defaultValue) {
Element opt = DOMUtil.getElement(dom,"options");
if (opt != null) {
String s = DOMUtil.getAttribute(opt,attName);
if (s != null)
return s;
}
return defaultValue;
}
private static final SchemaCache bindingFileSchema = new SchemaCache("bindingfile.xsd", BindInfo.class, true);
private static Document parse( Model model, InputSource is, ErrorReceiver receiver ) throws AbortException {
try {
ValidatorHandler validator = bindingFileSchema.newValidator();
SAXParserFactory pf = XmlFactory.createParserFactory(model.options.disableXmlSecurity);
DocumentBuilderFactory domFactory = XmlFactory.createDocumentBuilderFactory(model.options.disableXmlSecurity);
DOMBuilder builder = new DOMBuilder(domFactory);
ErrorReceiverFilter controller = new ErrorReceiverFilter(receiver);
validator.setErrorHandler(controller);
XMLReader reader = pf.newSAXParser().getXMLReader();
reader.setErrorHandler(controller);
DTDExtensionBindingChecker checker = new DTDExtensionBindingChecker("", model.options, controller);
checker.setContentHandler(validator);
reader.setContentHandler(new ForkContentHandler(checker,builder));
reader.parse(is);
if(controller.hadError()) throw new AbortException();
return (Document)builder.getDOM();
} catch( IOException e ) {
receiver.error( new SAXParseException2(e.getMessage(),null,e) );
} catch( SAXException e ) {
receiver.error( new SAXParseException2(e.getMessage(),null,e) );
} catch( ParserConfigurationException e ) {
receiver.error( new SAXParseException2(e.getMessage(),null,e) );
}
throw new AbortException();
}
}