Example usage for org.dom4j Namespace getPrefix

List of usage examples for org.dom4j Namespace getPrefix

Introduction

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

Prototype

public String getPrefix() 

Source Link

Document

DOCUMENT ME!

Usage

From source file:com.cladonia.xml.editor.Editor.java

License:Open Source License

private Vector getAttributeValues(String qname) {

    //      System.out.println( "getAttributeValues( "+qname+")");
    String attributeName = stripPrefix(qname);
    Tag child = getCurrentTag();//  w w w . ja v a  2  s.c  om
    Tag tag = getCurrentStartTag();
    if (document.isXSD()) {
        if (attributeName.equals("ref")) {
            if (child.getName().equals("attributeGroup")) {
                XElement root = document.getLastRoot();
                if (root != null) {
                    String targetNS = root.getAttribute("targetNamespace");
                    String prefix = null;
                    if (targetNS != null && targetNS.length() > 0) {
                        Namespace ns = root.getNamespaceForURI(targetNS);
                        prefix = ns.getPrefix();
                    }
                    XElement[] elements = root.getElements("attributeGroup");
                    Vector results = new Vector();
                    for (int i = 0; i < elements.length; i++) {
                        String value = elements[i].getAttribute("name");
                        if (value != null && value.length() > 0) {
                            if (prefix != null && prefix.length() > 0) {
                                results.addElement(
                                        new AttributeValue(prefix + ":" + value, AttributeValue.NORMAL_TYPE));
                            } else {
                                results.addElement(new AttributeValue(value, AttributeValue.NORMAL_TYPE));
                            }
                        }
                    }
                    return results;
                }
            } else if (child.getName().equals("attribute")) {
                XElement root = document.getLastRoot();
                if (root != null) {
                    String targetNS = root.getAttribute("targetNamespace");
                    String prefix = null;
                    if (targetNS != null && targetNS.length() > 0) {
                        Namespace ns = root.getNamespaceForURI(targetNS);
                        prefix = ns.getPrefix();
                    }
                    XElement[] elements = root.getElements("attribute");
                    Vector results = new Vector();
                    for (int i = 0; i < elements.length; i++) {
                        String value = elements[i].getAttribute("name");
                        if (value != null && value.length() > 0) {
                            if (prefix != null && prefix.length() > 0) {
                                results.addElement(
                                        new AttributeValue(prefix + ":" + value, AttributeValue.NORMAL_TYPE));
                            } else {
                                results.addElement(new AttributeValue(value, AttributeValue.NORMAL_TYPE));
                            }
                        }
                    }
                    return results;
                }
            } else if (child.getName().equals("element")) {
                XElement root = document.getLastRoot();
                if (root != null) {
                    String targetNS = root.getAttribute("targetNamespace");
                    String prefix = null;
                    if (targetNS != null && targetNS.length() > 0) {
                        Namespace ns = root.getNamespaceForURI(targetNS);
                        prefix = ns.getPrefix();
                    }
                    XElement[] elements = root.getElements("element");
                    Vector results = new Vector();
                    for (int i = 0; i < elements.length; i++) {
                        String value = elements[i].getAttribute("name");
                        if (value != null && value.length() > 0) {
                            if (prefix != null && prefix.length() > 0) {
                                results.addElement(
                                        new AttributeValue(prefix + ":" + value, AttributeValue.NORMAL_TYPE));
                            } else {
                                results.addElement(new AttributeValue(value, AttributeValue.NORMAL_TYPE));
                            }
                        }
                    }
                    return results;
                }
            } else if (child.getName().equals("group")) {
                XElement root = document.getLastRoot();
                if (root != null) {
                    String targetNS = root.getAttribute("targetNamespace");
                    String prefix = null;
                    if (targetNS != null && targetNS.length() > 0) {
                        Namespace ns = root.getNamespaceForURI(targetNS);
                        prefix = ns.getPrefix();
                    }
                    XElement[] elements = root.getElements("group");
                    Vector results = new Vector();
                    for (int i = 0; i < elements.length; i++) {
                        String value = elements[i].getAttribute("name");
                        if (value != null && value.length() > 0) {
                            if (prefix != null && prefix.length() > 0) {
                                results.addElement(
                                        new AttributeValue(prefix + ":" + value, AttributeValue.NORMAL_TYPE));
                            } else {
                                results.addElement(new AttributeValue(value, AttributeValue.NORMAL_TYPE));
                            }
                        }
                    }
                    return results;
                }
            }
        } else if (attributeName.equals("base")) {
            Tag parent = getParentStartTag(child);
            if (parent != null
                    && (parent.getName().equals("simpleContent") || parent.getName().equals("simpleType"))) {
                Vector results = getSimpleTypeValues();
                XElement root = document.getLastRoot();
                if (root != null) {
                    String targetNS = root.getAttribute("targetNamespace");
                    String prefix = null;
                    if (targetNS != null && targetNS.length() > 0) {
                        Namespace ns = root.getNamespaceForURI(targetNS);
                        prefix = ns.getPrefix();
                    }
                    XElement[] elements = root.getElements("simpleType");
                    for (int i = 0; i < elements.length; i++) {
                        String value = elements[i].getAttribute("name");
                        if (value != null && value.length() > 0) {
                            if (prefix != null && prefix.length() > 0) {
                                results.addElement(
                                        new AttributeValue(prefix + ":" + value, AttributeValue.NORMAL_TYPE));
                            } else {
                                results.addElement(new AttributeValue(value, AttributeValue.NORMAL_TYPE));
                            }
                        }
                    }
                }
                return results;
            } else {
                Vector results = new Vector();
                XElement root = document.getLastRoot();
                if (root != null) {
                    String targetNS = root.getAttribute("targetNamespace");
                    String prefix = null;
                    if (targetNS != null && targetNS.length() > 0) {
                        Namespace ns = root.getNamespaceForURI(targetNS);
                        prefix = ns.getPrefix();
                    }
                    XElement[] elements = root.getElements("complexType");
                    for (int i = 0; i < elements.length; i++) {
                        String value = elements[i].getAttribute("name");
                        if (value != null && value.length() > 0) {
                            if (prefix != null && prefix.length() > 0) {
                                results.addElement(
                                        new AttributeValue(prefix + ":" + value, AttributeValue.NORMAL_TYPE));
                            } else {
                                results.addElement(new AttributeValue(value, AttributeValue.NORMAL_TYPE));
                            }
                        }
                    }
                    return results;
                }
            }
        } else if (attributeName.equals("type")) {
            if (child != null && (child.getName().equals("attribute"))) {
                Vector results = getSimpleTypeValues();
                XElement root = document.getLastRoot();
                if (root != null) {
                    String targetNS = root.getAttribute("targetNamespace");
                    String prefix = null;
                    if (targetNS != null && targetNS.length() > 0) {
                        Namespace ns = root.getNamespaceForURI(targetNS);
                        prefix = ns.getPrefix();
                    }
                    XElement[] elements = root.getElements("simpleType");
                    for (int i = 0; i < elements.length; i++) {
                        String value = elements[i].getAttribute("name");
                        if (value != null && value.length() > 0) {
                            if (prefix != null && prefix.length() > 0) {
                                results.addElement(
                                        new AttributeValue(prefix + ":" + value, AttributeValue.NORMAL_TYPE));
                            } else {
                                results.addElement(new AttributeValue(value, AttributeValue.NORMAL_TYPE));
                            }
                        }
                    }
                }
                return results;
            } else if (child != null && (child.getName().equals("element"))) {
                Vector results = getSimpleTypeValues();
                XElement root = document.getLastRoot();
                if (root != null) {
                    String targetNS = root.getAttribute("targetNamespace");
                    String prefix = null;
                    if (targetNS != null && targetNS.length() > 0) {
                        Namespace ns = root.getNamespaceForURI(targetNS);
                        prefix = ns.getPrefix();
                    }
                    XElement[] elements = root.getElements("simpleType");
                    for (int i = 0; i < elements.length; i++) {
                        String value = elements[i].getAttribute("name");
                        if (value != null && value.length() > 0) {
                            if (prefix != null && prefix.length() > 0) {
                                results.addElement(
                                        new AttributeValue(prefix + ":" + value, AttributeValue.NORMAL_TYPE));
                            } else {
                                results.addElement(new AttributeValue(value, AttributeValue.NORMAL_TYPE));
                            }
                        }
                    }
                    elements = root.getElements("complexType");
                    for (int i = 0; i < elements.length; i++) {
                        String value = elements[i].getAttribute("name");
                        if (value != null && value.length() > 0) {
                            if (prefix != null && prefix.length() > 0) {
                                results.addElement(
                                        new AttributeValue(prefix + ":" + value, AttributeValue.NORMAL_TYPE));
                            } else {
                                results.addElement(new AttributeValue(value, AttributeValue.NORMAL_TYPE));
                            }
                        }
                    }
                }
                return results;
            }
        }
    } else if (document.isRNG()) {
        if (attributeName.equals("name")) {
            if (child.getName().equals("ref")) {
                XElement root = document.getLastRoot();
                Vector results = new Vector();
                if (root != null) {
                    XElement[] elements = root.getElements("define");
                    for (int i = 0; i < elements.length; i++) {
                        String value = elements[i].getAttribute("name");
                        if (value != null && value.length() > 0) {
                            results.addElement(new AttributeValue(value, AttributeValue.NORMAL_TYPE));
                        }
                    }
                }
                return results;
            }
        } else if (attributeName.equals("type")) {
            Vector results = new Vector();
            for (int i = 0; i < XSD_SIMPLE_TYPES.length; i++) {
                results.addElement(new AttributeValue(XSD_SIMPLE_TYPES[i], AttributeValue.NORMAL_TYPE));
            }
            return results;
        }
    } else if (document.isXSL()) {
        if (attributeName.equals("name")) {
            if (child.getName().equals("call-template")) {
                XElement root = document.getLastRoot();
                Vector results = new Vector();
                if (root != null) {
                    XElement[] elements = root.getElements("template");
                    for (int i = 0; i < elements.length; i++) {
                        String value = elements[i].getAttribute("name");
                        if (value != null && value.length() > 0) {
                            results.addElement(new AttributeValue(value, AttributeValue.NORMAL_TYPE));
                        }
                    }
                }
                return results;
            }
        } else {
            String val = ((XmlDocument) getEditor().getDocument()).getAttributeValue(getCursorPosition());
            int index = val.lastIndexOf('$');
            if (index != -1) {
                Vector results = new Vector();
                XElement root = document.getLastRoot();
                String prefix = "xsl";
                if (root != null) {
                    Namespace ns = root.getNamespaceForURI("http://www.w3.org/1999/XSL/Transform");
                    prefix = ns.getPrefix();
                }
                Vector elements = document
                        .searchLastWellFormedDocument("//" + prefix + ":param | //" + prefix + ":variable");
                for (int i = 0; i < elements.size(); i++) {
                    XElement element = (XElement) elements.elementAt(i);
                    String value = element.getAttribute("name");
                    if (value != null && value.length() > 0) {
                        results.addElement(new AttributeValue("$" + value, AttributeValue.NORMAL_TYPE));
                    }
                }
                return results;
            }
        }
    }
    while (tag != null) {
        ElementInformation element = getElement(tag, child);
        if (element != null) {
            Vector attributes = element.getAttributes();

            //tcurley - fixed this bug which caused an infinite loop
            //of null pointer exceptions
            if (attributes != null) {

                attributes.addAll(getAnyAttributes());

                for (int k = 0; k < attributes.size(); k++) {
                    AttributeInformation a = (AttributeInformation) attributes.elementAt(k);
                    if (a.getName().equals(attributeName)) {
                        Vector results = new Vector();
                        Vector values = a.getValues();
                        if (attributeNames != null) {
                            Vector otherValues = (Vector) attributeNames.get(qname);
                            if (otherValues != null) {
                                for (int i = 0; i < otherValues.size(); i++) {
                                    if (!contains(values, (String) otherValues.elementAt(i))) {
                                        results.addElement(new AttributeValue((String) otherValues.elementAt(i),
                                                AttributeValue.NORMAL_TYPE));
                                    }
                                }
                            }
                        }
                        if (values != null) {
                            for (int i = 0; i < values.size(); i++) {
                                results.addElement(values.elementAt(i));
                            }
                        }
                        return results;
                    }
                }
                if (attributeNames != null) {
                    Vector otherValues = (Vector) attributeNames.get(qname);
                    if (otherValues != null) {
                        Vector results = new Vector();
                        for (int i = 0; i < otherValues.size(); i++) {
                            results.addElement(new AttributeValue((String) otherValues.elementAt(i),
                                    AttributeValue.NORMAL_TYPE));
                        }
                        return results;
                    }
                }
                return null;
            }
        }
        tag = getParentStartTag(tag);
    }
    if (attributeNames != null) {
        Vector otherValues = (Vector) attributeNames.get(qname);
        if (otherValues != null) {
            Vector results = new Vector();
            for (int i = 0; i < otherValues.size(); i++) {
                results.addElement(
                        new AttributeValue((String) otherValues.elementAt(i), AttributeValue.NORMAL_TYPE));
            }
            return results;
        }
    }
    return null;
}

From source file:com.cladonia.xml.editor.Editor.java

License:Open Source License

private Vector getSimpleTypeValues() {

    Vector result = new Vector();
    XElement root = document.getLastRoot();
    String prefix = "xsd";
    if (root != null) {
        Namespace ns = root.getNamespaceForURI("http://www.w3.org/2001/XMLSchema");
        prefix = ns.getPrefix();
    }/*from  w w  w .  ja v  a  2s.  c  o  m*/
    for (int i = 0; i < XSD_SIMPLE_TYPES.length; i++) {
        if (prefix != null && prefix.length() > 0) {
            result.add(new AttributeValue(prefix + ":" + XSD_SIMPLE_TYPES[i], AttributeValue.NORMAL_TYPE));
        } else {
            result.add(new AttributeValue(XSD_SIMPLE_TYPES[i], AttributeValue.NORMAL_TYPE));
        }
    }
    return result;
}

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

License:Open Source License

protected void writeAttributes(Element element) throws IOException {

    // I do not yet handle the case where the same prefix maps to
    // two different URIs. For attributes on the same element
    // this is illegal; but as yet we don't throw an exception
    // if someone tries to do this
    for (int i = 0, size = element.attributeCount(); i < size; i++) {
        Attribute attribute = element.attribute(i);
        Namespace ns = attribute.getNamespace();
        if (ns != null && ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) {
            String prefix = ns.getPrefix();
            String uri = namespaceStack.getURI(prefix);
            if (!ns.getURI().equals(uri)) { // output a new namespace declaration
                writeNamespace(ns);/*  w  w w  .  ja v a 2 s .c o  m*/
                namespaceStack.push(ns);
            }
        }

        writeAttribute(attribute);
    }
}

From source file:com.cladonia.xml.navigator.Navigator.java

License:Open Source License

private void updateOutlineInternal() {
    if (DEBUG)//from www. j a  va 2s .  co m
        System.out.println("Navigator.updateOutline()");

    boolean showAll = false;

    if (document != null) {
        XElement root = (XElement) document.getLastRoot();

        String xpathValue = null;

        if (xpathPanel.isVisible()) {
            xpathValue = xpathField.getText();
        } else {
            int type = selectedNamespaceAction.getType();
            Namespace namespace = selectedNamespaceAction.getNamespace();

            if (showAttributes() && showElementContent()) {
                if (type == SELECTED_NAMESPACE_TYPE_ALL) {
                    showAll = true;

                    xpathValue = "//*|//text()|//@*";
                } else if (type == SELECTED_NAMESPACE_TYPE_NONE) {
                    xpathValue = "//*[not(namespace-uri())]|//*[not(namespace-uri())]/text()|//*[not(namespace-uri())]/@*";
                } else if (type == SELECTED_NAMESPACE_TYPE_OTHER) {
                    String prefix = namespace.getPrefix();
                    String uri = namespace.getURI();

                    if (prefix != null && prefix.trim().length() > 0) {
                        xpathValue = "//" + prefix + ":*|//" + prefix + ":*/text()|//" + prefix + ":*/@*";
                    } else {
                        xpathValue = "//*[namespace-uri()='" + uri + "']|//*[namespace-uri()='" + uri
                                + "']/text()|//*[namespace-uri()='" + uri + "']/@*";
                    }
                }
            } else if (showAttributes() && !showElementContent()) {
                if (type == SELECTED_NAMESPACE_TYPE_ALL) {
                    showAll = true;

                    xpathValue = "//*|//@*";
                } else if (type == SELECTED_NAMESPACE_TYPE_NONE) {
                    xpathValue = "//*[not(namespace-uri())]|//*[not(namespace-uri())]/@*";
                } else if (type == SELECTED_NAMESPACE_TYPE_OTHER) {
                    String prefix = namespace.getPrefix();
                    String uri = namespace.getURI();

                    if (prefix != null && prefix.trim().length() > 0) {
                        xpathValue = "//" + prefix + ":*|//" + prefix + ":*/@*";
                    } else {
                        xpathValue = "//*[namespace-uri()='" + uri + "']|//*[namespace-uri()='" + uri + "']/@*";
                    }
                }
            } else if (!showAttributes() && showElementContent()) {
                if (type == SELECTED_NAMESPACE_TYPE_ALL) {
                    showAll = true;

                    xpathValue = "//*|//text()";
                } else if (type == SELECTED_NAMESPACE_TYPE_NONE) {
                    xpathValue = "//*[not(namespace-uri())]|//*[not(namespace-uri())]/text()";
                } else if (type == SELECTED_NAMESPACE_TYPE_OTHER) {
                    String prefix = namespace.getPrefix();
                    String uri = namespace.getURI();

                    if (prefix != null && prefix.trim().length() > 0) {
                        xpathValue = "//" + prefix + ":*|//" + prefix + ":*/text()";
                    } else {
                        xpathValue = "//*[namespace-uri()='" + uri + "']|//*[namespace-uri()='" + uri
                                + "']/text()";
                    }
                }
            } else {
                if (type == SELECTED_NAMESPACE_TYPE_ALL) {
                    showAll = true;

                    xpathValue = "//*";
                } else if (type == SELECTED_NAMESPACE_TYPE_NONE) {
                    xpathValue = "//*[not(namespace-uri())]";
                } else if (type == SELECTED_NAMESPACE_TYPE_OTHER) {
                    String prefix = namespace.getPrefix();
                    String uri = namespace.getURI();

                    if (prefix != null && prefix.trim().length() > 0) {
                        xpathValue = "//" + prefix + ":*";
                    } else {
                        xpathValue = "//*[namespace-uri()='" + uri + "']";
                    }
                }
            }
        }

        Vector results = null;

        try {
            results = document.searchLastWellFormedDocument(xpathValue);

            xpathField.setForeground(Color.black);
        } catch (Exception e) {
            if (xpathField.getText() != null && xpathField.getText().trim().length() > 0) {
                xpathField.setForeground(Color.red);
            }
        }

        elementContentInResult = false;
        attributeInResult = false;

        if (root != null) {
            long time = System.currentTimeMillis();

            //            System.out.println( "Building Tree...");
            Vector shrinkingNodes = new Vector();

            if (results != null) {
                for (int i = 0; i < results.size(); i++) {
                    Object object = results.elementAt(i);

                    shrinkingNodes.addElement(object);
                    if (object instanceof Text) {
                        elementContentInResult = true;
                    }

                    if (object instanceof Attribute) {
                        attributeInResult = true;
                    }
                }
            }
            XmlElementNode node = new XmlElementNode(this, (XElement) document.getLastRoot(), results,
                    shrinkingNodes, getDeclaredNamespaces(), true, showAll);
            time = System.currentTimeMillis() - time;
            //            System.out.println( "Tree Build ["+time+"]");

            //            System.out.println( "Showing Tree!");
            tree.setRoot(node);
            tree.expand(3);

            forceSelectedElement(xngreditor.getView().getSelectedElement());
            //            System.out.println( "Tree Shown!");
        } else {
            tree.setRoot(null);
        }
    } else {
        tree.setRoot(null);
    }

    hasLatest = true;
}

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   www .  j a v  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.viewer.XmlElementNode.java

License:Mozilla Public License

private StyledElement formatNamespace(Namespace n) {
    StyledElement styledNamespace = null;

    String prefix = n.getPrefix();
    String value = n.getText();//from  www  . j  a  v a 2  s  .  c o  m

    if (value != null && value.length() > 0) {
        styledNamespace = new StyledElement();
        styledNamespace.addString(NAMESPACE_NAME);

        if (prefix != null && prefix.length() > 0) {
            styledNamespace.addString(NAMESPACE_COLON);
            styledNamespace.addString(new NamespacePrefix(prefix));
        }

        styledNamespace.addString(NAMESPACE_ASIGN);
        styledNamespace.addString(new NamespaceURI(value));
    }

    return styledNamespace;
}

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  .java  2  s  . co m*/
        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.xml.xdiff.XmlElementNode.java

License:Open Source License

private StyledElement formatNamespace(Namespace n) {
    StyledElement styledNamespace = null;

    String prefix = n.getPrefix();
    String value = n.getText();/*from   w  w w . j  a va2s .c om*/
    String name = "xmlns:" + prefix;

    boolean localInsertNamespace = false;
    boolean localDeleteNamespace = false;

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

    if (value != null && value.length() > 0) {
        styledNamespace = new StyledElement();

        if (localInsertNamespace) {
            styledNamespace.addString(INSERT_NAMESPACE_NAME);
        } else if (localDeleteNamespace) {
            styledNamespace.addString(DELETE_NAMESPACE_NAME);
        } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) {
            styledNamespace.addString(MERGED_NAMESPACE_NAME);
        } else {
            styledNamespace.addString(NAMESPACE_NAME);
        }

        if (prefix != null && prefix.length() > 0) {

            if (localInsertNamespace) {
                styledNamespace.addString(INSERT_NAMESPACE_COLON);
            } else if (localDeleteNamespace) {
                styledNamespace.addString(DELETE_NAMESPACE_COLON);
            } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) {
                styledNamespace.addString(MERGED_NAMESPACE_COLON);
            } else {
                styledNamespace.addString(NAMESPACE_COLON);
            }

            styledNamespace.addString(new NamespacePrefix(prefix));
        }

        if (localInsertNamespace) {
            styledNamespace.addString(INSERT_NAMESPACE_ASIGN);
            styledNamespace.addString(INSERT_NAMESPACE_QUOTE);
        } else if (localDeleteNamespace) {
            styledNamespace.addString(DELETE_NAMESPACE_ASIGN);
            styledNamespace.addString(DELETE_NAMESPACE_QUOTE);
        } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) {
            styledNamespace.addString(MERGED_NAMESPACE_ASIGN);
            styledNamespace.addString(MERGED_NAMESPACE_QUOTE);
        } else {
            styledNamespace.addString(NAMESPACE_ASIGN);
            styledNamespace.addString(NAMESPACE_QUOTE);
        }

        String namespacePrevious = findUpdatedAttribute(name, updateAttributes);
        if (namespacePrevious != null) {
            StyledString updatedValue = new AttributePreviousValue(namespacePrevious);
            updatedValue.setStrikeThrough(true);
            styledNamespace.addString(updatedValue);

            currentColor = Color.BLUE;
        }

        styledNamespace.addString(new NamespaceURI(value));

        if (localInsertNamespace) {
            styledNamespace.addString(INSERT_NAMESPACE_QUOTE);
        } else if (localDeleteNamespace) {
            styledNamespace.addString(DELETE_NAMESPACE_QUOTE);
        } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) {
            styledNamespace.addString(MERGED_NAMESPACE_QUOTE);
        } else {
            styledNamespace.addString(NAMESPACE_QUOTE);
        }
    }

    currentColor = Color.BLACK;
    return styledNamespace;
}

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

License:Open Source License

/**
* Sets the Schema Location attribute on the root element
*
* @param document The Exchanger document
* @param schemaType The schema type (either schemaLocation or noNamespaceSchemaLocation)
* @param namespace The namespace//  w  w w  .ja v a2 s .  c o  m
* @param schemaURL The URL or the schema
* 
*/
public static void setSchemaLocation(ExchangerDocument document, String schemaType, String namespace,
        String schemaURL) {
    schemaURL = URLUtilities.encodeURL(schemaURL);

    XDocument xdoc = document.getDocument();
    Element root = xdoc.getRootElement();

    if (schemaType.equals(SCHEMALOCATION)) {
        Attribute attrNoNS = root.attribute(NOSCHEMALOCATION);
        if (attrNoNS != null) {
            root.remove(attrNoNS);
        }

        // need to set both namspace and url
        Attribute attr = root.attribute(SCHEMALOCATION);
        if (attr == null) {
            // does the schema instance already exist
            Namespace ns = root.getNamespaceForURI(SCHEMAINSTANCE);
            if (ns != null) {
                String schemaInstancePrefix = ns.getPrefix();
                StringBuffer name = new StringBuffer();
                name.append(schemaInstancePrefix);
                name.append(":");
                name.append(SCHEMALOCATION);

                StringBuffer value = new StringBuffer();
                value.append(namespace);
                value.append(" ");
                value.append(schemaURL);

                root.addAttribute(name.toString(), value.toString());
            } else {
                root.addNamespace("xsi", SCHEMAINSTANCE);

                StringBuffer name = new StringBuffer();
                name.append("xsi");
                name.append(":");
                name.append(SCHEMALOCATION);

                StringBuffer value = new StringBuffer();
                value.append(namespace);
                value.append(" ");
                value.append(schemaURL);

                root.addAttribute(name.toString(), value.toString());
            }
        } else {
            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());
            }

            // update existing attribute, Note: it may have multiple attribute pairs
            StringBuffer value = new StringBuffer();
            value.append(namespace);
            value.append(" ");
            value.append(schemaURL);

            //need to start at the third value (i.e we only replace the first namespace-url pair)
            for (int i = 2; i < stringValues.size(); i++) {
                value.append(" ");
                value.append((String) stringValues.get(i));
            }

            attr.setValue(value.toString());
        }
    } else {
        // is of type "no schema location"
        Attribute attrSchema = root.attribute(SCHEMALOCATION);
        if (attrSchema != null) {
            root.remove(attrSchema);
        }

        // just need to set the url
        Attribute attr = root.attribute(NOSCHEMALOCATION);
        if (attr == null) {
            // does the schema instance already exist
            Namespace ns = root.getNamespaceForURI(SCHEMAINSTANCE);
            if (ns != null) {
                String schemaInstancePrefix = ns.getPrefix();
                StringBuffer name = new StringBuffer();
                name.append(schemaInstancePrefix);
                name.append(":");
                name.append(NOSCHEMALOCATION);

                root.addAttribute(name.toString(), schemaURL);
            } else {
                root.addNamespace("xsi", SCHEMAINSTANCE);

                StringBuffer name = new StringBuffer();
                name.append("xsi");
                name.append(":");
                name.append(NOSCHEMALOCATION);

                root.addAttribute(name.toString(), schemaURL);
            }
        } else {
            // update existing attribute
            attr.setValue(schemaURL);
        }
    }
}

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

License:Open Source License

private String moveNamespaces(ExchangerDocument document) throws Exception {

    try {//from  www. j a v  a  2s.  com
        XElement root = document.getRoot();
        int numberChanged = 0;
        boolean allow_one_ns_in_root = true;

        //get all namespaces in the root
        List rootNamespaces = root.declaredNamespaces();
        for (int cnt = 0; cnt < rootNamespaces.size(); ++cnt) {
            //search through and see if any of these have no prefix.
            //if they all have prefixes, then allow a non-prefixed namespace
            //else dont allow any non-prefixed namespaces
            Namespace ns = (Namespace) rootNamespaces.get(cnt);
            if (ns.getPrefix().length() == 0) {
                allow_one_ns_in_root = false;
            }
        }

        Vector namespaces = document.getDeclaredNamespaces();
        if (namespaces.size() > 0) {
            for (int cnt = 0; cnt < namespaces.size(); ++cnt) {
                Namespace ns1 = (Namespace) namespaces.get(cnt);
                //this will skip namespaces which do not have prefixes (e.g. Default namespaces)
                if ((allow_one_ns_in_root == true) || (ns1.getPrefix().length() > 0)) {
                    root.add(ns1);
                    numberChanged++;
                    if (ns1.getPrefix().length() == 0) {
                        allow_one_ns_in_root = false;
                    }
                }

            }
            if (numberChanged > 0) {
                //if the number of namespaces moved to root is greater than zero

                //check if root has 2 different namespace uris with the same prefix.
                rootNamespaces = root.declaredNamespaces();
                for (int cnt = 0; cnt < rootNamespaces.size(); ++cnt) {
                    Namespace ns = (Namespace) rootNamespaces.get(cnt);
                    for (int icnt = 0; icnt < rootNamespaces.size(); ++icnt) {

                        //skip the namespace we're comparing to 
                        Namespace ns1 = (Namespace) rootNamespaces.get(icnt);
                        if (ns != ns1) {
                            if (ns.getPrefix().equals(ns1.getPrefix())) {
                                //error
                                MessageHandler.showError(parent, "Cannot move namespaces.\n"
                                        + "Document contains multiple namespaces with the same prefixes.",
                                        "Tools Move Namespaces To Root Error");
                                //else - nothing has changed, just return null
                                return (null);
                            }
                        }
                    }
                }

                //then update the document
                document.update();
            } else {
                MessageHandler.showError(parent,
                        "No namespaces were moved.\n"
                                + "Since the root already declares one non-prefixed namespace.",
                        "Tools Move Namespaces To Root Error");
                //else - nothing has changed, just return null
                return (null);
            }
        } else {
            //there are no namespaces declared
            MessageHandler.showError(parent, "There are no namespaces declared in this document",
                    "Tools Move Namespaces To Root Error");
            return (null);
        }

    } catch (NullPointerException e) {
        MessageHandler.showError(parent, "Error - Cannot Move Namespaces",
                "Tools Move Namespaces To Root Error");
        return (null);
    } catch (Exception e) {
        MessageHandler.showError(parent, "Error - Cannot Move Namespaces",
                "Tools Move Namespaces To Root Error");
        return (null);
    }
    return document.getText();
}