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:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

protected void writeElement(Element element) throws IOException {
    if (DEBUG)/*from w  w  w.j av  a 2  s  .co  m*/
        System.out.println("XMLFormatter.writeElement( " + element + ")");
    //      if ( indentMixed) {
    //         super.writeElement( element);
    //      } else {
    int size = element.nodeCount();
    String qualifiedName = element.getQualifiedName();

    boolean hasElement = false;
    boolean hasText = false;

    // first test to see if this element has mixed content, 
    // if whitespace is significant ...
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);

        if (node instanceof Element) {
            hasElement = true;
        } else if (node instanceof Text) {
            String text = node.getText();

            if (text != null && text.trim().length() > 0) {
                hasText = true;
            }
        }
    }

    Attribute space = element.attribute("space");
    boolean preserveSpace = false;

    if (space != null) {
        String prefix = space.getNamespacePrefix();
        String value = space.getValue();
        //         System.out.println( "prefix = "+prefix+" value = "+value);
        if (prefix != null && "xml".equals(prefix) && "preserve".equals(value)) {
            preserveSpace = true;
        }
    }

    writePrintln();
    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
    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);
            }
        }
    }

    writeAttributes(element);

    lastOutputNodeType = Node.ELEMENT_NODE;

    if (size <= 0) {
        writeEmptyElementClose(qualifiedName);
    } else {
        writer.write(">");

        if (!hasElement && !preserveSpace) { // text only
            // we have at least one text node so lets assume
            // that its non-empty
            //            System.out.println( "writeElementContent (Text) ...");
            boolean previousWrapText = wrapText;
            wrapText = true;
            writeElementContent(element);
            wrapText = previousWrapText;
        } else if (preserveMixedContent && hasElement && hasText) { // preserve space
            // Mixed content
            //                System.out.println( "writeMixedElementContent ...");
            Node lastNode = writeMixedElementContent(element);

        } else if (preserveSpace) { // preserve space
            // Mixed content
            //                System.out.println( "writePreserveElementContent ...");
            Node lastNode = writePreservedElementContent(element);

        } else { // hasElement && !hasText
            //               System.out.println( "writeElementContent (Element) ...");

            boolean previousWrapText = wrapText;
            wrapText = true;
            ++indentLevel;
            writeElementContent(element);
            --indentLevel;
            wrapText = previousWrapText;

            writePrintln();
            indent();
        }

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

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

    lastOutputNodeType = Node.ELEMENT_NODE;
    //      }
}

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

protected void writeMixedElement(Element element) throws IOException {
    if (DEBUG)//from ww w. jav a  2  s  .c o  m
        System.out.println("XMLFormatter.writeMixedElement( " + element + ")");

    int size = element.nodeCount();
    String qualifiedName = element.getQualifiedName();

    boolean previousTrim = format.isTrimText();
    boolean previousWrapText = wrapText;

    if (isPreserveMixedContent()) {
        format.setTrimText(false);
        wrapText = false;
    } else {
        wrapText = true;
    }

    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;
        }
    }

    writeAttributes(element);

    lastOutputNodeType = Node.ELEMENT_NODE;

    if (size <= 0) {
        writeEmptyElementClose(qualifiedName);
        //           writePrintln();
    } else {
        writer.write(">");
        writeMixedElementContent(element);
        writer.write("</");
        writer.write(qualifiedName);
        writer.write(">");
    }

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

    lastOutputNodeType = Node.ELEMENT_NODE;

    format.setTrimText(previousTrim);
    wrapText = previousWrapText;
}

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

protected void writePreservedElement(Element element) throws IOException {
    if (DEBUG)/*w w  w.ja  v a2  s .  c o m*/
        System.out.println("XMLFormatter.writePreservedElement( " + element + ")");
    int size = element.nodeCount();
    String qualifiedName = element.getQualifiedName();

    boolean previousTrim = format.isTrimText();
    boolean previousWrapText = wrapText;

    format.setTrimText(false);
    wrapText = false;

    boolean hasElement = false;
    boolean hasText = false;

    // first test to see if this element has mixed content, 
    // if whitespace is significant ...
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);

        if (node instanceof Element) {
            hasElement = true;
        } else if (node instanceof Text) {
            String text = node.getText();

            if (text != null && text.trim().length() > 0) {
                hasText = true;
            }
        }
    }

    Attribute space = element.attribute("space");
    boolean defaultSpace = false;

    if (space != null) {
        String prefix = space.getNamespacePrefix();
        String value = space.getValue();
        //            System.out.println( "prefix = "+prefix+" value = "+value);
        if (prefix != null && "xml".equals(prefix) && "default".equals(value)) {
            defaultSpace = true;
        }
    }

    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
    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);
            }
        }
    }

    writeAttributes(element);

    lastOutputNodeType = Node.ELEMENT_NODE;

    if (size <= 0) {
        writeEmptyElementClose(qualifiedName);
    } else {
        writer.write(">");

        if (preserveMixedContent && hasElement && hasText) { // mixed content
            //                System.out.println( "writeMixedElementContent ...");

            Node lastNode = writeMixedElementContent(element);
        } else if (!defaultSpace) { // preserve space
            //                System.out.println( "writePreservedElementContent ...");

            Node lastNode = writePreservedElementContent(element);
        } else {
            //                System.out.println( "writeElementContent ...");

            format.setTrimText(isTrimText());
            boolean prevWrapText = wrapText;
            wrapText = true;
            writeElementContent(element);
            wrapText = prevWrapText;
            format.setTrimText(false);
        }

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

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

    lastOutputNodeType = Node.ELEMENT_NODE;

    format.setTrimText(previousTrim);
    wrapText = previousWrapText;
}

From source file:com.cladonia.xngreditor.SchemaLocationDialog.java

License:Open Source License

/**
 * Sets the original values displayed by the dialog, plus what should be enabled\disabled
 *//*  w  w  w.j a  v a2 s  .c o m*/
private void setCurrentValues() {
    XDocument xdoc = document.getDocument();
    Element ele = xdoc.getRootElement();

    Attribute attr = ele.attribute(SCHEMALOCATION);

    if (attr == null) {
        // check for no namespace schema
        Attribute attr2 = ele.attribute(NOSCHEMALOCATION);
        if (attr2 == null) {
            // set the defaults
            schemaButton.setSelected(true);
            Namespace ns = ele.getNamespace();
            if (ns != Namespace.NO_NAMESPACE) {
                namespaceField.setText(ns.getURI());
            } else {
                namespaceField.setText("");
            }

            urlField.setText("");
        } else {
            // we have a "no namespace schema"
            schemaNoNSButton.setSelected(true);
            namespaceField.setText("");
            namespaceField.setEnabled(false);
            namespaceLabel.setEnabled(false);
            urlField.setText(attr2.getValue());
        }
    } else {
        // we hava a namepace schema

        schemaButton.setSelected(true);

        // we have namespace schema, set the namespace and the URL
        String attrValue = attr.getValue();

        // break up all the namespace and url pairs
        ArrayList stringValues = new ArrayList();

        StringTokenizer st = new StringTokenizer(attrValue);
        while (st.hasMoreTokens()) {
            stringValues.add(st.nextToken());
        }

        String namespaceValue = (String) stringValues.get(0);
        if (namespaceValue != null) {
            namespaceField.setText(namespaceValue);
        } else {
            namespaceField.setText("");
        }

        String urlValue = (String) stringValues.get(1);
        if (urlValue != null) {
            urlField.setText(urlValue);
        } else {
            urlField.setText("");
        }
    }
}

From source file:com.flaptor.util.parser.HtmlParser.java

License:Apache License

private void removeNamespace(Element elem) {
    if (null != elem) {
        elem.remove(elem.getNamespace());
        elem.setQName(QName.get(elem.getName(), Namespace.NO_NAMESPACE));
        removeNamespace(elem.content());
    }//from   w ww .  java  2s  . c o  m
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.util.IQUtils.java

License:Apache License

/**
 * Create an IQ MMX result from the request.  The element, namespace and
 * command are cloned.  The payload content type is set to JSON.
 * @param iq The request IQ.//  w w  w  . j a  va 2s  .c o m
 * @param payload The JSON payload.
 * @return An IQ MMX result.
 */
public static IQ createResultIQ(IQ iq, String payload) {
    IQ result = IQ.createResultIQ(iq);
    Element rqtElt = iq.getChildElement();
    Element rstElt = result.setChildElement(rqtElt.getName(), rqtElt.getNamespace().getText());
    rstElt.addAttribute(Constants.MMX_ATTR_COMMAND, rqtElt.attributeValue(Constants.MMX_ATTR_COMMAND));
    rstElt.addAttribute(Constants.MMX_ATTR_CTYPE, GsonData.CONTENT_TYPE_JSON);
    rstElt.setText(payload);
    return result;
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.util.IQUtils.java

License:Apache License

/**
 * Create an error IQ from the request.  The element, namespace and command
 * are cloned.  The content type is set to JSON.
 * @param iq The request IQ.//from  ww w. ja  v  a 2s.  com
 * @param msg  Optional message.
 * @param code
 * @return An IQ MMX error.
 */
public static IQ createErrorIQ(IQ iq, String msg, int code) {
    IQ error = IQ.createResultIQ(iq);
    error.setType(IQ.Type.error);
    Element rqtElt = iq.getChildElement();
    Element errElt = error.setChildElement(rqtElt.getName(), rqtElt.getNamespace().getText());
    errElt.addAttribute(Constants.MMX_ATTR_COMMAND, rqtElt.attributeValue(Constants.MMX_ATTR_COMMAND));
    errElt.addAttribute(Constants.MMX_ATTR_CTYPE, GsonData.CONTENT_TYPE_JSON);
    MMXStatus status = new MMXStatus();
    status.setCode(code);
    status.setMessage(msg);
    errElt.setText(status.toJson());
    return error;
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.util.IQUtils.java

License:Apache License

/**
 * Create an error IQ from the request using the XMPP error stanza.  The 
 * element, namespace and command are cloned.  The content type is XML.
 * @param iq//from w  ww .ja  v  a2 s  .c  o m
 * @param condition
 * @return An IQ MMX error with XMPP error stanza.
 */
public static IQ createErrorIQ(IQ iq, Condition condition) {
    IQ error = IQ.createResultIQ(iq);
    error.setType(IQ.Type.error);
    Element rqtElt = iq.getChildElement();
    Element errElt = error.setChildElement(rqtElt.getName(), rqtElt.getNamespace().getText());
    errElt.addAttribute(Constants.MMX_ATTR_COMMAND, rqtElt.attributeValue(Constants.MMX_ATTR_COMMAND));
    errElt.addAttribute(Constants.MMX_ATTR_CTYPE, "application/xml");
    error.setError(condition);
    return error;
}

From source file:com.rayo.core.xml.providers.HandsetProvider.java

License:Apache License

@Override
protected Object processElement(Element element) throws Exception {
    if (ONHOOK_QNAME.equals(element.getQName())) {
        return buildOnHookCommand(element);

    } else if (OFFHOOK_QNAME.equals(element.getQName())) {
        return buildOffHookCommand(element);

    } else if (PRIVATE_QNAME.equals(element.getQName())) {
        return buildPrivateCommand(element);

    } else if (PUBLIC_QNAME.equals(element.getQName())) {
        return buildPublicCommand(element);

    } else if (MUTE_QNAME.equals(element.getQName())) {
        return buildMuteCommand(element);

    } else if (UNMUTE_QNAME.equals(element.getQName())) {
        return buildUnmuteCommand(element);

    } else if (HOLD_QNAME.equals(element.getQName())) {
        return buildHoldCommand(element);

    } else if (TALK_QNAME.equals(element.getQName())) {
        return buildTalkCommand(element);

    } else if (UNTALK_QNAME.equals(element.getQName())) {
        return buildUntalkCommand(element);

    } else if (ONSPEAKER_QNAME.equals(element.getQName())) {
        return buildOnSpeakerCommand(element);

    } else if (OFFSPEAKER_QNAME.equals(element.getQName())) {
        return buildOffSpeakerCommand(element);

    } else if (CREATE_SPEAKER_QNAME.equals(element.getQName())) {
        return buildCreateSpeakerCommand(element);

    } else if (DESTROY_SPEAKER_QNAME.equals(element.getQName())) {
        return buildDestroySpeakerCommand(element);

    } else if (element.getNamespace().equals(RAYO_COMPONENT_NAMESPACE)) {
        return buildCompleteCommand(element);
    }/* ww  w  . jav a  2 s .c o  m*/
    return null;
}

From source file:com.shopximity.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

    // use macro//  ww w .  j  av  a 2s. c  om
    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) {
            // 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());
        }
    }
}