Adding recursive child xml node deletion to support GS*Encoder-centric implementation of #199.
This commit is contained in:
parent
f251419bd1
commit
5163af60ff
@ -3273,14 +3273,8 @@ public class GeoServerRESTPublisher {
|
|||||||
|
|
||||||
String sUrl = baseUrl + "?recalculate=" + calculationMode.getParamValue();
|
String sUrl = baseUrl + "?recalculate=" + calculationMode.getParamValue();
|
||||||
LOGGER.debug("Constructed the following url for bounding box recalculation: " + sUrl);
|
LOGGER.debug("Constructed the following url for bounding box recalculation: " + sUrl);
|
||||||
|
|
||||||
|
|
||||||
// String body = wsenc.toString();
|
renc.recursivelyRemoveEmptyChildren();
|
||||||
// String body = "<" + xmlElementName +"><name>" + layerName + "</name>" +
|
|
||||||
// "<enabled>" + enabled + "</enabled></" + xmlElementName + ">";
|
|
||||||
renc.remove(GSResourceEncoder.KEYWORDS);
|
|
||||||
renc.remove(GSResourceEncoder.METADATA);
|
|
||||||
renc.remove(GSResourceEncoder.METADATALINKS);
|
|
||||||
String body = renc.toString();
|
String body = renc.toString();
|
||||||
String sendResult = HTTPUtils.putXml(sUrl, body, gsuser, gspass);
|
String sendResult = HTTPUtils.putXml(sUrl, body, gsuser, gspass);
|
||||||
boolean success = sendResult != null;
|
boolean success = sendResult != null;
|
||||||
|
|||||||
@ -26,9 +26,13 @@
|
|||||||
package it.geosolutions.geoserver.rest.encoder.utils;
|
package it.geosolutions.geoserver.rest.encoder.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import org.jdom.Content;
|
import org.jdom.Content;
|
||||||
import org.jdom.Element;
|
import org.jdom.Element;
|
||||||
import org.jdom.Text;
|
import org.jdom.Text;
|
||||||
|
import org.jdom.filter.ContentFilter;
|
||||||
|
import org.jdom.filter.ElementFilter;
|
||||||
import org.jdom.output.Format;
|
import org.jdom.output.Format;
|
||||||
import org.jdom.output.XMLOutputter;
|
import org.jdom.output.XMLOutputter;
|
||||||
|
|
||||||
@ -122,6 +126,35 @@ public class XmlElement{
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void recursivelyRemoveEmptyChildren(){
|
||||||
|
//must make a copy to avoid ConcurrentModificationException
|
||||||
|
List<Content> children = new ArrayList<Content>(this.root.getContent());
|
||||||
|
|
||||||
|
for(Content child : children){
|
||||||
|
if(child instanceof Element){
|
||||||
|
recursivelyRemoveEmptyChildren(this.root, (Element)child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void recursivelyRemoveEmptyChildren(Element grandparent, Element parent){
|
||||||
|
//must make a copy to avoid ConcurrentModificationException
|
||||||
|
List<Content> childrenPreRemoval;
|
||||||
|
childrenPreRemoval = new ArrayList<Content>(parent.getContent());
|
||||||
|
|
||||||
|
//base case: the parent has no children
|
||||||
|
for(Content child : childrenPreRemoval){
|
||||||
|
//recursive case: the parent has children
|
||||||
|
if(child instanceof Element){
|
||||||
|
recursivelyRemoveEmptyChildren(parent, (Element)child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//if the parent node contains no children, remove it from the parent
|
||||||
|
List<Content> childrenPostRemoval = parent.getContent();
|
||||||
|
if(childrenPostRemoval.isEmpty()){
|
||||||
|
grandparent.removeContent(parent);
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @return an xml String
|
* @return an xml String
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -0,0 +1,119 @@
|
|||||||
|
package it.geosolutions.geoserver.rest.encoder.utils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import org.jdom.Document;
|
||||||
|
import org.jdom.Element;
|
||||||
|
import org.jdom.JDOMException;
|
||||||
|
import org.jdom.input.SAXBuilder;
|
||||||
|
import org.jdom.output.XMLOutputter;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class XmlElementTest {
|
||||||
|
|
||||||
|
public XmlElementTest() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUpClass() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void tearDownClass() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
}
|
||||||
|
|
||||||
|
private XmlElement makeElement(String docString){
|
||||||
|
Document doc;
|
||||||
|
SAXBuilder builder = new SAXBuilder();
|
||||||
|
try {
|
||||||
|
doc = builder.build(new StringReader(docString));
|
||||||
|
} catch (JDOMException ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
Element root = doc.getRootElement();
|
||||||
|
|
||||||
|
return new XmlElement(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertEqualXml(String message, XmlElement expected, XmlElement actual){
|
||||||
|
XMLOutputter out = new XMLOutputter();
|
||||||
|
String expectedElementString = out.outputString(expected.getRoot());
|
||||||
|
String actualElementString = out.outputString(actual.getRoot());
|
||||||
|
assertEquals(message, expectedElementString, actualElementString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRecursiveRemovalOnChildlessParent(){
|
||||||
|
XmlElement root = makeElement("<parent/>");
|
||||||
|
root.recursivelyRemoveEmptyChildren();
|
||||||
|
assertEqualXml("no child elements expected", makeElement("<parent/>"), root);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRecursiveRemovalOfOneChild(){
|
||||||
|
XmlElement root = makeElement("<parent><child/></parent>");
|
||||||
|
root.recursivelyRemoveEmptyChildren();
|
||||||
|
assertEqualXml("no child elements expected", makeElement("<parent/>"), root);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRecursiveRemovalOfOneChildWithOneKeeper(){
|
||||||
|
XmlElement root = makeElement("<parent><child/><keep>keep</keep></parent>");
|
||||||
|
root.recursivelyRemoveEmptyChildren();
|
||||||
|
assertEqualXml("one child element expected", makeElement("<parent><keep>keep</keep></parent>"), root);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRecursiveRemovalOfOneParentAndOneChild() {
|
||||||
|
XmlElement root = makeElement("<grandparent><parent><child/></parent></grandparent>");
|
||||||
|
root.recursivelyRemoveEmptyChildren();
|
||||||
|
assertEqualXml("no child elements expected", makeElement("<grandparent/>"), root);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRecursiveRemovalOfOneParentAndManyChildren() {
|
||||||
|
XmlElement root = makeElement("<grandparent><parent><child/><child/><child/></parent></grandparent>");
|
||||||
|
root.recursivelyRemoveEmptyChildren();
|
||||||
|
assertEqualXml("no child elements expected", makeElement("<grandparent/>"), root);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRecursiveRemovalOfManyParentsWithOneChild() {
|
||||||
|
XmlElement root = makeElement("<grandparent><parent><child/></parent><parent><child/></parent><parent><child/></parent></grandparent>");
|
||||||
|
root.recursivelyRemoveEmptyChildren();
|
||||||
|
assertEqualXml("no child elements expected", makeElement("<grandparent/>"), root);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRecursiveRemovalOfManyParentsAndManyChildren() {
|
||||||
|
XmlElement root = makeElement("<grandparent><parent><child/><child/><child/></parent><parent><child/><child/><child/></parent><parent><child/><child/><child/></parent></grandparent>");
|
||||||
|
root.recursivelyRemoveEmptyChildren();
|
||||||
|
assertEqualXml("no child elements expected", makeElement("<grandparent/>"), root);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRecursiveRemovalOfManyParentsAndManyChildrenWithSomeKeepers() {
|
||||||
|
XmlElement root = makeElement("<grandparent><keep>keep</keep><parent><keep>keep</keep><child/><child><keep>keep</keep></child><child/></parent><parent><child/><child/><child/></parent><parent><child/><child/><child><keep>keep</keep></child></parent></grandparent>");
|
||||||
|
root.recursivelyRemoveEmptyChildren();
|
||||||
|
assertEqualXml("only non-empty child elements should remain", makeElement("<grandparent><keep>keep</keep><parent><keep>keep</keep><child><keep>keep</keep></child></parent><parent><child><keep>keep</keep></child></parent></grandparent>"), root);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user