Example usage for org.dom4j Namespace equals

List of usage examples for org.dom4j Namespace equals

Introduction

In this page you can find the example usage for org.dom4j Namespace equals.

Prototype

public boolean equals(Object object) 

Source Link

Document

Checks whether this Namespace equals the given Namespace.

Usage

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

License:Mozilla Public License

protected Line parseStartTag(Vector lines, Line current, XElement elem) {
    StyledElement styledElement = new StyledElement();
    styledElement.addString(OPEN_BRACKET);

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

        if (prefix != null && prefix.length() > 0) {
            styledElement.addString(new ElementPrefix(prefix));
            styledElement.addString(ELEMENT_COLON);
        }/*from   w w  w.j av  a 2 s. c o  m*/
    }

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

    if (_viewer.showNamespaces()) {
        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);
                }
            }
        }
    }

    if (_viewer.showAttributes()) {
        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()) {
        current.addStyledString(SLASH);
    } else if (hasTextOnly(elem) && !_viewer.showValues()) {
        current.addStyledString(SLASH);
    } else if (!requiresEndTag(elem) && _viewer.showInline()) {
        current.addStyledString(SLASH);
    }

    //      else if (_viewer.showInline())
    //      {
    //         if (!_viewer.showValues())
    //         {
    //            if ( hasTextOrCDATAOnly(elem))
    //            {
    //               current.addStyledString( SLASH);
    //            }
    //            else if (hasComment(elem) && !_viewer.showComments() && !hasProcessingInstruction(elem) 
    //                  && !hasEntity(elem))
    //            {
    //               current.addStyledString( SLASH);
    //            }
    //            else if (hasProcessingInstruction(elem) && !_viewer.showPI() && !hasComment(elem) 
    //                  && !hasEntity(elem))
    //            {
    //               current.addStyledString( SLASH);
    //            }
    //            else if (hasComment(elem) && hasProcessingInstruction(elem) && !_viewer.showComments() 
    //                  && !hasProcessingInstruction(elem) && !hasEntity(elem))
    //            {
    //               current.addStyledString( SLASH);
    //            }
    //         }
    //         else
    //         {
    //            if (!hasTextOrCDATA(elem))
    //            {
    //               if (hasComment(elem) && !_viewer.showComments() && !hasProcessingInstruction(elem) 
    //                     && !hasEntity(elem))
    //               {
    //                  current.addStyledString( SLASH);
    //               }
    //               else if (hasProcessingInstruction(elem) && !_viewer.showPI() && !hasComment(elem) 
    //                     && !hasEntity(elem))
    //               {
    //                  current.addStyledString( SLASH);
    //               }
    //               else if (hasComment(elem) && hasProcessingInstruction(elem) && !_viewer.showComments() 
    //                     && !hasProcessingInstruction(elem) && !hasEntity(elem))
    //               {
    //                  current.addStyledString( SLASH);
    //               }
    //            }
    //         }
    //      }

    current.addStyledString(CLOSE_BRACKET);

    return current;
}

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;/* w ww . jav  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;
}

From source file:com.cladonia.xngreditor.actions.ToolsChangeNSPrefixAction.java

License:Open Source License

/**
 * Sets the prefixes on the attributes at a parent element
 * @param parent The parent element//w  ww  . j a va2 s.  c  o  m
 * @param allNamespaces the full vector of namespaces
 * @param newNamespaces the vector of new namespaces
 * @return the new list of attributes for the element
 * @throws Exception
 */
private List setPrefixOnAttributes(XElement parent, Vector allNamespaces, Vector newNamespaces)
        throws Exception {

    int attributeCount = parent.attributeCount();
    List attributeList = parent.attributes();
    //create an array to hold all the attributes
    Attribute[] attArray = new Attribute[attributeCount];

    for (int cnt = 0; cnt < attributeCount; ++cnt) {
        //add each attribute to the array
        attArray[cnt] = (Attribute) attributeList.get(cnt);
    }
    //work on the array
    for (int cnt = 0; cnt < attributeCount; ++cnt) {
        //String prefix = attArray[cnt].getNamespacePrefix();
        Namespace ns = attArray[cnt].getNamespace();

        int match = -1;

        //if(!prefix.equals("")) {
        if (!ns.equals(null)) {
            //see if this prefix matches anyone in the vector
            //match = searchForPrefix(prefix, allNamespaces);
            for (int icnt = 0; icnt < allNamespaces.size(); ++icnt) {
                if (ns == (Namespace) allNamespaces.get(icnt)) {
                    match = icnt;
                }

            }
            if (match > -1) {

                //replace the elements prefix with the corresponding one in the
                //other vector
                Namespace tempNs = (Namespace) newNamespaces.get(match);

                //get any declared namespaces as well

                //prefix = tempNs.getPrefix();
                String name = attArray[cnt].getName();
                String value = attArray[cnt].getValue();

                attArray[cnt] = new XAttribute(new QName(name, tempNs), value);

            }
        }
    }

    //then remove all previous and add all the attributes back into the document
    List newAttributes = Arrays.asList(attArray);
    return (newAttributes);
}

From source file:fr.gouv.culture.vitam.utils.XmlDom.java

License:Open Source License

/**
 * Recursively sets the namespace of the List and all children if the current namespace is match
 *///from  w ww.  j a va 2s. c om
private final static void setNamespaces(List<?> l, Namespace ns) {
    Node n = null;
    for (int i = 0; i < l.size(); i++) {
        n = (Node) l.get(i);

        if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
            Namespace namespace = ((Attribute) n).getNamespace();
            if (!namespace.equals(ns)) {
                ((Attribute) n).setNamespace(ns);
            }
        }
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            Namespace namespace = ((Element) n).getNamespace();
            if (!namespace.equals(ns)) {
                if (ns.equals(Namespace.NO_NAMESPACE)) {
                    ((Element) n).remove(namespace);
                }
                setNamespaces((Element) n, ns);
            }
        }
    }
}

From source file:org.orbeon.oxf.xml.dom4j.NonLazyUserDataElement.java

License:Open Source License

/**
 * If parent != null checks with ancestors and removes any redundant namespace declarations.
 *//*from  www  .  ja  v a  2s . c om*/
public void setParent(final org.dom4j.Element prnt) {
    super.setParent(prnt);
    done: if (prnt != null) {
        final org.dom4j.Namespace myNs = getNamespace();
        if (myNs != org.dom4j.Namespace.NO_NAMESPACE) {
            final String myPfx = myNs.getPrefix();
            final org.dom4j.Namespace prntNs = prnt.getNamespaceForPrefix(myPfx);
            if (myPfx.equals(prntNs)) {
                final String myNm = myNs.getName();
                final org.dom4j.QName newNm = new org.dom4j.QName(myNm);
                setQName(newNm);
            }
        }
        if (content == null)
            break done;
        for (final java.util.Iterator itr = content.iterator(); itr.hasNext();) {
            final org.dom4j.Node chld = (org.dom4j.Node) itr.next();
            if (chld.getNodeType() != org.dom4j.Node.NAMESPACE_NODE)
                continue;

            final org.dom4j.Namespace ns = (org.dom4j.Namespace) chld;
            final String pfx = ns.getPrefix();

            final org.dom4j.Namespace prntNs = prnt.getNamespaceForPrefix(pfx);
            if (ns.equals(prntNs))
                itr.remove();
        }
    }
}

From source file:org.talend.camel.designer.ui.wizards.export.RouteJavaScriptOSGIForESBManager.java

License:Open Source License

private void handleSpringXml(String targetFilePath, ProcessItem processItem, InputStream springInput,
        ExportFileResource osgiResource, boolean convertToBP, boolean convertImports) {

    File targetFile = new File(getTmpFolder() + PATH_SEPARATOR + targetFilePath);
    try {//from  w  w w. j  av  a2  s .  co m
        SAXReader saxReader = new SAXReader();
        saxReader.setStripWhitespaceText(false);
        Document document = saxReader.read(springInput);
        Element root = document.getRootElement();

        if (convertToBP) {
            if ("blueprint".equals(root.getName())) {
                formatSchemaLocation(root, false, false);
                InputStream inputStream = new ByteArrayInputStream(root.asXML().getBytes());
                FilesUtils.copyFile(inputStream, targetFile);
                osgiResource.addResource(FileConstants.BLUEPRINT_FOLDER_NAME, targetFile.toURI().toURL());
                return;
            }

            String bpPrefix = "bp";
            int cnt = 0;
            while (root.getNamespaceForPrefix(bpPrefix) != null) {
                bpPrefix = "bp" + (++cnt);
            }
            root.setQName(QName.get("blueprint", bpPrefix, BLUEPRINT_NSURI));
        }

        Namespace springCamelNsp = Namespace.get("camel", CAMEL_SPRING_NSURI);
        boolean addCamel = springCamelNsp.equals(root.getNamespaceForPrefix("camel"));
        formatSchemaLocation(root, convertToBP, addCamel);

        for (Iterator<?> i = root.elementIterator("import"); i.hasNext();) {
            Element ip = (Element) i.next();
            Attribute resource = ip.attribute("resource");
            URL path = dummyURL(resource.getValue());
            for (ResourceDependencyModel resourceModel : RouteResourceUtil
                    .getResourceDependencies(processItem)) {
                if (matches(path, resourceModel)) {
                    IFile resourceFile = RouteResourceUtil.getSourceFile(resourceModel.getItem());
                    String cpUrl = adaptedClassPathUrl(resourceModel, convertImports);
                    handleSpringXml(cpUrl, processItem, resourceFile.getContents(), osgiResource,
                            convertImports, convertImports);
                    resource.setValue(IMPORT_RESOURCE_PREFIX + cpUrl);
                }
            }
            if (convertImports) {
                i.remove();
            }
        }

        if (CONVERT_CAMEL_CONTEXT) {
            if (CONVERT_CAMEL_CONTEXT_ALL) {
                moveNamespace(root, CAMEL_SPRING_NSURI, CAMEL_BLUEPRINT_NSURI);
            } else {
                Namespace blueprintCamelNsp = Namespace.get("camel", CAMEL_BLUEPRINT_NSURI);
                moveNamespace(root, springCamelNsp, blueprintCamelNsp);
                if (springCamelNsp.equals(root.getNamespaceForPrefix("camel"))) {
                    root.remove(springCamelNsp);
                    root.add(blueprintCamelNsp);
                }
                Namespace springCamelDefNsp = Namespace.get(CAMEL_SPRING_NSURI);
                Namespace blueprintCamelDefNsp = Namespace.get(CAMEL_BLUEPRINT_NSURI);
                for (Iterator<?> i = root.elementIterator("camelContext"); i.hasNext();) {
                    Element cc = (Element) i.next();
                    if (springCamelDefNsp.equals(cc.getNamespace())) {
                        moveNamespace(cc, springCamelDefNsp, blueprintCamelDefNsp);
                    }
                }
            }
        }

        InputStream inputStream = new ByteArrayInputStream(root.asXML().getBytes());
        FilesUtils.copyFile(inputStream, targetFile);
        osgiResource.addResource(adaptedResourceFolderName(targetFilePath, convertToBP),
                targetFile.toURI().toURL());
    } catch (Exception e) {
        Logger.getAnonymousLogger().log(Level.WARNING, "Custom Spring to OSGi conversion failed. ", e);
    } finally {
        try {
            springInput.close();
        } catch (IOException e) {
            Logger.getAnonymousLogger().log(Level.WARNING, "Unexpected File closing failure. ", e);
        }
    }
}