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:architecture.common.xml.XmlWriter.java

License:Apache License

protected void writeNamespace(Namespace namespace) throws IOException {
    if (namespace != null) {
        writeNamespace(namespace.getPrefix(), namespace.getURI());
    }/*ww  w.  j a  v a  2 s.  c  o m*/
}

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

License:Apache License

/**
 * Writes the attributes of the given element
 *
 *///from  w ww .j  a  v a2s  .c  o  m
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);
                namespaceStack.push(ns);
            }
        }

        // If the attribute is a namespace declaration, check if we have
        // already
        // written that declaration elsewhere (if that's the case, it must
        // be
        // in the namespace stack
        String attName = attribute.getName();
        if (attName.startsWith("xmlns:")) {
            String prefix = attName.substring(6);
            if (namespaceStack.getNamespaceForPrefix(prefix) == null) {
                String uri = attribute.getValue();
                namespaceStack.push(prefix, uri);
                writeNamespace(prefix, uri);
            }
        } else if (attName.equals("xmlns")) {
            if (namespaceStack.getDefaultNamespace() == null) {
                String uri = attribute.getValue();
                namespaceStack.push(null, uri);
                writeNamespace(null, uri);
            }
        } else {
            char quote = format.getAttributeQuoteCharacter();
            writer.write(" ");
            writer.write(attribute.getQualifiedName());
            writer.write("=");
            writer.write(quote);
            writeEscapeAttributeEntities(attribute.getValue());
            writer.write(quote);
        }
    }
}

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

License:Apache License

/** Writes the attributes of the given element
  *//from  ww w.  j  a  v a2  s. co m
  */
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);
                namespaceStack.push(ns);
            }
        }

        // If the attribute is a namespace declaration, check if we have already
        // written that declaration elsewhere (if that's the case, it must be
        // in the namespace stack
        String attName = attribute.getName();
        if (attName.startsWith("xmlns:")) {
            String prefix = attName.substring(6);
            if (namespaceStack.getNamespaceForPrefix(prefix) == null) {
                String uri = attribute.getValue();
                namespaceStack.push(prefix, uri);
                writeNamespace(prefix, uri);
            }
        } else if (attName.equals("xmlns")) {
            if (namespaceStack.getDefaultNamespace() == null) {
                String uri = attribute.getValue();
                namespaceStack.push(null, uri);
                writeNamespace(null, uri);
            }
        } else {
            char quote = format.getAttributeQuoteCharacter();
            writer.write(" ");
            writer.write(attribute.getQualifiedName());
            writer.write("=");
            writer.write(quote);
            writeEscapeAttributeEntities(attribute.getValue());
            writer.write(quote);
        }
    }
}

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

License:Open Source License

private void defaultContent(Element element, ContentHandler contentHandler, LexicalHandler lexicalHandler,
        Interpreter beanShell, Stack slotStack) throws SAXException, PageTemplateException, IOException {
    // Use default template content
    for (Iterator i = element.nodeIterator(); i.hasNext();) {
        Node node = (Node) i.next();
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            processElement((Element) node, contentHandler, lexicalHandler, beanShell, slotStack);
            break;

        case Node.TEXT_NODE:
            char[] text = node.getText().toCharArray();
            contentHandler.characters(text, 0, text.length);
            break;

        case Node.COMMENT_NODE:
            char[] comment = node.getText().toCharArray();
            lexicalHandler.comment(comment, 0, comment.length);
            break;

        case Node.CDATA_SECTION_NODE:
            lexicalHandler.startCDATA();
            char[] cdata = node.getText().toCharArray();
            contentHandler.characters(cdata, 0, cdata.length);
            lexicalHandler.endCDATA();/*from  ww w.  j a v  a 2  s .  c  om*/
            break;

        case Node.NAMESPACE_NODE:
            Namespace declared = (Namespace) node;
            //System.err.println( "Declared namespace: " + declared.getPrefix() + ":" + declared.getURI() );
            namespaces.put(declared.getPrefix(), declared.getURI());
            //if ( declared.getURI().equals( TAL_NAMESPACE_URI ) ) {
            //    this.talNamespacePrefix = declared.getPrefix();
            //} 
            //else if (declared.getURI().equals( METAL_NAMESPACE_URI ) ) {
            //    this.metalNamespacePrefix = declared.getPrefix();
            //}
            break;

        case Node.ATTRIBUTE_NODE:
            // Already handled
            break;

        case Node.DOCUMENT_TYPE_NODE:
        case Node.ENTITY_REFERENCE_NODE:
        case Node.PROCESSING_INSTRUCTION_NODE:
        default:
            //System.err.println( "WARNING: Node type not supported: " + node.getNodeTypeName() );       
        }
    }
}

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

License:Open Source License

/**
 * With all of our namespace woes, getting an XPath expression
 * to work has proven futile, so we'll recurse through the tree
 * ourselves to find what we need.//w  w  w.  j  a  v  a2s. c o m
 */
private void findMacros(Element element, Map macros) {
    // Process any declared namespaces
    for (Iterator i = element.declaredNamespaces().iterator(); i.hasNext();) {
        Namespace namespace = (Namespace) i.next();
        namespaces.put(namespace.getPrefix(), namespace.getURI());
        //if ( namespace.getURI().equals( TAL_NAMESPACE_URI ) ) {
        //    this.talNamespacePrefix = namespace.getPrefix();
        //}
        //else if ( namespace.getURI().equals( METAL_NAMESPACE_URI ) ) {
        //    this.metalNamespacePrefix = namespace.getPrefix();
        //}
    }

    // Look for our attribute
    //String qualifiedAttributeName = this.metalNamespacePrefix + ":define-macro";
    //String name = element.attributeValue( qualifiedAttributeName );
    String name = element.attributeValue("define-macro");
    //if ( name == null ) {
    //    name = element.attributeValue
    //        ( new QName( "define-macro", new Namespace( metalNamespacePrefix, METAL_NAMESPACE_URI ) ) );
    //}
    if (name != null) {
        macros.put(name, new MacroImpl(element));
    }

    // Recurse into child elements
    for (Iterator i = element.elementIterator(); i.hasNext();) {
        findMacros((Element) i.next(), macros);
    }
}

From source file:com.cladonia.schema.dtd.DTDDocument.java

License:Open Source License

public void updatePrefixes(Vector declarations) {
    Vector allElements = getElements();

    for (int i = 0; i < allElements.size(); i++) {
        ElementInformation model = (ElementInformation) allElements.elementAt(i);
        Vector attributes = model.getAttributes();
        //         Vector children = model.getChildElements();

        //         if ( children != null) {
        //            for ( int j = 0; j < children.size(); j++) {
        //               ElementInformation child = (ElementInformation)children.elementAt(j);
        //               
        //               for ( int k = 0; k < declarations.size(); k++) {
        //                  Namespace ns = (Namespace)declarations.elementAt(k);
        ////from www.  j a  v a 2  s  .co m
        //                  if ( ns.getURI().equals( child.getNamespace())) {// && ns.getPrefix() != null && ns.getPrefix().trim().length() > 0) {
        //                     child.setPrefix( ns.getPrefix());
        //                     break;
        //                  }
        //               }
        //            }
        //         }

        if (attributes != null) {
            for (int j = 0; j < attributes.size(); j++) {
                AttributeInformation attribute = (AttributeInformation) attributes.elementAt(j);

                for (int k = 0; k < declarations.size(); k++) {
                    Namespace ns = (Namespace) declarations.elementAt(k);

                    if (ns.getURI().equals(attribute.getNamespace())) { // && ns.getPrefix() != null && ns.getPrefix().trim().length() > 0) {
                        attribute.setPrefix(ns.getPrefix());
                        break;
                    }
                }
            }
        }

        for (int j = 0; j < declarations.size(); j++) {
            Namespace ns = (Namespace) declarations.elementAt(j);

            if (ns.getURI().equals(model.getNamespace())) { // && ns.getPrefix() != null && ns.getPrefix().trim().length() > 0) {
                model.setPrefix(ns.getPrefix());
                break;
            }
        }
    }
}

From source file:com.cladonia.schema.XMLSchema.java

License:Open Source License

private void updatePrefixes(Vector declarations, ElementInformation element) {
    Vector attributes = element.getAttributes();
    Vector children = element.getChildElements();

    for (int i = 0; i < children.size(); i++) {
        ElementInformation child = (ElementInformation) children.elementAt(i);

        for (int k = 0; k < declarations.size(); k++) {
            Namespace ns = (Namespace) declarations.elementAt(k);

            if (ns.getURI().equals(child.getNamespace())) {// && ns.getPrefix() != null && ns.getPrefix().trim().length() > 0) {
                child.setPrefix(ns.getPrefix());
                break;
            }/*from   w  w  w. j  a v a2  s.  c om*/
        }
    }

    if (attributes != null) {
        for (int j = 0; j < attributes.size(); j++) {
            AttributeInformation attribute = (AttributeInformation) attributes.elementAt(j);

            for (int k = 0; k < declarations.size(); k++) {
                Namespace ns = (Namespace) declarations.elementAt(k);

                if (ns.getURI().equals(attribute.getNamespace())) { // && ns.getPrefix() != null && ns.getPrefix().trim().length() > 0) {
                    attribute.setPrefix(ns.getPrefix());
                    break;
                }
            }
        }
    }
}

From source file:com.cladonia.schema.XMLSchema.java

License:Open Source License

public void updatePrefixes(Vector declarations) {
    Vector allElements = getAllElements();

    for (int i = 0; i < allElements.size(); i++) {
        ElementInformation model = (ElementInformation) allElements.elementAt(i);
        Vector attributes = model.getAttributes();
        Vector children = model.getChildElements();

        if (children != null) {
            for (int j = 0; j < children.size(); j++) {
                ElementInformation child = (ElementInformation) children.elementAt(j);

                updatePrefixes(declarations, child);

                for (int k = 0; k < declarations.size(); k++) {
                    Namespace ns = (Namespace) declarations.elementAt(k);

                    if (ns.getURI().equals(child.getNamespace())) {// && ns.getPrefix() != null && ns.getPrefix().trim().length() > 0) {
                        child.setPrefix(ns.getPrefix());
                        break;
                    }//www  .  j a  va  2  s  .  c o  m
                }
            }
        }

        if (attributes != null) {
            for (int j = 0; j < attributes.size(); j++) {
                AttributeInformation attribute = (AttributeInformation) attributes.elementAt(j);

                for (int k = 0; k < declarations.size(); k++) {
                    Namespace ns = (Namespace) declarations.elementAt(k);

                    if (ns.getURI().equals(attribute.getNamespace())) { // && ns.getPrefix() != null && ns.getPrefix().trim().length() > 0) {
                        attribute.setPrefix(ns.getPrefix());
                        break;
                    }
                }
            }
        }

        for (int j = 0; j < declarations.size(); j++) {
            Namespace ns = (Namespace) declarations.elementAt(j);

            if (ns.getURI().equals(model.getNamespace())) { // && ns.getPrefix() != null && ns.getPrefix().trim().length() > 0) {
                model.setPrefix(ns.getPrefix());
                break;
            }
        }
    }
}

From source file:com.cladonia.xml.designer.ElementNode.java

License:Open Source License

/**
 * Wether the current uri has already been declared 
 * as a namespace?/*from   www .java 2  s .c  o  m*/
 *
 * @param uri the namespace uri.
 *
 * @return true when the namespace has been declared by a parent element.
 */
public boolean isNamespaceDeclared() {
    XElement element = parent.getElement();
    Namespace ns = element.getNamespaceForURI(type.getNamespace());

    if (DEBUG) {
        if (ns != null) {
            System.out.println("ns: xmlns:" + ns.getPrefix() + "=" + ns.getURI());
        }
    }

    return ns != null;
}

From source file:com.cladonia.xml.designer.ElementNode.java

License:Open Source License

/**
 * Adds an element to the virtual node supplied.
 *
 * @param node the virtual node.//w  w w  . j  a  v a2 s. c o  m
 */
protected void addNode(ElementNode node) {
    XElement e = null;
    SchemaElement type = node.getType();
    String uri = type.getNamespace();
    Namespace ns = element.getNamespaceForURI(uri);
    String prefix = null;

    if (ns != null) {
        prefix = ns.getPrefix();
    }

    if (prefix != null) {
        e = new XElement(type.getName(), uri, prefix, false);
    } else {
        e = new XElement(type.getName(), uri, false);
    }

    addNode(node, e);
}