package com.sun.tools.internal.xjc.reader.dtd.bindinfo;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.JExpr;
import com.sun.codemodel.internal.JExpression;
import com.sun.codemodel.internal.JMethod;
import com.sun.codemodel.internal.JMod;
import com.sun.codemodel.internal.JPackage;
import com.sun.codemodel.internal.JPrimitiveType;
import com.sun.codemodel.internal.JType;
import com.sun.codemodel.internal.JVar;
import com.sun.tools.internal.xjc.model.CAdapter;
import com.sun.tools.internal.xjc.model.CBuiltinLeafInfo;
import com.sun.tools.internal.xjc.model.TypeUse;
import com.sun.tools.internal.xjc.model.TypeUseFactory;
import com.sun.xml.internal.bind.v2.util.XmlFactory;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
public class BIUserConversion implements BIConversion
{
BIUserConversion( BindInfo bi, Element _e ) {
this.owner = bi;
this.e = _e;
}
private static void add( Map<String,BIConversion> m, BIConversion c ) {
m.put( c.name(), c );
}
static void addBuiltinConversions( BindInfo bi, Map<String,BIConversion> m ) {
add( m, new BIUserConversion( bi, parse("<conversion name='boolean' type='java.lang.Boolean' parse='getBoolean' />")));
add( m, new BIUserConversion( bi, parse("<conversion name='byte' type='java.lang.Byte' parse='parseByte' />")));
add( m, new BIUserConversion( bi, parse("<conversion name='short' type='java.lang.Short' parse='parseShort' />")));
add( m, new BIUserConversion( bi, parse("<conversion name='int' type='java.lang.Integer' parse='parseInt' />")));
add( m, new BIUserConversion( bi, parse("<conversion name='long' type='java.lang.Long' parse='parseLong' />")));
add( m, new BIUserConversion( bi, parse("<conversion name='float' type='java.lang.Float' parse='parseFloat' />")));
add( m, new BIUserConversion( bi, parse("<conversion name='double' type='java.lang.Double' parse='parseDouble' />")));
}
private static Element parse(String text) {
try {
DocumentBuilderFactory dbf = XmlFactory.createDocumentBuilderFactory(false);
InputSource is = new InputSource(new StringReader(text));
return dbf.newDocumentBuilder().parse(is).getDocumentElement();
} catch (SAXException x) {
throw new Error(x);
} catch (IOException x) {
throw new Error(x);
} catch (ParserConfigurationException x) {
throw new Error(x);
}
}
private final BindInfo owner;
private final Element e;
public Locator getSourceLocation() {
return DOMLocator.getLocationInfo(e);
}
public String name() { return DOMUtil.getAttribute(e,"name"); }
public TypeUse getTransducer() {
String ws = DOMUtil.getAttribute(e,"whitespace");
if(ws==null) ws = "collapse";
String type = DOMUtil.getAttribute(e,"type");
if(type==null) type=name();
JType t=null;
int idx = type.lastIndexOf('.');
if(idx<0) {
try {
t = JPrimitiveType.parse(owner.codeModel,type);
} catch( IllegalArgumentException ex ) {
type = owner.getTargetPackage().name()+'.'+type;
}
}
if(t==null) {
try {
JDefinedClass cls = owner.codeModel._class(type);
cls.hide();
t = cls;
} catch( JClassAlreadyExistsException ex ) {
t = ex.getExistingClass();
}
}
String parse = DOMUtil.getAttribute(e,"parse");
if(parse==null) parse="new";
String print = DOMUtil.getAttribute(e,"print");
if(print==null) print="toString";
JDefinedClass adapter = generateAdapter(owner.codeModel, parse, print, t.boxify());
return TypeUseFactory.adapt(CBuiltinLeafInfo.STRING,new CAdapter(adapter));
}
private JDefinedClass generateAdapter(JCodeModel cm, String parseMethod, String printMethod, JClass inMemoryType) {
JDefinedClass adapter = null;
int id = 1;
while(adapter==null) {
try {
JPackage pkg = owner.getTargetPackage();
adapter = pkg._class("Adapter"+id);
} catch (JClassAlreadyExistsException ex) {
id++;
}
}
adapter._extends(cm.ref(XmlAdapter.class).narrow(String.class).narrow(inMemoryType));
JMethod unmarshal = adapter.method(JMod.PUBLIC, inMemoryType, "unmarshal");
JVar $value = unmarshal.param(String.class, "value");
JExpression inv;
if( parseMethod.equals("new") ) {
inv = JExpr._new(inMemoryType).arg($value);
} else {
int idx = parseMethod.lastIndexOf('.');
if(idx<0) {
inv = inMemoryType.staticInvoke(parseMethod).arg($value);
} else {
inv = JExpr.direct(parseMethod+"(value)");
}
}
unmarshal.body()._return(inv);
JMethod marshal = adapter.method(JMod.PUBLIC, String.class, "marshal");
$value = marshal.param(inMemoryType,"value");
int idx = printMethod.lastIndexOf('.');
if(idx<0) {
inv = $value.invoke(printMethod);
} else {
inv = JExpr.direct(printMethod+"(value)");
}
marshal.body()._return(inv);
return adapter;
}
}