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

org.magicwerk.brownies.jdom.JdomMapAccessor Maven / Gradle / Ivy

/*
 * Copyright 2010 by Thomas Mauch
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * $Id$
 */
package org.magicwerk.brownies.jdom;

import org.jdom2.Element;
import org.magicwerk.brownies.core.CheckTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class {@link JdomMapAccessor} allows to handle map like structures in an XML tree.
 * It supports different ways of representing the map entries controlled by mode.
 * 

* NAMED_ATTR: *

{@code
 * 
 * }
* * NAMED_ELEM_WITH_TEXT: *
{@code
 * 
 * 	VALUE1
 * 	VALUE2
 * 
 * }
* * ENTRY_ELEM_WITH_TEXT: *
{@code
 * 
 * 	VALUE1
 * 	VALUE2
 * 
 * }
* * ENTRY_ELEM_WITH_ATTR: *
{@code
 * 
 * 	
 * 	
 * 
 * }
* * @author Thomas Mauch * @version $Id$ */ public class JdomMapAccessor { static final Logger LOG = LoggerFactory.getLogger(JdomMapAccessor.class); public enum Mode { NAMED_ATTR, NAMED_ELEM_WITH_TEXT, ENTRY_ELEM_WITH_TEXT, ENTRY_ELEM_WITH_ATTR } Mode mode; Element element; String entryName; String keyAttr; String valueAttr; // public JdomMapAccessor(Mode mode) { CheckTools.check(mode == Mode.NAMED_ATTR || mode == Mode.NAMED_ELEM_WITH_TEXT, "mode must be NAMED_ATTR or NAMED_ELEM_WITH_TEXT for this constructor"); this.mode = mode; } public JdomMapAccessor(Mode mode, String entryName, String keyAttr) { CheckTools.check(mode == Mode.ENTRY_ELEM_WITH_TEXT, "mode must be ENTRY_ELEM_WITH_TEXT for this constructor"); this.mode = mode; this.entryName = entryName; this.keyAttr = keyAttr; } public JdomMapAccessor(Mode mode, String entryName, String keyAttr, String valueAttr) { CheckTools.check(mode == Mode.ENTRY_ELEM_WITH_ATTR, "mode must be ENTRY_ELEM_WITH_ATTR for this constructor"); this.mode = mode; this.entryName = entryName; this.keyAttr = keyAttr; this.valueAttr = valueAttr; } public JdomMapAccessor attach(Element elem) { this.element = elem; return this; } public Element getElement() { return element; } public Mode getMode() { return mode; } // public boolean contains(String key) { CheckTools.checkNonNull(element, "call attach() to attach element"); if (mode == Mode.NAMED_ATTR) { return JdomTools.getAttribute(element, key) != null; } else if (mode == Mode.NAMED_ELEM_WITH_TEXT) { return JdomTools.getChild(element, key) != null; } else if (mode == Mode.ENTRY_ELEM_WITH_TEXT) { Element entry = getEntry(key); return entry != null; } else if (mode == Mode.ENTRY_ELEM_WITH_ATTR) { Element entry = getEntry(key); return (entry != null) ? JdomTools.getAttribute(entry, keyAttr) != null : false; } else { throw new AssertionError(); } } public String get(String key) { CheckTools.checkNonNull(element, "call attach() to attach element"); if (mode == Mode.NAMED_ATTR) { return JdomTools.getAttributeValue(element, key); } else if (mode == Mode.NAMED_ELEM_WITH_TEXT) { return JdomTools.getTextValue(element, key); } else if (mode == Mode.ENTRY_ELEM_WITH_TEXT) { Element entry = getEntry(key); return (entry != null) ? entry.getText() : null; } else if (mode == Mode.ENTRY_ELEM_WITH_ATTR) { Element entry = getEntry(key); return (entry != null) ? JdomTools.getAttributeValue(entry, keyAttr) : null; } else { throw new AssertionError(); } } public void set(String key, String value) { CheckTools.checkNonNull(element, "call attach() to attach element"); if (mode == Mode.NAMED_ATTR) { JdomTools.setAttributeValue(element, key, value); } else if (mode == Mode.NAMED_ELEM_WITH_TEXT) { JdomTools.setTextValue(element, key, value); } else if (mode == Mode.ENTRY_ELEM_WITH_TEXT) { Element entry = getOrCreateEntry(key); entry.setText(value); } else if (mode == Mode.ENTRY_ELEM_WITH_ATTR) { Element entry = getOrCreateEntry(key); entry.setAttribute(valueAttr, value); } else { throw new AssertionError(); } } public void remove(String key) { CheckTools.checkNonNull(element, "call attach() to attach element"); if (mode == Mode.NAMED_ATTR) { JdomTools.removeAttribute(element, key); } else if (mode == Mode.NAMED_ELEM_WITH_TEXT) { JdomTools.removeChild(element, key); } else if (mode == Mode.ENTRY_ELEM_WITH_TEXT || mode == Mode.ENTRY_ELEM_WITH_ATTR) { Element entry = getOrCreateEntry(key); if (entry != null) { entry.detach(); } } else { throw new AssertionError(); } } Element getOrCreateEntry(String key) { Element entry = JdomTools.getChild(element, entryName, keyAttr, key); if (entry == null) { entry = JdomTools.addChild(element, entryName); entry.setAttribute(keyAttr, key); } return entry; } Element getEntry(String key) { Element entry = JdomTools.getChild(element, entryName, keyAttr, key); return entry; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy