XML-based configuration utilities for Eclipse Jetty providing IoC mechanism for component configuration.
—
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.
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);
}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();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);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);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);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();
}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();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 ConfigureXmlAppendable 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();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");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();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();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();
}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());
}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>Text content is automatically escaped for XML safety:
< becomes <> becomes >& becomes &" becomes " in attributes' becomes ' in attributesCDATA sections preserve content without escaping:
<script><![CDATA[
function test() {
return x < y && a > b;
}
]]></script>Install with Tessl CLI
npx tessl i tessl/maven-org-eclipse-jetty--jetty-xml