Example usage for org.xml.sax.helpers AttributesImpl setAttributes

List of usage examples for org.xml.sax.helpers AttributesImpl setAttributes

Introduction

In this page you can find the example usage for org.xml.sax.helpers AttributesImpl setAttributes.

Prototype

public void setAttributes(Attributes atts) 

Source Link

Document

Copy an entire Attributes object.

Usage

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

/**
 * Serialize a JAF DataHandler quantity.
 *//* w  w w  . ja  v  a  2  s .c  o m*/
public void serialize(QName name, Attributes attributes, Object value, SerializationContext context)
        throws IOException {
    DataHandler dh = (DataHandler) value;
    //Add the attachment content to the message.
    Attachments attachments = context.getCurrentMessage().getAttachmentsImpl();

    if (attachments == null) {
        // Attachments apparently aren't supported.
        // Instead of throwing NullPointerException like
        // we used to do, throw something meaningful.
        throw new IOException(Messages.getMessage("noAttachments"));
    }
    SOAPConstants soapConstants = context.getMessageContext().getSOAPConstants();
    Part attachmentPart = attachments.createAttachmentPart(dh);

    AttributesImpl attrs = new AttributesImpl();
    if (attributes != null && 0 < attributes.getLength())
        attrs.setAttributes(attributes); //copy the existing ones.

    int typeIndex = -1;
    if ((typeIndex = attrs.getIndex(Constants.URI_DEFAULT_SCHEMA_XSI, "type")) != -1) {

        //Found a xsi:type which should not be there for attachments.
        attrs.removeAttribute(typeIndex);
    }

    if (attachments.getSendType() == Attachments.SEND_TYPE_MTOM) {
        context.setWriteXMLType(null);
        context.startElement(name, attrs);
        AttributesImpl attrs2 = new AttributesImpl();
        attrs2.addAttribute("", soapConstants.getAttrHref(), soapConstants.getAttrHref(), "CDATA",
                attachmentPart.getContentIdRef());
        context.startElement(new QName(Constants.URI_XOP_INCLUDE, Constants.ELEM_XOP_INCLUDE), attrs2);
        context.endElement();
        context.endElement();
    } else {
        boolean doTheDIME = false;
        if (attachments.getSendType() == Attachments.SEND_TYPE_DIME)
            doTheDIME = true;

        attrs.addAttribute("", soapConstants.getAttrHref(), soapConstants.getAttrHref(), "CDATA",
                doTheDIME ? attachmentPart.getContentId() : attachmentPart.getContentIdRef());

        context.startElement(name, attrs);
        context.endElement(); //There is no data to so end the element.
    }
}

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

/**
 * Serialize the indicated value as an element with the name
 * indicated by elemQName.//from w  w w  .ja va 2  s  .c  o  m
 * The attributes are additional attribute to be serialized on the element.
 * The value is the object being serialized.  (It may be serialized
 * directly or serialized as an mult-ref'd item)
 * The value is an Object, which may be a wrapped primitive.
 * The xmlType (if specified) is the QName of the type that is used to set
 * xsi:type.
 * The sendNull flag indicates whether to end an element with an xsi:nil="true" attribute for null
 * variables (if Boolean.TRUE), or nothing (if Boolean.FALSE).
 * The sendType flag indicates whether the xsi:type flag should be sent
 * (default is true).
 * @param elemQName is the QName of the element
 * @param attributes are additional attributes
 * @param value is the object to serialize
 * @param xmlType is the qname of the type or null.
 * @param javaType is the java type of the value
 * @param sendNull determines whether to send null values.
 * @param sendType determines whether to set xsi:type attribute.
 */
public void serialize(QName elemQName, Attributes attributes, Object value, QName xmlType, Class javaClass,
        Boolean sendNull, Boolean sendType) throws IOException {
    boolean sendXSITypeCache = sendXSIType;
    if (sendType != null) {
        sendXSIType = sendType.booleanValue();
    }
    boolean shouldSendType = shouldSendXSIType();

    try {
        Boolean sendNullCache = this.sendNull;
        if (sendNull != null) {
            this.sendNull = sendNull;
        } else {
            sendNull = this.sendNull;
        }

        if (value == null) {
            // If the value is null, the element is
            // passed with xsi:nil="true" to indicate that no object is present.
            if (this.sendNull.booleanValue()) {
                AttributesImpl attrs = new AttributesImpl();
                if (attributes != null && 0 < attributes.getLength())
                    attrs.setAttributes(attributes);
                if (shouldSendType)
                    attrs = (AttributesImpl) setTypeAttribute(attrs, xmlType);
                String nil = schemaVersion.getNilQName().getLocalPart();
                attrs.addAttribute(schemaVersion.getXsiURI(), nil, "xsi:" + nil, "CDATA", "true");
                startElement(elemQName, attrs);
                endElement();
            }
            this.sendNull = sendNullCache;
            return;
        }

        Message msg = getCurrentMessage();
        if (null != msg) {
            //Get attachments. returns null if no attachment support.
            Attachments attachments = getCurrentMessage().getAttachmentsImpl();

            if (null != attachments && attachments.isAttachment(value)) {
                //Attachment support and this is an object that should be treated as an attachment.

                //Allow an the attachment to do its own serialization.
                serializeActual(elemQName, attributes, value, xmlType, javaClass, sendType);

                //No need to add to mulitRefs. Attachment data stream handled by
                // the message;
                this.sendNull = sendNullCache;
                return;
            }
        }

        // If multi-reference is enabled and this object value is not a primitive
        // and we are not forcing serialization of the object, then generate
        // an element href (and store the object for subsequent outputMultiRef
        // processing).

        // NOTE : you'll notice that everywhere we register objects in the
        // multiRefValues and secondLevelObjects collections, we key them
        // using getIdentityKey(value) instead of the Object reference itself.
        // THIS IS IMPORTANT, and please make sure you understand what's
        // going on if you change any of this code.  It's this way to make
        // sure that individual Objects are serialized separately even if the
        // hashCode() and equals() methods have been overloaded to make two
        // Objects appear equal.

        if (doMultiRefs && isEncoded() && (value != forceSer) && !isPrimitive(value)) {
            if (multiRefIndex == -1)
                multiRefValues = new HashMap();

            String id;

            // Look for a multi-ref descriptor for this Object.
            MultiRefItem mri = (MultiRefItem) multiRefValues.get(getIdentityKey(value));
            if (mri == null) {
                // Didn't find one, so create one, give it a new ID, and store
                // it for next time.
                multiRefIndex++;
                id = "id" + multiRefIndex;
                mri = new MultiRefItem(id, xmlType, sendType, value);
                multiRefValues.put(getIdentityKey(value), mri);

                /**
                 * If we're SOAP 1.2, we can "inline" the serializations,
                 * so put it out now, with it's ID.
                 */
                if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
                    AttributesImpl attrs = new AttributesImpl();
                    if (attributes != null && 0 < attributes.getLength())
                        attrs.setAttributes(attributes);
                    attrs.addAttribute("", Constants.ATTR_ID, "id", "CDATA", id);
                    serializeActual(elemQName, attrs, value, xmlType, javaClass, sendType);
                    this.sendNull = sendNullCache;
                    return;
                }

                /** If we're in the middle of writing out
                 * the multi-refs, we've already cloned the list of objects
                 * and so even though we add a new one to multiRefValues,
                 * it won't get serialized this time around.
                 *
                 * To deal with this, we maintain a list of "second level"
                 * Objects - ones that need serializing as a result of
                 * serializing the first level.  When outputMultiRefs() is
                 * nearly finished, it checks to see if secondLevelObjects
                 * is empty, and if not, it goes back and loops over those
                 * Objects.  This can happen N times depending on how deep
                 * the Object graph goes.
                 */
                if (outputMultiRefsFlag) {
                    if (secondLevelObjects == null)
                        secondLevelObjects = new HashSet();
                    secondLevelObjects.add(getIdentityKey(value));
                }
            } else {
                // Found one, remember it's ID
                id = mri.id;
            }

            // Serialize an HREF to our object
            AttributesImpl attrs = new AttributesImpl();
            if (attributes != null && 0 < attributes.getLength())
                attrs.setAttributes(attributes);
            attrs.addAttribute("", soapConstants.getAttrHref(), soapConstants.getAttrHref(), "CDATA", '#' + id);

            startElement(elemQName, attrs);
            endElement();
            this.sendNull = sendNullCache;
            return;
        }

        // The forceSer variable is set by outputMultiRefs to force
        // serialization of this object via the serialize(...) call
        // below.  However, if the forced object contains a self-reference, we
        // get into an infinite loop..which is why it is set back to null
        // before the actual serialization.
        if (value == forceSer)
            forceSer = null;

        // Actually serialize the value.  (i.e. not an href like above)
        serializeActual(elemQName, attributes, value, xmlType, javaClass, sendType);
    } finally {
        sendXSIType = sendXSITypeCache;
    }
}

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

/**
 * Obtains the type attribute that should be serialized and returns the new list of Attributes
 * @param attributes of the qname//from  w w  w.  ja va  2s.c o m
 * @param type is the qname of the type
 * @return new list of Attributes
 */
public Attributes setTypeAttribute(Attributes attributes, QName type) {
    SchemaVersion schema = SchemaVersion.SCHEMA_2001;
    if (msgContext != null) {
        schema = msgContext.getSchemaVersion();
    }

    if (type == null || type.getLocalPart().indexOf(SymbolTable.ANON_TOKEN) >= 0
            || ((attributes != null) && (attributes.getIndex(schema.getXsiURI(), "type") != -1)))
        return attributes;

    AttributesImpl attrs = new AttributesImpl();
    if (attributes != null && 0 < attributes.getLength())
        attrs.setAttributes(attributes);

    String prefix = getPrefixForURI(schema.getXsiURI(), "xsi");

    attrs.addAttribute(schema.getXsiURI(), "type", prefix + ":type", "CDATA", attributeQName2String(type));
    return attrs;
}

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

/**
 * Serialize the indicated value as an element with the name
 * indicated by elemQName.//from www  . j  a  v  a 2 s .  c  o  m
 * The attributes are additional attribute to be serialized on the element.
 * The value is the object being serialized.  (It may be serialized
 * directly or serialized as an mult-ref'd item)
 * The value is an Object, which may be a wrapped primitive.
 * The xmlType (if specified) is the QName of the type that is used to set
 * xsi:type.
 * The sendNull flag indicates whether null values should be sent over the
 * wire (default is to send such values with xsi:nil="true").
 * The sendType flag indicates whether the xsi:type flag should be sent
 * (default is true).
 * @param elemQName is the QName of the element
 * @param attributes are additional attributes
 * @param value is the object to serialize
 * @param xmlType is the qname of the type or null.
 * @param sendNull determines whether to send null values.
 * @param sendType determines whether to set xsi:type attribute.
 */
public void serialize(QName elemQName,
                      Attributes attributes,
                      Object value,
                      QName xmlType,
                      boolean sendNull,
                      Boolean sendType)
    throws IOException
{
    boolean shouldSendType = (sendType == null) ? shouldSendXSIType() :
        sendType.booleanValue();

    if (value == null) {
        // If the value is null, the element is
        // passed with xsi:nil="true" to indicate that no object is present.
        if (sendNull) {
            AttributesImpl attrs = new AttributesImpl();
            if (attributes != null && 0 < attributes.getLength())
                attrs.setAttributes(attributes);
            if (shouldSendType)
                attrs = (AttributesImpl) setTypeAttribute(attrs, xmlType);
            String nil = schemaVersion.getNilQName().getLocalPart();
            attrs.addAttribute(schemaVersion.getXsiURI(), nil, "xsi:" + nil,
                               "CDATA", "true");
            startElement(elemQName, attrs);
            endElement();
        }
        return;
    }

    Message msg= getCurrentMessage();
    if(null != msg){
        //Get attachments. returns null if no attachment support.
        Attachments attachments= getCurrentMessage().getAttachmentsImpl();

        if( null != attachments && attachments.isAttachment(value)){
            //Attachment support and this is an object that should be treated as an attachment.

            //Allow an the attachment to do its own serialization.
            serializeActual(elemQName, attributes, value,
                            xmlType, sendType);

            //No need to add to mulitRefs. Attachment data stream handled by
            // the message;
            return;
        }
    }

    // If multi-reference is enabled and this object value is not a primitive
    // and we are not forcing serialization of the object, then generate
    // an element href (and store the object for subsequent outputMultiRef
    // processing).

    // NOTE : you'll notice that everywhere we register objects in the
    // multiRefValues and secondLevelObjects collections, we key them
    // using getIdentityKey(value) instead of the Object reference itself.
    // THIS IS IMPORTANT, and please make sure you understand what's
    // going on if you change any of this code.  It's this way to make
    // sure that individual Objects are serialized separately even if the
    // hashCode() and equals() methods have been overloaded to make two
    // Objects appear equal.

    if (doMultiRefs && (msgContext == null || msgContext.isEncoded()) &&
            (value != forceSer) && !isPrimitive(value)) {
        if (multiRefIndex == -1)
            multiRefValues = new HashMap();

        String id;

        // Look for a multi-ref descriptor for this Object.
        MultiRefItem mri = (MultiRefItem)multiRefValues.get(
                                                    getIdentityKey(value));
        if (mri == null) {
            // Didn't find one, so create one, give it a new ID, and store
            // it for next time.
            multiRefIndex++;
            id = "id" + multiRefIndex;
            mri = new MultiRefItem (id, xmlType, sendType, value);
            multiRefValues.put(getIdentityKey(value), mri);

            /**
             * If we're SOAP 1.2, we can "inline" the serializations,
             * so put it out now, with it's ID.
             */
            if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
                AttributesImpl attrs = new AttributesImpl();
                if (attributes != null && 0 < attributes.getLength())
                    attrs.setAttributes(attributes);
                attrs.addAttribute("", Constants.ATTR_ID, "id", "CDATA",
                                   id);
                serializeActual(elemQName, attrs, value, xmlType, sendType);
                return;
            }


            /** If we're in the middle of writing out
             * the multi-refs, we've already cloned the list of objects
             * and so even though we add a new one to multiRefValues,
             * it won't get serialized this time around.
             *
             * To deal with this, we maintain a list of "second level"
             * Objects - ones that need serializing as a result of
             * serializing the first level.  When outputMultiRefs() is
             * nearly finished, it checks to see if secondLevelObjects
             * is empty, and if not, it goes back and loops over those
             * Objects.  This can happen N times depending on how deep
             * the Object graph goes.
             */
            if (outputMultiRefsFlag) {
                if (secondLevelObjects == null)
                    secondLevelObjects = new HashSet();
                secondLevelObjects.add(getIdentityKey(value));
            }
        } else {
            // Found one, remember it's ID
            id = mri.id;
        }

        // Serialize an HREF to our object
        AttributesImpl attrs = new AttributesImpl();
        if (attributes != null && 0 < attributes.getLength())
            attrs.setAttributes(attributes);
        attrs.addAttribute("", soapConstants.getAttrHref(), soapConstants.getAttrHref(),
                           "CDATA", '#' + id);

        startElement(elemQName, attrs);
        endElement();
        return;
    }

    // The forceSer variable is set by outputMultiRefs to force
    // serialization of this object via the serialize(...) call
    // below.  However, if the forced object contains a self-reference, we
    // get into an infinite loop..which is why it is set back to null
    // before the actual serialization.
    if (value == forceSer)
        forceSer = null;

    // Actually serialize the value.  (i.e. not an href like above)
    serializeActual(elemQName, attributes, value, xmlType, sendType);
}

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

/**
 * Obtains the type attribute that should be serialized and returns the new list of Attributes
 * @param attributes of the qname//  w  ww  .ja v a 2 s  .c  o  m
 * @param type is the qname of the type
 * @return new list of Attributes
 */
public Attributes setTypeAttribute(Attributes attributes, QName type)
{
    if (type == null ||
         type.getLocalPart().indexOf(SymbolTable.ANON_TOKEN) >= 0 ||
        ((attributes != null) &&
         (attributes.getIndex(Constants.URI_DEFAULT_SCHEMA_XSI,
                            "type") != -1)))
        return attributes;

    AttributesImpl attrs = new AttributesImpl();
    if (attributes != null && 0 < attributes.getLength() )
        attrs.setAttributes(attributes);

    String prefix = getPrefixForURI(Constants.URI_DEFAULT_SCHEMA_XSI,
                                       "xsi");

    attrs.addAttribute(Constants.URI_DEFAULT_SCHEMA_XSI,
                       "type",
                       prefix + ":type",
                       "CDATA", attributeQName2String(type));
    return attrs;
}