Java Code Examples for org.dom4j.Element#hasContent()

The following examples show how to use org.dom4j.Element#hasContent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BehaviorTreeManager.java    From game-server with MIT License 6 votes vote down vote up
/**
 * 递归添加行为树子节点 <br>
 * 
 * @param element
 *            xml配置节点
 * @param task
 *            父任务
 */
private void addTask(Element element, Task<Person> task) {
	Iterator<Element> iterator = element.elementIterator();
	while (iterator.hasNext()) {
		Element secondElement = iterator.next();
		Task<Person> secondTask = createTask(secondElement);
		if (secondElement.getName().equalsIgnoreCase(XML_GUARD)) {
			task.setGuard(secondTask);
		} else {
			task.addChild(secondTask);
		}
		if (secondElement.hasContent()) {
			addTask(secondElement, secondTask);
		}
	}
}
 
Example 2
Source File: DefaultXmlCodecWalker.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public void walk(XmlListener listener, Element element,
                 Element rootElement) {
    try {
        Element newElement = element.createCopy();
        newElement.remove(element.getNamespace());

        listener.enterXmlElement(element, getElementType(newElement),
                                 rootElement);

        if (element.hasContent() && !element.isTextOnly()) {
            for (Iterator i = element.elementIterator(); i.hasNext();) {
                Element childElement = (Element) i.next();
                walk(listener, childElement, rootElement);
            }
        }

        listener.exitXmlElement(element, getElementType(element),
                                rootElement);
    } catch (Exception e) {
        log.error("Exception occurred when walk xml element: {}", element);
    }
}
 
Example 3
Source File: BehaviorTreeManager.java    From game-server with MIT License 5 votes vote down vote up
/**
 * 创建行为树
 * 
 * @param file
 * @return
 */
private Args.Two<String, BehaviorTree<? extends Person>> createBehaviorTree(File file) throws Exception {
	String xmlStr = FileUtil.readTxtFile(file.getPath());
	Document document = DocumentHelper.parseText(xmlStr);
	Element rootElement = document.getRootElement(); // 根节点

	Element idElement = rootElement.element(XML_ID); // id节点
	if (idElement == null) {
		throw new RuntimeException(String.format("%s 行为树id未配置", file.getPath()));
	}
	String id = idElement.getTextTrim();
	Element treeElement = rootElement.element(XML_TREE); // 行为树节点
	// 获取根节点
	if (treeElement == null || !treeElement.hasContent()) {
		throw new RuntimeException(String.format("%s 行为树节点未配置", file.getPath()));
	}
	List<?> treeRootElements = treeElement.elements();
	if (treeRootElements.size() > 1) {
		throw new RuntimeException(String.format("%s 行为树存在%d根节点", file.getPath(), treeRootElements.size()));
	}
	// 行为树xml根节点
	Element rootTaskElement = (Element) treeRootElements.get(0);
	// 行为树根任务
	Task<Person> rootTask = createTask(rootTaskElement);
	// 递归设置分支节点和叶子节点
	addTask(rootTaskElement, rootTask);

	BehaviorTree<? extends Person> behaviorTree = new BehaviorTree<>(rootTask);
	return Args.of(id, behaviorTree);
}
 
Example 4
Source File: XMLUtil.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 递归显示文档内容
 * @param els elements数组
 */
private static void print(List<Element> els) {
    for (Element el : els) {
        if (el.hasContent()) {
            print(el.elements());
        }
    }
}
 
Example 5
Source File: NetconfCodec.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the data root element based on the NETCONF operation parameter.
 *
 * @param rootElement root element of document tree to find the root node
 * @param opType      protocol operation being performed
 * @return the data root node element
 */
public Element getDataRootElement(Element rootElement,
                                  YmsOperationType opType) {

    Element retElement = null;
    String elementName = rootElement.getName();
    try {
        validateOpType(elementName, opType);
        // If config tag name is found then set the root element node.
        if (DATA.equals(elementName)
                || CONFIG.equals(elementName)
                || FILTER.equals(elementName)) {
            return rootElement;
        }

        // If element has child node then traverse through the child node
        // by recursively calling getDataRootElement method.
        if (rootElement.hasContent() && !rootElement.isTextOnly()) {
            for (Iterator i = rootElement.elementIterator();
                 i.hasNext();) {
                Element childElement = (Element) i.next();
                retElement = getDataRootElement(childElement, opType);
            }
        }
    } catch (Exception e) {
        // TODO
    }

    return retElement;
}
 
Example 6
Source File: DefaultXmlCodecWalker.java    From onos with Apache License 2.0 2 votes vote down vote up
/**
 * Determine the type of an element.
 *
 * @param element to be analysed
 * @return type of the element
 */
private XmlNodeType getElementType(Element element) {
    return element.hasContent() && element.isTextOnly() ?
            TEXT_NODE : OBJECT_NODE;
}