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 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. */
  50     private final XMLSerializer serializer;
  51 
  52     private final StringBuffer text = new StringBuffer();
  53 
  54 
  55     ContentHandlerAdaptor( XMLSerializer _serializer ) {
  56         this.serializer = _serializer;
  57     }
  58 
  59     public void startDocument() {
  60         prefixMap.clear();
  61     }
  62 
  63     public void startPrefixMapping(String prefix, String uri) {
  64         prefixMap.add(prefix);
  65         prefixMap.add(uri);
  66     }
  67 
  68     private boolean containsPrefixMapping(String prefix, String uri) {
  69         for( int i=0; i<prefixMap.size(); i+=2 ) {
  70             if(prefixMap.get(i).equals(prefix)
  71             && prefixMap.get(i+1).equals(uri))
  72                 return true;
  73         }
  74         return false;
  75     }
  76 
  77     public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
  78         throws SAXException {
  79         try {
  80             flushText();
  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);
 150             text.setLength(0);
 151         }
 152     }
 153 
 154     public void characters(char[] ch, int start, int length) {
 155         text.append(ch,start,length);
 156     }
 157 }