use of com.sun.tools.xjc.BadCommandLineException in project cxf by apache.
the class JAXBDataBinding method initialize.
public void initialize(ToolContext c) throws ToolException {
this.context = c;
checkEncoding(c);
SchemaCompiler schemaCompiler = XJC.createSchemaCompiler();
Bus bus = context.get(Bus.class);
OASISCatalogManager catalog = bus.getExtension(OASISCatalogManager.class);
Options opts = null;
opts = getOptions(schemaCompiler);
hackInNewInternalizationLogic(schemaCompiler, catalog, opts);
ClassCollector classCollector = context.get(ClassCollector.class);
ClassNameAllocatorImpl allocator = new ClassNameAllocatorImpl(classCollector, c.optionSet(ToolConstants.CFG_AUTORESOLVE));
schemaCompiler.setClassNameAllocator(allocator);
listener = new JAXBBindErrorListener(context.isVerbose(), context.getErrorListener());
schemaCompiler.setErrorListener(listener);
// Collection<SchemaInfo> schemas = serviceInfo.getSchemas();
List<InputSource> jaxbBindings = context.getJaxbBindingFile();
SchemaCollection schemas = (SchemaCollection) context.get(ToolConstants.XML_SCHEMA_COLLECTION);
List<String> args = new ArrayList<>();
if (context.get(ToolConstants.CFG_NO_ADDRESS_BINDING) == null) {
// hard code to enable jaxb extensions
args.add("-extension");
String name = "/org/apache/cxf/tools/common/jaxb/W3CEPRJaxbBinding.xml";
if (isJAXB22()) {
name = "/org/apache/cxf/tools/common/jaxb/W3CEPRJaxbBinding_jaxb22.xml";
}
URL bindingFileUrl = getClass().getResource(name);
InputSource ins = new InputSource(bindingFileUrl.toString());
opts.addBindFile(ins);
}
if (context.get(ToolConstants.CFG_XJC_ARGS) != null) {
Object o = context.get(ToolConstants.CFG_XJC_ARGS);
if (o instanceof String) {
o = new String[] { (String) o };
}
String[] xjcArgss = (String[]) o;
for (String xjcArgs : xjcArgss) {
StringTokenizer tokenizer = new StringTokenizer(xjcArgs, ",", false);
while (tokenizer.hasMoreTokens()) {
String arg = tokenizer.nextToken();
args.add(arg);
LOG.log(Level.FINE, "xjc arg:" + arg);
}
}
}
if (context.get(ToolConstants.CFG_NO_ADDRESS_BINDING) == null || context.get(ToolConstants.CFG_XJC_ARGS) != null) {
try {
// keep parseArguments happy, supply dummy required command-line
// opts
opts.addGrammar(new InputSource("null"));
opts.parseArguments(args.toArray(new String[args.size()]));
} catch (BadCommandLineException e) {
StringBuilder msg = new StringBuilder("XJC reported 'BadCommandLineException' for -xjc argument:");
for (String arg : args) {
msg.append(arg + " ");
}
LOG.log(Level.FINE, msg.toString(), e);
if (opts != null) {
String pluginUsage = getPluginUsageString(opts);
msg.append(System.getProperty("line.separator"));
if (args.contains("-X")) {
throw new ToolException(pluginUsage, e);
}
msg.append(pluginUsage);
}
throw new ToolException(msg.toString(), e);
}
}
if (context.optionSet(ToolConstants.CFG_MARK_GENERATED)) {
// '-mark-generated' attribute to jaxb xjc.
try {
opts.parseArgument(new String[] { "-mark-generated" }, 0);
} catch (BadCommandLineException e) {
LOG.log(Level.SEVERE, e.getMessage());
throw new ToolException(e);
}
}
addSchemas(opts, schemaCompiler, schemas);
addBindingFiles(opts, jaxbBindings, schemas);
parseSchemas(schemaCompiler);
rawJaxbModelGenCode = schemaCompiler.bind();
addedEnumClassToCollector(schemas, allocator);
if (context.get(ToolConstants.CFG_DEFAULT_VALUES) != null) {
String cname = (String) context.get(ToolConstants.CFG_DEFAULT_VALUES);
if (StringUtils.isEmpty(cname)) {
defaultValues = new RandomValueProvider();
} else {
if (cname.charAt(0) == '=') {
cname = cname.substring(1);
}
try {
defaultValues = (DefaultValueProvider) Class.forName(cname).newInstance();
} catch (Exception e) {
LOG.log(Level.SEVERE, e.getMessage());
throw new ToolException(e);
}
}
}
initialized = true;
}
use of com.sun.tools.xjc.BadCommandLineException in project drools by kiegroup.
the class DroolsJaxbHelperProviderImpl method addXsdModel.
public static String[] addXsdModel(Resource resource, KnowledgeBuilderImpl kBuilder, Options xjcOpts, String systemId) throws IOException {
InputSource source = new InputSource(new CachingRewindableReader(resource.getReader()));
source.setSystemId(systemId.trim().startsWith(".") ? systemId : "." + systemId);
xjcOpts.addGrammar(source);
try {
xjcOpts.parseArguments(new String[] { "-npa" });
} catch (BadCommandLineException e) {
throw new IllegalArgumentException("Unable to parse arguments", e);
}
ErrorReceiver errorReceiver = new JaxbErrorReceiver4Drools();
Model model = ModelLoader.load(xjcOpts, new JCodeModel(), errorReceiver);
model.generateCode(xjcOpts, errorReceiver);
MapVfsCodeWriter codeWriter = new MapVfsCodeWriter();
model.codeModel.build(xjcOpts.createCodeWriter(codeWriter));
MemoryResourceReader src = new MemoryResourceReader();
boolean useProjectClassLoader = kBuilder.getRootClassLoader() instanceof ProjectClassLoader;
List<String> classNames = new ArrayList<String>();
List<String> srcNames = new ArrayList<String>();
for (Entry<String, byte[]> entry : codeWriter.getMap().entrySet()) {
String name = entry.getKey();
int dotPos = name.lastIndexOf('.');
String pkgName = name.substring(0, dotPos);
if (!name.endsWith("package-info.java")) {
classNames.add(pkgName);
}
dotPos = pkgName.lastIndexOf('.');
if (dotPos != -1) {
pkgName = pkgName.substring(0, dotPos);
}
PackageRegistry pkgReg = kBuilder.getPackageRegistry(pkgName);
if (pkgReg == null) {
kBuilder.addPackage(new PackageDescr(pkgName));
pkgReg = kBuilder.getPackageRegistry(pkgName);
}
if (useProjectClassLoader) {
String srcName = convertToResource(entry.getKey());
src.add(srcName, entry.getValue());
srcNames.add(srcName);
} else {
JavaDialect dialect = (JavaDialect) pkgReg.getDialectCompiletimeRegistry().getDialect("java");
dialect.addSrc(convertToResource(entry.getKey()), entry.getValue());
}
}
if (useProjectClassLoader) {
ProjectJavaCompiler compiler = new ProjectJavaCompiler(kBuilder.getBuilderConfiguration());
List<KnowledgeBuilderResult> results = compiler.compileAll((ProjectClassLoader) kBuilder.getRootClassLoader(), srcNames, src);
for (String className : classNames) {
Class<?> clazz = null;
try {
clazz = Class.forName(className, true, kBuilder.getRootClassLoader());
} catch (ClassNotFoundException e) {
continue;
}
String pkgName = className.substring(0, className.lastIndexOf('.'));
PackageRegistry pkgReg = kBuilder.getPackageRegistry(pkgName);
pkgReg.getPackage().addTypeDeclaration(TypeDeclaration.createTypeDeclarationForBean(clazz));
}
kBuilder.updateResults(results);
} else {
kBuilder.compileAll();
kBuilder.updateResults();
}
return classNames.toArray(new String[classNames.size()]);
}
Aggregations