Example usage for org.dom4j Element hasMixedContent

List of usage examples for org.dom4j Element hasMixedContent

Introduction

In this page you can find the example usage for org.dom4j Element hasMixedContent.

Prototype

boolean hasMixedContent();

Source Link

Document

Returns true if this Element has mixed content.

Usage

From source file:ch.javasoft.xml.config.XmlPrint.java

License:BSD License

/**
 * Print the given element using the given print writer and initial 
 * indention//www .  j a v a2 s  . co m
 * @param elem      the xml element
 * @param indention   the initial indention
 * @param writer   the print writer to use for the output
 */
@SuppressWarnings("unchecked")
public void print(Element elem, String indention, PrintWriter writer) {
    writer.print(indention + "<" + elem.getName());

    Iterator<Attribute> itAtt = elem.attributeIterator();
    Iterator<Element> itElem = elem.elementIterator();

    if (elem.hasMixedContent() || (elem.hasContent() && !itElem.hasNext())) {
        Iterator<Node> it = elem.nodeIterator();
        while (it.hasNext()) {
            Node node = it.next();
            if (node instanceof CharacterData) {
                if (!(node instanceof Comment) && node.getText().trim().length() != 0) {
                    throw new IllegalArgumentException(
                            "text content not supported: \"" + node.getText() + "\"");
                }
            } else if (!(node instanceof Element || node instanceof Attribute)) {
                throw new IllegalArgumentException("only attributes and elements are supported");
            }
        }
    }
    while (itAtt.hasNext()) {
        Attribute att = itAtt.next();
        final String attName = att.getName();
        final String attValue = att.getValue();
        writer.print(" " + attName + "=\"" + escapeAttributeValue(attValue) + "\"");
    }
    if (!itElem.hasNext()) {
        writer.println("/>");
    } else {
        writer.println(">");
        while (itElem.hasNext()) {
            print(itElem.next(), indention + getIndention(), writer);
        }
        writer.println(indention + "</" + elem.getName() + ">");
    }
    writer.flush();
}

From source file:com.mpaike.core.config.xml.elementreader.GenericElementReader.java

License:Open Source License

/**
 * Creates a ConfigElementImpl object from the given element.
 * //from  w w w  .j a va  2 s  .c  om
 * @param element The element to parse
 * @return The GenericConfigElement representation of the given element
 */
@SuppressWarnings("unchecked")
private GenericConfigElement createConfigElement(Element element) {
    // get the name and value of the given element
    String name = element.getName();

    // create the config element object and populate with value
    // and attributes
    GenericConfigElement configElement = new GenericConfigElement(name);
    if ((element.hasContent()) && (element.hasMixedContent() == false)) {
        String value = element.getTextTrim();
        if (value != null && value.length() > 0) {
            if (propertyConfigurer != null) {
                value = propertyConfigurer.resolveValue(value);
            }
            configElement.setValue(value);
        }
    }

    Iterator<Attribute> attrs = element.attributeIterator();
    while (attrs.hasNext()) {
        Attribute attr = attrs.next();
        String attrName = attr.getName();
        String attrValue = attr.getValue();

        if (propertyConfigurer != null) {
            attrValue = propertyConfigurer.resolveValue(attrValue);
        }

        configElement.addAttribute(attrName, attrValue);
    }

    return configElement;
}

From source file:de.innovationgate.wgpublisher.webtml.utils.TagOutputFormatter.java

License:Open Source License

public String format(Object obj) throws FormattingException {

    try {//from ww  w .ja  va2s .c  o m
        if (obj == null) {
            return "";
        } else if (obj instanceof java.util.Date) {
            return this.getDateFormatter().format((java.util.Date) obj);
        } else if (obj instanceof Number) {
            return this.getNumberFormatter().format((Number) obj);
        } else if (obj instanceof Element) {
            Element element = (Element) obj;
            if (element.hasMixedContent()) {
                Iterator nodes = element.content().iterator();
                Node node;
                StringBuffer output = new StringBuffer();
                while (nodes.hasNext()) {
                    node = (Node) nodes.next();
                    output.append(node.asXML());
                }
                return (trim ? output.toString().trim() : output.toString());
            } else {
                return (trim ? element.getStringValue().trim() : element.getStringValue());
            }

        } else if (obj instanceof Node) {
            return ((Node) obj).getStringValue();
        } else {
            return (trim ? obj.toString().trim() : obj.toString());
        }
    } catch (WGException e) {
        throw new FormattingException("Exception formatting tag", e);
    }

}

From source file:org.springframework.extensions.config.xml.elementreader.GenericElementReader.java

License:Apache License

/**
 * Creates a ConfigElementImpl object from the given element.
 * /*from w  ww  . j  a v  a 2  s  .com*/
 * @param element The element to parse
 * @return The GenericConfigElement representation of the given element
 */
@SuppressWarnings("unchecked")
protected GenericConfigElement createConfigElement(Element element) {
    // get the name and value of the given element
    String name = element.getName();

    // create the config element object and populate with value
    // and attributes
    GenericConfigElement configElement = new GenericConfigElement(name);
    if ((element.hasContent()) && (element.hasMixedContent() == false)) {
        String value = element.getTextTrim();
        if (value != null && value.length() > 0) {
            if (propertyConfigurer != null) {
                value = propertyConfigurer.resolveValue(value);
            }
            configElement.setValue(value);
        }
    }

    Iterator<Attribute> attrs = element.attributeIterator();
    while (attrs.hasNext()) {
        Attribute attr = attrs.next();
        String attrName = attr.getName();
        String attrValue = attr.getValue();

        if (propertyConfigurer != null) {
            attrValue = propertyConfigurer.resolveValue(attrValue);
        }

        configElement.addAttribute(attrName, attrValue);
    }

    return configElement;
}