001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
022: *
023: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
024: *
025: * The contents of this file are subject to the terms of either the GNU
026: * General Public License Version 2 only ("GPL") or the Common Development
027: * and Distribution License("CDDL") (collectively, the "License"). You
028: * may not use this file except in compliance with the License. You can obtain
029: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
030: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
031: * language governing permissions and limitations under the License.
032: *
033: * When distributing the software, include this License Header Notice in each
034: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
035: * Sun designates this particular file as subject to the "Classpath" exception
036: * as provided by Sun in the GPL Version 2 section of the License file that
037: * accompanied this code. If applicable, add the following below the License
038: * Header, with the fields enclosed by brackets [] replaced by your own
039: * identifying information: "Portions Copyrighted [year]
040: * [name of copyright owner]"
041: *
042: * Contributor(s):
043: *
044: * If you wish your version of this file to be governed by only the CDDL or
045: * only the GPL Version 2, indicate your decision by adding "[Contributor]
046: * elects to include this software in this distribution under the [CDDL or GPL
047: * Version 2] license." If you don't indicate a single choice of license, a
048: * recipient has the option to distribute your version of this file under
049: * either the CDDL, the GPL Version 2 or to extend the choice of license to
050: * its licensees as provided above. However, if you add GPL Version 2 code
051: * and therefore, elected the GPL Version 2 license, then the option applies
052: * only if the new code is made subject to such option by the copyright
053: * holder.
054: */
055: package com.sun.xml.messaging.saaj.util;
056:
057: import java.io.*;
058: import java.util.HashMap;
059: import java.util.Iterator;
060:
061: import javax.xml.transform.TransformerException;
062:
063: /*
064: * Class that parses the very first construct in the document i.e.
065: * <?xml ... ?>
066: *
067: * @author Panos Kougiouris (panos@acm.org)
068: * @version $Revision: 1.3 $ $Date: 2007/07/16 16:41:26 $
069: */
070:
071: public class XMLDeclarationParser {
072: private String m_encoding;
073: private PushbackReader m_pushbackReader;
074: private boolean m_hasHeader; // preserve the case where no XML Header exists
075: private String xmlDecl = null;
076: static String gt16 = null;
077: static String utf16Decl = null;
078:
079: static {
080: try {
081: gt16 = new String(">".getBytes("utf-16"));
082: utf16Decl = new String("<?xml".getBytes("utf-16"));
083: } catch (Exception e) {
084: }
085: }
086:
087: //---------------------------------------------------------------------
088:
089: public XMLDeclarationParser(PushbackReader pr) {
090: m_pushbackReader = pr;
091: m_encoding = "utf-8";
092: m_hasHeader = false;
093: }
094:
095: //---------------------------------------------------------------------
096: public String getEncoding() {
097: return m_encoding;
098: }
099:
100: public String getXmlDeclaration() {
101: return xmlDecl;
102: }
103:
104: //---------------------------------------------------------------------
105:
106: public void parse() throws TransformerException, IOException {
107: int c = 0;
108: int index = 0;
109: char[] aChar = new char[65535];
110: StringBuffer xmlDeclStr = new StringBuffer();
111: while ((c = m_pushbackReader.read()) != -1) {
112: aChar[index] = (char) c;
113: xmlDeclStr.append((char) c);
114: index++;
115: if (c == '>') {
116: break;
117: }
118: }
119: int len = index;
120:
121: String decl = xmlDeclStr.toString();
122: boolean utf16 = false;
123: boolean utf8 = false;
124:
125: int xmlIndex = decl.indexOf(utf16Decl);
126: if (xmlIndex > -1) {
127: utf16 = true;
128: } else {
129: xmlIndex = decl.indexOf("<?xml");
130: if (xmlIndex > -1) {
131: utf8 = true;
132: }
133: }
134:
135: // no XML decl
136: if (!utf16 && !utf8) {
137: m_pushbackReader.unread(aChar, 0, len);
138: return;
139: }
140: m_hasHeader = true;
141:
142: if (utf16) {
143: xmlDecl = new String(decl.getBytes(), "utf-16");
144: xmlDecl = xmlDecl.substring(xmlDecl.indexOf("<"));
145: } else {
146: xmlDecl = decl;
147: }
148: // do we want to check that there are no other characters preceeding <?xml
149: if (xmlIndex != 0) {
150: throw new IOException(
151: "Unexpected characters before XML declaration");
152: }
153:
154: int versionIndex = xmlDecl.indexOf("version");
155: if (versionIndex == -1) {
156: throw new IOException(
157: "Mandatory 'version' attribute Missing in XML declaration");
158: }
159:
160: // now set
161: int encodingIndex = xmlDecl.indexOf("encoding");
162: if (encodingIndex == -1) {
163: return;
164: }
165:
166: if (versionIndex > encodingIndex) {
167: throw new IOException(
168: "The 'version' attribute should preceed the 'encoding' attribute in an XML Declaration");
169: }
170:
171: int stdAloneIndex = xmlDecl.indexOf("standalone");
172: if ((stdAloneIndex > -1)
173: && ((stdAloneIndex < versionIndex) || (stdAloneIndex < encodingIndex))) {
174: throw new IOException(
175: "The 'standalone' attribute should be the last attribute in an XML Declaration");
176: }
177:
178: int eqIndex = xmlDecl.indexOf("=", encodingIndex);
179: if (eqIndex == -1) {
180: throw new IOException(
181: "Missing '=' character after 'encoding' in XML declaration");
182: }
183:
184: m_encoding = parseEncoding(xmlDecl, eqIndex);
185: if (m_encoding.startsWith("\"")) {
186: m_encoding = m_encoding.substring(
187: m_encoding.indexOf("\"") + 1, m_encoding
188: .lastIndexOf("\""));
189: } else if (m_encoding.startsWith("\'")) {
190: m_encoding = m_encoding.substring(
191: m_encoding.indexOf("\'") + 1, m_encoding
192: .lastIndexOf("\'"));
193: }
194: }
195:
196: //--------------------------------------------------------------------
197:
198: public void writeTo(Writer wr) throws IOException {
199: if (!m_hasHeader)
200: return;
201: wr.write(xmlDecl.toString());
202: }
203:
204: private String parseEncoding(String xmlDeclFinal, int eqIndex)
205: throws IOException {
206: java.util.StringTokenizer strTok = new java.util.StringTokenizer(
207: xmlDeclFinal.substring(eqIndex + 1));
208: if (strTok.hasMoreTokens()) {
209: String encodingTok = strTok.nextToken();
210: int indexofQ = encodingTok.indexOf("?");
211: if (indexofQ > -1) {
212: return encodingTok.substring(0, indexofQ);
213: } else {
214: return encodingTok;
215: }
216: } else {
217: throw new IOException(
218: "Error parsing 'encoding' attribute in XML declaration");
219: }
220: }
221:
222: }
|