01: /*
02: * $Id: XMLInputReader.java,v 1.3 2004/07/08 08:03:04 yuvalo Exp $
03: *
04: * (C) Copyright 2002-2004 by Yuval Oren. All rights reserved.
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package com.bluecast.xml;
20:
21: import java.io.*;
22: import com.bluecast.io.*;
23: import java.util.*;
24:
25: /**
26: * A Reader for XML documents
27: * the proper character set to use based on Byte Order Marks and XML
28: * declarations.
29: *
30: * @author Yuval Oren, yuval@bluecast.com
31: * @version $Revision: 1.3 $
32: */
33: abstract public class XMLInputReader extends Reader {
34: private String xmlVersion = null;
35: private boolean xmlStandaloneDeclared = false;
36: private boolean xmlStandalone = false;
37: private String xmlDeclaredEncoding = null;
38:
39: private XMLDeclParser parser = new XMLDeclParser();
40:
41: protected void resetInput() {
42: xmlVersion = xmlDeclaredEncoding = null;
43: xmlStandaloneDeclared = xmlStandalone = false;
44: }
45:
46: /**
47: * Call this to parse the XML declaration. Returns the number
48: * of characters used by the declaration, or zero if no declaration
49: * was found.
50: */
51: protected int parseXMLDeclaration(char[] cbuf, int offset,
52: int length) throws IOException {
53: parser.reset(cbuf, offset, length);
54: if (parser.parse() == parser.SUCCESS) {
55: xmlVersion = parser.getXMLVersion();
56: xmlStandalone = parser.isXMLStandalone();
57: xmlStandaloneDeclared = parser.isXMLStandaloneDeclared();
58: xmlDeclaredEncoding = parser.getXMLEncoding();
59: return parser.getCharsRead();
60: } else
61: return 0;
62: }
63:
64: /**
65: * Gets the version string from the XML declaration, or null.
66: */
67: public String getXMLVersion() {
68: return xmlVersion;
69: }
70:
71: /**
72: * Returns the Standalone value from the XML declaration.
73: * Defaults to false if no value was declared.
74: */
75: public boolean isXMLStandalone() {
76: return xmlStandalone;
77: }
78:
79: /**
80: * Returns true if an XML "standalone" declaration was found.
81: */
82: public boolean isXMLStandaloneDeclared() {
83: return xmlStandaloneDeclared;
84: }
85:
86: /**
87: * Gets the declared encoding from the XML declaration,
88: * or null if no value was found.
89: */
90: public String getXMLDeclaredEncoding() {
91: return xmlDeclaredEncoding;
92: }
93: }
|