List of usage examples for org.xml.sax.helpers AttributesImpl setAttribute
public void setAttribute(int index, String uri, String localName, String qName, String type, String value)
From source file:org.apache.axis.encoding.ser.ArraySerializer.java
/** * Serialize an element that is an array. * @param name is the element name/*from w w w . ja v a 2s . c o m*/ * @param attributes are the attributes...serialize is free to add more. * @param value is the value * @param context is the SerializationContext */ public void serialize(QName name, Attributes attributes, Object value, SerializationContext context) throws IOException { if (value == null) throw new IOException(Messages.getMessage("cantDoNullArray00")); MessageContext msgContext = context.getMessageContext(); SchemaVersion schema = SchemaVersion.SCHEMA_2001; SOAPConstants soap = SOAPConstants.SOAP11_CONSTANTS; boolean encoded = context.isEncoded(); if (msgContext != null) { schema = msgContext.getSchemaVersion(); soap = msgContext.getSOAPConstants(); } Class cls = value.getClass(); Collection list = null; if (!cls.isArray()) { if (!(value instanceof Collection)) { throw new IOException(Messages.getMessage("cantSerialize00", cls.getName())); } list = (Collection) value; } // Get the componentType of the array/list Class componentClass; if (list == null) { componentClass = cls.getComponentType(); } else { componentClass = Object.class; } // Get the QName of the componentType // if it wasn't passed in from the constructor QName componentTypeQName = this.componentType; // Check to see if componentType is also an array. // If so, set the componentType to the most nested non-array // componentType. Increase the dims string by "[]" // each time through the loop. // Note from Rich Scheuerle: // This won't handle Lists of Lists or // arrays of Lists....only arrays of arrays. String dims = ""; if (componentTypeQName != null) { // if we have a Type QName at this point, // this is because ArraySerializer has been instanciated with it TypeMapping tm = context.getTypeMapping(); SerializerFactory factory = (SerializerFactory) tm.getSerializer(componentClass, componentTypeQName); while (componentClass.isArray() && factory instanceof ArraySerializerFactory) { ArraySerializerFactory asf = (ArraySerializerFactory) factory; componentClass = componentClass.getComponentType(); QName componentType = null; if (asf.getComponentType() != null) { componentType = asf.getComponentType(); if (encoded) { componentTypeQName = componentType; } } // update factory with the new values factory = (SerializerFactory) tm.getSerializer(componentClass, componentType); if (soap == SOAPConstants.SOAP12_CONSTANTS) dims += "* "; else dims += "[]"; } } else { // compatibility mode while (componentClass.isArray()) { componentClass = componentClass.getComponentType(); if (soap == SOAPConstants.SOAP12_CONSTANTS) dims += "* "; else dims += "[]"; } } // Try the current XML type from the context if (componentTypeQName == null) { componentTypeQName = context.getCurrentXMLType(); if (componentTypeQName != null) { if ((componentTypeQName.equals(xmlType) || componentTypeQName.equals(Constants.XSD_ANYTYPE) || componentTypeQName.equals(soap.getArrayType()))) { componentTypeQName = null; } } } if (componentTypeQName == null) { componentTypeQName = context.getItemType(); } // Then check the type mapping for the class if (componentTypeQName == null) { componentTypeQName = context.getQNameForClass(componentClass); } // If still not found, look at the super classes if (componentTypeQName == null) { Class searchCls = componentClass; while (searchCls != null && componentTypeQName == null) { searchCls = searchCls.getSuperclass(); componentTypeQName = context.getQNameForClass(searchCls); } if (componentTypeQName != null) { componentClass = searchCls; } } // Still can't find it? Throw an error. if (componentTypeQName == null) { throw new IOException(Messages.getMessage("noType00", componentClass.getName())); } int len = (list == null) ? Array.getLength(value) : list.size(); String arrayType = ""; int dim2Len = -1; if (encoded) { if (soap == SOAPConstants.SOAP12_CONSTANTS) { arrayType = dims + len; } else { arrayType = dims + "[" + len + "]"; } // Discover whether array can be serialized directly as a two-dimensional // array (i.e. arrayType=int[2,3]) versus an array of arrays. // Benefits: // - Less text passed on the wire. // - Easier to read wire format // - Tests the deserialization of multi-dimensional arrays. // Drawbacks: // - Is not safe! It is possible that the arrays are multiply // referenced. Transforming into a 2-dim array will cause the // multi-referenced information to be lost. Plus there is no // way to determine whether the arrays are multi-referenced. // - .NET currently (Dec 2002) does not support 2D SOAP-encoded arrays // // OLD Comment as to why this was ENABLED: // It is necessary for // interoperability (echo2DStringArray). It is 'safe' for now // because Axis treats arrays as non multi-ref (see the note // in SerializationContext.isPrimitive(...) ) // More complicated processing is necessary for 3-dim arrays, etc. // // Axis 1.1 - December 2002 // Turned this OFF because Microsoft .NET can not deserialize // multi-dimensional SOAP-encoded arrays, and this interopability // is pretty high visibility. Make it a global configuration parameter: // <parameter name="enable2DArrayEncoding" value="true"/> (tomj) // // Check the message context to see if we should turn 2D processing ON // Default is OFF boolean enable2Dim = false; // Vidyanand : added this check if (msgContext != null) { enable2Dim = JavaUtils .isTrueExplicitly(msgContext.getProperty(AxisEngine.PROP_TWOD_ARRAY_ENCODING)); } if (enable2Dim && !dims.equals("")) { if (cls.isArray() && len > 0) { boolean okay = true; // Make sure all of the component arrays are the same size for (int i = 0; i < len && okay; i++) { Object elementValue = Array.get(value, i); if (elementValue == null) okay = false; else if (dim2Len < 0) { dim2Len = Array.getLength(elementValue); if (dim2Len <= 0) { okay = false; } } else if (dim2Len != Array.getLength(elementValue)) { okay = false; } } // Update the arrayType to use mult-dim array encoding if (okay) { dims = dims.substring(0, dims.length() - 2); if (soap == SOAPConstants.SOAP12_CONSTANTS) arrayType = dims + len + " " + dim2Len; else arrayType = dims + "[" + len + "," + dim2Len + "]"; } else { dim2Len = -1; } } } } // Need to distinguish if this is array processing for an // actual schema array or for a maxOccurs usage. // For the maxOccurs case, the currentXMLType of the context is // the same as the componentTypeQName. QName itemQName = context.getItemQName(); boolean maxOccursUsage = !encoded && itemQName == null && componentTypeQName.equals(context.getCurrentXMLType()); if (encoded) { AttributesImpl attrs; if (attributes == null) { attrs = new AttributesImpl(); } else if (attributes instanceof AttributesImpl) { attrs = (AttributesImpl) attributes; } else { attrs = new AttributesImpl(attributes); } String compType = context.attributeQName2String(componentTypeQName); if (attrs.getIndex(soap.getEncodingURI(), soap.getAttrItemType()) == -1) { String encprefix = context.getPrefixForURI(soap.getEncodingURI()); if (soap != SOAPConstants.SOAP12_CONSTANTS) { compType = compType + arrayType; attrs.addAttribute(soap.getEncodingURI(), soap.getAttrItemType(), encprefix + ":arrayType", "CDATA", compType); } else { attrs.addAttribute(soap.getEncodingURI(), soap.getAttrItemType(), encprefix + ":itemType", "CDATA", compType); attrs.addAttribute(soap.getEncodingURI(), "arraySize", encprefix + ":arraySize", "CDATA", arrayType); } } // Force type to be SOAP_ARRAY for all array serialization. // // There are two choices here: // Force the type to type=SOAP_ARRAY // Pros: More interop test successes. // Cons: Since we have specific type information it // is more correct to use it. Plus the specific // type information may be important on the // server side to disambiguate overloaded operations. // Use the specific type information: // Pros: The specific type information is more correct // and may be useful for operation overloading. // Cons: More interop test failures (as of 2/6/2002). // String qname = context.getPrefixForURI(schema.getXsiURI(), "xsi") + ":type"; QName soapArray; if (soap == SOAPConstants.SOAP12_CONSTANTS) { soapArray = Constants.SOAP_ARRAY12; } else { soapArray = Constants.SOAP_ARRAY; } int typeI = attrs.getIndex(schema.getXsiURI(), "type"); if (typeI != -1) { attrs.setAttribute(typeI, schema.getXsiURI(), "type", qname, "CDATA", context.qName2String(soapArray)); } else { attrs.addAttribute(schema.getXsiURI(), "type", qname, "CDATA", context.qName2String(soapArray)); } attributes = attrs; } // For the maxOccurs case, each item is named with the QName // we got in the arguments. For normal array case, we write an element with // that QName, and then serialize each item as <item> QName elementName = name; Attributes serializeAttr = attributes; if (!maxOccursUsage) { serializeAttr = null; // since we are putting them here context.startElement(name, attributes); if (itemQName != null) elementName = itemQName; else if (componentQName != null) elementName = componentQName; } if (dim2Len < 0) { // Normal case, serialize each array element if (list == null) { for (int index = 0; index < len; index++) { Object aValue = Array.get(value, index); // Serialize the element. context.serialize(elementName, (serializeAttr == null ? serializeAttr : new AttributesImpl(serializeAttr)), aValue, componentTypeQName, componentClass); // prefered type QName } } else { for (Iterator iterator = list.iterator(); iterator.hasNext();) { Object aValue = iterator.next(); // Serialize the element. context.serialize(elementName, (serializeAttr == null ? serializeAttr : new AttributesImpl(serializeAttr)), aValue, componentTypeQName, componentClass); // prefered type QName } } } else { // Serialize as a 2 dimensional array for (int index = 0; index < len; index++) { for (int index2 = 0; index2 < dim2Len; index2++) { Object aValue = Array.get(Array.get(value, index), index2); context.serialize(elementName, null, aValue, componentTypeQName, componentClass); } } } if (!maxOccursUsage) context.endElement(); }
From source file:org.apache.axis.encoding.SerializationContext.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. *//* w ww.j a v a 2 s . c om*/ 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()) { AttributesImpl attrs2 = new AttributesImpl(attrs); Object val = i.next(); MultiRefItem mri = (MultiRefItem) multiRefValues.get(val); attrs2.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, attrs2, mri.value, mri.xmlType, null, this.sendNull, 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
/** * 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. *//*from w w w .ja va2s .c om*/ 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.fop.render.svg.EmbeddedSVGImageHandler.java
/** {@inheritDoc} */ public void handleImage(RenderingContext context, Image image, final Rectangle pos) throws IOException { SVGRenderingContext svgContext = (SVGRenderingContext) context; ImageXMLDOM svg = (ImageXMLDOM) image; ContentHandler handler = svgContext.getContentHandler(); AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "x", "x", CDATA, SVGUtil.formatMptToPt(pos.x)); atts.addAttribute("", "y", "y", CDATA, SVGUtil.formatMptToPt(pos.y)); atts.addAttribute("", "width", "width", CDATA, SVGUtil.formatMptToPt(pos.width)); atts.addAttribute("", "height", "height", CDATA, SVGUtil.formatMptToPt(pos.height)); try {//from w w w . j av a2 s .c om Document doc = (Document) svg.getDocument(); Element svgEl = (Element) doc.getDocumentElement(); if (svgEl.getAttribute("viewBox").length() == 0) { log.warn("SVG doesn't have a viewBox. The result might not be scaled correctly!"); } TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource src = new DOMSource(svg.getDocument()); SAXResult res = new SAXResult(new DelegatingFragmentContentHandler(handler) { private boolean topLevelSVGFound = false; private void setAttribute(AttributesImpl atts, String localName, String value) { int index; index = atts.getIndex("", localName); if (index < 0) { atts.addAttribute("", localName, localName, CDATA, value); } else { atts.setAttribute(index, "", localName, localName, CDATA, value); } } public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException { if (!topLevelSVGFound && SVG_ELEMENT.getNamespaceURI().equals(uri) && SVG_ELEMENT.getLocalName().equals(localName)) { topLevelSVGFound = true; AttributesImpl modAtts = new AttributesImpl(atts); setAttribute(modAtts, "x", SVGUtil.formatMptToPt(pos.x)); setAttribute(modAtts, "y", SVGUtil.formatMptToPt(pos.y)); setAttribute(modAtts, "width", SVGUtil.formatMptToPt(pos.width)); setAttribute(modAtts, "height", SVGUtil.formatMptToPt(pos.height)); super.startElement(uri, localName, name, modAtts); } else { super.startElement(uri, localName, name, atts); } } }); transformer.transform(src, res); } catch (TransformerException te) { throw new IOException(te.getMessage()); } }
From source file:org.dita.dost.util.XMLUtils.java
/** * Add or set attribute./*w ww. java2s . com*/ * * @param atts attributes * @param uri namespace URI * @param localName local name * @param qName qualified name * @param type attribute type * @param value attribute value */ public static void addOrSetAttribute(final AttributesImpl atts, final String uri, final String localName, final String qName, final String type, final String value) { final int i = atts.getIndex(qName); if (i != -1) { atts.setAttribute(i, uri, localName, qName, type, value); } else { atts.addAttribute(uri, localName, qName, type, value); } }
From source file:org.tizzit.util.xml.SAXHelper.java
/** * Sets an attribute of an SAX AttributesImpl to a specific value (namespace supported).<br/> * If it exists, the value will be overwritten. * * @param attr The AttributesImpl//w w w . j a v a 2 s.c o m * @param namespaceUri * @param attrName The name * @param attrValue The (new) value * @since tizzit-common 15.10.2009 */ public static void setSAXAttr(AttributesImpl attr, String namespaceUri, String attrName, String attrValue) { if (attrName != null) { int idx = attr.getIndex(attrName); String saveVal = (attrValue == null) ? "" : attrValue; if (idx >= 0) { String dd = attr.getType(attrName); attr.setAttribute(idx, namespaceUri, attrName, attrName, dd, saveVal); } else { attr.addAttribute(namespaceUri, attrName, attrName, "CDATA", saveVal); } } }