EN · DE · RU · FR · ES

#755: XmlHelper.java

projectforge-business/src/main/java/org/projectforge/framework/xmlstream/XmlHelper.java Java utility class — XML parsing and formatting bridge. 135 lines. Uses dom4j. Source: projectforge-business/src/main/java/org/projectforge/framework/xmlstream/XmlHelper.java 135 lines · 97 code · 28 comments · 10 blank
Purpose: A utility class for XML operations — converting strings to dom4j Elements, Elements back to strings, and formatting. 135 lines. Uses dom4j for parsing/serialization of regular XML (not XStream, not W3C DOM). This is the third XML library in the project alongside XStream (user preferences) and W3C DOM API (SVG via Batik). The constant XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" — the standard XML header used when generating XML export files. Encoding is hardcoded as UTF-8 — a code-level decision, not configuration.

Methods

replaceQuotes(String xml) — quote normalization

Replaces single quotes (') with double quotes (") in XML strings. Javadoc: "This is useful for writing xml string constants e.g. in test cases." In Java string literals, single quotes for attributes avoid escaping, but valid XML requires double quotes. This method allows test code like: XmlHelper.replaceQuotes("<node name='test'/>")"<node name=\"test\"/>".

fromString(String str) — string → dom4j Element

Parses an XML string into a dom4j Element. Uses DocumentHelper.parseText(). Returns the root element (document.getRootElement()). On parse error, logs and returns null (doesn't throw — caller must null-check). For empty/blank strings, returns null.

toString(Element el, boolean prettyFormat) — dom4j Element → formatted XML string

Serializes a dom4j Element back to a string. Uses dom4j's XMLWriter with OutputFormat. When prettyFormat=true: enables newlines and 2-space indentation. Important: writer.close() is called to flush the buffer.

toString(org.w3c.dom.Document, boolean prettyFormat) — W3C DOM Document → string

Works with a different XML typeorg.w3c.dom.Document (used by SVG/Batik). Uses javax.xml.transform.Transformer (XSLT processor). With prettyFormat=true: adds INDENT=yes and Xalan-specific indent-amount=2. All errors are wrapped in InternalErrorException (unlike the dom4j method which logs and returns null) — because W3C DOM transformation errors are considered critical internal errors.

Two XML Libraries in One Class — Architectural Debt

Having methods for two different XML representations (dom4j and W3C DOM) in one class is a historical artifact. dom4j is used for XmlStream (data serialization), W3C DOM for SVG rendering (Batik). They ended up in the same utility class because "XML work" was perceived as one responsibility, though they serve different subsystems. A proper refactoring would extract the W3C method into an SvgXmlHelper or inline it into BatikImageRenderer.

Git History

868d6abb7 2025 → 2026 (copyright year update)
63081666f Source file headers: 2024→2025
b6092df09 Copyright 2023 → 2024
ab45d51fa Copyright 2001-2022 → 2001-2023
5f7ef41b8 Copyright 2021 → 2022
cd27dd997 package xstream → xmlstream
ceb63e8a1 Source code header: (C) 2001-2021
7c79f1922 Copyright of source header → 2020
32f634b88 Optimize imports
000ca723d Remove pointless boolean expressions (business)
dd5ca38ac CopyRight of all java file-header updated or created
a5bbdca6a Change logger to slf4j
f979e8a42 MGC-UPDATE: Update auf Version 3.0.0-SNAPSHOT
9ebb88522 Initial commit
Notable change: a5bbdca6a switched the logger from a proprietary implementation to SLF4J — part of a project-wide logging standardization. 000ca723d removed pointless boolean expressions (e.g., == true or redundant null checks) during a code cleanup pass. f979e8a42 was a major version bump from 2.x to 3.0.0-SNAPSHOT.