下面列出了org.slf4j.helpers.NOPLoggerFactory#com.sun.tools.xjc.Options 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void compilesWFS_V_2_0() throws Exception {
new File("target/generated-sources/wfs-v_2_0").mkdirs();
URL schema = getClass().getResource("/ogc/wfs/2.0/wfs.xsd");
URL binding = getClass().getResource("/ogc/wfs-v_2_0.xjb");
final String[] arguments = new String[] { "-xmlschema",
schema.toExternalForm(), "-b", binding.toExternalForm(), "-d",
"target/generated-sources/wfs-v_2_0", "-extension", "-Xjsonix"
};
Options options = new Options();
options.parseArguments(arguments);
ConsoleErrorReporter receiver = new ConsoleErrorReporter();
Model model = ModelLoader.load(options, new JCodeModel(), receiver);
model.generateCode(options, receiver);
com.sun.codemodel.CodeWriter cw = options.createCodeWriter();
model.codeModel.build(cw);
}
public boolean run(Outline model, Options opt, ErrorHandler errorHandler) {
try {
for (ClassOutline co : model.getClasses()) {
List<CPropertyInfo> properties = co.target.getProperties();
for (CPropertyInfo property : properties) {
if (property instanceof CElementPropertyInfo) {
processElement((CElementPropertyInfo) property, co, model);
} else if (property instanceof CAttributePropertyInfo) {
processAttribute((CAttributePropertyInfo) property, co, model);
} else if (property instanceof CValuePropertyInfo) {
processAttribute((CValuePropertyInfo) property, co, model);
}
}
}
return true;
} catch (Exception e) {
log(e);
return false;
}
}
@Override
public boolean run(Outline outline, Options options, ErrorHandler errorHandler) {
// for each generated classes
for (ClassOutline generatedClassOutline : outline.getClasses()) {
JDefinedClass generatedClass = generatedClassOutline.implClass;
if (!isAbstractClass(generatedClass) &&
!generatedClass.fields().isEmpty()) {
boolean commandExecuted = false;
for (Command command : commands.values()) {
if (command.isEnabled()) {
command.editGeneratedClass(generatedClass);
commandExecuted = true;
}
}
if (!commandExecuted) {
defaultCommand.editGeneratedClass(generatedClass);
}
}
}
return true;
}
@Test
public void compilesContext_V_1_1_0() throws Exception {
new File("target/generated-sources/context-v_1_1_0").mkdirs();
final String[] arguments = new String[] {
"-xmlschema",
getClass().getResource("/ogc/context/1.1.0/wmcAll.xsd")
.toExternalForm(),
"-b",
getClass().getResource("/ogc/context-v_1_1_0.xjb")
.toExternalForm(), "-d",
"target/generated-sources/context-v_1_1_0", "-extension",
"-Xjsonix"
};
Options options = new Options();
options.parseArguments(arguments);
ConsoleErrorReporter receiver = new ConsoleErrorReporter();
Model model = ModelLoader.load(options, new JCodeModel(), receiver);
model.generateCode(options, receiver);
com.sun.codemodel.CodeWriter cw = options.createCodeWriter();
model.codeModel.build(cw);
}
@Test
public void compilesSchema() throws Exception {
new File("target/generated-sources/xjc").mkdirs();
URL schema = getClass().getResource("/cases.xsd");
// URL binding = getClass().getResource("/cases.xjb");
final String[] arguments = new String[] { "-xmlschema",
schema.toExternalForm(),
// "-b", binding.toExternalForm(),
"-d", "target/generated-sources/xjc", "-extension",
"-XsimpleHashCode", "-XsimpleEquals",
// "-XsimpleToString"
};
Options options = new Options();
options.parseArguments(arguments);
ConsoleErrorReporter receiver = new ConsoleErrorReporter();
Model model = ModelLoader.load(options, new JCodeModel(), receiver);
model.generateCode(options, receiver);
com.sun.codemodel.CodeWriter cw = options.createCodeWriter();
model.codeModel.build(cw);
}
@Test
public void compilesContext_V_1_1_0() throws Exception {
new File("target/generated-sources/xjc").mkdirs();
final String[] arguments = new String[] { "-xmlschema",
new File("src/main/resources/schema.xsd").toURI().toString(),
"-d",
"target/generated-sources/xjc",
"-XfixJAXB1058",
"-extension", "-XtoString",
"-Xequals", "-XhashCode", "-Xcopyable", "-Xmergeable"};
Options options = new Options();
options.parseArguments(arguments);
ConsoleErrorReporter receiver = new ConsoleErrorReporter();
Model model = ModelLoader.load(options, new JCodeModel(), receiver);
model.generateCode(options, receiver);
com.sun.codemodel.CodeWriter cw = options.createCodeWriter();
model.codeModel.build(cw);
}
@Test
public void compilesSchema() throws Exception {
new File("target/generated-sources/xjc").mkdirs();
URL schema = getClass().getResource("/schema.xsd");
URL binding = getClass().getResource("/binding.xjb");
final String[] arguments = new String[] { "-xmlschema",
schema.toExternalForm(), "-b", binding.toExternalForm(), "-d",
"target/generated-sources/xjc", "-extension", "-XtoString",
"-Xequals", "-XhashCode", "-Xcopyable", "-Xmergeable" };
Options options = new Options();
options.parseArguments(arguments);
ConsoleErrorReporter receiver = new ConsoleErrorReporter();
Model model = ModelLoader.load(options, new JCodeModel(), receiver);
model.generateCode(options, receiver);
com.sun.codemodel.CodeWriter cw = options.createCodeWriter();
model.codeModel.build(cw);
}
@Test
public void compilesOWS_V_1_1_0() throws Exception {
new File("target/generated-sources/ows-v_1_1_0").mkdirs();
URL schema = getClass().getResource("/ogc/ows/1.1.0/owsAll.xsd");
URL binding = getClass().getResource("/ogc/ows-v_1_1_0.xjb");
final String[] arguments = new String[] { "-xmlschema",
schema.toExternalForm(), "-b", binding.toExternalForm(), "-d",
"target/generated-sources/ows-v_1_1_0", "-extension",
"-Xjsonix"
};
Options options = new Options();
options.parseArguments(arguments);
ConsoleErrorReporter receiver = new ConsoleErrorReporter();
Model model = ModelLoader.load(options, new JCodeModel(), receiver);
model.generateCode(options, receiver);
com.sun.codemodel.CodeWriter cw = options.createCodeWriter();
model.codeModel.build(cw);
}
public MModelInfo<NType, NClass> loadModel(String resource)
throws Exception {
System.setProperty("javax.xml.accessExternalSchema", "all");
new File("target/generated-sources/" + resource).mkdirs();
final String[] arguments = new String[] { "-xmlschema",
getClass().getResource("/" + resource).toExternalForm(), "-d",
"target/generated-sources/" + resource, "-extension",
"-Xjsonix", "-Xjsonix-generateJsonSchema" };
Options options = new Options();
options.parseArguments(arguments);
ConsoleErrorReporter receiver = new ConsoleErrorReporter();
Model model = ModelLoader.load(options, new JCodeModel(), receiver);
model.generateCode(options, receiver);
com.sun.codemodel.CodeWriter cw = options.createCodeWriter();
model.codeModel.build(cw);
final MModelInfo<NType, NClass> modelInfo = new XJCCMInfoFactory(model)
.createModel();
return modelInfo;
}
@Override
public int parseArgument(Options opt, String[] args, int start)
throws BadCommandLineException, IOException {
final int result = super.parseArgument(opt, args, start);
for (int i = 0; i < args.length; i++) {
if (args[i].length() != 0) {
if (args[i].charAt(0) != '-') {
if (args[i].endsWith(".jar")) {
episodeURLs.add(new File(args[i]).toURI().toURL());
}
}
}
}
return result;
}
@Override
public void onActivated(Options options) throws BadCommandLineException {
Thread.currentThread().setContextClassLoader(
getClass().getClassLoader());
super.onActivated(options);
final FieldRendererFactory fieldRendererFactory = new FieldRendererFactory() {
public FieldRenderer getList(JClass coreList) {
return new UntypedListFieldRenderer(coreList);
}
};
options.setFieldRendererFactory(fieldRendererFactory, this);
}
@Test
public void compilesBasicZero() throws Exception {
new File("target/generated-sources/basic/zero").mkdirs();
final String[] arguments = new String[] {
"-xmlschema",
getClass().getResource("/basic/zero/schema.xsd")
.toExternalForm(), "-d",
"target/generated-sources/basic/zero", "-extension", "-Xjsonix", "-Xjsonix-generateJsonSchema"
};
Options options = new Options();
options.parseArguments(arguments);
ConsoleErrorReporter receiver = new ConsoleErrorReporter();
Model model = ModelLoader.load(options, new JCodeModel(), receiver);
model.generateCode(options, receiver);
com.sun.codemodel.CodeWriter cw = options.createCodeWriter();
model.codeModel.build(cw);
}
public Collection<ClassOutline> process(EjbPlugin context, Outline outline,
Options options) throws Exception {
logger.debug("Processing outline with context path ["
+ OutlineUtils.getContextPath(outline) + "].");
final Collection<? extends ClassOutline> classes = outline.getClasses();
final Collection<ClassOutline> processedClassOutlines = new ArrayList<ClassOutline>(
classes.size());
for (final ClassOutline classOutline : classes) {
if (!getIgnoring()
.isClassOutlineIgnored(getMapping(), classOutline)) {
final ClassOutline processedClassOutline = process(this,
classOutline, options);
if (processedClassOutline != null) {
processedClassOutlines.add(processedClassOutline);
}
}
}
return processedClassOutlines;
}
public EmbeddableAttributes process(Mapping context,
ClassOutline classOutline, Options options) {
final EmbeddableAttributes attributes = new EmbeddableAttributes();
final FieldOutline[] fieldOutlines = classOutline.getDeclaredFields();
for (final FieldOutline fieldOutline : fieldOutlines) {
final Object attributeMapping = getAttributeMapping(context,
fieldOutline, options).process(context, fieldOutline,
options);
if (attributeMapping instanceof Basic) {
attributes.getBasic().add((Basic) attributeMapping);
} else if (attributeMapping instanceof Transient) {
attributes.getTransient().add((Transient) attributeMapping);
}
}
return attributes;
}
@Override
public boolean run(final Outline outline, final Options opt, final ErrorHandler errorHandler) throws SAXException {
final PluginContext pluginContext = PluginContext.get(outline, opt, errorHandler);
for (final ClassOutline classOutline : outline.getClasses()) {
try {
final GroupInterfacePlugin groupInterfacePlugin = pluginContext.findPlugin(GroupInterfacePlugin.class);
if (groupInterfacePlugin != null) {
ModifierGenerator.generateClass(pluginContext, new DefinedClassOutline(pluginContext, classOutline), this.modifierClassName, this.modifierClassName, groupInterfacePlugin.getGroupInterfacesForClass(pluginContext, classOutline.implClass.fullName()), this.modifierMethodName);
} else {
ModifierGenerator.generateClass(pluginContext, new DefinedClassOutline(pluginContext, classOutline), this.modifierClassName, this.modifierMethodName);
}
} catch (final JClassAlreadyExistsException e) {
errorHandler.error(new SAXParseException(e.getMessage(), classOutline.target.getLocator()));
}
}
return true;
}
public Basic process(Mapping context, FieldOutline fieldOutline,
Options options) {
final Basic basic = context.getCustomizing().getBasic(fieldOutline);
createBasic$Name(context, fieldOutline, basic);
createBasic$Column(context, fieldOutline, basic);
if (basic.getLob() == null && basic.getTemporal() == null
&& basic.getEnumerated() == null) {
if (context.getAttributeMapping().isTemporal(context, fieldOutline)) {
basic.setTemporal(context.getAttributeMapping().createTemporalType(
context, fieldOutline));
} else if (context.getAttributeMapping().isEnumerated(context, fieldOutline)) {
basic.setEnumerated(context.getAttributeMapping()
.createEnumerated(context, fieldOutline));
} else if (context.getAttributeMapping().isLob(context, fieldOutline)) {
basic.setLob(context.getAttributeMapping().createLob(context, fieldOutline));
}
}
return basic;
}
@Test
public void compilesIssues() throws Exception {
new File("target/generated-sources/xjc").mkdirs();
URL schema = getClass().getResource("/schema.xsd");
URL binding = getClass().getResource("/binding.xjb");
final String[] arguments = new String[] {
"-xmlschema", schema.toExternalForm(),
"-b", binding.toExternalForm(),
"-d", "target/generated-sources/xjc",
"-extension",
"-Xjsonix",
"-Xjsonix-generateJsonSchema"
};
Options options = new Options();
options.parseArguments(arguments);
ConsoleErrorReporter receiver = new ConsoleErrorReporter();
Model model = ModelLoader.load(options, new JCodeModel(), receiver);
model.generateCode(options, receiver);
com.sun.codemodel.CodeWriter cw = options.createCodeWriter();
model.codeModel.build(cw);
}
public Object process(Mapping context, FieldOutline fieldOutline,
Options options) {
final Object toOne = context.getCustomizing().getToOne(fieldOutline);
if (toOne instanceof ManyToOne) {
return context.getManyToOneMapping().process(context, fieldOutline,
options);
} else if (toOne instanceof OneToOne) {
return context.getOneToOneMapping().process(context, fieldOutline,
options);
} else if (toOne instanceof Embedded) {
return context.getEmbeddedMapping().process(context, fieldOutline,
options);
} else {
throw new AssertionError(
"Either many-to-one or one-to-one mappings are expected.");
}
}
/**
* Prints the usage screen and exits the process.
*
* @param opts
* If the parsing of options have started, set a partly populated
* {@link Options} object.
*/
public static void usage( @Nullable Options opts, boolean privateUsage ) {
if( privateUsage ) {
System.out.println(Messages.format(Messages.DRIVER_PRIVATE_USAGE));
} else {
System.out.println(Messages.format(Messages.DRIVER_PUBLIC_USAGE));
}
// do not show plugin usage
// if( opts!=null && opts.getAllPlugins().size()!=0 ) {
// System.out.println(Messages.format(Messages.ADDON_USAGE));
// for (Plugin p : opts.getAllPlugins()) {
// System.out.println(p.getUsage());
// }
// }
}
public static ConfigDef config() {
SchemaCompiler schemaCompiler = XJC.createSchemaCompiler();
Options options = schemaCompiler.getOptions();
return new ConfigDef()
.define(
ConfigKeyBuilder.of(SCHEMA_PATH_CONFIG, ConfigDef.Type.LIST)
.documentation(SCHEMA_PATH_DOC)
.importance(ConfigDef.Importance.HIGH)
.validator(new ValidUrl())
.build()
).define(
ConfigKeyBuilder.of(PACKAGE_CONFIG, ConfigDef.Type.STRING)
.documentation(PACKAGE_DOC)
.importance(ConfigDef.Importance.HIGH)
.defaultValue(FromXmlConfig.class.getPackage().getName() + ".model")
.build()
).define(
ConfigKeyBuilder.of(XJC_OPTIONS_STRICT_CHECK_CONFIG, ConfigDef.Type.BOOLEAN)
.documentation(XJC_OPTIONS_STRICT_CHECK_DOC)
.importance(ConfigDef.Importance.LOW)
.defaultValue(options.strictCheck)
.build()
).define(
ConfigKeyBuilder.of(XJC_OPTIONS_AUTOMATIC_NAME_CONFLICT_RESOLUTION_ENABLED_CONFIG, ConfigDef.Type.BOOLEAN)
.documentation(XJC_OPTIONS_AUTOMATIC_NAME_CONFLICT_RESOLUTION_ENABLED_DOC)
.importance(ConfigDef.Importance.LOW)
.defaultValue(options.automaticNameConflictResolution)
.build()
).define(
ConfigKeyBuilder.of(XJC_OPTIONS_VERBOSE_CONFIG, ConfigDef.Type.BOOLEAN)
.documentation(XJC_OPTIONS_VERBOSE_DOC)
.importance(ConfigDef.Importance.LOW)
.defaultValue(options.verbose)
.build()
);
}
@Override
public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) throws SAXException {
JCodeModel model = outline.getModel().codeModel;
for (ClassOutline co : outline.getClasses()) {
updateArrayOfGetters(co, model);
updateArrayOfSetters(co, model);
}
return true;
}
@Override
public boolean run(Outline model, Options options, ErrorHandler errorHandler) throws SAXException {
JCodeModel codeModel = model.getCodeModel();
for (ClassOutline classOutline : model.getClasses()) {
log.trace("run - {}", classOutline.implClass.name());
JFieldVar schemaField = processSchema(codeModel, classOutline);
processToStruct(schemaField, codeModel, classOutline);
processFromStruct(codeModel, classOutline);
}
return true;
}
@Override
public int parseArgument(Options opt, String[] args, int i) {
String arg = args[i].trim();
Command command = commands.get(arg);
if (command != null) {
command.setEnabled(true);
return 1;
}
return 0;
}
private void addSchemasForServiceInfos(OASISCatalogManager catalog,
List<ServiceInfo> serviceList,
Options opts,
SchemaCompiler schemaCompiler,
SchemaCollection schemaCollection) {
Set<String> ids = new HashSet<>();
for (ServiceInfo si : serviceList) {
for (SchemaInfo sci : si.getSchemas()) {
String key = sci.getSystemId();
if (ids.contains(key)) {
continue;
}
ids.add(key);
Element ele = sci.getElement();
if (context.fullValidateWSDL()) {
validateSchema(ele, sci.getSystemId(), catalog, schemaCollection);
}
ele = removeImportElement(ele, key, catalog);
InputSource is = new InputSource((InputStream)null);
//key = key.replaceFirst("#types[0-9]+$", "");
is.setSystemId(key);
is.setPublicId(key);
opts.addGrammar(is);
try {
XMLStreamReader reader = createNoCDATAReader(StaxUtils.createXMLStreamReader(ele, key));
schemaCompiler.parseSchema(key, reader);
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
}
}
@Override
public void onActivated(Options options) throws BadCommandLineException {
super.onActivated(options);
try {
init(options);
} catch (Exception ex) {
throw new BadCommandLineException(
"Could not initialize the plugin [" + getOptionName()
+ "].", ex);
}
}
@Override
public boolean run(Outline outline, Options options, ErrorHandler errorHandler) throws SAXException {
helper = new SchemagenHelper(outline);
// set up special schemas
helper.getSpecialSchemas().put(ReferenceAvroType.class, new HashSet<String>());
helper.getSpecialSchemas().put(DateAvroType.class, new HashSet<String>());
// do the work
inferAvroSchema(outline);
// true because we're done and processing should continue
return true;
}
/**
* Parses the arguments and injects values into the beans via properties.
*/
public int parseArgument(Options opt, String[] args, int start)
throws BadCommandLineException, IOException {
int consumed = 0;
final String optionPrefix = "-" + getOptionName() + "-";
final int optionPrefixLength = optionPrefix.length();
final String arg = args[start];
final int equalsPosition = arg.indexOf('=');
if (arg.startsWith(optionPrefix) && equalsPosition > optionPrefixLength) {
final String propertyName = arg.substring(optionPrefixLength,
equalsPosition);
final String value = arg.substring(equalsPosition + 1);
consumed++;
try {
BeanUtils.setProperty(this, propertyName, value);
} catch (Exception ex) {
ex.printStackTrace();
throw new BadCommandLineException("Error setting property ["
+ propertyName + "], value [" + value + "].");
}
}
return consumed;
}
@Override
public boolean run(Outline arg0, Options arg1, ErrorHandler arg2) {
for (ClassOutline classOutline : arg0.getClasses()) {
JDefinedClass implClass = classOutline.implClass;
JCodeModel codeModel = implClass.owner();
JMethod dummyMethod =
implClass.method(JMod.PUBLIC, codeModel.ref(String.class), "dummy");
dummyMethod.body()._return(JExpr.lit("dummy"));
}
return true;
}
private String getPluginUsageString(Options opts) {
StringBuilder buf = new StringBuilder(128);
buf.append("\nAvailable plugin options:\n");
for (Plugin pl : opts.getAllPlugins()) {
buf.append(pl.getUsage());
buf.append('\n');
}
return buf.toString();
}
public Map<ClassOutline, T> process(C context, Outline outline,
Options options) throws Exception {
final Map<ClassOutline, T> classMappingsMap = new HashMap<ClassOutline, T>();
for (final ClassOutline classOutline : outline.getClasses()) {
final T result = getClassOutlineProcessor().process(context,
classOutline, options);
if (result != null) {
classMappingsMap.put(classOutline, result);
}
}
return classMappingsMap;
}