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

org.yaoqiang.bpmn.model.elements.XMLExtensionElement Maven / Gradle / Ivy

There is a newer version: 2.2.18
Show newest version
package org.yaoqiang.bpmn.model.elements;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;

/**
 * XMLExtensionElement
 * 
 * @author Shi Yaoqiang([email protected])
 */
public class XMLExtensionElement extends XMLElement {

	protected LinkedHashMap elements = new LinkedHashMap();

	public XMLExtensionElement(XMLElement parent, String name) {
		super(parent, name);
	}

	public XMLExtensionElement(XMLElement parent, String name, String value) {
		super(parent, name, value);
	}

	public void addAttribute(XMLAttribute el) {
		elements.put(el.toName(), el);
	}

	public void addChildElement(XMLExtensionElement el) {
		int id = elements.size();
		while (contains(String.valueOf(id))) {
			id++;
		}
		elements.put(String.valueOf(id), el);
		el.setParent(this);
	}

	public void removeChildElement(XMLExtensionElement el) {
		for (Entry e : elements.entrySet()) {
			if (e.getValue() == el) {
				elements.remove(e.getKey());
				break;
			}
		}
	}

	public XMLElement getAttribute(String name) {
		return elements.get(name);
	}

	public void setAttribute(String name, String value) {
		XMLElement el = getAttribute(name);
		if (el == null) {
			el = new XMLAttribute(this, name);
			this.addAttribute((XMLAttribute) el);
		}
		el.setValue(value);
	}

	public XMLElement removeAttribute(String name) {
		return elements.remove(name);
	}

	public boolean contains(XMLExtensionElement el) {
		for (Entry e : elements.entrySet()) {
			if (e.getValue() == el) {
				return true;
			}
		}
		return false;
	}

	public boolean contains(String id) {
		return elements.containsKey(id);
	}

	public List getChildElements(String name) {
		List children = new ArrayList();
		for (XMLElement el : elements.values()) {
			if (el instanceof XMLExtensionElement && (name == null || name.length() == 0 || name.equals(el.toName()))) {
				children.add((XMLExtensionElement) el);
			}
		}
		return children;
	}

	public XMLExtensionElement getChildElement(String name) {
		for (XMLElement el : elements.values()) {
			if (el instanceof XMLExtensionElement && el.toName().equals(name)) {
				return (XMLExtensionElement) el;
			}
		}
		return null;
	}

	public List toElements() {
		return new ArrayList(elements.values());
	}

	public String toValue() {
		XMLExtensionElement el = getChildElement("#text");
		if (el == null) {
			el = getChildElement("#cdata-section");
		}
		if (el == null) {
			return super.toValue();
		}
		return el.toValue();
	}

	public void setValue(String value) {
		XMLExtensionElement el = getChildElement("#text");
		if (el == null) {
			el = getChildElement("#cdata-section");
		}
		if (el == null) {
			super.setValue(value);
		} else {
			el.setValue(value);
		}
	}

	public boolean isEmpty() {
		boolean isEmpty = true;
		for (XMLElement el : elements.values()) {
			isEmpty = isEmpty && el.isEmpty();
		}
		isEmpty = isEmpty && value.trim().length() == 0;
		return isEmpty;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy