All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.geosolutions.geoserver.rest.encoder.utils.NestedElementEncoder Maven / Gradle / Ivy

Go to download

GeoServer Manager is a library to interact with GeoServer The scope of this library is to have a simple API, and use as few external libs as possible.

There is a newer version: 1.8.7
Show newest version
/*
 *  GeoServer-Manager - Simple Manager Library for GeoServer
 *  
 *  Copyright (C) 2007,2011 GeoSolutions S.A.S.
 *  http://www.geo-solutions.it
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package it.geosolutions.geoserver.rest.encoder.utils;

import java.util.Iterator;
import java.util.List;

import org.jdom.Element;
import org.jdom.filter.Filter;

/**
 * Encodes lists of entries with key attribute.
 * e.g.:
 *
 * 
 * {@code
 *  
 *   val1
 *   val2
 *   val3
 * }
 * 
* * This can be also add compounded Elements * e.g.: * *
 * {@code
 * 
 *  
 *   	
 * 			false
 * 		
 * 	
 * 	
 * 		
 * 			true
 * 			ele
 * 			LIST
 * 		
 * 	
 * }
 * 
* * This can be also add list of compounded Elements * *
 * {@code
 * 
 *  
 *   	AllowMultithreading
 * 		false
 * 	
 * 	
 *
 * 			true
 * 			ele
 * 			LIST
 * 		
 * 	
 * 
 * }
 * 
* * @author ETj (etj at geo-solutions.it) * @author Carlo Cancellieri - [email protected] * @version $Id: $ */ public class NestedElementEncoder extends XmlElement { /** Constant ENTRY="entry" */ public final static String ENTRY = "entry"; /** Constant KEY="key" */ public final static String KEY = "key"; static class NestedElementFilter implements Filter { private static final long serialVersionUID = 1L; private final String key; private final String value; private final Element root; /** * if key is null we only check for children name if value is null we * only check for key attribute * * @param root * @param key * @param value */ public NestedElementFilter(Element root, String key, String value) { this.key = key; this.root = root; this.value = value; } public boolean matches(Object obj) { if (obj instanceof Element) { final Element el = ((Element) obj); if (root.isAncestor(el)) { if (el.getName().equals(ENTRY)/* && el.getText().equals(value) */) { boolean keyCheck=true; if (key != null) { if (el.getAttribute(KEY).getValue().equals(key)) { keyCheck=true; } else { keyCheck=false; } } if (value != null) return keyCheck&&checkChilds(el, value); else return keyCheck; } } } return false; } private static boolean checkChilds(Element el, String value) { final List childList = el.getChildren(); final Iterator childIt = childList.iterator(); while (childIt.hasNext()) { final Element child = childIt.next(); if (child.getName().equals(value)) { return true; } } return false; } }; /** *

Constructor for NestedElementEncoder.

* * @param listName a {@link java.lang.String} object. */ public NestedElementEncoder(String listName) { super(listName); } /** *

set

* * @param key a {@link java.lang.String} object. * @param value a {@link java.lang.String} object. */ public void set(final String key, final String value) { // if some previous similar object is found final Element search; if ((search = ElementUtils.contains(getRoot(), new NestedElementFilter( getRoot(), key, null))) != null) { // remove it ElementUtils.remove(getRoot(), search); } // add the new entry add(key, value); } /** *

set

* * @param key a {@link java.lang.String} object. * @param value a {@link org.jdom.Element} object. */ public void set(final String key, final Element value) { // if some previous similar object is found final Element search; if ((search = ElementUtils.contains(getRoot(), new NestedElementFilter( getRoot(), key, value.getName()))) != null) { // remove it ElementUtils.remove(getRoot(), search); } // add the new entry add(key, value); } /** *

add

* * @param key a {@link java.lang.String} object. * @param value a {@link org.jdom.Element} object. */ public void add(final String key, final Element value) { final Element entryElem = new Element(ENTRY); if (key != null) entryElem.setAttribute(KEY, key); entryElem.addContent(value); this.addContent(entryElem); } /** *

add

* * @param key a {@link java.lang.String} object. * @param value a {@link java.lang.String} object. */ public void add(final String key, final String value) { final Element entryElem = new Element(ENTRY); if (key != null) entryElem.setAttribute(KEY, key); entryElem.setText(value); this.addContent(entryElem); } /** *

add

* * @param key a {@link java.lang.String} object. * @param list a {@link java.util.List} object. */ public void add(final String key, final List list) { final Element entryElem = new Element(ENTRY); if (key != null) entryElem.setAttribute(KEY, key); // final Iterator it=list.iterator(); // while (it.hasNext()){ // final Element child=it.next(); entryElem.addContent(list); // } this.addContent(entryElem); } /** *

set

* * @param key a {@link java.lang.String} object. * @param value a {@link java.util.List} object. */ public void set(final String key, final List value) { // if some previous similar object is found final Element search; if ((search = ElementUtils.contains(getRoot(), new NestedElementFilter( getRoot(), key, value.get(0).getValue()))) != null) { // remove it ElementUtils.remove(search, search); } // add the new entry add(key, value); } /** {@inheritDoc} */ public boolean remove(final String key) { // if some previous similar object is found final Element search; if ((search = ElementUtils.contains(getRoot(), new NestedElementFilter( getRoot(), key, null))) != null) { return ElementUtils.remove(search, search); } else return false; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy