Example usage for javax.xml.namespace QName getNamespaceURI

List of usage examples for javax.xml.namespace QName getNamespaceURI

Introduction

In this page you can find the example usage for javax.xml.namespace QName getNamespaceURI.

Prototype

public String getNamespaceURI() 

Source Link

Document

Get the Namespace URI of this QName.

Usage

From source file:org.apache.axis.encoding.ser.BeanSerializer.java

/**
 * write a schema representation of the given Class field and append it to
 * the where Node, recurse on complex types
 * @param fieldName name of the field/* w w  w . java  2 s .c  o  m*/
 * @param xmlType the schema type of the field
 * @param fieldType type of the field
 * @param isUnbounded causes maxOccurs="unbounded" if set
 * @param where location for the generated schema node
 * @param itemQName
 * @throws Exception
 */
protected void writeField(Types types, String fieldName, QName xmlType, Class fieldType, boolean isUnbounded,
        boolean isOmittable, Element where, boolean isAnonymous, QName itemQName) throws Exception {
    Element elem;
    String elementType = null;

    if (isAnonymous) {
        elem = types.createElementWithAnonymousType(fieldName, fieldType, isOmittable,
                where.getOwnerDocument());
    } else {
        if (!SchemaUtils.isSimpleSchemaType(xmlType) && Types.isArray(fieldType)) {
            xmlType = null;
        }

        if (itemQName != null && SchemaUtils.isSimpleSchemaType(xmlType) && Types.isArray(fieldType)) {
            xmlType = null;
        }

        QName typeQName = types.writeTypeAndSubTypeForPart(fieldType, xmlType);
        elementType = types.getQNameString(typeQName);

        if (elementType == null) {
            // If writeType returns null, then emit an anytype.
            QName anyQN = Constants.XSD_ANYTYPE;
            String prefix = types.getNamespaces().getCreatePrefix(anyQN.getNamespaceURI());
            elementType = prefix + ":" + anyQN.getLocalPart();
        }

        // isNillable default value depends on the field type
        boolean isNillable = Types.isNullable(fieldType);
        if (typeDesc != null) {
            FieldDesc field = typeDesc.getFieldByName(fieldName);
            if (field != null && field.isElement()) {
                isNillable = ((ElementDesc) field).isNillable();
            }
        }

        elem = types.createElement(fieldName, elementType, isNillable, isOmittable, where.getOwnerDocument());
    }

    if (isUnbounded) {
        elem.setAttribute("maxOccurs", "unbounded");
    }

    where.appendChild(elem);
}

From source file:org.apache.axis.encoding.ser.BeanSerializer.java

private void setAttributeProperty(Object propValue, QName qname, QName xmlType, Class javaType,
        AttributesImpl attrs, SerializationContext context) throws Exception {

    String namespace = qname.getNamespaceURI();
    String localName = qname.getLocalPart();

    // org.xml.sax.helpers.AttributesImpl JavaDoc says: "For the
    // sake of speed, this method does no checking to see if the
    // attribute is already in the list: that is the
    // responsibility of the application." check for the existence
    // of the attribute to avoid adding it more than once.
    if (attrs.getIndex(namespace, localName) != -1) {
        return;//  w w  w .j av a2  s .  c  om
    }

    String propString = context.getValueAsString(propValue, xmlType, javaType);

    attrs.addAttribute(namespace, localName, context.attributeQName2String(qname), "CDATA", propString);
}

From source file:org.apache.axis.encoding.SerializationContext.java

/**
 * Convert QName to a string of the form <prefix>:<localpart>
 * @param qName/*from   ww  w  .  j  a va 2 s . co m*/
 * @return prefixed qname representation for serialization.
 */
public String qName2String(QName qName, boolean writeNS) {
    String prefix = null;
    String namespaceURI = qName.getNamespaceURI();
    String localPart = qName.getLocalPart();

    if (localPart != null && localPart.length() > 0) {
        int index = localPart.indexOf(':');
        if (index != -1) {
            prefix = localPart.substring(0, index);
            if (prefix.length() > 0 && !prefix.equals("urn")) {
                registerPrefixForURI(prefix, namespaceURI);
                localPart = localPart.substring(index + 1);
            } else {
                prefix = null;
            }
        }
        localPart = Utils.getLastLocalPart(localPart);
    }

    if (namespaceURI.length() == 0) {
        if (writeNS) {
            // If this is unqualified (i.e. prefix ""), set the default
            // namespace to ""
            String defaultNS = nsStack.getNamespaceURI("");
            if (defaultNS != null && defaultNS.length() > 0) {
                registerPrefixForURI("", "");
            }
        }
    } else {
        prefix = getPrefixForURI(namespaceURI);
    }

    if ((prefix == null) || (prefix.length() == 0))
        return localPart;

    return prefix + ':' + localPart;
}

From source file:org.apache.axis.encoding.SerializationContext.java

/**
 * Convert attribute QName to a string of the form <prefix>:<localpart>
 * There are slightly different rules for attributes:
 *  - There is no default namespace//from w  w  w  . j a  va  2  s  .  co m
 *  - any attribute in a namespace must have a prefix
 *
 * @param qName QName
 * @return prefixed qname representation for serialization.
 */
public String attributeQName2String(QName qName) {
    String prefix = null;
    String uri = qName.getNamespaceURI();
    if (uri.length() > 0) {
        prefix = getPrefixForURI(uri, null, true);
    }

    if ((prefix == null) || (prefix.length() == 0))
        return qName.getLocalPart();

    return prefix + ':' + qName.getLocalPart();
}

From source file:org.apache.axis.encoding.SerializationContext.java

/**
 * Indicates whether the object should be interpretted as a primitive
 * for the purposes of multi-ref processing.  A primitive value
 * is serialized directly instead of using id/href pairs.  Thus
 * primitive serialization/deserialization is slightly faster.
 * @param value to be serialized//from  ww  w.  j  a  va 2s .  c  om
 * @return true/false
 */
public boolean isPrimitive(Object value) {
    if (value == null)
        return true;

    Class javaType = value.getClass();

    if (javaType.isPrimitive())
        return true;

    if (javaType == String.class)
        return true;
    if (Calendar.class.isAssignableFrom(javaType))
        return true;
    if (Date.class.isAssignableFrom(javaType))
        return true;
    if (HexBinary.class.isAssignableFrom(javaType))
        return true;
    if (Element.class.isAssignableFrom(javaType))
        return true;
    if (javaType == byte[].class)
        return true;

    // There has been discussion as to whether arrays themselves should
    // be regarded as multi-ref.
    // Here are the three options:
    //   1) Arrays are full-fledged Objects and therefore should always be
    //      multi-ref'd  (Pro: This is like java.  Con: Some runtimes don't
    //      support this yet, and it requires more stuff to be passed over the wire.)
    //   2) Arrays are not full-fledged Objects and therefore should
    //      always be passed as single ref (note the elements of the array
    //      may be multi-ref'd.) (Pro:  This seems reasonable, if a user
    //      wants multi-referencing put the array in a container.  Also
    //      is more interop compatible.  Con: Not like java serialization.)
    //   3) Arrays of primitives should be single ref, and arrays of
    //      non-primitives should be multi-ref.  (Pro: Takes care of the
    //      looping case.  Con: Seems like an obtuse rule.)
    //
    // Changing the code from (1) to (2) to see if interop fairs better.
    if (javaType.isArray())
        return true;

    // Note that java.lang wrapper classes (i.e. java.lang.Integer) are
    // not primitives unless the corresponding type is an xsd type.
    // (If the wrapper maps to a soap encoded primitive, it can be nillable
    // and multi-ref'd).
    QName qName = getQNameForClass(javaType);
    if (qName != null && Constants.isSchemaXSD(qName.getNamespaceURI())) {
        if (SchemaUtils.isSimpleSchemaType(qName)) {
            return true;
        }
    }

    return false;
}

From source file:org.apache.axis.encoding.SerializationContext.java

/**
 * Writes (using the Writer) the start tag for element QName along with the
 * indicated attributes and namespace mappings.
 * @param qName is the name of the element
 * @param attributes are the attributes to write
 *///from   www. j a  va  2s.c  o  m
public void startElement(QName qName, Attributes attributes) throws IOException {
    java.util.ArrayList vecQNames = null;
    if (debugEnabled) {
        log.debug(Messages.getMessage("startElem00",
                "[" + qName.getNamespaceURI() + "]:" + qName.getLocalPart()));
    }

    if (startOfDocument && sendXMLDecl) {
        writeXMLDeclaration();
    }

    if (writingStartTag) {
        writer.write('>');
        if (pretty)
            writer.write('\n');
        indent++;
    }

    if (pretty)
        for (int i = 0; i < indent; i++)
            writer.write(' ');
    String elementQName = qName2String(qName, true);
    writer.write('<');

    writer.write(elementQName);

    if (writeXMLType != null) {
        attributes = setTypeAttribute(attributes, writeXMLType);
        writeXMLType = null;
    }

    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            String qname = attributes.getQName(i);
            writer.write(' ');

            String prefix = "";
            String uri = attributes.getURI(i);
            if (uri != null && uri.length() > 0) {
                if (qname.length() == 0) {
                    // If qname isn't set, generate one
                    prefix = getPrefixForURI(uri);
                } else {
                    // If it is, make sure the prefix looks reasonable.
                    int idx = qname.indexOf(':');
                    if (idx > -1) {
                        prefix = qname.substring(0, idx);
                        prefix = getPrefixForURI(uri, prefix, true);
                    }
                }
                if (prefix.length() > 0) {
                    qname = prefix + ':' + attributes.getLocalName(i);
                } else {
                    qname = attributes.getLocalName(i);
                }
            } else {
                qname = attributes.getQName(i);
                if (qname.length() == 0)
                    qname = attributes.getLocalName(i);
            }

            if (qname.startsWith("xmlns")) {
                if (vecQNames == null)
                    vecQNames = new ArrayList();
                vecQNames.add(qname);
            }
            writer.write(qname);
            writer.write("=\"");

            getEncoder().writeEncoded(writer, attributes.getValue(i));

            writer.write('"');
        }
    }

    if (noNamespaceMappings) {
        nsStack.push();
    } else {
        for (Mapping map = nsStack.topOfFrame(); map != null; map = nsStack.next()) {
            if (!(map.getNamespaceURI().equals(Constants.NS_URI_XMLNS) && map.getPrefix().equals("xmlns"))
                    && !(map.getNamespaceURI().equals(Constants.NS_URI_XML) && map.getPrefix().equals("xml"))) {
                StringBuffer sb = new StringBuffer("xmlns");
                if (map.getPrefix().length() > 0) {
                    sb.append(':');
                    sb.append(map.getPrefix());
                }
                if ((vecQNames == null) || (vecQNames.indexOf(sb.toString()) == -1)) {
                    writer.write(' ');
                    sb.append("=\"");
                    sb.append(map.getNamespaceURI());
                    sb.append('"');
                    writer.write(sb.toString());
                }
            }
        }

        noNamespaceMappings = true;
    }

    writingStartTag = true;

    elementStack.push(elementQName);

    onlyXML = true;
}

From source file:org.apache.axis.encoding.SerializationContext.java

/**
 * Invoked to do the actual serialization of the qName (called by serialize above).
 * additional attributes that will be serialized with the qName.
 * @param elemQName is the QName of the element
 * @param attributes are additional attributes
 * @param value is the object to serialize
 * @param xmlType (optional) is the desired type QName.
 * @param sendType indicates whether the xsi:type attribute should be set.
 *//*from www  . jav  a2 s  . c o m*/
private void serializeActual(QName elemQName, Attributes attributes, Object value, QName xmlType,
        Class javaClass, Boolean sendType) throws IOException {
    boolean shouldSendType = (sendType == null) ? shouldSendXSIType() : sendType.booleanValue();

    if (value != null) {
        TypeMapping tm = getTypeMapping();

        if (tm == null) {
            throw new IOException(Messages.getMessage("noSerializer00", value.getClass().getName(), "" + this));
        }

        // Set currentXMLType to the one desired one.
        // Note for maxOccurs usage this xmlType is the
        // type of the component not the type of the array.
        currentXMLType = xmlType;

        // if we're looking for xsd:anyType, accept anything...
        if (Constants.equals(Constants.XSD_ANYTYPE, xmlType)) {
            xmlType = null;
            shouldSendType = true;
        }

        // Try getting a serializer for the prefered xmlType
        QNameHolder actualXMLType = new QNameHolder();

        Class javaType = getActualJavaClass(xmlType, javaClass, value);

        Serializer ser = getSerializer(javaType, xmlType, actualXMLType);

        if (ser != null) {
            // Send the xmlType if indicated or if
            // the actual xmlType is different than the
            // prefered xmlType
            if (shouldSendType || (xmlType != null && (!xmlType.equals(actualXMLType.value)))) {

                if (!isEncoded()) {
                    if (Constants.isSOAP_ENC(actualXMLType.value.getNamespaceURI())) {
                        // Don't write SOAP_ENC types (i.e. Array) if we're not using encoding
                    } else if (javaType.isPrimitive() && javaClass != null
                            && JavaUtils.getWrapperClass(javaType) == javaClass) {
                        // Don't write xsi:type when serializing primitive wrapper value as primitive type.
                    } else {
                        if (!(javaType.isArray() && xmlType != null
                                && Constants.isSchemaXSD(xmlType.getNamespaceURI()))) {
                            writeXMLType = actualXMLType.value;
                        }
                    }
                } else {
                    writeXMLType = actualXMLType.value;
                }
            }

            // -----------------
            // NOTE: I have seen doc/lit tests that use
            // the type name as the element name in multi-ref cases
            // (for example <soapenc:Array ... >)
            // In such cases the xsi:type is not passed along.
            // -----------------
            // The multiref QName is our own fake name.
            // It may be beneficial to set the name to the
            // type name, but I didn't see any improvements
            // in the interop tests.
            //if (name.equals(multirefQName) && type != null)
            //    name = type;
            ser.serialize(elemQName, attributes, value, this);
            return;
        }
        throw new IOException(Messages.getMessage("noSerializer00", value.getClass().getName(), "" + tm));
    }
    // !!! Write out a generic null, or get type info from somewhere else?
}

From source file:org.apache.axis.encoding.SerializationContext.java

/**
 * Returns the java class for serialization. 
 * If the xmlType is xsd:anyType or javaType is array or javaType is java.lang.Object
 * the java class for serialization is the class of obj.
 * If the obj is not array and the obj's class does not match with the javaType,
 * the java class for serialization is the javaType.
 * Otherwise, the java class for serialization is the obj's class.
 * // w  w w .  j a v a  2 s. co  m
 * @param xmlType    the qname of xml type
 * @param javaType   the java class from serializer 
 * @param obj        the object to serialize
 * @return the java class for serialization
 */
private Class getActualJavaClass(QName xmlType, Class javaType, Object obj) {
    Class cls = obj.getClass();

    if ((xmlType != null && Constants.isSchemaXSD(xmlType.getNamespaceURI())
            && "anyType".equals(xmlType.getLocalPart()))
            || (javaType != null && (javaType.isArray() || javaType == Object.class))) {
        return cls;
    }

    if (javaType != null && !javaType.isAssignableFrom(cls) && !cls.isArray()) {
        return javaType;
    }

    return cls;
}

From source file:org.apache.axis.encoding.SerializationContextImpl.java

/**
 * Convert QName to a string of the form <prefix>:<localpart>
 * @param qName//from  w  ww .j a  v  a 2  s. c  o  m
 * @return prefixed qname representation for serialization.
 */
public String qName2String(QName qName, boolean writeNS)
{
    String prefix = null;
    String namespaceURI = qName.getNamespaceURI();

    if (namespaceURI.length() == 0) {
        if (writeNS) {
            // If this is unqualified (i.e. prefix ""), set the default
            // namespace to ""
            String defaultNS = nsStack.getNamespaceURI("");
            if (defaultNS != null && defaultNS.length() > 0) {
                registerPrefixForURI("", "");
            }
        }
    } else {
        prefix = getPrefixForURI(namespaceURI);
    }

    if ((prefix == null) || (prefix.length() == 0))
       return qName.getLocalPart();

    StringBuffer sb = new StringBuffer(prefix);
    sb.append(':');
    sb.append(qName.getLocalPart());
    return sb.toString();
}

From source file:org.apache.axis.encoding.SerializationContextImpl.java

/**
 * Convert attribute QName to a string of the form <prefix>:<localpart>
 * There are slightly different rules for attributes:
 *  - There is no default namespace//from   ww w .  ja  v  a2  s.c o m
 *  - any attribute in a namespace must have a prefix
 *
 * @param qName QName
 * @return prefixed qname representation for serialization.
 */
public String attributeQName2String(QName qName) {
    String prefix = null;

    if (qName.getNamespaceURI().length() > 0) {
        prefix = getPrefixForURI(qName.getNamespaceURI(), null, true);
    }

    if ((prefix == null) || (prefix.length() == 0))
       return qName.getLocalPart();

    StringBuffer sb = new StringBuffer(prefix);
    sb.append(':');
    sb.append(qName.getLocalPart());
    return sb.toString();
}