Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, 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 | |
aoqi@0 | 26 | package com.sun.xml.internal.messaging.saaj.util; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.*; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import javax.xml.transform.TransformerException; |
aoqi@0 | 31 | |
aoqi@0 | 32 | /* |
aoqi@0 | 33 | * Class that parses the very first construct in the document i.e. |
aoqi@0 | 34 | * <?xml ... ?> |
aoqi@0 | 35 | * |
aoqi@0 | 36 | * @author Panos Kougiouris (panos@acm.org) |
aoqi@0 | 37 | * @version |
aoqi@0 | 38 | */ |
aoqi@0 | 39 | |
aoqi@0 | 40 | public class XMLDeclarationParser { |
aoqi@0 | 41 | private String m_encoding; |
aoqi@0 | 42 | private PushbackReader m_pushbackReader; |
aoqi@0 | 43 | private boolean m_hasHeader; // preserve the case where no XML Header exists |
aoqi@0 | 44 | private String xmlDecl = null; |
aoqi@0 | 45 | static String gt16 = null; |
aoqi@0 | 46 | static String utf16Decl = null; |
aoqi@0 | 47 | static { |
aoqi@0 | 48 | try { |
aoqi@0 | 49 | gt16 = new String(">".getBytes("utf-16")); |
aoqi@0 | 50 | utf16Decl = new String("<?xml".getBytes("utf-16")); |
aoqi@0 | 51 | } catch (Exception e) {} |
aoqi@0 | 52 | } |
aoqi@0 | 53 | |
aoqi@0 | 54 | //--------------------------------------------------------------------- |
aoqi@0 | 55 | |
aoqi@0 | 56 | public XMLDeclarationParser(PushbackReader pr) |
aoqi@0 | 57 | { |
aoqi@0 | 58 | m_pushbackReader = pr; |
aoqi@0 | 59 | m_encoding = "utf-8"; |
aoqi@0 | 60 | m_hasHeader = false; |
aoqi@0 | 61 | } |
aoqi@0 | 62 | |
aoqi@0 | 63 | //--------------------------------------------------------------------- |
aoqi@0 | 64 | public String getEncoding() |
aoqi@0 | 65 | { |
aoqi@0 | 66 | return m_encoding; |
aoqi@0 | 67 | } |
aoqi@0 | 68 | |
aoqi@0 | 69 | public String getXmlDeclaration() { |
aoqi@0 | 70 | return xmlDecl; |
aoqi@0 | 71 | } |
aoqi@0 | 72 | |
aoqi@0 | 73 | //--------------------------------------------------------------------- |
aoqi@0 | 74 | |
aoqi@0 | 75 | public void parse() throws TransformerException, IOException |
aoqi@0 | 76 | { |
aoqi@0 | 77 | int c = 0; |
aoqi@0 | 78 | int index = 0; |
aoqi@0 | 79 | char[] aChar = new char[65535]; |
aoqi@0 | 80 | StringBuffer xmlDeclStr = new StringBuffer(); |
aoqi@0 | 81 | while ((c = m_pushbackReader.read()) != -1) { |
aoqi@0 | 82 | aChar[index] = (char)c; |
aoqi@0 | 83 | xmlDeclStr.append((char)c); |
aoqi@0 | 84 | index++; |
aoqi@0 | 85 | if (c == '>') { |
aoqi@0 | 86 | break; |
aoqi@0 | 87 | } |
aoqi@0 | 88 | } |
aoqi@0 | 89 | int len = index; |
aoqi@0 | 90 | |
aoqi@0 | 91 | String decl = xmlDeclStr.toString(); |
aoqi@0 | 92 | boolean utf16 = false; |
aoqi@0 | 93 | boolean utf8 = false; |
aoqi@0 | 94 | |
aoqi@0 | 95 | int xmlIndex = decl.indexOf(utf16Decl); |
aoqi@0 | 96 | if (xmlIndex > -1) { |
aoqi@0 | 97 | utf16 = true; |
aoqi@0 | 98 | } else { |
aoqi@0 | 99 | xmlIndex = decl.indexOf("<?xml"); |
aoqi@0 | 100 | if (xmlIndex > -1) { |
aoqi@0 | 101 | utf8 = true; |
aoqi@0 | 102 | } |
aoqi@0 | 103 | } |
aoqi@0 | 104 | |
aoqi@0 | 105 | // no XML decl |
aoqi@0 | 106 | if (!utf16 && !utf8) { |
aoqi@0 | 107 | m_pushbackReader.unread(aChar, 0, len); |
aoqi@0 | 108 | return; |
aoqi@0 | 109 | } |
aoqi@0 | 110 | m_hasHeader = true; |
aoqi@0 | 111 | |
aoqi@0 | 112 | if (utf16) { |
aoqi@0 | 113 | xmlDecl = new String(decl.getBytes(), "utf-16"); |
aoqi@0 | 114 | xmlDecl = xmlDecl.substring(xmlDecl.indexOf("<")); |
aoqi@0 | 115 | } else { |
aoqi@0 | 116 | xmlDecl = decl; |
aoqi@0 | 117 | } |
aoqi@0 | 118 | // do we want to check that there are no other characters preceeding <?xml |
aoqi@0 | 119 | if (xmlIndex != 0) { |
aoqi@0 | 120 | throw new IOException("Unexpected characters before XML declaration"); |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | int versionIndex = xmlDecl.indexOf("version"); |
aoqi@0 | 124 | if (versionIndex == -1) { |
aoqi@0 | 125 | throw new IOException("Mandatory 'version' attribute Missing in XML declaration"); |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | // now set |
aoqi@0 | 129 | int encodingIndex = xmlDecl.indexOf("encoding"); |
aoqi@0 | 130 | if (encodingIndex == -1) { |
aoqi@0 | 131 | return; |
aoqi@0 | 132 | } |
aoqi@0 | 133 | |
aoqi@0 | 134 | if (versionIndex > encodingIndex) { |
aoqi@0 | 135 | throw new IOException("The 'version' attribute should preceed the 'encoding' attribute in an XML Declaration"); |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | int stdAloneIndex = xmlDecl.indexOf("standalone"); |
aoqi@0 | 139 | if ((stdAloneIndex > -1) && ((stdAloneIndex < versionIndex) || (stdAloneIndex < encodingIndex))) { |
aoqi@0 | 140 | throw new IOException("The 'standalone' attribute should be the last attribute in an XML Declaration"); |
aoqi@0 | 141 | } |
aoqi@0 | 142 | |
aoqi@0 | 143 | int eqIndex = xmlDecl.indexOf("=", encodingIndex); |
aoqi@0 | 144 | if (eqIndex == -1) { |
aoqi@0 | 145 | throw new IOException("Missing '=' character after 'encoding' in XML declaration"); |
aoqi@0 | 146 | } |
aoqi@0 | 147 | |
aoqi@0 | 148 | m_encoding = parseEncoding(xmlDecl, eqIndex); |
aoqi@0 | 149 | if(m_encoding.startsWith("\"")){ |
aoqi@0 | 150 | m_encoding = m_encoding.substring(m_encoding.indexOf("\"")+1, m_encoding.lastIndexOf("\"")); |
aoqi@0 | 151 | } else if(m_encoding.startsWith("\'")){ |
aoqi@0 | 152 | m_encoding = m_encoding.substring(m_encoding.indexOf("\'")+1, m_encoding.lastIndexOf("\'")); |
aoqi@0 | 153 | } |
aoqi@0 | 154 | } |
aoqi@0 | 155 | |
aoqi@0 | 156 | //-------------------------------------------------------------------- |
aoqi@0 | 157 | |
aoqi@0 | 158 | public void writeTo(Writer wr) throws IOException { |
aoqi@0 | 159 | if (!m_hasHeader) return; |
aoqi@0 | 160 | wr.write(xmlDecl.toString()); |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | private String parseEncoding(String xmlDeclFinal, int eqIndex) throws IOException { |
aoqi@0 | 164 | java.util.StringTokenizer strTok = new java.util.StringTokenizer( |
aoqi@0 | 165 | xmlDeclFinal.substring(eqIndex + 1)); |
aoqi@0 | 166 | if (strTok.hasMoreTokens()) { |
aoqi@0 | 167 | String encodingTok = strTok.nextToken(); |
aoqi@0 | 168 | int indexofQ = encodingTok.indexOf("?"); |
aoqi@0 | 169 | if (indexofQ > -1) { |
aoqi@0 | 170 | return encodingTok.substring(0,indexofQ); |
aoqi@0 | 171 | } else { |
aoqi@0 | 172 | return encodingTok; |
aoqi@0 | 173 | } |
aoqi@0 | 174 | } else { |
aoqi@0 | 175 | throw new IOException("Error parsing 'encoding' attribute in XML declaration"); |
aoqi@0 | 176 | } |
aoqi@0 | 177 | } |
aoqi@0 | 178 | |
aoqi@0 | 179 | } |