Example usage for org.dom4j Attribute getQualifiedName

List of usage examples for org.dom4j Attribute getQualifiedName

Introduction

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

Prototype

String getQualifiedName();

Source Link

Document

Returns the fully qualified name of this element.

Usage

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

License:Apache License

/**
 * Writes the attributes of the given element
 *
 *//*w w  w . ja va 2 s .  c  om*/
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.common.xml.XmlWriter.java

License:Apache License

protected void writeAttribute(Attribute attribute) throws IOException {
    writer.write(" ");
    writer.write(attribute.getQualifiedName());
    writer.write("=");

    char quote = format.getAttributeQuoteCharacter();
    writer.write(quote);//from   www  .ja  v  a2  s.c  om

    writeEscapeAttributeEntities(attribute.getValue());

    writer.write(quote);
    lastOutputNodeType = Node.ATTRIBUTE_NODE;
}

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

License:Apache License

/** Writes the attributes of the given element
  *//from w  w  w . ja  v  a 2s.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

AttributesImpl getAttributes(Element element, Expressions expressions) throws PageTemplateException {
    AttributesImpl attributes = new AttributesImpl();
    for (Iterator i = element.attributeIterator(); i.hasNext();) {
        Attribute attribute = (Attribute) i.next();
        Namespace namespace = attribute.getNamespace();
        //String prefix = namespace.getPrefix();
        //System.err.println( "attribute: name=" + attribute.getName() + "\t" +
        //                    "qualified name=" + attribute.getQualifiedName() + "\t" +
        //                    "ns prefix=" + namespace.getPrefix() + "\t" +
        //                    "ns uri=" + namespace.getURI() );
        //String qualifiedName = attribute.getName();
        //String name = qualifiedName;
        //if ( qualifiedName.startsWith( prefix + ":" ) ) {
        //    name = qualifiedName.substring( prefix.length() + 1 );
        //}//from www.  j a v a 2  s .c  o m
        String name = attribute.getName();

        // Handle JPT attributes
        //if ( prefix.equals( talNamespacePrefix ) ) {
        if (TAL_NAMESPACE_URI.equals(namespace.getURI())) {
            // tal:define
            if (name.equals("define")) {
                expressions.define = attribute.getValue();
            }

            // tal:condition
            else if (name.equals("condition")) {
                expressions.condition = attribute.getValue();
            }

            // tal:repeat
            else if (name.equals("repeat")) {
                expressions.repeat = attribute.getValue();
            }

            // tal:content
            else if (name.equals("content")) {
                expressions.content = attribute.getValue();
            }

            // tal:replace
            else if (name.equals("replace")) {
                if (expressions.omitTag == null) {
                    expressions.omitTag = "";
                }
                expressions.content = attribute.getValue();
            }

            // tal:attributes
            else if (name.equals("attributes")) {
                expressions.attributes = attribute.getValue();
            }

            // tal:omit-tag
            else if (name.equals("omit-tag")) {
                expressions.omitTag = attribute.getValue();
            }

            // tal:evaluate
            else if (name.equals("evaluate")) {
                expressions.evaluate = attribute.getValue();
            }

            // error
            else {
                throw new PageTemplateException("unknown tal attribute: " + name);
            }
        }
        //else if ( prefix.equals( metalNamespacePrefix ) ) 
        else if (METAL_NAMESPACE_URI.equals(namespace.getURI())) {
            // metal:use-macro
            if (name.equals("use-macro")) {
                expressions.useMacro = attribute.getValue();
            }

            // metal:define-slot
            else if (name.equals("define-slot")) {
                expressions.defineSlot = attribute.getValue();
            }

            // metal:define-macro
            // metal:fill-slot
            else if (name.equals("define-macro") || name.equals("fill-slot")) {
                // these are ignored here, as they don't affect processing of current
                // template, but are called from other templates
            }

            // error
            else {
                throw new PageTemplateException("unknown metal attribute: " + name);
            }
        }

        // Pass on all other attributes
        else {
            //String qualifiedName = namespace.getPrefix() + ":" + name;
            attributes.addAttribute(namespace.getURI(), name, attribute.getQualifiedName(), "CDATA",
                    attribute.getValue());
            //attributes.addAttribute( getNamespaceURIFromPrefix(prefix), name, qualifiedName, "CDATA", attribute.getValue() );
        }
    }
    return attributes;
}

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

License:Open Source License

protected void writeAttribute(Attribute attribute) throws IOException {
    StringBuffer buffer = new StringBuffer();
    XAttribute a = (XAttribute) attribute;

    buffer.append(attribute.getQualifiedName());
    buffer.append("=");

    String value = attribute.getValue();
    boolean hasQuote = value.indexOf("\"") != -1;

    if (hasQuote) {
        buffer.append("'");
        buffer.append(value);//  w  ww.  jav a  2s  .c  om
        buffer.append("'");
    } else {
        buffer.append("\"");
        buffer.append(value);
        buffer.append("\"");
    }

    String attr = buffer.toString();

    //      System.out.println( "Wrap ["+isWrapText()+", "+getMaxLineLength()+", "+((PositionedWriter)writer).getColumn()+", "+attr.length()+"]");

    if (getMaxLineLength() != -1
            && (((PositionedWriter) writer).getColumn() + attr.length() + 1) >= getMaxLineLength()) {
        //         System.out.println( "Wrapping ... ["+attr+"]");
        ++indentLevel;

        format.setNewlines(true);
        format.setIndent(indentString);

        writePrintln();
        indent();

        --indentLevel;

        format.setNewlines(false);
        format.setIndent("");
    } else {
        writer.write(" ");
    }

    a.setAttributeStartPosition(((PositionedWriter) writer).getPosition());

    writer.write(attr);

    a.setAttributeEndPosition(((PositionedWriter) writer).getPosition());

    lastOutputNodeType = Node.ATTRIBUTE_NODE;
}

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();//www .  j a  v a  2 s.com
    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

/** Writes the attributes of the given element
  *//w  w w. ja  v  a  2s  . c  o  m
  */
//    protected void writeAttributes( Element element ) throws IOException {
//       if (DEBUG) System.out.println( "XMLFormatter.writeAttributes( "+element+")");
//       if (indentMixed) {
//          super.writeAttributes( element);
//       } else {

// 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);
//                   }
//               }
//
//               writer.write(" ");
//               writer.write(attribute.getQualifiedName());
//               writer.write("=\"");
//               writeEscapeAttributeEntities(attribute.getValue());
//               writer.write("\"");
//           }
////       }
//    }

protected void writeAttribute(Attribute attribute) throws IOException {
    XAttribute a = (XAttribute) attribute;

    //       if ( attributeOnNewLine) {
    //          ++indentLevel;
    //
    //          format.setNewlines( true);
    //          format.setIndent( indentString);
    //          
    //           writePrintln();
    //           indent();
    //       } else {
    writer.write(" ");
    //       }

    writer.write(attribute.getQualifiedName());
    writer.write("=");

    String value = attribute.getValue();

    if (format.isTrimText() && value != null && value.trim().length() > 0) {
        StringTokenizer tokenizer = new StringTokenizer(value);
        StringBuffer buffer = new StringBuffer();
        boolean first = true;

        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();

            if (!first) {
                buffer.append(" ");
            }

            buffer.append(token);
            first = false;
        }

        value = buffer.toString();
    }

    boolean hasQuote = value.indexOf("\"") != -1;

    if (hasQuote) {
        writer.write("'");
        writeEscapeAttributeEntities(value);
        writer.write("'");
    } else {
        writer.write("\"");
        writeEscapeAttributeEntities(value);
        writer.write("\"");
    }

    lastOutputNodeType = Node.ATTRIBUTE_NODE;

    // reset...
    //       if ( attributeOnNewLine) {
    //          --indentLevel;
    //
    //          format.setNewlines( false);
    //          format.setIndent( "");
    //       }
}

From source file:com.doculibre.constellio.utils.xml.SolrShemaXmlReader.java

License:Open Source License

private static Map<String, Map<String, String>> readFields(Document schemaDocument, Boolean readDynamicFields,
        Boolean checkTypes) {//from   ww  w  .  j ava2  s .co  m
    Map<String, Map<String, String>> returnList = new HashMap<String, Map<String, String>>();
    //AnalyzerClassServices analyzerClassServices = ConstellioSpringUtils.getAnalyzerClassServices();
    FieldTypeServices fieldTypeServices = ConstellioSpringUtils.getFieldTypeServices();
    if (schemaDocument != null) {

        Element fieldsElement = schemaDocument.getRootElement().element("fields");

        List<Element> fieldElements;
        if (readDynamicFields) {
            fieldElements = fieldsElement.elements("dynamicField");
        } else {
            fieldElements = fieldsElement.elements("field");
        }
        for (Iterator<Element> it = fieldElements.iterator(); it.hasNext();) {
            Element fieldElement = it.next();

            if (checkTypes) {
                /*String analyzerClassName = fieldElement.attributeValue("analyzer");
                if (analyzerClassName != null) {
                AnalyzerClass analyzerClass = analyzerClassServices.get(analyzerClassName);
                    if (analyzerClass == null) {
                       throw new RuntimeException("New Analyzers must be defined throught Constellio Interface");
                    }
                }*/
                String typeName = fieldElement.attributeValue("type");
                if (typeName == null) {
                    throw new RuntimeException("A Field must have a type");
                }
                FieldType fieldType = fieldTypeServices.get(typeName);
                if (fieldType == null) {
                    throw new RuntimeException(
                            "New Field type " + typeName + " must be defined throught Constellio Interface");
                }
            }
            String fieldName = fieldElement.attributeValue("name");
            if (fieldName == null) {
                throw new RuntimeException("A Field must have a name");
            }

            List<Attribute> attributes = fieldElement.attributes();
            Map<String, String> attributesToMap = new HashMap<String, String>();
            for (Attribute att : attributes) {
                if (!att.getQualifiedName().equals("name")) {
                    attributesToMap.put(att.getQualifiedName(), att.getValue());
                }
            }

            returnList.put(fieldName, attributesToMap);
        }
    }
    return returnList;
}

From source file:com.nokia.config.SAXConfigParser.java

License:Open Source License

/**
 * Constructor/*from w  ww  .  ja  v a 2  s.  co  m*/
 * @return list of available configurations that can be built.
 */
public String getConfigs() {
    File file = new File(sysdefFile);
    SAXReader reader = new SAXReader();
    reader.addHandler("/SystemDefinition/build/target", new ElementHandler() {
        public void onStart(ElementPath path) {
        }

        public void onEnd(ElementPath path) {
            Element row = path.getCurrent();
            Iterator itr = row.attributeIterator();
            while (itr.hasNext()) {
                Attribute child = (Attribute) itr.next();
                String attrName = child.getQualifiedName();
                if (attrName.equals("name")) {
                    configs += (String) child.getValue() + ",";
                }
            }
            row.detach();
        }
    });
    try {
        Document doc = reader.read(file);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return configs;
}

From source file:com.nokia.helium.sbs.SAXSysdefParser.java

License:Open Source License

/**
 * Constructor/*from   w w w.j  a  v a  2  s .  c  om*/
 * 
 * @return list of available configurations that can be built.
 */
public void parseConfig(String nodeToGet) {
    layers = new ArrayList<String>();
    SAXReader reader = new SAXReader();
    reader.addHandler("/SystemDefinition/systemModel/" + nodeToGet, new ElementHandler() {
        public void onStart(ElementPath path) {
        }

        public void onEnd(ElementPath path) {
            Element row = path.getCurrent();
            Iterator itr = row.attributeIterator();
            while (itr.hasNext()) {
                Attribute child = (Attribute) itr.next();
                String attrName = child.getQualifiedName();
                if (attrName.equals("name")) {
                    layers.add(child.getValue());
                }
            }
            row.detach();
        }
    });
    try {
        reader.read(sysdefFile);
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}