1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/util/XMLDeclarationParser.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,179 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.messaging.saaj.util; 1.30 + 1.31 +import java.io.*; 1.32 + 1.33 +import javax.xml.transform.TransformerException; 1.34 + 1.35 +/* 1.36 + * Class that parses the very first construct in the document i.e. 1.37 + * <?xml ... ?> 1.38 + * 1.39 + * @author Panos Kougiouris (panos@acm.org) 1.40 + * @version 1.41 + */ 1.42 + 1.43 +public class XMLDeclarationParser { 1.44 + private String m_encoding; 1.45 + private PushbackReader m_pushbackReader; 1.46 + private boolean m_hasHeader; // preserve the case where no XML Header exists 1.47 + private String xmlDecl = null; 1.48 + static String gt16 = null; 1.49 + static String utf16Decl = null; 1.50 + static { 1.51 + try { 1.52 + gt16 = new String(">".getBytes("utf-16")); 1.53 + utf16Decl = new String("<?xml".getBytes("utf-16")); 1.54 + } catch (Exception e) {} 1.55 + } 1.56 + 1.57 + //--------------------------------------------------------------------- 1.58 + 1.59 + public XMLDeclarationParser(PushbackReader pr) 1.60 + { 1.61 + m_pushbackReader = pr; 1.62 + m_encoding = "utf-8"; 1.63 + m_hasHeader = false; 1.64 + } 1.65 + 1.66 + //--------------------------------------------------------------------- 1.67 + public String getEncoding() 1.68 + { 1.69 + return m_encoding; 1.70 + } 1.71 + 1.72 + public String getXmlDeclaration() { 1.73 + return xmlDecl; 1.74 + } 1.75 + 1.76 + //--------------------------------------------------------------------- 1.77 + 1.78 + public void parse() throws TransformerException, IOException 1.79 + { 1.80 + int c = 0; 1.81 + int index = 0; 1.82 + char[] aChar = new char[65535]; 1.83 + StringBuffer xmlDeclStr = new StringBuffer(); 1.84 + while ((c = m_pushbackReader.read()) != -1) { 1.85 + aChar[index] = (char)c; 1.86 + xmlDeclStr.append((char)c); 1.87 + index++; 1.88 + if (c == '>') { 1.89 + break; 1.90 + } 1.91 + } 1.92 + int len = index; 1.93 + 1.94 + String decl = xmlDeclStr.toString(); 1.95 + boolean utf16 = false; 1.96 + boolean utf8 = false; 1.97 + 1.98 + int xmlIndex = decl.indexOf(utf16Decl); 1.99 + if (xmlIndex > -1) { 1.100 + utf16 = true; 1.101 + } else { 1.102 + xmlIndex = decl.indexOf("<?xml"); 1.103 + if (xmlIndex > -1) { 1.104 + utf8 = true; 1.105 + } 1.106 + } 1.107 + 1.108 + // no XML decl 1.109 + if (!utf16 && !utf8) { 1.110 + m_pushbackReader.unread(aChar, 0, len); 1.111 + return; 1.112 + } 1.113 + m_hasHeader = true; 1.114 + 1.115 + if (utf16) { 1.116 + xmlDecl = new String(decl.getBytes(), "utf-16"); 1.117 + xmlDecl = xmlDecl.substring(xmlDecl.indexOf("<")); 1.118 + } else { 1.119 + xmlDecl = decl; 1.120 + } 1.121 + // do we want to check that there are no other characters preceeding <?xml 1.122 + if (xmlIndex != 0) { 1.123 + throw new IOException("Unexpected characters before XML declaration"); 1.124 + } 1.125 + 1.126 + int versionIndex = xmlDecl.indexOf("version"); 1.127 + if (versionIndex == -1) { 1.128 + throw new IOException("Mandatory 'version' attribute Missing in XML declaration"); 1.129 + } 1.130 + 1.131 + // now set 1.132 + int encodingIndex = xmlDecl.indexOf("encoding"); 1.133 + if (encodingIndex == -1) { 1.134 + return; 1.135 + } 1.136 + 1.137 + if (versionIndex > encodingIndex) { 1.138 + throw new IOException("The 'version' attribute should preceed the 'encoding' attribute in an XML Declaration"); 1.139 + } 1.140 + 1.141 + int stdAloneIndex = xmlDecl.indexOf("standalone"); 1.142 + if ((stdAloneIndex > -1) && ((stdAloneIndex < versionIndex) || (stdAloneIndex < encodingIndex))) { 1.143 + throw new IOException("The 'standalone' attribute should be the last attribute in an XML Declaration"); 1.144 + } 1.145 + 1.146 + int eqIndex = xmlDecl.indexOf("=", encodingIndex); 1.147 + if (eqIndex == -1) { 1.148 + throw new IOException("Missing '=' character after 'encoding' in XML declaration"); 1.149 + } 1.150 + 1.151 + m_encoding = parseEncoding(xmlDecl, eqIndex); 1.152 + if(m_encoding.startsWith("\"")){ 1.153 + m_encoding = m_encoding.substring(m_encoding.indexOf("\"")+1, m_encoding.lastIndexOf("\"")); 1.154 + } else if(m_encoding.startsWith("\'")){ 1.155 + m_encoding = m_encoding.substring(m_encoding.indexOf("\'")+1, m_encoding.lastIndexOf("\'")); 1.156 + } 1.157 + } 1.158 + 1.159 + //-------------------------------------------------------------------- 1.160 + 1.161 + public void writeTo(Writer wr) throws IOException { 1.162 + if (!m_hasHeader) return; 1.163 + wr.write(xmlDecl.toString()); 1.164 + } 1.165 + 1.166 + private String parseEncoding(String xmlDeclFinal, int eqIndex) throws IOException { 1.167 + java.util.StringTokenizer strTok = new java.util.StringTokenizer( 1.168 + xmlDeclFinal.substring(eqIndex + 1)); 1.169 + if (strTok.hasMoreTokens()) { 1.170 + String encodingTok = strTok.nextToken(); 1.171 + int indexofQ = encodingTok.indexOf("?"); 1.172 + if (indexofQ > -1) { 1.173 + return encodingTok.substring(0,indexofQ); 1.174 + } else { 1.175 + return encodingTok; 1.176 + } 1.177 + } else { 1.178 + throw new IOException("Error parsing 'encoding' attribute in XML declaration"); 1.179 + } 1.180 + } 1.181 + 1.182 +}