PomWrapper { public String resolveProperties(String src) { if (src == null) return ""; StringBuilder dst = new StringBuilder(256).append(src); int propIdx = 0; while ((propIdx = dst.indexOf("${", propIdx)) >= 0) { int endPropIdx = dst.indexOf("}", propIdx); if (endPropIdx < 0) break; String propName = dst.substring(propIdx+2, endPropIdx); if (propName.contains("{")) break; String propValue = lookupPropertyValue(propName); if (! StringUtil.isBlank(propValue)) { dst.delete(propIdx, endPropIdx + 1); if (propIdx < dst.length()) dst.insert(propIdx, propValue); else dst.append(propValue); } else { propIdx = endPropIdx; } } return dst.toString(); } PomWrapper(Document dom);  PomWrapper(File file); Document getDom(); void setDom(Document dom); String selectValue(String xpath); String selectValue(Node node, String xpath); Element selectElement(String xpath); Element selectElement(Node node, String xpath); List<Element> selectElements(String... paths); List<Element> selectElements(Node node, String... paths); Element selectOrCreateElement(String xpath); Element clear(String xpath); Map<String,String> getProperties(); String getProperty(String name); void setProperty(String name, String value); void deleteProperty(String name); String resolveProperties(String src); Artifact getGAV(); Artifact getParent(); @Override String toString(); List<Element> filterByGroupAndArtifact(List<Element> elements, String withGroupId, String withArtifactId);  }
@Test public void testResolveUserProperties() throws Exception { PomWrapper wrapper = new PomWrapper(ParseUtil.parseFromClasspath("PomWrapper3.xml")); assertEquals("null source", "", wrapper.resolveProperties(null)); assertEquals("empty source", "", wrapper.resolveProperties("")); assertEquals("no substitution required", "foo", wrapper.resolveProperties("foo")); assertEquals("simple substitution", "bar", wrapper.resolveProperties("${foo}")); assertEquals("embedded substitution", "arglebargle", wrapper.resolveProperties("argle${foo}gle")); assertEquals("repeated substitution", "bar-bar", wrapper.resolveProperties("${foo}-${foo}")); assertEquals("recursive substitution", "bar", wrapper.resolveProperties("${bargle}")); assertEquals("missing property", "${bar}", wrapper.resolveProperties("${bar}")); assertEquals("missing followed by present", "${bar}bar", wrapper.resolveProperties("${bar}${foo}")); assertEquals("unterminated propname 1", "${foo", wrapper.resolveProperties("${foo")); assertEquals("unterminated propname 2", "bar${foo", wrapper.resolveProperties("${foo}${foo")); assertEquals("unterminated propname 3", "${foogle", wrapper.resolveProperties("${foogle")); assertEquals("unterminated propname 4", "${foo${foo}", wrapper.resolveProperties("${foo${foo}")); }
@Test public void testResolveProjectProperties() throws Exception { PomWrapper wrapper = new PomWrapper(ParseUtil.parseFromClasspath("PomWrapper3.xml")); assertEquals("project.groupId", "com.example.pom", wrapper.resolveProperties("${project.groupId}")); assertEquals("project.artifactId", "wrapper3", wrapper.resolveProperties("${project.artifactId}")); assertEquals("project.version", "0.0.0-SNAPSHOT", wrapper.resolveProperties("${project.version}")); }