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

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

Introduction

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

Prototype

public void addAttribute(String uri, String localName, String qName, String type, String value) 

Source Link

Document

Add an attribute to the end of the list.

Usage

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

/**
 * Serialize the indicated value as an element with the name
 * indicated by elemQName./*from  w  w  w  .  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

/**
 * The serialize method uses hrefs to reference all non-primitive
 * values.  These values are stored and serialized by calling
 * outputMultiRefs after the serialize method completes.
 *///ww w. j ava2s.c  o  m
public void outputMultiRefs() throws IOException
{
    if (!doMultiRefs || (multiRefValues == null) ||
            soapConstants == SOAPConstants.SOAP12_CONSTANTS)
        return;
    outputMultiRefsFlag = true;
    AttributesImpl attrs = new AttributesImpl();
    attrs.addAttribute("","","","","");

    String encodingURI = soapConstants.getEncodingURI();
    // explicitly state that this attribute is not a root
    String prefix = getPrefixForURI(encodingURI);
    String root = prefix + ":root";
    attrs.addAttribute(encodingURI, Constants.ATTR_ROOT, root,
                       "CDATA", "0");

    // Make sure we put the encodingStyle on each multiref element we
    // output.
    String encodingStyle;
    if (msgContext != null) {
        encodingStyle = msgContext.getEncodingStyle();
    } else {
        encodingStyle = soapConstants.getEncodingURI();
    }
    String encStyle = getPrefixForURI(soapConstants.getEnvelopeURI()) +
                                      ':' + Constants.ATTR_ENCODING_STYLE;
    attrs.addAttribute(soapConstants.getEnvelopeURI(),
                       Constants.ATTR_ENCODING_STYLE,
                       encStyle,
                       "CDATA",
                       encodingStyle);

    // Make a copy of the keySet because it could be updated
    // during processing
    HashSet keys = new HashSet();
    keys.addAll(multiRefValues.keySet());
    Iterator i = keys.iterator();
    while (i.hasNext()) {
        while (i.hasNext()) {
            Object val = i.next();
            MultiRefItem mri = (MultiRefItem) multiRefValues.get(val);
            attrs.setAttribute(0, "", Constants.ATTR_ID, "id", "CDATA",
                               mri.id);

            forceSer = mri.value;

            // Now serialize the value.
            // The sendType parameter is defaulted for interop purposes.
            // Some of the remote services do not know how to
            // ascertain the type in these circumstances (though Axis does).
            serialize(multirefQName, attrs, mri.value,
                      mri.xmlType,
                      true,
                      Boolean.TRUE);   // mri.sendType
        }

        // Done processing the iterated values.  During the serialization
        // of the values, we may have run into new nested values.  These
        // were placed in the secondLevelObjects map, which we will now
        // process by changing the iterator to locate these values.
        if (secondLevelObjects != null) {
            i = secondLevelObjects.iterator();
            secondLevelObjects = null;
        }
    }

    // Reset maps and flags
    forceSer = null;
    outputMultiRefsFlag = false;
    multiRefValues = null;
    multiRefIndex = -1;
    secondLevelObjects = null;
}

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

/**
 * Output a DOM representation to a SerializationContext
 * @param el is a DOM Element//from   w  w w.  j  a  v a  2 s.  c  o m
 */
public void writeDOMElement(Element el)
    throws IOException
{
    AttributesImpl attributes = null;
    NamedNodeMap attrMap = el.getAttributes();

    if (attrMap.getLength() > 0) {
        attributes = new AttributesImpl();
        for (int i = 0; i < attrMap.getLength(); i++) {
            Attr attr = (Attr)attrMap.item(i);
            String tmp = attr.getNamespaceURI();
            if ( tmp != null && tmp.equals(Constants.NS_URI_XMLNS) ) {
                String prefix = attr.getLocalName();
                if (prefix != null) {
                    if (prefix.equals("xmlns"))
                        prefix = "";
                    String nsURI = attr.getValue();
                    registerPrefixForURI(prefix, nsURI);
                }
                continue;
            }

            attributes.addAttribute(attr.getNamespaceURI(),
                                    attr.getLocalName(),
                                    attr.getName(),
                                    "CDATA", attr.getValue());
        }
    }

    String namespaceURI = el.getNamespaceURI();
    String localPart = el.getLocalName();
    if(namespaceURI == null || namespaceURI.length()==0)
        localPart = el.getNodeName();
    QName qName = new QName(namespaceURI, localPart);

    startElement(qName, attributes);

    NodeList children = el.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child instanceof Element) {
            writeDOMElement((Element)child);
        } else if (child instanceof CDATASection) {
            writeString("<![CDATA[");
            writeString(((Text)child).getData());
            writeString("]]>");
        } else if (child instanceof Comment) {
            writeString("<!--");
            writeString(((CharacterData)child).getData());
            writeString("-->");
        } else if (child instanceof Text) {
            writeSafeString(((Text)child).getData());
        }
    }

    endElement();
}

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//from w w w.  j av  a2 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;
}

From source file:org.apache.axis.message.MessageElement.java

/**
 * Obtain an Attributes collection consisting of all attributes
 * for this MessageElement, including namespace declarations.
 *
 * @return Attributes collection/*from   w w w  .  ja  va 2s .  c  o m*/
 */
public Attributes getCompleteAttributes() {
    if (namespaces == null) {
        return attributes;
    }

    AttributesImpl attrs = null;
    if (attributes == NullAttributes.singleton) {
        attrs = new AttributesImpl();
    } else {
        attrs = new AttributesImpl(attributes);
    }

    for (Iterator iterator = namespaces.iterator(); iterator.hasNext();) {
        Mapping mapping = (Mapping) iterator.next();
        String prefix = mapping.getPrefix();
        String nsURI = mapping.getNamespaceURI();
        attrs.addAttribute(Constants.NS_URI_XMLNS, prefix, "xmlns:" + prefix, nsURI, "CDATA");
    }
    return attrs;
}

From source file:org.apache.axis.message.MessageElement.java

/**
 * add a normal CDATA/text attribute.//from  ww  w .  j a  va2  s.  co  m
 * There is no check whether or not the attribute already exists.
 * @param namespace namespace URI
 * @param localName local anme
 * @param value value
 */
public void addAttribute(String namespace, String localName, String value) {
    AttributesImpl attributes = makeAttributesEditable();
    attributes.addAttribute(namespace, localName, "", "CDATA", value);
}

From source file:org.apache.axis.message.MessageElement.java

/**
 * add an attribute./*from   w  w  w.  jav a 2  s  . c  o  m*/
 * Note that the prefix is not added to our mapping list.
 * Also, there is no check whether or not the attribute already exists.
 * @param attrPrefix prefix.
 * @param namespace namespace URI
 * @param localName
 * @param value
 */
public void addAttribute(String attrPrefix, String namespace, String localName, String value) {
    AttributesImpl attributes = makeAttributesEditable();
    String attrName = localName;
    if (attrPrefix != null && attrPrefix.length() > 0) {
        attrName = attrPrefix + ":" + localName;
    }
    attributes.addAttribute(namespace, localName, attrName, "CDATA", value);
}

From source file:org.apache.axis.message.MessageElement.java

/**
 * remove a named attribute./*from ww  w  .ja  v  a2 s  .c  o  m*/
 * @see org.w3c.dom.Element#removeAttribute(String)
 * @param attrName name of the attributes
 * @throws DOMException
 */
public void removeAttribute(String attrName) throws DOMException {
    AttributesImpl impl = (AttributesImpl) attributes;
    int index = impl.getIndex(attrName);
    if (index >= 0) {
        AttributesImpl newAttrs = new AttributesImpl();
        // copy except the removed attribute
        for (int i = 0; i < impl.getLength(); i++) { // shift after removal
            if (i != index) {
                String uri = impl.getURI(i);
                String local = impl.getLocalName(i);
                String qname = impl.getQName(i);
                String type = impl.getType(i);
                String value = impl.getValue(i);
                newAttrs.addAttribute(uri, local, qname, type, value);
            }
        }
        // replace it
        attributes = newAttrs;
    }
}

From source file:org.apache.axis.message.MessageElement.java

/**
 * set or update an attribute.// www  .j  a va2s. c  om
 * @see org.w3c.dom.Element#setAttribute(String, String)
 * @param name attribute name
 * @param value attribute value
 * @throws DOMException
 */
public void setAttribute(String name, String value) throws DOMException {
    AttributesImpl impl = makeAttributesEditable();
    int index = impl.getIndex(name);
    if (index < 0) { // not found
        String uri = "";
        String localname = name;
        String qname = name;
        String type = "CDDATA";
        impl.addAttribute(uri, localname, qname, type, value);
    } else { // found
        impl.setLocalName(index, value);
    }
}

From source file:org.apache.axis.message.MessageElement.java

/**
 * set an attribute as a node/*from  w  w  w .  ja v a 2 s . co  m*/
 * @see org.w3c.dom.Element#setAttributeNodeNS(org.w3c.dom.Attr)
 * @todo implement properly.
 * @param newAttr
 * @return null
 * @throws DOMException
 */
public Attr setAttributeNodeNS(Attr newAttr) throws DOMException {
    //attributes.
    AttributesImpl attributes = makeAttributesEditable();
    // how to convert to DOM ATTR
    attributes.addAttribute(newAttr.getNamespaceURI(), newAttr.getLocalName(), newAttr.getLocalName(), "CDATA",
            newAttr.getValue());
    return null;
}