CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-glassfish-jaxb--jaxb-xjc

JAXB Binding Compiler (XJC) that generates Java classes from XML Schema definitions with both command-line and programmatic APIs

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

JAXB XJC (XML to Java Compiler)

JAXB XJC is a comprehensive code generation tool that automatically transforms XML Schema definitions (XSD) into strongly-typed Java classes, enabling seamless XML data binding within Java applications. As part of the Eclipse Implementation of JAXB (Jakarta XML Binding), this tool provides both command-line and programmatic interfaces for enterprise XML processing workflows.

Package Information

  • Package Name: jaxb-xjc
  • Package Type: maven
  • Language: Java
  • Installation:
    <dependency>
      <groupId>org.glassfish.jaxb</groupId>
      <artifactId>jaxb-xjc</artifactId>
      <version>4.0.5</version>
    </dependency>

Core Imports

import com.sun.tools.xjc.api.XJC;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.Driver;

For plugin development:

import com.sun.tools.xjc.Plugin;
import com.sun.tools.xjc.outline.Outline;
import com.sun.tools.xjc.Options;

Basic Usage

Command Line Usage

# Basic schema compilation
xjc schema.xsd

# Generate into specific package
xjc -p com.example.generated schema.xsd

# Generate into target directory
xjc -d target/generated-sources schema.xsd

# Enable verbose output
xjc -verbose schema.xsd

Programmatic Usage

import com.sun.tools.xjc.api.XJC;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.ErrorListener;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.writer.FileCodeWriter;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
import java.io.File;

// Create schema compiler
SchemaCompiler compiler = XJC.createSchemaCompiler();

// Configure compiler
compiler.setDefaultPackageName("com.example.generated");
compiler.setErrorListener(new ErrorListener() {
    public void error(SAXParseException exception) { /* handle error */ }
    public void warning(SAXParseException exception) { /* handle warning */ }
    public void info(SAXParseException exception) { /* handle info */ }
});

// Parse schema
compiler.parseSchema(new InputSource("schema.xsd"));

// Compile and generate code
S2JJAXBModel model = compiler.bind();
JCodeModel codeModel = model.generateCode(null, null);

// Write generated classes to filesystem
codeModel.build(new FileCodeWriter(new File("target/generated")));

Architecture

JAXB XJC is built around several key architectural components:

  • Schema Parsing: Multi-format schema reader supporting XML Schema, DTD, RelaxNG, and WSDL
  • Model Building: Internal object model representing parsed schemas and binding decisions
  • Code Generation: Bean generator producing JAXB-annotated Java classes with proper type safety
  • Plugin System: Extensible architecture allowing custom code generation and model modification
  • CLI Interface: Command-line driver providing comprehensive configuration options
  • Programmatic API: Type-safe Java API for embedding XJC in build tools and applications

Capabilities

Command Line Interface (CLI)

Command-line tool for batch processing XML schemas with comprehensive configuration options. Ideal for build scripts and automated code generation workflows.

public final class Driver {
    public static void main(String[] args) throws Exception;
    public static int run(String[] args, PrintStream status, PrintStream out) throws Exception;
    public static int run(String[] args, XJCListener listener) throws BadCommandLineException;
    public static void usage(Options opts, boolean privateUsage);
    public static String getBuildID();
}

Command Line Interface

Programmatic API

Type-safe Java API for embedding schema compilation in applications, build tools, and IDEs. Provides full control over compilation process and generated code.

public final class XJC {
    public static SchemaCompiler createSchemaCompiler();
    public static String getDefaultPackageName(String namespaceUri);
}

public interface SchemaCompiler {
    ContentHandler getParserHandler(String systemId);
    void parseSchema(InputSource source);
    void parseSchema(String systemId, Element element);
    void parseSchema(String systemId, XMLStreamReader reader);
    void setTargetVersion(SpecVersion version);
    void setErrorListener(ErrorListener errorListener);
    void setEntityResolver(EntityResolver entityResolver);
    void setDefaultPackageName(String packageName);
    void forcePackageName(String packageName);
    void setClassNameAllocator(ClassNameAllocator allocator);
    void resetSchema();
    S2JJAXBModel bind();
}

Programmatic API

Plugin System

Extensible plugin architecture for customizing code generation, adding annotations, and implementing domain-specific features.

public abstract class Plugin {
    public abstract String getOptionName();
    public abstract String getUsage();
    public abstract boolean run(Outline outline, Options opt, ErrorHandler errorHandler) throws SAXException;
    public int parseArgument(Options opt, String[] args, int i) throws BadCommandLineException, IOException;
    public List<String> getCustomizationURIs();
    public void onActivated(Options opts) throws BadCommandLineException;
    public void postProcessModel(Model model, ErrorHandler errorHandler);
}

Plugin System

Code Generation and Customization

Bean generation system with customizable field rendering and type mapping for generating optimized JAXB classes.

public interface Outline {
    Model getModel();
    JCodeModel getCodeModel();
    Collection<? extends ClassOutline> getClasses();
    ClassOutline getClazz(CClassInfo clazz);
    FieldOutline getField(CPropertyInfo fu);
    PackageOutline getPackageContext(JPackage _Package);
}

public interface FieldRendererFactory {
    FieldRenderer getDefault();
    FieldRenderer getArray();
    FieldRenderer getList(JClass coreList);
    FieldRenderer getSingle();
    FieldRenderer getConst(FieldRenderer fallback);
}

Code Generation

Error Handling and Validation

Comprehensive error reporting system with configurable error listeners and validation support for schema processing and code generation.

public interface ErrorListener {
    void error(SAXParseException exception);
    void warning(SAXParseException exception);
    void info(SAXParseException exception);
}

public abstract class ErrorReceiver {
    public abstract void error(SAXParseException exception);
    public abstract void warning(SAXParseException exception);
    public abstract void info(SAXParseException exception);
    public void pollAbort() throws AbortException;
}

Error Handling

Build Tool Integration

Ant tasks and build system integration components for Maven, Gradle, and other build tools.

public class XJC2Task extends Task {
    // Ant task implementation for XJC
}

public class XJCBase {
    // Base class for build tool integration
}

Build Integration

Types

Core Model Types

public interface S2JJAXBModel extends JAXBModel {
    Mapping get(QName elementName);
    List<JClass> getAllObjectFactories();
    Collection<? extends Mapping> getMappings();
    TypeAndAnnotation getJavaType(QName xmlTypeName);
    JCodeModel generateCode(Plugin[] extensions, ErrorListener errorListener) throws IOException;
}

public interface JAXBModel {
    List<String> getClassList();
}

public class Model implements TypeInfoSet<TypeInfo,CClassInfo,CElementInfo,CEnumLeafInfo> {
    public Outline generateCode(Options opt, ErrorReceiver receiver);
    public JCodeModel codeModel;
}

Configuration Types

public class Options {
    public boolean debugMode;
    public boolean verbose;
    public boolean quiet;
    public boolean readOnly;
    public String encoding;
    public File targetDir;
    public String defaultPackage;
    
    public int parseArgument(String[] args, int i) throws BadCommandLineException;
    public void parseArguments(String[] args) throws BadCommandLineException;
    public FieldRendererFactory getFieldRendererFactory();
    public void setFieldRendererFactory(FieldRendererFactory frf, Plugin owner);
    public ClassLoader getUserClassLoader(ClassLoader parent);
}

Outline Types

public abstract class ClassOutline {
    public final CClassInfo target;
    public final JDefinedClass ref;
    public abstract Outline parent();
}

public abstract class FieldOutline {
    // Field-specific outline information
}

public interface PackageOutline {
    // Package-specific outline information
}

API Utility Types

public interface Property {
    String name();
    JType type();
    QName elementName();
    QName rawName();
}

public interface ClassNameAllocator {
    String assignClassName(String packageName, String className);
}

public interface TypeAndAnnotation {
    // Type and annotation information
}

public interface Mapping {
    // XML to Java mapping information
}

Enumeration Types

public enum SpecVersion {
    V2_3("2.3"),
    V3_0("3.0");
    
    public final String version;
    
    SpecVersion(String version);
    public static SpecVersion parse(String token);
    public boolean isLaterThan(SpecVersion other);
}

public enum Language {
    DTD("DTD", "dtd"),
    XMLSCHEMA("XML Schema", "xsd"),
    RELAXNG("RELAX NG", "rng"),
    RELAXNG_COMPACT("RELAX NG (compact syntax)", "rnc"),
    WSDL("WSDL", "wsdl");
    
    public final String displayName;
    public final String[] expectedExtensions;
    
    Language(String displayName, String... expectedExtensions);
}

Exception Types

public class BadCommandLineException extends Exception {
    public BadCommandLineException(String message);
    public BadCommandLineException(String message, Throwable cause);
}

public class AbortException extends RuntimeException {
    public AbortException();
    public AbortException(String message);
}

docs

build-integration.md

cli.md

code-generation.md

error-handling.md

index.md

plugin-system.md

programmatic-api.md

tile.json