in maven-release-manager/src/main/java/org/apache/maven/shared/release/transform/jdom2/JDomModelETL.java [95:150]
public void extract(File pomFile) throws ReleaseExecutionException {
try {
String content = ReleaseUtil.readXmlFile(pomFile, ls);
// we need to eliminate any extra whitespace inside elements, as JDOM2 will nuke it
content = content.replaceAll("<([^!][^>]*?)\\s{2,}([^>]*?)>", "<$1 $2>");
content = content.replaceAll("(\\s{2,})/>", "$1 />");
SAXBuilder builder = new SAXBuilder();
document = builder.build(new StringReader(content));
// Normalize line endings to platform's style (XML processors like JDOM2 normalize line endings to "\n" as
// per section 2.11 of the XML spec)
normaliseLineEndings(document);
// rewrite DOM as a string to find differences, since text outside the root element is not tracked
StringWriter w = new StringWriter();
Format format = Format.getRawFormat();
format.setLineSeparator(ls);
XMLOutputter out = new XMLOutputter(format);
out.output(document.getRootElement(), w);
int index = content.indexOf(w.toString());
if (index >= 0) {
intro = content.substring(0, index);
outtro = content.substring(index + w.toString().length());
} else {
/*
* NOTE: Due to whitespace, attribute reordering or entity expansion the above indexOf test can easily
* fail. So let's try harder. Maybe some day, when JDOM2 offers a StaxBuilder and this builder employes
* XMLInputFactory2.P_REPORT_PROLOG_WHITESPACE, this whole mess can be avoided.
*/
// CHECKSTYLE_OFF: LocalFinalVariableName
final String SPACE = "\\s++";
final String XML = "<\\?(?:(?:[^\"'>]++)|(?:\"[^\"]*+\")|(?:'[^\']*+'))*+>";
final String INTSUB = "\\[(?:(?:[^\"'\\]]++)|(?:\"[^\"]*+\")|(?:'[^\']*+'))*+\\]";
final String DOCTYPE =
"<!DOCTYPE(?:(?:[^\"'\\[>]++)|(?:\"[^\"]*+\")|(?:'[^\']*+')|(?:" + INTSUB + "))*+>";
final String PI = XML;
final String COMMENT = "<!--(?:[^-]|(?:-[^-]))*+-->";
final String INTRO =
"(?:(?:" + SPACE + ")|(?:" + XML + ")|(?:" + DOCTYPE + ")|(?:" + COMMENT + ")|(?:" + PI + "))*";
final String OUTRO = "(?:(?:" + SPACE + ")|(?:" + COMMENT + ")|(?:" + PI + "))*";
final String POM = "(?s)(" + INTRO + ")(.*?)(" + OUTRO + ")";
// CHECKSTYLE_ON: LocalFinalVariableName
Matcher matcher = Pattern.compile(POM).matcher(content);
if (matcher.matches()) {
intro = matcher.group(1);
outtro = matcher.group(matcher.groupCount());
}
}
} catch (JDOMException | IOException e) {
throw new ReleaseExecutionException("Error reading POM: " + e.getMessage(), e);
}
}