com.sun.tools.xjc.model.Model Java Examples

The following examples show how to use com.sun.tools.xjc.model.Model. 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: 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("/bindings.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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
Source File: JsonixPlugin.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public boolean run(Outline outline, Options opt,
		final ErrorHandler errorHandler) throws SAXException {

	final Model model = outline.getModel();
	final JCodeModel codeModel = outline.getCodeModel();

	final ProgramWriter<NType, NClass> programWriter = new CodeModelProgramWriter(
			codeModel, errorHandler);

	final JsonStructureWriter<NType, NClass> jsonStructureWriter = new CodeModelJsonStructureWriter(
			codeModel, errorHandler);

	new JsonixInvoker().execute(getSettings(), model, programWriter,
			jsonStructureWriter);

	return true;
}
 
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: 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 #12
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 #13
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 #14
Source File: SimplifyPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void postProcessReferencePropertyInfo(final Model model,
		final CClassInfo classInfo, CReferencePropertyInfo property) {
	if (CustomizationUtils
			.containsPropertyCustomizationInPropertyOrClass(
					property,
					org.jvnet.jaxb2_commons.plugin.simplify.Customizations.PROPERTY_ELEMENT_NAME,
					org.jvnet.jaxb2_commons.plugin.simplify.Customizations.AS_ELEMENT_PROPERTY_ELEMENT_NAME)) {
		simplifyReferencePropertyInfoAsElementPropertyInfo(model,
				classInfo, property);
	} else if (CustomizationUtils
			.containsPropertyCustomizationInPropertyOrClass(
					property,
					org.jvnet.jaxb2_commons.plugin.simplify.Customizations.PROPERTY_ELEMENT_NAME,
					org.jvnet.jaxb2_commons.plugin.simplify.Customizations.AS_REFERENCE_PROPERTY_ELEMENT_NAME)) {
		simplifyReferencePropertyInfoAsReferencePropertyInfo(model,
				classInfo, property);
	}
}
 
Example #15
Source File: SimplifyPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void simplifyElementPropertyInfoAsElementPropertyInfo(
		final Model model, final CClassInfo classInfo,
		CElementPropertyInfo property) {
	if (property.getTypes().size() > 1) {
		logger.debug(MessageFormat
				.format("Element property [{0}] has several types and will be simplified.",
						property.getName(false)));
		int index = classInfo.getProperties().indexOf(property);
		for (CTypeRef typeRef : property.getTypes()) {
			final CElementPropertyInfo elementPropertyInfo = createElementPropertyInfo(
					model, property, typeRef);
			classInfo.getProperties().add(index++, elementPropertyInfo);
		}
		classInfo.getProperties().remove(property);
	} else {
		logger.warn(MessageFormat
				.format("Element property [{0}] will not be simplified as it does not contain multiple types.",
						property.getName(false)));
	}
}
 
Example #16
Source File: SimplifyPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void simplifyReferencePropertyInfoAsReferencePropertyInfo(
		final Model model, final CClassInfo classInfo,
		CReferencePropertyInfo property) {
	if (property.getElements().size() <= 1 && !property.isMixed()) {
		logger.warn(MessageFormat
				.format("Element reference property [{0}] will not be simplified as it does not contain multiple elements and is not mixed.",
						property.getName(false)));
	} else {
		logger.debug(MessageFormat
				.format("Element reference property [{0}] contains multiple elements or is mixed and will be simplified.",
						property.getName(false)));
		int index = classInfo.getProperties().indexOf(property);
		for (CElement element : property.getElements()) {
			final CReferencePropertyInfo referencePropertyInfo = createReferencePropertyInfo(
					model, property, element);
			classInfo.getProperties().add(index++, referencePropertyInfo);
		}
		if (property.isMixed()) {
			classInfo.getProperties().add(index++,
					createContentReferencePropertyInfo(model, property));
		}
		classInfo.getProperties().remove(property);
	}
}
 
Example #17
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 #18
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 #19
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 #20
Source File: SimplifyPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private CElementPropertyInfo createElementPropertyInfo(final Model model,
		CElementPropertyInfo property, CTypeRef typeRef) {
	final String propertyName = createPropertyName(model, property, typeRef);
	boolean required = false;
	final CElementPropertyInfo elementPropertyInfo = new CElementPropertyInfo(
			propertyName,
			property.isCollection() ? CollectionMode.REPEATED_ELEMENT
					: CollectionMode.NOT_REPEATED, typeRef.getTarget()
					.idUse(), typeRef.getTarget().getExpectedMimeType(),
			property.getSchemaComponent(), property.getCustomizations(),
			property.getLocator(), required);
	final CAdapter adapter = property.getAdapter();
	if (adapter != null) {
		elementPropertyInfo.setAdapter(adapter);
	}
	elementPropertyInfo.getTypes().add(typeRef);
	return elementPropertyInfo;
}
 
Example #21
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 #22
Source File: DefaultCustomizing.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private <T> T findCustomization(Model model, QName name) {
	final CPluginCustomization customization = CustomizationUtils
			.findCustomization(model, name);
	@SuppressWarnings("unchecked")
	final T t = (T) unmarshalCustomization(customization);
	return t;
}
 
Example #23
Source File: CustomizationsPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void postProcessClassInfo(Model model, CClassInfo classInfo) {
	final String packagedClassName = ClassUtils.getPackagedClassName(classInfo);
	final String customizationsFileName = packagedClassName.replace(".", "/") + ".xml";
	
	final List<CPluginCustomization> customizations = readCustomizations(customizationsFileName);
	
	classInfo.getCustomizations().addAll(customizations);
	
	for (CPropertyInfo propertyInfo: classInfo.getProperties())
	{
		postProcessPropertyInfo(model, classInfo, propertyInfo);
	}
}
 
Example #24
Source File: CustomizationsPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void postProcessModel(Model model) {
	for (final CClassInfo classInfo : model.beans().values()) {
		postProcessClassInfo(model, classInfo);
	}
	for (final CEnumLeafInfo enumLeafInfo : model.enums().values()) {
		postProcessEnumLeafInfo(model, enumLeafInfo);
	}
}
 
Example #25
Source File: CustomizationsPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void postProcessEnumLeafInfo(Model model, CEnumLeafInfo enumLeafInfo) {
	final String packagedClassName = ClassUtils.getPackagedClassName(enumLeafInfo);
	final String customizationsFileName = packagedClassName.replace(".", "/") + ".xml";
	
	final List<CPluginCustomization> customizations = readCustomizations(customizationsFileName);
	
	enumLeafInfo.getCustomizations().addAll(customizations);
	for (CEnumConstant enumConstant : enumLeafInfo.getConstants()) {
		postProcessEnumConstant(model, enumLeafInfo, enumConstant);
	}
}
 
Example #26
Source File: DefaultIgnoring.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public boolean isPackageInfoIgnored(ProcessModel context, Model model,
		Package packageInfo) {
	for (IgnoredPackage ignoredPackage : getCustomizing()
			.<IgnoredPackage> findCustomizations(model,
					Customizations.IGNORED_PACKAGE_ELEMENT_NAME)) {
		if (packageInfo.pkg.name().equals(ignoredPackage.getName())) {
			return true;
		}
	}
	return false;
}
 
Example #27
Source File: XJC21Mojo.java    From maven-jaxb2-plugin with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void doExecute(Options options) throws MojoExecutionException {
	final Model model = loadModel(options);
	final Outline outline = generateCode(model);
	writeCode(outline);

}
 
Example #28
Source File: XJC21Mojo.java    From maven-jaxb2-plugin with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected Model loadModel(Options options) throws MojoExecutionException {
	if (getVerbose()) {
		getLog().info("Parsing input schema(s)...");
	}
	final Model model = ModelLoader.load(options, new JCodeModel(),
			new LoggingErrorReceiver("Error while parsing schema(s).",
					getLog(), getVerbose()));

	if (model == null)
		throw new MojoExecutionException(
				"Unable to parse input schema(s). Error messages should have been provided.");
	return model;
}
 
Example #29
Source File: InheritancePlugin.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void postProcessModel(Model model, ErrorHandler errorHandler){
	super.postProcessModel(model, errorHandler);

	Map<String, CClassRef> classRefCache = new HashMap<>();

	CClassRef defaultBaseClazz = getClassRef(classRefCache, model, "org.dmg.pmml.PMMLObject");

	Collection<CClassInfo> classInfos = (model.beans()).values();
	for(CClassInfo classInfo : classInfos){
		CClassRef baseClazz = defaultBaseClazz;

		CPluginCustomization extendsCustomization = CustomizationUtils.findCustomization(classInfo, Customizations.EXTENDS_ELEMENT_NAME);
		if(extendsCustomization != null){
			ExtendsClass extendsClass = (ExtendsClass)CustomizationUtils.unmarshall(Customizations.getContext(), extendsCustomization);

			String name = getClassName(extendsClass);

			int lt = name.indexOf('<');
			if(lt > -1){
				name = name.substring(0, lt);
			}

			baseClazz = getClassRef(classRefCache, model, name);
		}

		classInfo.setBaseClass(baseClazz);
	}
}
 
Example #30
Source File: XJC20Mojo.java    From maven-jaxb2-plugin with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected void writeCode(Outline outline) throws MojoExecutionException {

		if (getWriteCode()) {
			final Model model = outline.getModel();
			final JCodeModel codeModel = model.codeModel;
			final File targetDirectory = model.options.targetDir;
			if (getVerbose()) {
				getLog().info(
						MessageFormat.format("Writing output to [{0}].",
								targetDirectory.getAbsolutePath()));
			}
			try {
				if (getCleanPackageDirectories()) {
					if (getVerbose()) {
						getLog().info("Cleaning package directories.");
					}
					cleanPackageDirectories(targetDirectory, codeModel);
				}
				final CodeWriter codeWriter = new LoggingCodeWriter(
						model.options.createCodeWriter(), getLog(),
						getVerbose());
				codeModel.build(codeWriter);
			} catch (IOException e) {
				throw new MojoExecutionException("Unable to write files: "
						+ e.getMessage(), e);
			}
		} else {
			getLog().info(
					"The [writeCode] setting is set to false, the code will not be written.");
		}
	}