Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | * |
aoqi@0 | 25 | * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
aoqi@0 | 26 | */ |
aoqi@0 | 27 | |
aoqi@0 | 28 | package com.sun.xml.internal.fastinfoset.tools; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import javax.xml.namespace.QName; |
aoqi@0 | 31 | import javax.xml.stream.XMLStreamConstants; |
aoqi@0 | 32 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 33 | import javax.xml.stream.XMLStreamReader; |
aoqi@0 | 34 | import org.xml.sax.ContentHandler; |
aoqi@0 | 35 | import org.xml.sax.SAXException; |
aoqi@0 | 36 | import org.xml.sax.ext.LexicalHandler; |
aoqi@0 | 37 | import org.xml.sax.helpers.AttributesImpl; |
aoqi@0 | 38 | import com.sun.xml.internal.fastinfoset.CommonResourceBundle; |
aoqi@0 | 39 | |
aoqi@0 | 40 | public class StAX2SAXReader { |
aoqi@0 | 41 | |
aoqi@0 | 42 | /** |
aoqi@0 | 43 | * Content handler where events are pushed. |
aoqi@0 | 44 | */ |
aoqi@0 | 45 | ContentHandler _handler; |
aoqi@0 | 46 | |
aoqi@0 | 47 | /** |
aoqi@0 | 48 | * Lexical handler to report lexical events. |
aoqi@0 | 49 | */ |
aoqi@0 | 50 | LexicalHandler _lexicalHandler; |
aoqi@0 | 51 | |
aoqi@0 | 52 | /** |
aoqi@0 | 53 | * XML stream reader where events are pulled. |
aoqi@0 | 54 | */ |
aoqi@0 | 55 | XMLStreamReader _reader; |
aoqi@0 | 56 | |
aoqi@0 | 57 | public StAX2SAXReader(XMLStreamReader reader, ContentHandler handler) { |
aoqi@0 | 58 | _handler = handler; |
aoqi@0 | 59 | _reader = reader; |
aoqi@0 | 60 | } |
aoqi@0 | 61 | |
aoqi@0 | 62 | public StAX2SAXReader(XMLStreamReader reader) { |
aoqi@0 | 63 | _reader = reader; |
aoqi@0 | 64 | } |
aoqi@0 | 65 | |
aoqi@0 | 66 | public void setContentHandler(ContentHandler handler) { |
aoqi@0 | 67 | _handler = handler; |
aoqi@0 | 68 | } |
aoqi@0 | 69 | |
aoqi@0 | 70 | public void setLexicalHandler(LexicalHandler lexicalHandler) { |
aoqi@0 | 71 | _lexicalHandler = lexicalHandler; |
aoqi@0 | 72 | } |
aoqi@0 | 73 | |
aoqi@0 | 74 | public void adapt() throws XMLStreamException, SAXException { |
aoqi@0 | 75 | QName qname; |
aoqi@0 | 76 | String prefix, localPart; |
aoqi@0 | 77 | AttributesImpl attrs = new AttributesImpl(); |
aoqi@0 | 78 | char[] buffer; |
aoqi@0 | 79 | int nsc; |
aoqi@0 | 80 | int nat; |
aoqi@0 | 81 | |
aoqi@0 | 82 | _handler.startDocument(); |
aoqi@0 | 83 | |
aoqi@0 | 84 | try { |
aoqi@0 | 85 | |
aoqi@0 | 86 | while (_reader.hasNext()) { |
aoqi@0 | 87 | int event = _reader.next(); |
aoqi@0 | 88 | |
aoqi@0 | 89 | |
aoqi@0 | 90 | switch(event) { |
aoqi@0 | 91 | case XMLStreamConstants.START_ELEMENT: { |
aoqi@0 | 92 | // Report namespace events first |
aoqi@0 | 93 | nsc = _reader.getNamespaceCount(); |
aoqi@0 | 94 | for (int i = 0; i < nsc; i++) { |
aoqi@0 | 95 | _handler.startPrefixMapping(_reader.getNamespacePrefix(i), |
aoqi@0 | 96 | _reader.getNamespaceURI(i)); |
aoqi@0 | 97 | } |
aoqi@0 | 98 | |
aoqi@0 | 99 | // Collect list of attributes |
aoqi@0 | 100 | attrs.clear(); |
aoqi@0 | 101 | nat = _reader.getAttributeCount(); |
aoqi@0 | 102 | for (int i = 0; i < nat; i++) { |
aoqi@0 | 103 | QName q = _reader.getAttributeName(i); |
aoqi@0 | 104 | String qName = _reader.getAttributePrefix(i); |
aoqi@0 | 105 | if (qName == null || qName == "") { |
aoqi@0 | 106 | qName = q.getLocalPart(); |
aoqi@0 | 107 | } else { |
aoqi@0 | 108 | qName = qName + ":" + q.getLocalPart(); |
aoqi@0 | 109 | } |
aoqi@0 | 110 | attrs.addAttribute(_reader.getAttributeNamespace(i), |
aoqi@0 | 111 | q.getLocalPart(), |
aoqi@0 | 112 | qName, |
aoqi@0 | 113 | _reader.getAttributeType(i), |
aoqi@0 | 114 | _reader.getAttributeValue(i)); |
aoqi@0 | 115 | } |
aoqi@0 | 116 | |
aoqi@0 | 117 | // Report start element |
aoqi@0 | 118 | qname = _reader.getName(); |
aoqi@0 | 119 | prefix = qname.getPrefix(); |
aoqi@0 | 120 | localPart = qname.getLocalPart(); |
aoqi@0 | 121 | |
aoqi@0 | 122 | _handler.startElement(_reader.getNamespaceURI(), |
aoqi@0 | 123 | localPart, |
aoqi@0 | 124 | (prefix.length() > 0) ? |
aoqi@0 | 125 | (prefix + ":" + localPart) : localPart, |
aoqi@0 | 126 | attrs); |
aoqi@0 | 127 | break; |
aoqi@0 | 128 | } |
aoqi@0 | 129 | case XMLStreamConstants.END_ELEMENT: { |
aoqi@0 | 130 | // Report end element |
aoqi@0 | 131 | qname = _reader.getName(); |
aoqi@0 | 132 | prefix = qname.getPrefix(); |
aoqi@0 | 133 | localPart = qname.getLocalPart(); |
aoqi@0 | 134 | |
aoqi@0 | 135 | _handler.endElement(_reader.getNamespaceURI(), |
aoqi@0 | 136 | localPart, |
aoqi@0 | 137 | (prefix.length() > 0) ? |
aoqi@0 | 138 | (prefix + ":" + localPart) : localPart); |
aoqi@0 | 139 | |
aoqi@0 | 140 | // Report end namespace events |
aoqi@0 | 141 | nsc = _reader.getNamespaceCount(); |
aoqi@0 | 142 | for (int i = 0; i < nsc; i++) { |
aoqi@0 | 143 | _handler.endPrefixMapping(_reader.getNamespacePrefix(i)); |
aoqi@0 | 144 | } |
aoqi@0 | 145 | break; |
aoqi@0 | 146 | } |
aoqi@0 | 147 | case XMLStreamConstants.CHARACTERS: |
aoqi@0 | 148 | _handler.characters(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength()); |
aoqi@0 | 149 | break; |
aoqi@0 | 150 | case XMLStreamConstants.COMMENT: |
aoqi@0 | 151 | _lexicalHandler.comment(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength()); |
aoqi@0 | 152 | break; |
aoqi@0 | 153 | case XMLStreamConstants.PROCESSING_INSTRUCTION: |
aoqi@0 | 154 | _handler.processingInstruction(_reader.getPITarget(), _reader.getPIData()); |
aoqi@0 | 155 | break; |
aoqi@0 | 156 | case XMLStreamConstants.END_DOCUMENT: |
aoqi@0 | 157 | break; |
aoqi@0 | 158 | default: |
aoqi@0 | 159 | throw new RuntimeException(CommonResourceBundle.getInstance().getString("message.StAX2SAXReader", new Object[]{Integer.valueOf(event)})); |
aoqi@0 | 160 | } // switch |
aoqi@0 | 161 | } |
aoqi@0 | 162 | } |
aoqi@0 | 163 | catch (XMLStreamException e) { |
aoqi@0 | 164 | _handler.endDocument(); // flush whatever we have |
aoqi@0 | 165 | throw e; |
aoqi@0 | 166 | } |
aoqi@0 | 167 | |
aoqi@0 | 168 | _handler.endDocument(); |
aoqi@0 | 169 | } |
aoqi@0 | 170 | |
aoqi@0 | 171 | } |