CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-eclipse-jetty--jetty-xml

XML-based configuration utilities for Eclipse Jetty providing IoC mechanism for component configuration.

Pending
Overview
Eval results
Files

xml-generation.mddocs/

XML Generation and Output

Utilities for programmatically generating well-formed XML with proper indentation and encoding. The XmlAppendable class provides a fluent API for creating XML documents with automatic formatting and proper escaping.

Capabilities

XmlAppendable Class

Helper class for generating well-formed XML with proper indentation and character escaping.

/**
 * Helper for generating well-formed XML with proper indentation
 */
class XmlAppendable {
    /**
     * Create XML appendable writing to output stream
     * @param out output stream for XML content
     */
    XmlAppendable(OutputStream out);
}

Tag Creation

Create XML elements with opening and closing tags.

/**
 * Open XML tag
 * @param tag tag name
 * @return this XmlAppendable for chaining
 */
XmlAppendable openTag(String tag);

/**
 * Open tag with attributes
 * @param tag tag name
 * @param attributes map of attribute name-value pairs
 * @return this XmlAppendable for chaining
 */
XmlAppendable openTag(String tag, Map<String, String> attributes);

/**
 * Close current tag
 * @return this XmlAppendable for chaining
 */
XmlAppendable closeTag();

Self-Closing Tags

Create self-closing XML elements.

/**
 * Create self-closing tag
 * @param tag tag name
 * @return this XmlAppendable for chaining
 */
XmlAppendable tag(String tag);

/**
 * Create self-closing tag with attributes
 * @param tag tag name
 * @param attributes map of attribute name-value pairs
 * @return this XmlAppendable for chaining
 */
XmlAppendable tag(String tag, Map<String, String> attributes);

Tags with Content

Create complete XML elements with text content.

/**
 * Create tag with text content
 * @param tag tag name
 * @param content text content (will be escaped)
 * @return this XmlAppendable for chaining
 */
XmlAppendable tag(String tag, String content);

/**
 * Create tag with attributes and content
 * @param tag tag name
 * @param attributes map of attribute name-value pairs
 * @param content text content (will be escaped)
 * @return this XmlAppendable for chaining
 */
XmlAppendable tag(String tag, Map<String, String> attributes, String content);

Content Methods

Add various types of content to XML documents.

/**
 * Add sanitized text content (XML characters escaped)
 * @param s text content to add
 * @return this XmlAppendable for chaining
 */
XmlAppendable content(String s);

/**
 * Add CDATA section
 * @param s content for CDATA section
 * @return this XmlAppendable for chaining
 */
XmlAppendable cdata(String s);

/**
 * Create tag with CDATA content
 * @param tag tag name
 * @param data content for CDATA section
 * @return this XmlAppendable for chaining
 */
XmlAppendable tagCDATA(String tag, String data);

/**
 * Add literal XML content (not escaped)
 * @param xml XML content to add literally
 */
void literal(String xml);

Usage Examples

Basic XML Generation

import org.eclipse.jetty.xml.XmlAppendable;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

// Create XML generator
try (FileOutputStream out = new FileOutputStream("output.xml")) {
    XmlAppendable xml = new XmlAppendable(out);
    
    // Generate simple XML
    xml.openTag("root")
       .tag("element", "content")
       .closeTag();
}

XML with Attributes

import java.util.HashMap;

XmlAppendable xml = new XmlAppendable(outputStream);

// Create attributes map
Map<String, String> attributes = new HashMap<>();
attributes.put("class", "com.example.Server");
attributes.put("id", "mainServer");

// Generate XML with attributes
xml.openTag("Configure", attributes)
   .tag("Set", Map.of("name", "port"), "8080")
   .tag("Set", Map.of("name", "host"), "localhost")
   .closeTag();

Complex Configuration Generation

XmlAppendable xml = new XmlAppendable(outputStream);

// Generate Jetty-style configuration
xml.openTag("Configure", Map.of("class", "org.eclipse.jetty.server.Server"))
   
   // Set server port
   .openTag("Set", Map.of("name", "port"))
   .openTag("Property", Map.of("name", "jetty.port", "default", "8080"))
   .closeTag()
   .closeTag()
   
   // Add connector
   .openTag("Call", Map.of("name", "addConnector"))
   .openTag("Arg")
   .openTag("New", Map.of("class", "org.eclipse.jetty.server.ServerConnector"))
   .openTag("Arg")
   .openTag("Ref", Map.of("refid", "Server"))
   .closeTag()
   .closeTag()
   .closeTag()
   .closeTag()
   .closeTag()
   
   .closeTag(); // Close Configure

Self-Closing Tags

XmlAppendable xml = new XmlAppendable(outputStream);

// Generate self-closing elements
xml.openTag("configuration")
   .tag("property", Map.of("name", "enabled", "value", "true"))
   .tag("include", Map.of("file", "additional.xml"))
   .closeTag();

CDATA Content

XmlAppendable xml = new XmlAppendable(outputStream);

// Add CDATA sections for complex content
xml.openTag("script")
   .cdata("function test() { return x < y && a > b; }")
   .closeTag();

// Or use convenience method
xml.tagCDATA("description", "This contains <special> & characters");

Mixed Content Types

XmlAppendable xml = new XmlAppendable(outputStream);

xml.openTag("document")
   .content("This is plain text content with <escaped> characters")
   .openTag("code")
   .cdata("unescaped <code> & content")
   .closeTag()
   .literal("<!-- This is literal XML -->")
   .closeTag();

Fluent API Chaining

XmlAppendable xml = new XmlAppendable(outputStream);

// Chain multiple operations fluently
xml.openTag("server")
   .tag("name", "Production Server")
   .tag("port", "8080")
   .tag("ssl-enabled", "true")
   .openTag("connectors")
   .tag("connector", Map.of("type", "http", "port", "8080"))
   .tag("connector", Map.of("type", "https", "port", "8443"))
   .closeTag()
   .openTag("handlers")
   .tag("handler", Map.of("class", "WebAppHandler", "contextPath", "/"))
   .closeTag()
   .closeTag();

Generate Configuration from Data

import java.util.List;

public void generateServerConfig(List<ServerConfig> configs, OutputStream out) {
    XmlAppendable xml = new XmlAppendable(out);
    
    xml.openTag("Configure", Map.of("class", "org.eclipse.jetty.server.Server"));
    
    for (ServerConfig config : configs) {
        Map<String, String> setAttrs = Map.of("name", config.getPropertyName());
        xml.tag("Set", setAttrs, config.getPropertyValue());
    }
    
    xml.closeTag();
}

Error Handling

try (FileOutputStream out = new FileOutputStream("config.xml")) {
    XmlAppendable xml = new XmlAppendable(out);
    
    xml.openTag("configuration")
       .tag("setting", "value")
       .closeTag();
       
} catch (IOException e) {
    System.err.println("Failed to write XML: " + e.getMessage());
}

XML Output Features

Automatic Indentation

The XmlAppendable class automatically handles proper indentation:

<Configure class="org.eclipse.jetty.server.Server">
  <Set name="port">8080</Set>
  <Call name="addConnector">
    <Arg>
      <New class="org.eclipse.jetty.server.ServerConnector">
        <Arg><Ref refid="Server"/></Arg>
      </New>
    </Arg>
  </Call>
</Configure>

Character Escaping

Text content is automatically escaped for XML safety:

  • < becomes &lt;
  • > becomes &gt;
  • & becomes &amp;
  • " becomes &quot; in attributes
  • ' becomes &apos; in attributes

CDATA Support

CDATA sections preserve content without escaping:

<script><![CDATA[
function test() {
  return x < y && a > b;
}
]]></script>

Best Practices

  1. Always close tags: Use try-with-resources or ensure proper tag closure
  2. Use CDATA for complex content: When content contains XML metacharacters
  3. Validate attribute values: Ensure attribute values are properly formatted
  4. Handle I/O exceptions: Wrap file operations in proper exception handling
  5. Use fluent chaining: Take advantage of method chaining for cleaner code

Install with Tessl CLI

npx tessl i tessl/maven-org-eclipse-jetty--jetty-xml

docs

configuration-extension.md

index.md

xml-configuration.md

xml-generation.md

xml-parser.md

tile.json