KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > xml > DOMParser


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on 24.08.2004
33  */

34 package com.nightlabs.xml;
35
36 import org.apache.xerces.util.SymbolTable;
37 import org.apache.xerces.xni.grammars.XMLGrammarPool;
38 import org.apache.xerces.xni.parser.XMLParserConfiguration;
39 import org.xml.sax.EntityResolver JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41 import org.xml.sax.SAXNotRecognizedException JavaDoc;
42 import org.xml.sax.SAXNotSupportedException JavaDoc;
43
44 /**
45  * You should use always this DOMParser instead of the jakarta original one, because
46  * even if validation is switched off, it tries to resolve the external dtds, means
47  * it needs an online connection. Thus, this Parser can be switched to OFFLINE mode,
48  * which is the default.
49  * <p>
50  * To test the whole project for validity, you can set the global constant DEFAULT_ONLINE_MODE
51  * to true - in case you don't call setOnlineMode(...) within your modules.
52  *
53  * @author marco
54  */

55 public class DOMParser extends org.apache.xerces.parsers.DOMParser
56 {
57     public static final boolean DEFAULT_ONLINE_MODE = false;
58     
59     public DOMParser()
60         throws SAXException JavaDoc
61     {
62         super();
63         setOnlineMode(DEFAULT_ONLINE_MODE);
64     }
65     public DOMParser(SymbolTable arg0)
66         throws SAXException JavaDoc
67     {
68         super(arg0);
69         setOnlineMode(DEFAULT_ONLINE_MODE);
70     }
71     public DOMParser(SymbolTable arg0, XMLGrammarPool arg1)
72         throws SAXException JavaDoc
73     {
74         super(arg0, arg1);
75         setOnlineMode(DEFAULT_ONLINE_MODE);
76     }
77     public DOMParser(XMLParserConfiguration arg0)
78         throws SAXException JavaDoc
79     {
80         super(arg0);
81         setOnlineMode(DEFAULT_ONLINE_MODE);
82     }
83     
84     protected EntityResolver JavaDoc defaultEntityResolver = null;
85     protected DummyEntityResolver dummyEntityResolver = null;
86     
87     /**
88      * Even if the feature <i>validation</i> is switched off, the
89      * Parser tries to resolve the DTD. This means, to simply switch the feature
90      * off, is not sufficient. Hence, if online is false, this method
91      * creates a special DummyEntityResolver which returns empty Entities.
92      * Because they are unusable, <i>validation</i> is switched off additionally.
93      * If you call this method with online = <code>true</code> validation
94      * will be re-enabled.
95      * <p>
96      * for the list of parser features see
97      * <a HREF="http://xml.apache.org/xerces2-j/features.html">http://xml.apache.org/xerces2-j/features.html</a>
98      *
99      * @param online Whether we are online or not.
100      * @throws SAXNotRecognizedException
101      * @throws SAXNotSupportedException
102      */

103     public void setOnlineMode(boolean online)
104         throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc
105     {
106         // list of features: http://xml.apache.org/xerces2-j/features.html
107
// We deactivate validation because lomboz generates non-valid xml files, if
108
// there are no beans in a jar.
109
// And because we might be offline.
110
this.setFeature("http://xml.org/sax/features/validation", online);
111         if (!online && defaultEntityResolver == null) {
112             defaultEntityResolver = this.getEntityResolver();
113             if (dummyEntityResolver == null)
114                 dummyEntityResolver = new DummyEntityResolver();
115             this.setEntityResolver(dummyEntityResolver);
116         }
117         else if (online && dummyEntityResolver != null) // We check for dummyEntityResolver,
118
this.setEntityResolver(defaultEntityResolver); // because the defaultEntityResolver might
119
// be null from the beginning.
120
}
121 }
122
Popular Tags