1 /*
2 * Copyright (c) 1998, 2011, 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
40 import java.util.Vector;
41
42 /**
43 * This implements parsing of XML 1.0 DTDs.
44 * <p/>
45 * This conforms to the portion of the XML 1.0 specification related
46 * to the external DTD subset.
47 * <p/>
48 * For multi-language applications (such as web servers using XML
49 * processing to create dynamic content), a method supports choosing
50 * a locale for parser diagnostics which is both understood by the
51 * message recipient and supported by the parser.
52 * <p/>
53 * This parser produces a stream of parse events. It supports some
54 * features (exposing comments, CDATA sections, and entity references)
55 * which are not required to be reported by conformant XML processors.
56 *
57 * @author David Brownell
58 * @author Janet Koenig
59 * @author Kohsuke KAWAGUCHI
60 * @version $Id: DTDParser.java,v 1.2 2009-04-16 15:25:49 snajper Exp $
61 */
62 public class DTDParser {
63 public final static String TYPE_CDATA = "CDATA";
64 public final static String TYPE_ID = "ID";
65 public final static String TYPE_IDREF = "IDREF";
66 public final static String TYPE_IDREFS = "IDREFS";
67 public final static String TYPE_ENTITY = "ENTITY";
68 public final static String TYPE_ENTITIES = "ENTITIES";
69 public final static String TYPE_NMTOKEN = "NMTOKEN";
70 public final static String TYPE_NMTOKENS = "NMTOKENS";
71 public final static String TYPE_NOTATION = "NOTATION";
72 public final static String TYPE_ENUMERATION = "ENUMERATION";
73
74
75 // stack of input entities being merged
76 private InputEntity in;
77
78 // temporaries reused during parsing
79 private StringBuffer strTmp;
80 private char nameTmp [];
198 * Returns the handler used to for DTD parsing events.
199 */
200 public DTDEventListener getDtdHandler() {
201 return dtdHandler;
202 }
203
204 /**
205 * Parse a DTD.
206 */
207 public void parse(InputSource in)
208 throws IOException, SAXException {
209 init();
210 parseInternal(in);
211 }
212
213 /**
214 * Parse a DTD.
215 */
216 public void parse(String uri)
217 throws IOException, SAXException {
218 InputSource inSource;
219
220 init();
221 // System.out.println ("parse (\"" + uri + "\")");
222 inSource = resolver.resolveEntity(null, uri);
223
224 // If custom resolver punts resolution to parser, handle it ...
225 if (inSource == null) {
226 inSource = Resolver.createInputSource(new java.net.URL(uri), false);
227
228 // ... or if custom resolver doesn't correctly construct the
229 // input entity, patch it up enough so relative URIs work, and
230 // issue a warning to minimize later confusion.
231 } else if (inSource.getSystemId() == null) {
232 warning("P-065", null);
233 inSource.setSystemId(uri);
234 }
235
236 parseInternal(inSource);
237 }
238
239 // makes sure the parser is reset to "before a document"
240 private void init() {
241 in = null;
242
243 // alloc temporary data used in parsing
244 strTmp = new StringBuffer();
245 nameTmp = new char[20];
246 nameCache = new NameCache();
247
248 // reset doc info
249 // isInAttribute = false;
250
251 doLexicalPE = false;
252
253 entities.clear();
254 notations.clear();
255 params.clear();
256 // elements.clear ();
|
1 /*
2 * Copyright (c) 2009, 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
40 import java.util.Vector;
41
42 /**
43 * This implements parsing of XML 1.0 DTDs.
44 * <p/>
45 * This conforms to the portion of the XML 1.0 specification related
46 * to the external DTD subset.
47 * <p/>
48 * For multi-language applications (such as web servers using XML
49 * processing to create dynamic content), a method supports choosing
50 * a locale for parser diagnostics which is both understood by the
51 * message recipient and supported by the parser.
52 * <p/>
53 * This parser produces a stream of parse events. It supports some
54 * features (exposing comments, CDATA sections, and entity references)
55 * which are not required to be reported by conformant XML processors.
56 *
57 * @author David Brownell
58 * @author Janet Koenig
59 * @author Kohsuke KAWAGUCHI
60 * @version $Id: DTDParser.java,v 1.2 2009/04/16 15:25:49 snajper Exp $
61 */
62 public class DTDParser {
63 public final static String TYPE_CDATA = "CDATA";
64 public final static String TYPE_ID = "ID";
65 public final static String TYPE_IDREF = "IDREF";
66 public final static String TYPE_IDREFS = "IDREFS";
67 public final static String TYPE_ENTITY = "ENTITY";
68 public final static String TYPE_ENTITIES = "ENTITIES";
69 public final static String TYPE_NMTOKEN = "NMTOKEN";
70 public final static String TYPE_NMTOKENS = "NMTOKENS";
71 public final static String TYPE_NOTATION = "NOTATION";
72 public final static String TYPE_ENUMERATION = "ENUMERATION";
73
74
75 // stack of input entities being merged
76 private InputEntity in;
77
78 // temporaries reused during parsing
79 private StringBuffer strTmp;
80 private char nameTmp [];
198 * Returns the handler used to for DTD parsing events.
199 */
200 public DTDEventListener getDtdHandler() {
201 return dtdHandler;
202 }
203
204 /**
205 * Parse a DTD.
206 */
207 public void parse(InputSource in)
208 throws IOException, SAXException {
209 init();
210 parseInternal(in);
211 }
212
213 /**
214 * Parse a DTD.
215 */
216 public void parse(String uri)
217 throws IOException, SAXException {
218 InputSource in;
219
220 init();
221 // System.out.println ("parse (\"" + uri + "\")");
222 in = resolver.resolveEntity(null, uri);
223
224 // If custom resolver punts resolution to parser, handle it ...
225 if (in == null) {
226 in = Resolver.createInputSource(new java.net.URL(uri), false);
227
228 // ... or if custom resolver doesn't correctly construct the
229 // input entity, patch it up enough so relative URIs work, and
230 // issue a warning to minimize later confusion.
231 } else if (in.getSystemId() == null) {
232 warning("P-065", null);
233 in.setSystemId(uri);
234 }
235
236 parseInternal(in);
237 }
238
239 // makes sure the parser is reset to "before a document"
240 private void init() {
241 in = null;
242
243 // alloc temporary data used in parsing
244 strTmp = new StringBuffer();
245 nameTmp = new char[20];
246 nameCache = new NameCache();
247
248 // reset doc info
249 // isInAttribute = false;
250
251 doLexicalPE = false;
252
253 entities.clear();
254 notations.clear();
255 params.clear();
256 // elements.clear ();
|