Java Code Examples for com.sun.tools.xjc.model.Model#generateCode()

The following examples show how to use com.sun.tools.xjc.model.Model#generateCode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonixPluginTest.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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);
}
 
Example 2
Source File: JsonixPluginTest.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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);
}
 
Example 3
Source File: JsonixPluginZeroTest.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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);
}
 
Example 4
Source File: CompileIssues.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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);
}
 
Example 5
Source File: XJC20Mojo.java    From maven-jaxb2-plugin with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected Outline generateCode(final Model model)
		throws MojoExecutionException {
	if (getVerbose()) {
		getLog().info("Compiling input schema(s)...");
	}

	final Outline outline = model.generateCode(model.options,
			new LoggingErrorReceiver("Error while generating code.",
					getLog(), getVerbose()));
	if (outline == null) {
		throw new MojoExecutionException(
				"Failed to compile input schema(s)!  Error messages should have been provided.");
	} else {
		return outline;
	}
}
 
Example 6
Source File: XJC22Mojo.java    From maven-jaxb2-plugin with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected Outline generateCode(final Model model)
		throws MojoExecutionException {
	if (getVerbose()) {
		getLog().info("Compiling input schema(s)...");
	}

	final Outline outline = model.generateCode(model.options,
			new LoggingErrorReceiver("Error while generating code.",
					getLog(), getVerbose()));
	if (outline == null) {
		throw new MojoExecutionException(
				"Failed to compile input schema(s)! Error messages should have been provided.");
	} else {
		return outline;
	}
}
 
Example 7
Source File: XJC23Mojo.java    From maven-jaxb2-plugin with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected Outline generateCode(final Model model)
		throws MojoExecutionException {
	if (getVerbose()) {
		getLog().info("Compiling input schema(s)...");
	}

	final Outline outline = model.generateCode(model.options,
			new LoggingErrorReceiver("Error while generating code.",
					getLog(), getVerbose()));
	if (outline == null) {
		throw new MojoExecutionException(
				"Failed to compile input schema(s)! Error messages should have been provided.");
	} else {
		return outline;
	}
}
 
Example 8
Source File: XJC21Mojo.java    From maven-jaxb2-plugin with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected Outline generateCode(final Model model)
		throws MojoExecutionException {
	if (getVerbose()) {
		getLog().info("Compiling input schema(s)...");
	}

	final Outline outline = model.generateCode(model.options,
			new LoggingErrorReceiver("Error while generating code.",
					getLog(), getVerbose()));
	if (outline == null) {
		throw new MojoExecutionException(
				"Failed to compile input schema(s)!  Error messages should have been provided.");
	} else {
		return outline;
	}
}
 
Example 9
Source File: JsonixPluginTest.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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);
}
 
Example 10
Source File: RunPlugins.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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);
}
 
Example 11
Source File: MModelInfoLoader.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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;
}
 
Example 12
Source File: ExecuteJAXB1058.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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);
}
 
Example 13
Source File: RunSimplifyPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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", "-Xsimplify", "-Xsimplify-usePluralForm=true"};

	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);
}
 
Example 14
Source File: RunPluginsForCases.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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);
}
 
Example 15
Source File: RunPluginsForMain.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
	public void compilesSchema() throws Exception {

		new File("target/generated-sources/xjc").mkdirs();

		URL schema = getClass().getResource("/main.xsd");
		URL binding = getClass().getResource("/main.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);
	}
 
Example 16
Source File: RunPluginsForCustomer.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
	public void compilesCustomer() throws Exception {

		new File("target/generated-sources/xjc").mkdirs();

		URL schema = getClass().getResource("/customer.xsd");
		URL binding = getClass().getResource("/customer.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);
	}
 
Example 17
Source File: AlphaMInfoFactoryTest.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@BeforeClass
public static void createModel() throws BadCommandLineException,
		IOException {
	final String generateDirectory = "target/generated-sources/"
			+ AlphaMInfoFactoryTest.class.getPackage().getName();
	new File(generateDirectory).mkdirs();
	final URL schema = AlphaMInfoFactoryTest.class
			.getResource("schema.xsd");
	final URL binding = AlphaMInfoFactoryTest.class
			.getResource("binding.xjb");
	final String[] arguments = new String[] { "-xmlschema",
			schema.toExternalForm(), "-b", binding.toExternalForm(), "-d",
			generateDirectory, "-extension" };

	Options options = new Options();
	options.parseArguments(arguments);
	ConsoleErrorReporter receiver = new ConsoleErrorReporter();
	Model model = ModelLoader.load(options, new JCodeModel(), receiver);
	Assert.assertNotNull(model);

	final XJCCMInfoFactory factory = new XJCCMInfoFactory(model);

	AlphaMInfoFactoryTest.MODEL_INFO = factory.createModel();

	model.generateCode(options, receiver);
	com.sun.codemodel.CodeWriter cw = options.createCodeWriter();
	model.codeModel.build(cw);

}
 
Example 18
Source File: DynamicCompiler.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected void execute(final Options options, final JCodeModel codeModel,
		final CodeWriter codeWriter, final ErrorReceiver errorReceiver)
		throws IOException {
	logger.debug("Loading the model.");
	final Model model = ModelLoader.load(options, codeModel, errorReceiver);
	logger.debug("Generating the code.");
	final Outline outline = model.generateCode(options, errorReceiver);
	logger.debug("Writing the code.");
	model.codeModel.build(codeWriter);
	this.outline = outline;
	final StringBuffer contextPathStringBuffer = new StringBuffer();
	String delimiter = "";
	for (final PackageOutline packageOutline : this.outline
			.getAllPackageContexts()) {
		contextPathStringBuffer.append(delimiter);
		contextPathStringBuffer.append(packageOutline._package().name());
		delimiter = ":";
	}
	this.contextPath = contextPathStringBuffer.toString();
	logger.debug("Compiling the code.");
	final JavaCompiler systemJavaCompiler = ToolProvider
			.getSystemJavaCompiler();
	final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
	final StandardJavaFileManager standardFileManager = systemJavaCompiler
			.getStandardFileManager(diagnostics, null, null);

	final Collection<File> javaFiles = FileUtils.listFiles(targetDirectory,
			FileFilterUtils.suffixFileFilter(".java"),
			TrueFileFilter.INSTANCE);

	final Iterable<? extends JavaFileObject> fileObjects = standardFileManager
			.getJavaFileObjectsFromFiles(javaFiles);

	ClassLoader contextClassLoader = Thread.currentThread()
			.getContextClassLoader();

	final List<String> compilerOptions = new LinkedList<String>();

	if (contextClassLoader instanceof URLClassLoader) {
		final URLClassLoader urlClassLoader = (URLClassLoader) contextClassLoader;
		final URL[] urls = urlClassLoader.getURLs();
		if (urls.length > 0) {
			compilerOptions.add("-classpath");

			final StringBuffer classpathStringBuffer = new StringBuffer();
			String separator = "";
			for (final URL url : urls) {
				logger.debug("URL:" + url);
				classpathStringBuffer.append(separator);
				try {
					classpathStringBuffer.
					// append("\"").
							append(new File(url.toURI()).getAbsolutePath())
					// .append("\"")
					;
					separator = SystemUtils.PATH_SEPARATOR;
				} catch (URISyntaxException ignored) {
				}
			}
			compilerOptions.add(classpathStringBuffer.toString());
		}
	}
	compilerOptions.add("-verbose");
	compilerOptions.add("-d");
	compilerOptions.add(targetDirectory.getAbsolutePath());

	logger.debug("Compiler options:"
			+ StringUtils.join(compilerOptions.iterator(), " "));
	// // conte
	//
	// final String[] compilerOptions = new String[] { "-d",
	// targetDirectory.getAbsolutePath(), "-verbose" };

	systemJavaCompiler.getTask(null, standardFileManager, null,
			compilerOptions, null, fileObjects).call();
	standardFileManager.close();
}
 
Example 19
Source File: GenerateWithJaxbApi.java    From java-9-wtf with Apache License 2.0 4 votes vote down vote up
private void execute(String configuration) throws Exception {
	Options options = createOptions(configuration);
	Model model = ModelLoader.load(options, new JCodeModel(), ERROR_RECEIVER);
	Outline outline = model.generateCode(model.options, ERROR_RECEIVER);
	writeCode(outline);
}
 
Example 20
Source File: RunPlugins.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@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",
			//
			"-XhashCode",
			//
			"-Xequals",
			//
			"-XtoString",
			//
			"-Xcopyable",
			//
			"-Xmergeable",
			//
			"-Xinheritance",
			//
			"-Xsetters",
			//
			"-Xsetters-mode=direct",
			//
			"-Xwildcard",
			//
			"-XenumValue"
			// "-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);
}