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
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
|
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
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
|