jaxws/src/java.xml.bind/share/classes/com/sun/xml/internal/bind/v2/runtime/ContentHandlerAdaptor.java
changeset 27837 86d4f46e622a
parent 26775 810bea25280c
equal deleted inserted replaced
27581:9fffb959eb41 27837:86d4f46e622a
    25 
    25 
    26 package com.sun.xml.internal.bind.v2.runtime;
    26 package com.sun.xml.internal.bind.v2.runtime;
    27 
    27 
    28 import com.sun.istack.internal.FinalArrayList;
    28 import com.sun.istack.internal.FinalArrayList;
    29 import com.sun.istack.internal.SAXException2;
    29 import com.sun.istack.internal.SAXException2;
    30 
       
    31 import org.xml.sax.Attributes;
    30 import org.xml.sax.Attributes;
    32 import org.xml.sax.SAXException;
    31 import org.xml.sax.SAXException;
    33 import org.xml.sax.helpers.DefaultHandler;
    32 import org.xml.sax.helpers.DefaultHandler;
    34 
    33 
    35 import javax.xml.stream.XMLStreamException;
    34 import javax.xml.stream.XMLStreamException;
    67     }
    66     }
    68 
    67 
    69     private boolean containsPrefixMapping(String prefix, String uri) {
    68     private boolean containsPrefixMapping(String prefix, String uri) {
    70         for( int i=0; i<prefixMap.size(); i+=2 ) {
    69         for( int i=0; i<prefixMap.size(); i+=2 ) {
    71             if(prefixMap.get(i).equals(prefix)
    70             if(prefixMap.get(i).equals(prefix)
    72                     && prefixMap.get(i+1).equals(uri))
    71             && prefixMap.get(i+1).equals(uri))
    73                 return true;
    72                 return true;
    74         }
    73         }
    75         return false;
    74         return false;
    76     }
    75     }
    77 
    76 
    78     public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
    77     public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
    79             throws SAXException {
    78         throws SAXException {
    80         try {
    79         try {
    81             flushText();
    80             flushText();
    82 
    81 
    83             int len = atts.getLength();
    82             int len = atts.getLength();
    84 
    83 
    97                 // as-is in the marshalled output (instead of reassigned to something else,
    96                 // as-is in the marshalled output (instead of reassigned to something else,
    98                 // which may happen if you'd use declareNsUri.)
    97                 // which may happen if you'd use declareNsUri.)
    99                 serializer.getNamespaceContext().force(
    98                 serializer.getNamespaceContext().force(
   100                         prefixMap.get(i + 1), prefixMap.get(i));
    99                         prefixMap.get(i + 1), prefixMap.get(i));
   101             }
   100             }
       
   101 
   102             // make sure namespaces needed by attributes are bound
   102             // make sure namespaces needed by attributes are bound
   103             for( int i=0; i<len; i++ ) {
   103             for( int i=0; i<len; i++ ) {
   104                 String qname = atts.getQName(i);
   104                 String qname = atts.getQName(i);
   105                 if(qname.startsWith("xmlns") || atts.getURI(i).length() == 0)
   105                 if(qname.startsWith("xmlns") || atts.getURI(i).length() == 0)
   106                     continue;
   106                     continue;
   107                 String prefix = getPrefix(qname);
   107                 String prefix = getPrefix(qname);
   108 
   108 
   109                 serializer.getNamespaceContext().declareNamespace(
   109                 serializer.getNamespaceContext().declareNamespace(
   110                         atts.getURI(i), prefix, true );
   110                     atts.getURI(i), prefix, true );
   111             }
   111             }
   112 
   112 
   113             serializer.endNamespaceDecls(null);
   113             serializer.endNamespaceDecls(null);
   114             // fire attribute events
   114             // fire attribute events
   115             for( int i=0; i<len; i++ ) {
   115             for( int i=0; i<len; i++ ) {
   125         } catch (XMLStreamException e) {
   125         } catch (XMLStreamException e) {
   126             throw new SAXException2(e);
   126             throw new SAXException2(e);
   127         }
   127         }
   128     }
   128     }
   129 
   129 
   130     // make sure namespaces needed by attributes are bound
       
   131     private String getPrefix(String qname) {
   130     private String getPrefix(String qname) {
   132         int idx = qname.indexOf(':');
   131         int idx = qname.indexOf(':');
   133         String prefix = (idx == -1) ? "" : qname.substring(0, idx);
   132         String prefix = (idx == -1) ? "" : qname.substring(0, idx);
   134         return prefix;
   133         return prefix;
   135     }
   134     }