Example usage for org.dom4j Attribute getNamespacePrefix

List of usage examples for org.dom4j Attribute getNamespacePrefix

Introduction

In this page you can find the example usage for org.dom4j Attribute getNamespacePrefix.

Prototype

String getNamespacePrefix();

Source Link

Document

Returns the namespace prefix of this element if one exists otherwise an empty String is returned.

Usage

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

License:Apache License

/**
 * Determines if element is a special case of XML elements where it contains
 * an xml:space attribute of "preserve". If it does, then retain whitespace.
 *//*w  w  w  . j a  va 2s . c o m*/
protected final boolean isElementSpacePreserved(Element element) {
    final Attribute attr = (Attribute) element.attribute("space");
    boolean preserveFound = preserve; // default to global state
    if (attr != null) {
        if ("xml".equals(attr.getNamespacePrefix()) && "preserve".equals(attr.getText())) {
            preserveFound = true;
        } else {
            preserveFound = false;
        }
    }
    return preserveFound;
}

From source file:architecture.ee.util.xml.XmlWriter.java

License:Apache License

/**
 * Determines if element is a special case of XML elements
 * where it contains an xml:space attribute of "preserve".
 * If it does, then retain whitespace./*from  w w w.j a  v  a  2s  . c o m*/
 */
protected final boolean isElementSpacePreserved(Element element) {
    final Attribute attr = element.attribute("space");
    boolean preserveFound = preserve; //default to global state
    if (attr != null) {
        if ("xml".equals(attr.getNamespacePrefix()) && "preserve".equals(attr.getText())) {
            preserveFound = true;
        } else {
            preserveFound = false;
        }
    }
    return preserveFound;
}

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

License:Mozilla Public License

private StyledElement formatAttribute(Attribute a) {
    StyledElement styledAttribute = new StyledElement();

    String name = a.getName();/*from   ww  w . ja  v a 2  s. c  om*/
    String value = a.getValue();

    if (_viewer.showNamespaces()) {
        String prefix = a.getNamespacePrefix();

        if (prefix != null && prefix.length() > 0) {
            styledAttribute.addString(new AttributePrefix(prefix));
            styledAttribute.addString(ATTRIBUTE_COLON);
        }
    }

    styledAttribute.addString(new AttributeName(name));
    styledAttribute.addString(ATTRIBUTE_ASIGN);

    styledAttribute.addString(new AttributeValue(value));

    return styledAttribute;
}

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

License:Open Source License

private StyledElement formatAttribute(Attribute a) {
    StyledElement styledAttribute = new StyledElement();

    String name = a.getName();//from  w  w w. j  a  v  a 2s . c  o  m
    String qualifiedName = a.getQualifiedName();
    String value = a.getValue();

    boolean localInsertAttribute = false;
    boolean localDeleteAttribute = false;

    if (findAttribute(qualifiedName, insertAttributes) || insideInsertChain(this)) {
        localInsertAttribute = true;
        currentColor = COLOR_GREEN;
    } else if (findAttribute(qualifiedName, deleteAttributes) || insideDeleteChain(this)) {
        localDeleteAttribute = true;
        currentColor = Color.RED;
    } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) {
        currentColor = COLOR_MERGED;
    } else {
        currentColor = Color.BLACK;
    }

    String prefix = a.getNamespacePrefix();

    if (prefix != null && prefix.length() > 0) {
        styledAttribute.addString(new AttributePrefix(prefix));

        if (localInsertAttribute) {
            styledAttribute.addString(INSERT_ATTRIBUTE_COLON);
        } else if (localDeleteAttribute) {
            styledAttribute.addString(DELETE_ATTRIBUTE_COLON);
        } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) {
            styledAttribute.addString(MERGED_ATTRIBUTE_COLON);
        } else {
            styledAttribute.addString(ATTRIBUTE_COLON);
        }
    }

    styledAttribute.addString(new AttributeName(name));

    if (localInsertAttribute) {
        styledAttribute.addString(INSERT_ATTRIBUTE_ASIGN);
        styledAttribute.addString(INSERT_ATTRIBUTE_QUOTE);
    } else if (localDeleteAttribute) {
        styledAttribute.addString(DELETE_ATTRIBUTE_ASIGN);
        styledAttribute.addString(DELETE_ATTRIBUTE_QUOTE);
    } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) {
        styledAttribute.addString(MERGED_ATTRIBUTE_ASIGN);
        styledAttribute.addString(MERGED_ATTRIBUTE_QUOTE);
    } else {
        styledAttribute.addString(ATTRIBUTE_ASIGN);
        styledAttribute.addString(ATTRIBUTE_QUOTE);
    }

    String attrPreviousText = findUpdatedAttribute(qualifiedName, updateAttributes);
    if (attrPreviousText != null) {
        StyledString updatedValue = new AttributePreviousValue(attrPreviousText);
        updatedValue.setStrikeThrough(true);
        styledAttribute.addString(updatedValue);

        currentColor = Color.BLUE;
    }

    styledAttribute.addString(new AttributeValue(value));

    if (localInsertAttribute) {
        styledAttribute.addString(INSERT_ATTRIBUTE_QUOTE);
    } else if (localDeleteAttribute) {
        styledAttribute.addString(DELETE_ATTRIBUTE_QUOTE);
    } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) {
        styledAttribute.addString(MERGED_ATTRIBUTE_QUOTE);
    } else {
        styledAttribute.addString(ATTRIBUTE_QUOTE);
    }

    currentColor = Color.BLACK;
    return styledAttribute;
}

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

License:Mozilla Public License

protected void writeElement(Element element) throws IOException {
    if (DEBUG)/*from ww w. j a  v  a  2s .  c o 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 writePreservedElement(Element element) throws IOException {
    if (DEBUG)/*from   ww  w .ja va2  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.smartwork.im.utils.XMLWriter.java

License:Open Source License

/**
 * Determines if element is a special case of XML elements
 * where it contains an xml:space attribute of "preserve".
 * If it does, then retain whitespace./* ww  w  .  jav  a  2 s.c  o  m*/
 */
protected final boolean isElementSpacePreserved(Element element) {
    final Attribute attr = (Attribute) element.attribute("space");
    boolean preserveFound = preserve; //default to global state
    if (attr != null) {
        if ("xml".equals(attr.getNamespacePrefix()) && "preserve".equals(attr.getText())) {
            preserveFound = true;
        } else {
            preserveFound = false;
        }
    }
    return preserveFound;
}

From source file:com.taobao.android.builder.tasks.app.manifest.StandardizeLibManifestTask.java

License:Apache License

/**
 * ?mainifest//from ww w .j a  va 2 s .  c  om
 *
 * @param manifestFile
 * @return
 */
public static ManifestInfo getManifestFileObject(File manifestFile) throws DocumentException {

    ManifestInfo manifestFileObject = new ManifestInfo();
    manifestFileObject.setManifestFile(manifestFile);
    if (manifestFile.exists()) {
        Document document = XmlHelper.readXml(manifestFile);// ?XML
        Element root = document.getRootElement();// 
        for (Attribute attribute : root.attributes()) {
            if (StringUtils.isNotBlank(attribute.getNamespacePrefix())) {
                manifestFileObject.addManifestProperty(
                        attribute.getNamespacePrefix() + ":" + attribute.getName(), attribute.getValue());
            } else {
                manifestFileObject.addManifestProperty(attribute.getName(), attribute.getValue());
            }
        }
        Element useSdkElement = root.element("uses-sdk");
        Element applicationElement = root.element("application");
        if (null != useSdkElement) {
            for (Attribute attribute : useSdkElement.attributes()) {
                if (StringUtils.isNotBlank(attribute.getNamespacePrefix())) {
                    manifestFileObject.addUseSdkProperty(
                            attribute.getNamespacePrefix() + ":" + attribute.getName(), attribute.getValue());
                } else {
                    manifestFileObject.addUseSdkProperty(attribute.getName(), attribute.getValue());
                }
            }
        }
        if (null != applicationElement) {
            for (Attribute attribute : applicationElement.attributes()) {
                if (StringUtils.isNotBlank(attribute.getNamespacePrefix())) {
                    manifestFileObject.addApplicationProperty(
                            attribute.getNamespacePrefix() + ":" + attribute.getName(), attribute.getValue());
                } else {
                    manifestFileObject.addApplicationProperty(attribute.getName(), attribute.getValue());
                }
            }
        }
    }
    manifestFileObject.init();
    return manifestFileObject;
}

From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java

License:Apache License

/**
 * ?mainifest//  ww w.j a v a2 s  .  c o m
 *
 * @param manifestFile
 * @return
 */
public static ManifestFileObject getManifestFileObject(File manifestFile) throws DocumentException {
    SAXReader reader = new SAXReader();
    ManifestFileObject manifestFileObject = new ManifestFileObject();
    manifestFileObject.setManifestFile(manifestFile);
    if (manifestFile.exists()) {
        Document document = reader.read(manifestFile);// ?XML
        Element root = document.getRootElement();// 
        for (Attribute attribute : root.attributes()) {
            if (StringUtils.isNotBlank(attribute.getNamespacePrefix())) {
                manifestFileObject.addManifestProperty(
                        attribute.getNamespacePrefix() + ":" + attribute.getName(), attribute.getValue());
            } else {
                manifestFileObject.addManifestProperty(attribute.getName(), attribute.getValue());
            }
        }
        Element useSdkElement = root.element("uses-sdk");
        Element applicationElement = root.element("application");
        if (null != useSdkElement) {
            for (Attribute attribute : useSdkElement.attributes()) {
                if (StringUtils.isNotBlank(attribute.getNamespacePrefix())) {
                    manifestFileObject.addUseSdkProperty(
                            attribute.getNamespacePrefix() + ":" + attribute.getName(), attribute.getValue());
                } else {
                    manifestFileObject.addUseSdkProperty(attribute.getName(), attribute.getValue());
                }
            }
        }
        if (null != applicationElement) {
            for (Attribute attribute : applicationElement.attributes()) {
                if (StringUtils.isNotBlank(attribute.getNamespacePrefix())) {
                    manifestFileObject.addApplicationProperty(
                            attribute.getNamespacePrefix() + ":" + attribute.getName(), attribute.getValue());
                } else {
                    manifestFileObject.addApplicationProperty(attribute.getName(), attribute.getValue());
                }
            }
        }
    }
    return manifestFileObject;
}

From source file:net.sf.saxon.dom4j.NodeWrapper.java

License:Mozilla Public License

/**
* Get the value of a given attribute of this node
* @param fingerprint The fingerprint of the attribute name
* @return the attribute value if it exists or null if not
*//*  w w  w  .ja v  a  2  s  . co m*/

public String getAttributeValue(int fingerprint) {
    if (nodeKind == Type.ELEMENT) {
        Iterator list = ((Element) node).attributes().iterator();
        NamePool pool = docWrapper.getNamePool();
        while (list.hasNext()) {
            Attribute att = (Attribute) list.next();
            int nameCode = pool.allocate(att.getNamespacePrefix(), att.getNamespaceURI(), att.getName());
            if (fingerprint == (nameCode & 0xfffff)) {
                return att.getValue();
            }
        }
    }
    return null;
}