Return-Path: Delivered-To: apmail-xml-axis-dev-archive@xml.apache.org Received: (qmail 92407 invoked by uid 500); 3 Oct 2001 21:11:14 -0000 Mailing-List: contact axis-dev-help@xml.apache.org; run by ezmlm Precedence: bulk Reply-To: axis-dev@xml.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list axis-dev@xml.apache.org Received: (qmail 92397 invoked by uid 500); 3 Oct 2001 21:11:14 -0000 Delivered-To: apmail-xml-axis-cvs@apache.org Date: 3 Oct 2001 21:08:46 -0000 Message-ID: <20011003210846.14900.qmail@icarus.apache.org> From: gdaniels@apache.org To: xml-axis-cvs@apache.org Subject: cvs commit: xml-axis/java/src/org/apache/axis/encoding SerializationContext.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N gdaniels 01/10/03 14:08:46 Modified: java/src/org/apache/axis/encoding SerializationContext.java Log: Be smarter about writing attributes. This is a little complex, but seems like the right direction. There is a likely performance hit associated with this code, so we should look into tightening it up if we can. Basically, we now verify attributes which were set by the user. If I set an attribute "foo:bar" on some MessageElement, the SerializationContext will ensure that "foo" is mapped correctly to the right namespace. If no qname is specified in the attribute, the system will automatically use any prefix that maps to the attribute's namespace, or generate a new one if it hasn't been mapped yet. !!! TODO : Add a "dirty" bit which disables this check for untouched elements which were generated by a parse. Revision Changes Path 1.38 +81 -55 xml-axis/java/src/org/apache/axis/encoding/SerializationContext.java Index: SerializationContext.java =================================================================== RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/encoding/SerializationContext.java,v retrieving revision 1.37 retrieving revision 1.38 diff -u -r1.37 -r1.38 --- SerializationContext.java 2001/10/03 15:30:04 1.37 +++ SerializationContext.java 2001/10/03 21:08:46 1.38 @@ -90,24 +90,24 @@ public class SerializationContext { private static final boolean DEBUG_LOG = false; - + public NSStack nsStack = new NSStack(); - + boolean writingStartTag = false; boolean onlyXML = true; int indent=0; boolean startOfDocument = true; - + Stack elementStack = new Stack(); Writer writer; - + int lastPrefixIndex = 1; - + private MessageContext msgContext; - + /** * Should the XML be "pretty" printed on serialization? If false, the - * XML will be sent out verbatim. If true, ignorable white space may be + * XML will be sent out verbatim. If true, ignorable white space may be * inserted or removed. */ private boolean pretty = false; @@ -129,7 +129,7 @@ * serializations of identical objects). */ private boolean doMultiRefs = false; - + /** * Should I send an XML declaration? */ @@ -146,7 +146,7 @@ */ private HashMap multiRefValues = null; private int multiRefIndex = -1; - + /** * These two let us deal with multi-level object graphs for multi-ref * serialization. Each time around the serialization loop, we'll fill @@ -155,7 +155,7 @@ */ private Object currentSer = null; private HashSet secondLevelObjects = null; - + public SerializationContext(Writer writer, MessageContext msgContext) { this.writer = writer; @@ -176,12 +176,12 @@ sendXSIType = sd.getSendTypeAttr(); } } - + public void setSendDecl(boolean sendDecl) { sendXMLDecl = sendDecl; } - + public boolean shouldSendXSIType() { return sendXSIType; } @@ -190,34 +190,34 @@ { return msgContext.getServiceDescription(); } - + public TypeMappingRegistry getTypeMappingRegistry() { return msgContext.getTypeMappingRegistry(); } - + public String getPrefixForURI(String uri) { return getPrefixForURI(uri, "ns" + lastPrefixIndex++); } - + public String getPrefixForURI(String uri, String defaultPrefix) { if ((uri == null) || (uri.equals(""))) return null; - + String prefix = nsStack.getPrefix(uri); - + if (prefix == null && uri.equals(Constants.URI_SOAP_ENC)) { prefix = Constants.NSPREFIX_SOAP_ENC; registerPrefixForURI(prefix, uri); } - + if (prefix == null) { prefix = defaultPrefix; registerPrefixForURI(prefix, uri); } - + return prefix; } @@ -226,29 +226,29 @@ if (DEBUG_LOG) { System.out.println("register '" + prefix + "' - '" + uri + "'"); } - + if ((uri != null) && (prefix != null)) { nsStack.add(uri, prefix); } } - + public void endPrefix(String prefix) { // Do we need to do anything here? } - + public String qName2String(QName qName) { String prefix = getPrefixForURI(qName.getNamespaceURI()); return (((prefix != null)&&(!prefix.equals(""))) ? prefix + ":" : "") + qName.getLocalPart(); } - + public QName getQNameForClass(Class cls) { return getTypeMappingRegistry().getTypeQName(cls); } - + /** * Classes which are known to not require multi-ref. As multi-ref * requires additional parsing overhead and not all implementations @@ -269,7 +269,7 @@ if (type.isPrimitive()) return true; return false; } - + public void serialize(QName qName, Attributes attributes, Object value) throws IOException { @@ -282,17 +282,17 @@ startElement(qName, attrs); endElement(); } - + if (doMultiRefs && (value != currentSer) && !isPrimitive(value)) { if (multiRefIndex == -1) multiRefValues = new HashMap(); - + String href = (String)multiRefValues.get(value); if (href == null) { multiRefIndex++; href = "id" + multiRefIndex; multiRefValues.put(value, href); - + /** Problem - if we're in the middle of writing out * the multi-refs and hit another level of the * object graph, we need to make sure this object @@ -306,29 +306,29 @@ secondLevelObjects.add(value); } } - + AttributesImpl attrs = new AttributesImpl(); if (attributes != null) attrs.setAttributes(attributes); attrs.addAttribute("", Constants.ATTR_HREF, "href", "CDATA", "#" + href); - + startElement(qName, attrs); endElement(); return; } - + getTypeMappingRegistry().serialize(qName, attributes, value, this); } - + public void outputMultiRefs() throws IOException { if (!doMultiRefs || (multiRefValues == null)) return; - + AttributesImpl attrs = new AttributesImpl(); attrs.addAttribute("","","","",""); - + Iterator i = ((HashMap)multiRefValues.clone()).keySet().iterator(); while (i.hasNext()) { while (i.hasNext()) { @@ -339,7 +339,7 @@ currentSer = val; serialize(new QName("","multiRef"), attrs, val); } - + if (secondLevelObjects != null) { i = secondLevelObjects.iterator(); secondLevelObjects = null; @@ -347,7 +347,7 @@ } currentSer = null; } - + public void startElement(QName qName, Attributes attributes) throws IOException { @@ -359,29 +359,55 @@ writer.write("\n"); startOfDocument = false; } - + if (writingStartTag) { writer.write(">"); if (pretty) writer.write("\n"); indent++; } - + if (pretty) for (int i=0; i -1) { + prefix = qname.substring(0, idx); + prefix = getPrefixForURI(uri, + prefix); + } + } + if (!prefix.equals("")) { + qname = prefix + ":" + attributes.getLocalName(i); + } else { + qname = attributes.getLocalName(i); + } + } else { + qname = attributes.getLocalName(i); + } + + writer.write(qname); writer.write("=\""); writer.write(attributes.getValue(i)); writer.write("\""); } } - + ArrayList currentMappings = nsStack.peek(); for (int i = 0; i < currentMappings.size(); i++) { Mapping map = (Mapping)currentMappings.get(i); @@ -396,23 +422,23 @@ } writingStartTag = true; - + elementStack.push(elementQName); nsStack.push(); writer.flush(); onlyXML=true; } - + public void endElement() throws IOException { String elementQName = (String)elementStack.pop(); - + if (DEBUG_LOG) { System.out.println("Out: Ending element " + elementQName); } - + nsStack.pop(); nsStack.peek().clear(); @@ -422,7 +448,7 @@ writingStartTag = false; return; } - + if (onlyXML) { indent--; if (pretty) for (int i=0; i 0) { attributes = new AttributesImpl(); for (int i = 0; i < attrMap.getLength(); i++) { Attr attr = (Attr)attrMap.item(i); - + attributes.addAttribute(attr.getNamespaceURI(), attr.getName(), attr.getName(), "CDATA", attr.getValue()); } } - + QName qName = new QName(el.getNamespaceURI(), el.getTagName()); - + startElement(qName, attributes); - + NodeList children = el.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); @@ -498,8 +524,8 @@ writeString(((Text)child).getData()); } } - + endElement(); } - + }