Example usage for org.dom4j Element getNamespace

List of usage examples for org.dom4j Element getNamespace

Introduction

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

Prototype

Namespace getNamespace();

Source Link

Document

Returns the Namespace of this element if one exists otherwise Namespace.NO_NAMESPACE is returned.

Usage

From source file:architecture.common.xml.XmlWriter.java

License:Apache License

protected void writeElement(Element element) throws IOException {
    int size = element.nodeCount();
    String qualifiedName = element.getQualifiedName();

    writePrintln();//from  ww  w  .j  ava 2s .  c  om
    indent();

    writer.write("<");
    writer.write(qualifiedName);

    int previouslyDeclaredNamespaces = namespaceStack.size();
    Namespace ns = element.getNamespace();
    if (isNamespaceDeclaration(ns)) {
        namespaceStack.push(ns);
        writeNamespace(ns);
    }

    // Print out additional namespace declarations
    boolean textOnly = true;
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);
        if (node instanceof Namespace) {
            Namespace additional = (Namespace) node;
            if (isNamespaceDeclaration(additional)) {
                namespaceStack.push(additional);
                writeNamespace(additional);
            }
        } else if (node instanceof Element) {
            textOnly = false;
        } else if (node instanceof Comment) {
            textOnly = false;
        }
    }

    writeAttributes(element);

    lastOutputNodeType = Node.ELEMENT_NODE;

    if (size <= 0) {
        writeEmptyElementClose(qualifiedName);
    } else {
        writer.write(">");
        if (textOnly) {
            // we have at least one text node so lets assume
            // that its non-empty
            writeElementContent(element);
        } else {
            // we know it's not null or empty from above
            ++indentLevel;

            writeElementContent(element);

            --indentLevel;

            writePrintln();
            indent();
        }
        writer.write("</");
        writer.write(qualifiedName);
        writer.write(">");
    }

    // remove declared namespaceStack from stack
    while (namespaceStack.size() > previouslyDeclaredNamespaces) {
        namespaceStack.pop();
    }

    lastOutputNodeType = Node.ELEMENT_NODE;
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * Return the child element with the given name. The element must be in the
 * same name space as the parent element.
 * //from   w w w  . j  a v a 2 s.c o  m
 * @param element
 *            The parent element
 * @param name
 *            The child element name
 * @return The child element
 */
public static Element child(Element element, String name) {
    return element.element(new QName(name, element.getNamespace()));
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * ?/* www.j  av a  2 s . c o m*/
 * 
 * 
 * 
 * @param element
 *            
 * @param name
 *            ???
 * 
 * 
 * 
 * @param optional
 *            ??
 * @return ?
 * 
 * 
 * 
 * @throws XMLDocException
 * @throws BaseException
 */
public static Element child(Element element, String name, boolean optional) throws BaseException {
    Element child = element.element(new QName(name, element.getNamespace()));
    if (child == null && !optional) {
        throw new BaseException("UTIL-0001", name + " element expected as child of " + element.getName() + ".");
    }
    return child;
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * Return the child elements with the given name. The elements must be in
 * the same name space as the parent element.
 * //w ww.jav  a 2 s. c o  m
 * @param element
 *            The parent element
 * @param name
 *            The child element name
 * @return The child elements
 */
public static List<Element> children(Element element, String name) {
    return element.elements(new QName(name, element.getNamespace()));
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * /*from w  w w. j  a  v a  2 s . c  o  m*/
 * 
 * 
 * 
 * @param element
 *            
 * @param name
 *            
 * 
 * 
 * 
 * @param optional
 *            
 * @return 
 * @throws XMLDocException
 * @throws BaseException
 */
public static Element getFirstChild(Element element, String name, boolean optional) throws BaseException {
    List list = element.elements(new QName(name, element.getNamespace()));
    // 0

    if (list.size() > 0) {
        return (Element) list.get(0);
    } else {
        if (!optional) {
            throw new BaseException("UTIL-0001",
                    name + " element expected as first child of " + element.getName() + ".");
        } else {
            return null;
        }
    }
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * ElementString/*from w w  w .  j  ava 2  s.  c  o  m*/
 *
 * @param parent
 *            
 * @param name
 *            
 *
 *
 *
 * @param value
 *            
 * @return element
 * @throws XMLDocException
 */
public static Element appendChild(Element parent, String name, String value) {
    Element element = parent.addElement(new QName(name, parent.getNamespace()));
    if (value != null) {
        element.addText(value);
    }
    return element;
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * Element//w w w .  j  a va  2  s . c  o m
 *
 * @param parent
 *            
 * @param name
 *            
 *
 *
 *
 * @return Element 
 * @throws XMLDocException
 */
public static Element appendChild(Element parent, String name) {
    return parent.addElement(new QName(name, parent.getNamespace()));
}

From source file:com.buddycloud.channeldirectory.search.ChannelDirectoryComponent.java

License:Apache License

@Override
protected IQ handleIQGet(IQ iq) throws Exception {

    LOGGER.debug("R: " + iq.toXML());

    Element queryElement = iq.getElement().element("query");
    if (queryElement == null) {
        return XMPPUtils.error(iq, "IQ does not contain query element.", LOGGER);
    }//from w ww.j a v  a  2 s .  c om

    Namespace namespace = queryElement.getNamespace();

    QueryHandler queryHandler = queryHandlers.get(namespace.getURI());
    if (queryHandler == null) {
        return XMPPUtils.error(iq, "QueryHandler not found for namespace: " + namespace, LOGGER);
    }

    return queryHandler.handle(iq);
}

From source file:com.christophermrossi.jpt.PageTemplateImpl.java

License:Open Source License

private void processElement(Element element, ContentHandler contentHandler, LexicalHandler lexicalHandler,
        Interpreter beanShell, Stack slotStack) throws SAXException, PageTemplateException, IOException {
    // Get attributes
    Expressions expressions = new Expressions();
    AttributesImpl attributes = getAttributes(element, expressions);

    // Process instructions

    // evaluate expression
    if (expressions.evaluate != null) {
        Expression.evaluate(expressions.evaluate, beanShell);
    }//w w  w .jav a  2  s.  com

    // use macro
    if (expressions.useMacro != null) {
        processMacro(expressions.useMacro, element, contentHandler, lexicalHandler, beanShell, slotStack);
        return;
    }

    // fill slot
    if (expressions.defineSlot != null) {
        //System.err.println( "fill slot: " + expressions.defineSlot );
        if (!slotStack.isEmpty()) {
            Map slots = (Map) slotStack.pop();
            Slot slot = (Slot) slots.get(expressions.defineSlot);
            //System.err.println( "slot: " + slot );
            if (slot != null) {
                slot.process(contentHandler, lexicalHandler, beanShell, slotStack);
                slotStack.push(slots);
                return;
            }
            // else { use content in macro }
            slotStack.push(slots);
        } else {
            throw new PageTemplateException("slot definition not allowed outside of macro");
        }
    }

    // define
    if (expressions.define != null) {
        processDefine(expressions.define, beanShell);
    }

    // condition
    if (expressions.condition != null && !Expression.evaluateBoolean(expressions.condition, beanShell)) {
        // Skip this element (and children)
        return;
    }

    // repeat
    Loop loop = new Loop(expressions.repeat, beanShell);
    while (loop.repeat(beanShell)) {
        // content or replace
        Object jptContent = null;
        if (expressions.content != null) {
            jptContent = processContent(expressions.content, beanShell);
        }

        // attributes
        if (expressions.attributes != null) {
            processAttributes(attributes, expressions.attributes, beanShell);
        }

        // omit-tag
        boolean jptOmitTag = false;
        if (expressions.omitTag != null) {
            if (expressions.omitTag.equals("")) {
                jptOmitTag = true;
            } else {
                jptOmitTag = Expression.evaluateBoolean(expressions.omitTag, beanShell);
            }
        }

        // Declare element
        Namespace namespace = element.getNamespace();
        if (!jptOmitTag) {
            contentHandler.startElement(namespace.getURI(), element.getName(), element.getQualifiedName(),
                    attributes);
        }

        // Process content
        if (jptContent != null && jptContent != DEFAULT) {
            // Content for this element has been generated dynamically
            if (jptContent instanceof HTMLFragment) {
                HTMLFragment html = (HTMLFragment) jptContent;
                html.toXhtml(contentHandler, lexicalHandler);
            }

            // plain text
            else {
                char[] text = ((String) jptContent).toCharArray();
                contentHandler.characters(text, 0, text.length);
            }
        } else {
            defaultContent(element, contentHandler, lexicalHandler, beanShell, slotStack);
        }

        // End element
        if (!jptOmitTag) {
            contentHandler.endElement(namespace.getURI(), element.getName(), element.getQualifiedName());
        }
    }
}

From source file:com.cladonia.xml.xdiff.XmlElementNode.java

License:Open Source License

protected Line parseStartTag(Vector lines, Line current, Element elem) {

    boolean localInsertElement = false;
    boolean localDeleteElement = false;

    StyledElement styledElement = new StyledElement();

    if (insertElement || insideInsertChain(parent)) {
        localInsertElement = true;//from  w  w w  . j  a v a 2 s.c om
        styledElement.addString(INSERT_OPEN_BRACKET);
        currentColor = COLOR_GREEN;
    } else if (deleteElement || insideDeleteChain(parent)) {
        localDeleteElement = true;
        styledElement.addString(DELETE_OPEN_BRACKET);
        currentColor = Color.RED;
    } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) {
        styledElement.addString(MERGED_OPEN_BRACKET);
        currentColor = COLOR_MERGED;
    } else {
        styledElement.addString(OPEN_BRACKET);
        currentColor = Color.BLACK;
    }

    styledElement.addString(new ElementName(elem.getQualifiedName()));
    current.addStyledElement(styledElement);

    Namespace ns = elem.getNamespace();

    if (ns != null) {
        XElement parent = (XElement) elem.getParent();

        if (parent != null) {
            Namespace prev = parent.getNamespaceForPrefix(ns.getPrefix());

            if (prev == null || !ns.equals(prev)) {
                StyledElement sns = formatNamespace(ns);

                if (sns != null) {
                    if (current.length() + sns.length() + 1 > MAX_LINE_LENGTH) {
                        current = new Line();
                        lines.add(current);
                        current.addStyledString(TAB);
                    } else {
                        current.addStyledString(SPACE);
                    }

                    current.addStyledElement(sns);
                }
            }
        } else {
            StyledElement sns = formatNamespace(ns);

            if (sns != null) {
                if (current.length() + sns.length() + 1 > MAX_LINE_LENGTH) {
                    current = new Line();
                    lines.add(current);
                    current.addStyledString(TAB);
                } else {
                    current.addStyledString(SPACE);
                }

                current.addStyledElement(sns);
            }
        }
    }

    List namespaces = elem.additionalNamespaces();

    if (namespaces != null && namespaces.size() > 0) {
        Iterator iterator = namespaces.iterator();

        for (int i = 0; i < namespaces.size(); i++) {
            StyledElement sns = formatNamespace((Namespace) iterator.next());

            if (sns != null) {
                if (current.length() + sns.length() + 1 > MAX_LINE_LENGTH) {
                    current = new Line();
                    lines.add(current);
                    current.addStyledString(TAB);
                } else {
                    current.addStyledString(SPACE);
                }

                current.addStyledElement(sns);
            }
        }
    }

    List attributes = elem.attributes();

    if (attributes != null && attributes.size() > 0) {
        Iterator iterator = attributes.iterator();

        for (int i = 0; i < attributes.size(); i++) {
            StyledElement sa = formatAttribute((Attribute) iterator.next());

            if (current.length() + sa.length() + 1 > MAX_LINE_LENGTH) {
                current = new Line();
                lines.add(current);
                current.addStyledString(TAB);
            } else {
                current.addStyledString(SPACE);
            }

            current.addStyledElement(sa);
        }
    }

    if (!elem.hasContent() || hasPIorWhiteSpaceOnly((XElement) elem)) {
        if (updateElementFrom != null) {
            // content was blanked, don't add closing slash
        } else if (localInsertElement) {
            current.addStyledString(INSERT_SLASH);
        } else if (localDeleteElement) {
            current.addStyledString(DELETE_SLASH);
        } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) {
            current.addStyledString(MERGED_SLASH);
        } else {
            current.addStyledString(SLASH);
        }
    }

    if (localInsertElement) {
        current.addStyledString(INSERT_CLOSE_BRACKET);
    } else if (localDeleteElement) {
        current.addStyledString(DELETE_CLOSE_BRACKET);
    } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) {
        current.addStyledString(MERGED_CLOSE_BRACKET);
    } else {
        current.addStyledString(CLOSE_BRACKET);
    }

    currentColor = Color.BLACK;
    return current;
}