1 /*
   2  * Copyright (c) 1997, 2013, 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 
  26 package com.sun.xml.internal.messaging.saaj.util;
  27 
  28 import java.io.*;
  29 
  30 import 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 
  40 public 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         char[] aChar = new char[65535];
  80         StringBuffer xmlDeclStr = new StringBuffer();
  81         while ((c = m_pushbackReader.read()) != -1) {
  82             aChar[index] = (char)c;
  83             xmlDeclStr.append((char)c);
  84             index++;
  85             if (c == '>') {
  86                 break;
  87             }
  88         }
  89         int len = index;
  90 
  91         String decl = xmlDeclStr.toString();
  92         boolean utf16 = false;
  93         boolean utf8 = false;
  94 
  95         int xmlIndex = decl.indexOf(utf16Decl);
  96         if (xmlIndex > -1) {
  97             utf16 = true;
  98         } else {
  99             xmlIndex = decl.indexOf("<?xml");
 100             if (xmlIndex > -1) {
 101                 utf8 = true;
 102             }
 103         }
 104 
 105         // no XML decl
 106         if (!utf16 && !utf8) {
 107             m_pushbackReader.unread(aChar, 0, len);
 108             return;
 109         }
 110         m_hasHeader = true;
 111 
 112         if (utf16) {
 113             xmlDecl = new String(decl.getBytes(), "utf-16");
 114             xmlDecl = xmlDecl.substring(xmlDecl.indexOf("<"));
 115         } else {
 116             xmlDecl = decl;
 117         }
 118         // do we want to check that there are no other characters preceeding <?xml
 119         if (xmlIndex != 0) {
 120             throw new IOException("Unexpected characters before XML declaration");
 121         }
 122 
 123         int versionIndex =  xmlDecl.indexOf("version");
 124         if (versionIndex == -1) {
 125             throw new IOException("Mandatory 'version' attribute Missing in XML declaration");
 126         }
 127 
 128         // now set
 129         int encodingIndex = xmlDecl.indexOf("encoding");
 130         if (encodingIndex == -1) {
 131             return;
 132         }
 133 
 134         if (versionIndex > encodingIndex) {
 135             throw new IOException("The 'version' attribute should preceed the 'encoding' attribute in an XML Declaration");
 136         }
 137 
 138         int stdAloneIndex = xmlDecl.indexOf("standalone");
 139         if ((stdAloneIndex > -1) && ((stdAloneIndex < versionIndex) || (stdAloneIndex < encodingIndex))) {
 140             throw new IOException("The 'standalone' attribute should be the last attribute in an XML Declaration");
 141         }
 142 
 143         int eqIndex = xmlDecl.indexOf("=", encodingIndex);
 144         if (eqIndex == -1) {
 145             throw new IOException("Missing '=' character after 'encoding' in XML declaration");
 146         }
 147 
 148         m_encoding = parseEncoding(xmlDecl, eqIndex);
 149         if(m_encoding.startsWith("\"")){
 150             m_encoding = m_encoding.substring(m_encoding.indexOf("\"")+1, m_encoding.lastIndexOf("\""));
 151         } else if(m_encoding.startsWith("\'")){
 152             m_encoding = m_encoding.substring(m_encoding.indexOf("\'")+1, m_encoding.lastIndexOf("\'"));
 153         }
 154      }
 155 
 156      //--------------------------------------------------------------------
 157 
 158     public void writeTo(Writer wr) throws IOException {
 159         if (!m_hasHeader) return;
 160         wr.write(xmlDecl.toString());
 161     }
 162 
 163     private String parseEncoding(String xmlDeclFinal, int eqIndex) throws IOException {
 164         java.util.StringTokenizer strTok = new java.util.StringTokenizer(
 165             xmlDeclFinal.substring(eqIndex + 1));
 166         if (strTok.hasMoreTokens()) {
 167             String encodingTok = strTok.nextToken();
 168             int indexofQ = encodingTok.indexOf("?");
 169             if (indexofQ > -1) {
 170                 return encodingTok.substring(0,indexofQ);
 171             } else {
 172                 return encodingTok;
 173             }
 174         } else {
 175             throw new IOException("Error parsing 'encoding' attribute in XML declaration");
 176         }
 177     }
 178 
 179 }