KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > XML11DTDScannerImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999-2004 The Apache Software Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.impl;
59
60 import java.io.IOException JavaDoc;
61
62 import com.sun.org.apache.xerces.internal.util.SymbolTable;
63 import com.sun.org.apache.xerces.internal.util.XML11Char;
64 import com.sun.org.apache.xerces.internal.util.XMLChar;
65 import com.sun.org.apache.xerces.internal.util.XMLStringBuffer;
66 import com.sun.org.apache.xerces.internal.xni.XMLString;
67 import com.sun.org.apache.xerces.internal.xni.XNIException;
68
69 /**
70  * This class is responsible for scanning the declarations found
71  * in the internal and external subsets of a DTD in an XML document.
72  * The scanner acts as the sources for the DTD information which is
73  * communicated to the DTD handlers.
74  * <p>
75  * This component requires the following features and properties from the
76  * component manager that uses it:
77  * <ul>
78  * <li>http://xml.org/sax/features/validation</li>
79  * <li>http://apache.org/xml/features/scanner/notify-char-refs</li>
80  * <li>http://apache.org/xml/properties/internal/symbol-table</li>
81  * <li>http://apache.org/xml/properties/internal/error-reporter</li>
82  * <li>http://apache.org/xml/properties/internal/entity-manager</li>
83  * </ul>
84  *
85  * @author Arnaud Le Hors, IBM
86  * @author Andy Clark, IBM
87  * @author Glenn Marcy, IBM
88  * @author Eric Ye, IBM
89  *
90  * @version $Id: XML11DTDScannerImpl.java,v 1.10 2004/01/26 19:37:35 mrglavas Exp $
91  */

92 public class XML11DTDScannerImpl
93     extends XMLDTDScannerImpl {
94
95     /** Array of 3 strings. */
96     private String JavaDoc[] fStrings = new String JavaDoc[3];
97
98     /** String. */
99     private XMLString fString = new XMLString();
100
101     /** String buffer. */
102     private XMLStringBuffer fStringBuffer = new XMLStringBuffer();
103
104     /** String buffer. */
105     private XMLStringBuffer fStringBuffer2 = new XMLStringBuffer();
106     private XMLStringBuffer fStringBuffer3 = new XMLStringBuffer();
107
108     //
109
// Constructors
110
//
111

112     /** Default constructor. */
113     public XML11DTDScannerImpl() {super();} // <init>()
114

115     /** Constructor for he use of non-XMLComponentManagers. */
116     public XML11DTDScannerImpl(SymbolTable symbolTable,
117                 XMLErrorReporter errorReporter, XMLEntityManager entityManager) {
118         super(symbolTable, errorReporter, entityManager);
119     }
120
121     //
122
// XMLDTDScanner methods
123
//
124

125     //
126
// XMLScanner methods
127
//
128
// NOTE: this is a carbon copy of the code in XML11DocumentScannerImpl;
129
// we need to override these methods in both places. Ah for
130
// multiple inheritance...
131
// This needs to be refactored!!! - NG
132
/**
133      * Scans public ID literal.
134      *
135      * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
136      * [13] PubidChar::= #x20 | #xD | #xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%]
137      *
138      * The returned string is normalized according to the following rule,
139      * from http://www.w3.org/TR/REC-xml#dt-pubid:
140      *
141      * Before a match is attempted, all strings of white space in the public
142      * identifier must be normalized to single space characters (#x20), and
143      * leading and trailing white space must be removed.
144      *
145      * @param literal The string to fill in with the public ID literal.
146      * @return True on success.
147      *
148      * <strong>Note:</strong> This method uses fStringBuffer, anything in it at
149      * the time of calling is lost.
150      */

151     protected boolean scanPubidLiteral(XMLString literal)
152         throws IOException JavaDoc, XNIException
153     {
154         int quote = fEntityScanner.scanChar();
155         if (quote != '\'' && quote != '"') {
156             reportFatalError("QuoteRequiredInPublicID", null);
157             return false;
158         }
159
160         fStringBuffer.clear();
161         // skip leading whitespace
162
boolean skipSpace = true;
163         boolean dataok = true;
164         while (true) {
165             int c = fEntityScanner.scanChar();
166             // REVISIT: it could really only be \n or 0x20; all else is normalized, no? - neilg
167
if (c == ' ' || c == '\n' || c == '\r' || c == 0x85 || c == 0x2028) {
168                 if (!skipSpace) {
169                     // take the first whitespace as a space and skip the others
170
fStringBuffer.append(' ');
171                     skipSpace = true;
172                 }
173             }
174             else if (c == quote) {
175                 if (skipSpace) {
176                     // if we finished on a space let's trim it
177
fStringBuffer.length--;
178                 }
179                 literal.setValues(fStringBuffer);
180                 break;
181             }
182             else if (XMLChar.isPubid(c)) {
183                 fStringBuffer.append((char)c);
184                 skipSpace = false;
185             }
186             else if (c == -1) {
187                 reportFatalError("PublicIDUnterminated", null);
188                 return false;
189             }
190             else {
191                 dataok = false;
192                 reportFatalError("InvalidCharInPublicID",
193                                  new Object JavaDoc[]{Integer.toHexString(c)});
194             }
195         }
196         return dataok;
197    }
198    
199     /**
200      * Normalize whitespace in an XMLString converting all whitespace
201      * characters to space characters.
202      */

203     protected void normalizeWhitespace(XMLString value) {
204         int end = value.offset + value.length;
205         for (int i = value.offset; i < end; i++) {
206             int c = value.ch[i];
207             if (XMLChar.isSpace(c)) {
208                 value.ch[i] = ' ';
209             }
210         }
211     }
212
213     // returns true if the given character is not
214
// valid with respect to the version of
215
// XML understood by this scanner.
216
protected boolean isInvalid(int value) {
217         return (!XML11Char.isXML11Valid(value));
218     } // isInvalid(int): boolean
219

220     // returns true if the given character is not
221
// valid or may not be used outside a character reference
222
// with respect to the version of XML understood by this scanner.
223
protected boolean isInvalidLiteral(int value) {
224         return (!XML11Char.isXML11ValidLiteral(value));
225     } // isInvalidLiteral(int): boolean
226

227     // returns true if the given character is
228
// a valid nameChar with respect to the version of
229
// XML understood by this scanner.
230
protected boolean isValidNameChar(int value) {
231         return (XML11Char.isXML11Name(value));
232     } // isValidNameChar(int): boolean
233

234     // returns true if the given character is
235
// a valid nameStartChar with respect to the version of
236
// XML understood by this scanner.
237
protected boolean isValidNameStartChar(int value) {
238         return (XML11Char.isXML11NameStart(value));
239     } // isValidNameStartChar(int): boolean
240

241     // returns true if the given character is
242
// a valid NCName character with respect to the version of
243
// XML understood by this scanner.
244
protected boolean isValidNCName(int value) {
245         return (XML11Char.isXML11NCName(value));
246     } // isValidNCName(int): boolean
247

248     // returns true if the given character is
249
// a valid high surrogate for a nameStartChar
250
// with respect to the version of XML understood
251
// by this scanner.
252
protected boolean isValidNameStartHighSurrogate(int value) {
253         return XML11Char.isXML11NameHighSurrogate(value);
254     } // isValidNameStartHighSurrogate(int): boolean
255

256     // note that, according to 4.3.4 of the XML 1.1 spec, XML 1.1
257
// documents may invoke 1.0 entities; thus either version decl (or none!)
258
// is allowed to appear in this context
259
protected boolean versionSupported(String JavaDoc version) {
260         return version.equals("1.1") || version.equals ("1.0");
261     } // versionSupported(String): boolean
262

263     // returns the error message key for unsupported
264
// versions of XML with respect to the version of
265
// XML understood by this scanner.
266
protected String JavaDoc getVersionNotSupportedKey () {
267         return "VersionNotSupported11";
268     } // getVersionNotSupportedKey: String
269

270 } // class XML11DTDScannerImpl
271
Popular Tags