CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-jakarta-xml-bind--jakarta-xml-bind-api

Jakarta XML Binding API that automates the mapping between XML documents and Java objects through data binding

Pending
Overview
Eval results
Files

transform-integration.mddocs/

Transform Integration

Jakarta XML Binding provides utilities for seamless integration with JAXP Transform API, enabling JAXB objects to be used as Sources and Results in XSLT transformations and other XML processing pipelines.

Capabilities

JAXBSource - JAXB as Transform Source

JAXBSource allows JAXB objects to be used as javax.xml.transform.Source instances, enabling them to be input into XSLT transformations and other XML processing operations.

public class JAXBSource extends javax.xml.transform.sax.SAXSource {
    /**
     * Creates a new Source for the given content object using JAXBContext.
     * 
     * @param context JAXBContext that was used to create contentObject
     * @param contentObject An instance of a JAXB-generated class to be marshalled
     * @throws JAXBException if an error is encountered while creating the JAXBSource
     */
    public JAXBSource(JAXBContext context, Object contentObject) throws JAXBException;
    
    /**
     * Creates a new Source for the given content object using Marshaller.
     * 
     * @param marshaller A marshaller instance to marshal contentObject
     * @param contentObject An instance of a JAXB-generated class to be marshalled
     * @throws JAXBException if an error is encountered while creating the JAXBSource
     */
    public JAXBSource(Marshaller marshaller, Object contentObject) throws JAXBException;
}

Usage Examples:

import jakarta.xml.bind.*;
import jakarta.xml.bind.util.JAXBSource;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

// Create JAXB object to transform
Person person = new Person("Alice", 30);
JAXBContext context = JAXBContext.newInstance(Person.class);

// Create JAXBSource for XSLT transformation
JAXBSource source = new JAXBSource(context, person);

// Set up XSLT transformation
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer(new StreamSource("person-to-html.xsl"));

// Transform JAXB object to HTML
transformer.transform(source, new StreamResult(System.out));

// Using with custom Marshaller
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JAXBSource customSource = new JAXBSource(marshaller, person);
transformer.transform(customSource, new StreamResult("output.html"));

JAXBResult - Transform Result to JAXB

JAXBResult allows transformation results to be unmarshalled directly into JAXB objects, enabling XSLT transformations to produce Java objects.

public class JAXBResult extends javax.xml.transform.sax.SAXResult {
    /**
     * Creates a new instance that uses the specified JAXBContext to unmarshal.
     * 
     * @param context The JAXBContext used to create the necessary Unmarshaller
     * @throws JAXBException if an error is encountered while creating the JAXBResult
     */
    public JAXBResult(JAXBContext context) throws JAXBException;
    
    /**
     * Creates a new instance that uses the specified Unmarshaller.
     * 
     * @param unmarshaller The unmarshaller to use for unmarshalling
     * @throws JAXBException if an error is encountered while creating the JAXBResult
     */
    public JAXBResult(Unmarshaller unmarshaller) throws JAXBException;
    
    /**
     * Gets the unmarshalled object created by the transformation.
     * 
     * @return Always returns a non-null object
     * @throws IllegalStateException if called before an object is unmarshalled
     * @throws JAXBException if there is any unmarshalling error
     */
    public Object getResult() throws JAXBException;
}

Usage Examples:

import jakarta.xml.bind.*;
import jakarta.xml.bind.util.JAXBResult;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

// Create JAXBResult for transformation output
JAXBContext context = JAXBContext.newInstance(Person.class);
JAXBResult result = new JAXBResult(context);

// Set up XSLT transformation 
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer(new StreamSource("xml-to-person.xsl"));

// Transform XML document to JAXB object
transformer.transform(new StreamSource("input.xml"), result);

// Get the unmarshalled result
Person person = (Person) result.getResult();
System.out.println("Transformed person: " + person.getName());

// Using with custom Unmarshaller
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema(mySchema); // Add validation
JAXBResult validatedResult = new JAXBResult(unmarshaller);
transformer.transform(new StreamSource("input.xml"), validatedResult);
Person validatedPerson = (Person) validatedResult.getResult();

XML Processing Pipeline Integration

Transform integration utilities enable sophisticated XML processing workflows combining JAXB with other XML technologies.

Pipeline Example:

// Multi-step transformation pipeline
JAXBContext context = JAXBContext.newInstance(Order.class, Invoice.class);

// Step 1: Start with JAXB object
Order order = new Order(/* order data */);
JAXBSource orderSource = new JAXBSource(context, order);

// Step 2: Transform order to invoice XML
JAXBResult invoiceResult = new JAXBResult(context);
Transformer orderToInvoice = TransformerFactory.newInstance()
    .newTransformer(new StreamSource("order-to-invoice.xsl"));
orderToInvoice.transform(orderSource, invoiceResult);

// Step 3: Get invoice object
Invoice invoice = (Invoice) invoiceResult.getResult();

// Step 4: Transform invoice to PDF format
JAXBSource invoiceSource = new JAXBSource(context, invoice);
Transformer invoiceToPdf = TransformerFactory.newInstance()
    .newTransformer(new StreamSource("invoice-to-fo.xsl"));
invoiceToPdf.transform(invoiceSource, new StreamResult("invoice.fo"));

Types

Transform Source and Result Types

// JAXBSource extends SAXSource but applications should not access SAXSource methods
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.Source;
import javax.xml.transform.Result;

// Core transform interfaces
interface Source {
    String getSystemId();
    void setSystemId(String systemId);
}

interface Result {
    String getSystemId();
    void setSystemId(String systemId);
}

Important Notes:

  • JAXBSource and JAXBResult extend SAX-based classes as implementation details
  • Applications should not call methods inherited from SAXSource or SAXResult
  • Always use the public constructor and getResult() methods provided by these classes
  • These classes integrate seamlessly with any JAXP-compliant transformation engine

Install with Tessl CLI

npx tessl i tessl/maven-jakarta-xml-bind--jakarta-xml-bind-api

docs

binary-attachments.md

convenience-api.md

core-binding.md

data-type-conversion.md

index.md

transform-integration.md

type-adapters.md

validation-error-handling.md

xml-mapping-annotations.md

tile.json