jaxws/src/java.xml.bind/share/classes/com/sun/xml/internal/bind/v2/runtime/ContentHandlerAdaptor.java
changeset 26775 810bea25280c
parent 25871 b80b84e87032
child 27837 86d4f46e622a
equal deleted inserted replaced
26668:1b1ec4291abc 26775:810bea25280c
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package com.sun.xml.internal.bind.v2.runtime;
    26 package com.sun.xml.internal.bind.v2.runtime;
    27 
    27 
    28 import java.io.IOException;
       
    29 
       
    30 import javax.xml.stream.XMLStreamException;
       
    31 
       
    32 import com.sun.istack.internal.FinalArrayList;
    28 import com.sun.istack.internal.FinalArrayList;
    33 import com.sun.istack.internal.SAXException2;
    29 import com.sun.istack.internal.SAXException2;
    34 
    30 
    35 import org.xml.sax.Attributes;
    31 import org.xml.sax.Attributes;
    36 import org.xml.sax.SAXException;
    32 import org.xml.sax.SAXException;
    37 import org.xml.sax.helpers.DefaultHandler;
    33 import org.xml.sax.helpers.DefaultHandler;
       
    34 
       
    35 import javax.xml.stream.XMLStreamException;
       
    36 import java.io.IOException;
    38 
    37 
    39 /**
    38 /**
    40  * Receives SAX2 events and send the equivalent events to
    39  * Receives SAX2 events and send the equivalent events to
    41  * {@link XMLSerializer}
    40  * {@link XMLSerializer}
    42  *
    41  *
    68     }
    67     }
    69 
    68 
    70     private boolean containsPrefixMapping(String prefix, String uri) {
    69     private boolean containsPrefixMapping(String prefix, String uri) {
    71         for( int i=0; i<prefixMap.size(); i+=2 ) {
    70         for( int i=0; i<prefixMap.size(); i+=2 ) {
    72             if(prefixMap.get(i).equals(prefix)
    71             if(prefixMap.get(i).equals(prefix)
    73             && prefixMap.get(i+1).equals(uri))
    72                     && prefixMap.get(i+1).equals(uri))
    74                 return true;
    73                 return true;
    75         }
    74         }
    76         return false;
    75         return false;
    77     }
    76     }
    78 
    77 
    79     public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
    78     public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
    80         throws SAXException {
    79             throws SAXException {
    81         try {
    80         try {
    82             flushText();
    81             flushText();
    83 
    82 
    84             int len = atts.getLength();
    83             int len = atts.getLength();
    85 
    84 
    90                 serializer.startElementForce(namespaceURI,localName,p,null);
    89                 serializer.startElementForce(namespaceURI,localName,p,null);
    91             else
    90             else
    92                 serializer.startElement(namespaceURI,localName, p,null);
    91                 serializer.startElement(namespaceURI,localName, p,null);
    93 
    92 
    94             // declare namespace events
    93             // declare namespace events
    95             for( int i=0; i<prefixMap.size(); i+=2 ) {
    94             for (int i = 0; i < prefixMap.size(); i += 2) {
    96                 // forcibly set this binding, instead of using declareNsUri.
    95                 // forcibly set this binding, instead of using declareNsUri.
    97                 // this guarantees that namespaces used in DOM will show up
    96                 // this guarantees that namespaces used in DOM will show up
    98                 // as-is in the marshalled output (instead of reassigned to something else,
    97                 // as-is in the marshalled output (instead of reassigned to something else,
    99                 // which may happen if you'd use declareNsUri.)
    98                 // which may happen if you'd use declareNsUri.)
   100                 serializer.getNamespaceContext().force(
    99                 serializer.getNamespaceContext().force(
   101                     prefixMap.get(i+1), prefixMap.get(i) );
   100                         prefixMap.get(i + 1), prefixMap.get(i));
   102             }
   101             }
   103             // make sure namespaces needed by attributes are bound
   102             // make sure namespaces needed by attributes are bound
   104             for( int i=0; i<len; i++ ) {
   103             for( int i=0; i<len; i++ ) {
   105                 String qname = atts.getQName(i);
   104                 String qname = atts.getQName(i);
   106                 if(qname.startsWith("xmlns") || atts.getURI(i).length() == 0)
   105                 if(qname.startsWith("xmlns") || atts.getURI(i).length() == 0)
   107                     continue;
   106                     continue;
   108                 String prefix = getPrefix(qname);
   107                 String prefix = getPrefix(qname);
   109 
   108 
   110                 serializer.getNamespaceContext().declareNamespace(
   109                 serializer.getNamespaceContext().declareNamespace(
   111                     atts.getURI(i), prefix, true );
   110                         atts.getURI(i), prefix, true );
   112             }
   111             }
   113 
   112 
   114             serializer.endNamespaceDecls(null);
   113             serializer.endNamespaceDecls(null);
   115             // fire attribute events
   114             // fire attribute events
   116             for( int i=0; i<len; i++ ) {
   115             for( int i=0; i<len; i++ ) {
   126         } catch (XMLStreamException e) {
   125         } catch (XMLStreamException e) {
   127             throw new SAXException2(e);
   126             throw new SAXException2(e);
   128         }
   127         }
   129     }
   128     }
   130 
   129 
       
   130     // make sure namespaces needed by attributes are bound
   131     private String getPrefix(String qname) {
   131     private String getPrefix(String qname) {
   132         int idx = qname.indexOf(':');
   132         int idx = qname.indexOf(':');
   133         String prefix = (idx==-1)?qname:qname.substring(0,idx);
   133         String prefix = (idx == -1) ? "" : qname.substring(0, idx);
   134         return prefix;
   134         return prefix;
   135     }
   135     }
   136 
   136 
   137     public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
   137     public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
   138         try {
   138         try {