1 /*
   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.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  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. */
  51     private final XMLSerializer serializer;
  52 
  53     private final StringBuffer text = new StringBuffer();
  54 
  55 
  56     ContentHandlerAdaptor( XMLSerializer _serializer ) {
  57         this.serializer = _serializer;
  58     }
  59 
  60     public void startDocument() {
  61         prefixMap.clear();
  62     }
  63 
  64     public void startPrefixMapping(String prefix, String uri) {
  65         prefixMap.add(prefix);
  66         prefixMap.add(uri);
  67     }
  68 
  69     private boolean containsPrefixMapping(String prefix, String uri) {
  70         for( int i=0; i<prefixMap.size(); i+=2 ) {
  71             if(prefixMap.get(i).equals(prefix)
  72             && prefixMap.get(i+1).equals(uri))
  73                 return true;
  74         }
  75         return false;
  76     }
  77 
  78     public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
  79         throws SAXException {
  80         try {
  81             flushText();
  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);
 151             text.setLength(0);
 152         }
 153     }
 154 
 155     public void characters(char[] ch, int start, int length) {
 156         text.append(ch,start,length);
 157     }
 158 }