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