KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > parsers > xml > XmlParserJAXP


1 /*
2   Copyright (C) 2001 Zachary Medico
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.core.parsers.xml;
20
21 import java.io.*;
22 import javax.xml.parsers.*;
23
24 import org.xml.sax.*;
25 import org.w3c.dom.*;
26
27 import org.objectweb.jac.util.*;
28
29 public class XmlParserJAXP implements XmlParser{
30     
31     private DocumentBuilderFactory documentBuilderFactory;
32
33     /**
34      *
35      * @param fileLocation
36      * @param validating
37      *
38      */

39     public Document parse( String JavaDoc fileLocation, boolean validating ) throws Exception JavaDoc {
40         
41         if ( documentBuilderFactory==null )
42             documentBuilderFactory = DocumentBuilderFactory.newInstance();
43         
44         documentBuilderFactory.setValidating( validating );
45         
46         DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
47         
48         if (validating)
49             documentBuilder.setErrorHandler(new SaxParserErrorHandler());
50         
51         return parse( fileLocation, documentBuilder );
52         
53     }
54     
55     public Document parse(InputStream input, boolean validating) throws Exception JavaDoc {
56         if ( documentBuilderFactory==null )
57             documentBuilderFactory = DocumentBuilderFactory.newInstance();
58         
59         documentBuilderFactory.setValidating( validating );
60         
61         DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
62         
63         if (validating)
64             documentBuilder.setErrorHandler(new SaxParserErrorHandler());
65         
66         return parse( input, documentBuilder );
67     }
68     
69     
70     private static Document parse( String JavaDoc fileLocation, DocumentBuilder documentBuilder ) throws Exception JavaDoc {
71
72         InputStream is = new URLInputStream(fileLocation).getInputStream();
73         return documentBuilder.parse(is);
74     }
75     
76     private static Document parse( InputStream input, DocumentBuilder documentBuilder ) throws Exception JavaDoc {
77
78         return documentBuilder.parse(input);
79     }
80     
81
82     
83     private static class SaxParserErrorHandler implements ErrorHandler{
84         
85         public void error(SAXParseException e) throws SAXException {
86             System.err.println("error: "+ e.getMessage());
87         }
88         
89         public void fatalError(SAXParseException e) throws SAXException {
90             System.err.println("fatal error: "+ e.getMessage());
91         }
92         
93         public void warning(SAXParseException e) throws SAXException {
94             System.err.println("warning: "+ e.getMessage());
95         }
96         
97     }
98     
99 }
100
Popular Tags