< prev index next >

src/java.xml.bind/share/classes/com/sun/xml/internal/bind/v2/runtime/ContentHandlerAdaptor.java

Print this page




  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.bind.v2.runtime;
  27 
  28 import com.sun.istack.internal.FinalArrayList;
  29 import com.sun.istack.internal.SAXException2;
  30 
  31 import org.xml.sax.Attributes;
  32 import org.xml.sax.SAXException;
  33 import org.xml.sax.helpers.DefaultHandler;
  34 
  35 import javax.xml.stream.XMLStreamException;
  36 import java.io.IOException;
  37 
  38 /**
  39  * Receives SAX2 events and send the equivalent events to
  40  * {@link XMLSerializer}
  41  *
  42  * @author
  43  *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  44  */
  45 final class ContentHandlerAdaptor extends DefaultHandler {
  46 
  47     /** Stores newly declared prefix-URI mapping. */
  48     private final FinalArrayList<String> prefixMap = new FinalArrayList<String>();
  49 
  50     /** Events will be sent to this object. */


  82 
  83             int len = atts.getLength();
  84 
  85             String p = getPrefix(qName);
  86 
  87             // is this prefix going to be declared on this element?
  88             if(containsPrefixMapping(p,namespaceURI))
  89                 serializer.startElementForce(namespaceURI,localName,p,null);
  90             else
  91                 serializer.startElement(namespaceURI,localName, p,null);
  92 
  93             // declare namespace events
  94             for (int i = 0; i < prefixMap.size(); i += 2) {
  95                 // forcibly set this binding, instead of using declareNsUri.
  96                 // this guarantees that namespaces used in DOM will show up
  97                 // as-is in the marshalled output (instead of reassigned to something else,
  98                 // which may happen if you'd use declareNsUri.)
  99                 serializer.getNamespaceContext().force(
 100                         prefixMap.get(i + 1), prefixMap.get(i));
 101             }

 102             // make sure namespaces needed by attributes are bound
 103             for( int i=0; i<len; i++ ) {
 104                 String qname = atts.getQName(i);
 105                 if(qname.startsWith("xmlns") || atts.getURI(i).length() == 0)
 106                     continue;
 107                 String prefix = getPrefix(qname);
 108 
 109                 serializer.getNamespaceContext().declareNamespace(
 110                         atts.getURI(i), prefix, true );
 111             }
 112 
 113             serializer.endNamespaceDecls(null);
 114             // fire attribute events
 115             for( int i=0; i<len; i++ ) {
 116                 // be defensive.
 117                 if(atts.getQName(i).startsWith("xmlns"))
 118                     continue;
 119                 serializer.attribute( atts.getURI(i), atts.getLocalName(i), atts.getValue(i));
 120             }
 121             prefixMap.clear();
 122             serializer.endAttributes();
 123         } catch (IOException e) {
 124             throw new SAXException2(e);
 125         } catch (XMLStreamException e) {
 126             throw new SAXException2(e);
 127         }
 128     }
 129 
 130     // make sure namespaces needed by attributes are bound
 131     private String getPrefix(String qname) {
 132         int idx = qname.indexOf(':');
 133         String prefix = (idx == -1) ? "" : qname.substring(0, idx);
 134         return prefix;
 135     }
 136 
 137     public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
 138         try {
 139             flushText();
 140             serializer.endElement();
 141         } catch (IOException e) {
 142             throw new SAXException2(e);
 143         } catch (XMLStreamException e) {
 144             throw new SAXException2(e);
 145         }
 146     }
 147 
 148     private void flushText() throws SAXException, IOException, XMLStreamException {
 149         if( text.length()!=0 ) {
 150             serializer.text(text.toString(),null);


  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.bind.v2.runtime;
  27 
  28 import com.sun.istack.internal.FinalArrayList;
  29 import com.sun.istack.internal.SAXException2;

  30 import org.xml.sax.Attributes;
  31 import org.xml.sax.SAXException;
  32 import org.xml.sax.helpers.DefaultHandler;
  33 
  34 import javax.xml.stream.XMLStreamException;
  35 import java.io.IOException;
  36 
  37 /**
  38  * Receives SAX2 events and send the equivalent events to
  39  * {@link XMLSerializer}
  40  *
  41  * @author
  42  *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  43  */
  44 final class ContentHandlerAdaptor extends DefaultHandler {
  45 
  46     /** Stores newly declared prefix-URI mapping. */
  47     private final FinalArrayList<String> prefixMap = new FinalArrayList<String>();
  48 
  49     /** Events will be sent to this object. */


  81 
  82             int len = atts.getLength();
  83 
  84             String p = getPrefix(qName);
  85 
  86             // is this prefix going to be declared on this element?
  87             if(containsPrefixMapping(p,namespaceURI))
  88                 serializer.startElementForce(namespaceURI,localName,p,null);
  89             else
  90                 serializer.startElement(namespaceURI,localName, p,null);
  91 
  92             // declare namespace events
  93             for (int i = 0; i < prefixMap.size(); i += 2) {
  94                 // forcibly set this binding, instead of using declareNsUri.
  95                 // this guarantees that namespaces used in DOM will show up
  96                 // as-is in the marshalled output (instead of reassigned to something else,
  97                 // which may happen if you'd use declareNsUri.)
  98                 serializer.getNamespaceContext().force(
  99                         prefixMap.get(i + 1), prefixMap.get(i));
 100             }
 101 
 102             // make sure namespaces needed by attributes are bound
 103             for( int i=0; i<len; i++ ) {
 104                 String qname = atts.getQName(i);
 105                 if(qname.startsWith("xmlns") || atts.getURI(i).length() == 0)
 106                     continue;
 107                 String prefix = getPrefix(qname);
 108 
 109                 serializer.getNamespaceContext().declareNamespace(
 110                     atts.getURI(i), prefix, true );
 111             }
 112 
 113             serializer.endNamespaceDecls(null);
 114             // fire attribute events
 115             for( int i=0; i<len; i++ ) {
 116                 // be defensive.
 117                 if(atts.getQName(i).startsWith("xmlns"))
 118                     continue;
 119                 serializer.attribute( atts.getURI(i), atts.getLocalName(i), atts.getValue(i));
 120             }
 121             prefixMap.clear();
 122             serializer.endAttributes();
 123         } catch (IOException e) {
 124             throw new SAXException2(e);
 125         } catch (XMLStreamException e) {
 126             throw new SAXException2(e);
 127         }
 128     }
 129 

 130     private String getPrefix(String qname) {
 131         int idx = qname.indexOf(':');
 132         String prefix = (idx == -1) ? "" : qname.substring(0, idx);
 133         return prefix;
 134     }
 135 
 136     public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
 137         try {
 138             flushText();
 139             serializer.endElement();
 140         } catch (IOException e) {
 141             throw new SAXException2(e);
 142         } catch (XMLStreamException e) {
 143             throw new SAXException2(e);
 144         }
 145     }
 146 
 147     private void flushText() throws SAXException, IOException, XMLStreamException {
 148         if( text.length()!=0 ) {
 149             serializer.text(text.toString(),null);
< prev index next >