Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4cba97aff99c11af8a4d46f8362826c27f329401 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*******************************************************************************
 * Copyright (c) 2001, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.j2ee.internal.xml;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.jem.util.logger.proxy.Logger;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/**
 * Insert the type's description here.
 * Creation date: (4/11/2001 11:58:50 AM)
 * @author: Administrator
 */
public class GeneralXmlDocumentReader
{
    protected InputSource inputSource;
    protected EntityResolver entityResolver;
    protected ErrorHandler errorHandler;
    protected boolean doValidate = false;
    protected boolean doAllowJavaEncodings = false;

/**
 * GeneralXmlDocumentReader constructor comment.
 */
    public GeneralXmlDocumentReader() 
    {
        super();
    }
/**
 * GeneralXmlDocumentReader constructor comment.
 */
    public GeneralXmlDocumentReader(InputSource source) 
    {
        super();
        setInputSource(source);
    }
/**
 * GeneralXmlDocumentReader constructor comment.
 */
    public GeneralXmlDocumentReader(InputSource source, EntityResolver resolver, ErrorHandler handler) 
    {
        super();
        setInputSource(source);
        setEntityResolver(resolver);
        setErrorHandler(handler);
    }
/**
 * Insert the method's description here.
 * Creation date: (4/11/2001 12:04:18 PM)
 * @return org.xml.sax.EntityResolver
 */
    protected org.xml.sax.EntityResolver createDefaultEntityResolver() 
    {
        return null;
    }
/**
 * Insert the method's description here.
 * Creation date: (4/11/2001 12:04:18 PM)
 * @return org.xml.sax.ErrorHandler
 */
    protected org.xml.sax.ErrorHandler createDefaultErrorHandler() 
    {
        return new ErrorHandler() {
        	/* (non-Javadoc)
			 * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
			 */
			public void error(SAXParseException exception) throws SAXException {
				throw exception;
			}
			/* (non-Javadoc)
			 * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
			 */
			public void fatalError(SAXParseException exception) throws SAXException {
				throw exception;
			}
			/* (non-Javadoc)
			 * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
			 */
			public void warning(SAXParseException exception) throws SAXException {
				Logger.getLogger().logWarning(exception);
			}
        };
    }
    protected javax.xml.parsers.DocumentBuilder createNewDOMParser() 
    {
        try
        {
            javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
            setFeatures(dbf);
            return dbf.newDocumentBuilder();

        }
        catch (Exception ie)
        {
            handleException(J2EEXMLResourceHandler.getString(XMLParseResourceHandler.failed_instantiating_EXC_, (new Object[] {"Failed to instantiate parser"})), ie); //$NON-NLS-1$ // = "Failed instantiating: "
        }
        return null;
    }
/**
 * Insert the method's description here.
 * Creation date: (4/11/2001 12:01:34 PM)
 * @return boolean
 */
    public boolean doAllowJavaEncodings() 
    {
        return doAllowJavaEncodings;
    }
/**
 * Insert the method's description here.
 * Creation date: (4/11/2001 12:01:34 PM)
 * @return boolean
 */
    public boolean doValidate() 
    {
        return doValidate;
    }
/**
 * Insert the method's description here.
 * Creation date: (4/11/2001 1:34:14 PM)
 * @return java.lang.ClassLoader
 */
    public java.lang.ClassLoader getClassLoader() 
    {
        ClassLoader cl = getClass().getClassLoader();
        if (cl == null)
            cl = Thread.currentThread().getContextClassLoader();
        if (cl == null)
            cl = ClassLoader.getSystemClassLoader();

        return cl;
    }
    public Document getDocument() 
    {
        return parseDocument();
    }
    
    protected Document getDocument(javax.xml.parsers.DocumentBuilder domParser) 
    {
        return domParser.newDocument();
    }


/**
 * Insert the method's description here.
 * Creation date: (4/11/2001 12:04:18 PM)
 * @return org.xml.sax.EntityResolver
 */
    public org.xml.sax.EntityResolver getEntityResolver() 
    {
        if (entityResolver == null)
            entityResolver = createDefaultEntityResolver();
        return entityResolver;
    }
/**
 * Insert the method's description here.
 * Creation date: (4/11/2001 12:04:18 PM)
 * @return org.xml.sax.ErrorHandler
 */
    public org.xml.sax.ErrorHandler getErrorHandler() 
    {
        if (errorHandler == null)
        {
            errorHandler = createDefaultErrorHandler();
        }
        return errorHandler;
    }

/**
 * Insert the method's description here.
 * Creation date: (4/11/2001 12:04:18 PM)
 * @return org.xml.sax.InputSource
 */
    public org.xml.sax.InputSource getInputSource() 
    {
        return inputSource;
    }
    
    protected void handleException(String aMessage, Throwable anException) 
    {
        if (anException instanceof InvocationTargetException)
        {
            InvocationTargetException invocationEx =
            (InvocationTargetException) anException;
            if (invocationEx.getTargetException() != null)
                handleException(aMessage, invocationEx.getTargetException());
        }
        else
            primHandleException(aMessage, anException);
    }
    
    protected Document parse(javax.xml.parsers.DocumentBuilder domParser, InputSource is) 
    {
        try
        {
            return domParser.parse(is);
        }
        catch (org.xml.sax.SAXException ex)
        {
            handleException(J2EEXMLResourceHandler.getString(XMLParseResourceHandler.failed_instantiating_EXC_, (new Object[] {"SAX Parser error"})), ex); //$NON-NLS-1$ // = "Failed instantiating: "

        }
        catch (java.io.IOException ex)
        {
            handleException(J2EEXMLResourceHandler.getString(XMLParseResourceHandler.failed_instantiating_EXC_, (new Object[] {"IO Exception on input stream"})), ex); //$NON-NLS-1$ // = "Failed instantiating: "
        }
        return null;
    }


    public Document parseDocument() 
    {
    	ClassLoader prevClassLoader = Thread.currentThread().getContextClassLoader();
    	try {
    		Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
			javax.xml.parsers.DocumentBuilder parser = createNewDOMParser();
        	setEntityResolver(parser);
        	setErrorHandler(parser);
        	return parse(parser, getInputSource());
	} finally {
      Thread.currentThread().setContextClassLoader(prevClassLoader);
	}
    	
    	
    	
    	
        
    }
    
    protected void primHandleException(String aMessage, Throwable anException) 
    {
        org.eclipse.jem.util.logger.proxy.Logger.getLogger().logError(anException);
        throw new RuntimeException(aMessage);

    }
/**
 * Insert the method's description here.
 * Creation date: (4/11/2001 12:01:34 PM)
 * @param newAllowJavaEncodings boolean
 */
    public void setAllowJavaEncodings(boolean newAllowJavaEncodings) 
    {
        doAllowJavaEncodings = newAllowJavaEncodings;
    }
    
    protected void setEntityResolver(javax.xml.parsers.DocumentBuilder domParser) 
    {
        if (getEntityResolver() != null)
        {
            domParser.setEntityResolver(getEntityResolver());
        }
    }


/**
 * Insert the method's description here.
 * Creation date: (4/11/2001 12:04:18 PM)
 * @param newEntityResolver org.xml.sax.EntityResolver
 */
    public void setEntityResolver(org.xml.sax.EntityResolver newEntityResolver) 
    {
        entityResolver = newEntityResolver;
    }
    

    protected void setErrorHandler(javax.xml.parsers.DocumentBuilder domParser) 
    {
        if (getErrorHandler() != null)
        {
            domParser.setErrorHandler(getErrorHandler());
        }
    }


/**
 * Insert the method's description here.
 * Creation date: (4/11/2001 12:04:18 PM)
 * @param newErrorHandler org.xml.sax.ErrorHandler
 */
    public void setErrorHandler(org.xml.sax.ErrorHandler newErrorHandler) 
    {
        errorHandler = newErrorHandler;
    }

    protected void setFeatures(javax.xml.parsers.DocumentBuilderFactory domParser) 
    {
        try {
			domParser.setValidating(doValidate());
	        domParser.setAttribute("http://apache.org/xml/features/allow-java-encodings", new Boolean(doAllowJavaEncodings())); //$NON-NLS-1$
	        domParser.setExpandEntityReferences(true);
		} catch(IllegalArgumentException e) {
			//Ignore
		}
    }


/**
 * Insert the method's description here.
 * Creation date: (4/11/2001 12:04:18 PM)
 * @param newInputSource org.xml.sax.InputSource
 */
    public void setInputSource(org.xml.sax.InputSource newInputSource) 
    {
        inputSource = newInputSource;
    }
/**
 * Insert the method's description here.
 * Creation date: (4/11/2001 12:01:34 PM)
 * @param newValidate boolean
 */
    public void setValidate(boolean newValidate) 
    {
        doValidate = newValidate;
    }
}


Back to the top