KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > XML11DTDScannerImpl


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl;
18
19 import java.io.IOException JavaDoc;
20
21 import org.apache.xerces.util.SymbolTable;
22 import org.apache.xerces.util.XML11Char;
23 import org.apache.xerces.util.XMLChar;
24 import org.apache.xerces.util.XMLStringBuffer;
25 import org.apache.xerces.xni.XMLString;
26 import org.apache.xerces.xni.XNIException;
27
28 /**
29  * This class is responsible for scanning the declarations found
30  * in the internal and external subsets of a DTD in an XML document.
31  * The scanner acts as the sources for the DTD information which is
32  * communicated to the DTD handlers.
33  * <p>
34  * This component requires the following features and properties from the
35  * component manager that uses it:
36  * <ul>
37  * <li>http://xml.org/sax/features/validation</li>
38  * <li>http://apache.org/xml/features/scanner/notify-char-refs</li>
39  * <li>http://apache.org/xml/properties/internal/symbol-table</li>
40  * <li>http://apache.org/xml/properties/internal/error-reporter</li>
41  * <li>http://apache.org/xml/properties/internal/entity-manager</li>
42  * </ul>
43  *
44  * @xerces.internal
45  *
46  * @author Arnaud Le Hors, IBM
47  * @author Andy Clark, IBM
48  * @author Glenn Marcy, IBM
49  * @author Eric Ye, IBM
50  *
51  * @version $Id: XML11DTDScannerImpl.java,v 1.13 2004/10/04 21:45:48 mrglavas Exp $
52  */

53 public class XML11DTDScannerImpl
54     extends XMLDTDScannerImpl {
55
56     /** Array of 3 strings. */
57     private String JavaDoc[] fStrings = new String JavaDoc[3];
58
59     /** String. */
60     private XMLString fString = new XMLString();
61
62     /** String buffer. */
63     private XMLStringBuffer fStringBuffer = new XMLStringBuffer();
64
65     /** String buffer. */
66     private XMLStringBuffer fStringBuffer2 = new XMLStringBuffer();
67     private XMLStringBuffer fStringBuffer3 = new XMLStringBuffer();
68
69     //
70
// Constructors
71
//
72

73     /** Default constructor. */
74     public XML11DTDScannerImpl() {super();} // <init>()
75

76     /** Constructor for he use of non-XMLComponentManagers. */
77     public XML11DTDScannerImpl(SymbolTable symbolTable,
78                 XMLErrorReporter errorReporter, XMLEntityManager entityManager) {
79         super(symbolTable, errorReporter, entityManager);
80     }
81
82     //
83
// XMLDTDScanner methods
84
//
85

86     //
87
// XMLScanner methods
88
//
89
// NOTE: this is a carbon copy of the code in XML11DocumentScannerImpl;
90
// we need to override these methods in both places. Ah for
91
// multiple inheritance...
92
// This needs to be refactored!!! - NG
93
/**
94      * Scans public ID literal.
95      *
96      * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
97      * [13] PubidChar::= #x20 | #xD | #xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%]
98      *
99      * The returned string is normalized according to the following rule,
100      * from http://www.w3.org/TR/REC-xml#dt-pubid:
101      *
102      * Before a match is attempted, all strings of white space in the public
103      * identifier must be normalized to single space characters (#x20), and
104      * leading and trailing white space must be removed.
105      *
106      * @param literal The string to fill in with the public ID literal.
107      * @return True on success.
108      *
109      * <strong>Note:</strong> This method uses fStringBuffer, anything in it at
110      * the time of calling is lost.
111      */

112     protected boolean scanPubidLiteral(XMLString literal)
113         throws IOException JavaDoc, XNIException
114     {
115         int quote = fEntityScanner.scanChar();
116         if (quote != '\'' && quote != '"') {
117             reportFatalError("QuoteRequiredInPublicID", null);
118             return false;
119         }
120
121         fStringBuffer.clear();
122         // skip leading whitespace
123
boolean skipSpace = true;
124         boolean dataok = true;
125         while (true) {
126             int c = fEntityScanner.scanChar();
127             // REVISIT: it could really only be \n or 0x20; all else is normalized, no? - neilg
128
if (c == ' ' || c == '\n' || c == '\r' || c == 0x85 || c == 0x2028) {
129                 if (!skipSpace) {
130                     // take the first whitespace as a space and skip the others
131
fStringBuffer.append(' ');
132                     skipSpace = true;
133                 }
134             }
135             else if (c == quote) {
136                 if (skipSpace) {
137                     // if we finished on a space let's trim it
138
fStringBuffer.length--;
139                 }
140                 literal.setValues(fStringBuffer);
141                 break;
142             }
143             else if (XMLChar.isPubid(c)) {
144                 fStringBuffer.append((char)c);
145                 skipSpace = false;
146             }
147             else if (c == -1) {
148                 reportFatalError("PublicIDUnterminated", null);
149                 return false;
150             }
151             else {
152                 dataok = false;
153                 reportFatalError("InvalidCharInPublicID",
154                                  new Object JavaDoc[]{Integer.toHexString(c)});
155             }
156         }
157         return dataok;
158    }
159    
160     /**
161      * Normalize whitespace in an XMLString converting all whitespace
162      * characters to space characters.
163      */

164     protected void normalizeWhitespace(XMLString value) {
165         int end = value.offset + value.length;
166         for (int i = value.offset; i < end; ++i) {
167             int c = value.ch[i];
168             if (XMLChar.isSpace(c)) {
169                 value.ch[i] = ' ';
170             }
171         }
172     }
173     
174     /**
175      * Normalize whitespace in an XMLString converting all whitespace
176      * characters to space characters.
177      */

178     protected void normalizeWhitespace(XMLString value, int fromIndex) {
179         int end = value.offset + value.length;
180         for (int i = value.offset + fromIndex; i < end; ++i) {
181             int c = value.ch[i];
182             if (XMLChar.isSpace(c)) {
183                 value.ch[i] = ' ';
184             }
185         }
186     }
187     
188     /**
189      * Checks whether this string would be unchanged by normalization.
190      *
191      * @return -1 if the value would be unchanged by normalization,
192      * otherwise the index of the first whitespace character which
193      * would be transformed.
194      */

195     protected int isUnchangedByNormalization(XMLString value) {
196         int end = value.offset + value.length;
197         for (int i = value.offset; i < end; ++i) {
198             int c = value.ch[i];
199             if (XMLChar.isSpace(c)) {
200                 return i - value.offset;
201             }
202         }
203         return -1;
204     }
205
206     // returns true if the given character is not
207
// valid with respect to the version of
208
// XML understood by this scanner.
209
protected boolean isInvalid(int value) {
210         return (!XML11Char.isXML11Valid(value));
211     } // isInvalid(int): boolean
212

213     // returns true if the given character is not
214
// valid or may not be used outside a character reference
215
// with respect to the version of XML understood by this scanner.
216
protected boolean isInvalidLiteral(int value) {
217         return (!XML11Char.isXML11ValidLiteral(value));
218     } // isInvalidLiteral(int): boolean
219

220     // returns true if the given character is
221
// a valid nameChar with respect to the version of
222
// XML understood by this scanner.
223
protected boolean isValidNameChar(int value) {
224         return (XML11Char.isXML11Name(value));
225     } // isValidNameChar(int): boolean
226

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

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

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

249     // note that, according to 4.3.4 of the XML 1.1 spec, XML 1.1
250
// documents may invoke 1.0 entities; thus either version decl (or none!)
251
// is allowed to appear in this context
252
protected boolean versionSupported(String JavaDoc version) {
253         return version.equals("1.1") || version.equals ("1.0");
254     } // versionSupported(String): boolean
255

256     // returns the error message key for unsupported
257
// versions of XML with respect to the version of
258
// XML understood by this scanner.
259
protected String JavaDoc getVersionNotSupportedKey () {
260         return "VersionNotSupported11";
261     } // getVersionNotSupportedKey: String
262

263 } // class XML11DTDScannerImpl
264
Popular Tags