Source code: javatools/xml/XMLUtils.java
1 /*
2 * XMLUtils.java
3 *
4 * Created on 22 aprile 2003, 9.54
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 *
24 * This file contains code from the "XMLGrammarBuilder.java" of "Xerces2-J"
25 * package from Apache. Here is their Copyright notice:
26 *
27 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
28 * reserved.
29 */
30
31 package javatools.xml;
32
33 import java.io.IOException;
34 import javax.xml.parsers.*;
35 import org.xml.sax.*;
36 import org.apache.xerces.parsers.*;
37 import org.apache.xerces.util.*;
38 import org.apache.xerces.impl.Constants;
39 import org.apache.xerces.xni.grammars.*;
40 import org.apache.xerces.xni.parser.*;
41
42 /**
43 *
44 * @author Antonio Petrelli
45 * @version 0.2.0
46 */
47 public class XMLUtils {
48
49 public static SymbolTable newSymbolTable() {
50 return new SymbolTable(BIG_PRIME);
51 }
52
53 public static XMLGrammarPool newXMLGrammarPool(SymbolTable sym, String schema)
54 throws IOException {
55 XMLGrammarPreparser preparser;
56 Grammar g;
57 XMLGrammarPool grammarPool;
58 String tempString;
59
60 preparser = new XMLGrammarPreparser(sym);
61 grammarPool = new XMLGrammarPoolImpl();
62 preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
63 preparser.setProperty(GRAMMAR_POOL, grammarPool);
64 preparser.setFeature(NAMESPACES_FEATURE_ID, true);
65 preparser.setFeature(VALIDATION_FEATURE_ID, true);
66 // note we can set schema features just in case...
67 preparser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
68 preparser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, false);
69 tempString = XMLUtils.class.getClassLoader().getResource(schema).toString();
70 g = preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,
71 new XMLInputSource(null, tempString, null));
72 return grammarPool;
73 }
74
75 public static void setValidating(SAXParserFactory factory, boolean value)
76 throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
77 factory.setFeature(NAMESPACES_FEATURE_ID, value);
78 factory.setFeature(VALIDATION_FEATURE_ID, value);
79 factory.setFeature(SCHEMA_VALIDATION_FEATURE_ID, value);
80 }
81
82 public static void setSchemaFullChecking(SAXParserFactory factory, boolean value)
83 throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
84 factory.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, value);
85 }
86
87 public static void configureParser(javax.xml.parsers.SAXParser parser,
88 SymbolTable sym, XMLGrammarPool grammarPool)
89 throws SAXNotRecognizedException, SAXNotSupportedException {
90 parser.setProperty(SYMBOL_TABLE, sym);
91 parser.setProperty(GRAMMAR_POOL, grammarPool);
92 }
93
94 /** Property identifier: symbol table. */
95 protected static final String SYMBOL_TABLE =
96 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
97
98 /** Property identifier: grammar pool. */
99 protected static final String GRAMMAR_POOL =
100 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
101
102 // feature ids
103
104 /** Namespaces feature id (http://xml.org/sax/features/namespaces). */
105 protected static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces";
106
107 /** Validation feature id (http://xml.org/sax/features/validation). */
108 protected static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation";
109
110 /** Schema validation feature id (http://apache.org/xml/features/validation/schema). */
111 protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema";
112
113 /** Schema full checking feature id (http://apache.org/xml/features/validation/schema-full-checking). */
114 protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking";
115
116 // a larg(ish) prime to use for a symbol table to be shared
117 // among
118 // potentially man parsers. Start one as close to 2K (20
119 // times larger than normal) and see what happens...
120 protected static final int BIG_PRIME = 2039;
121 }