001    /*
002      Copyright (C) 2001 Zachary Medico
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.core.parsers.xml;
020    
021    import java.io.*;
022    import javax.xml.parsers.*;
023    
024    import org.xml.sax.*;
025    import org.w3c.dom.*;
026    
027    import org.objectweb.jac.util.*;
028    
029    public class XmlParserJAXP implements XmlParser{
030        
031        private DocumentBuilderFactory documentBuilderFactory;
032    
033        /**
034         *
035         *  @param fileLocation
036         *  @param validating
037         *
038         */
039        public Document parse( String fileLocation, boolean validating ) throws Exception {
040            
041            if ( documentBuilderFactory==null )
042                documentBuilderFactory = DocumentBuilderFactory.newInstance();
043            
044            documentBuilderFactory.setValidating( validating );
045            
046            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
047            
048            if (validating)
049                documentBuilder.setErrorHandler(new SaxParserErrorHandler());
050            
051            return parse( fileLocation, documentBuilder );
052            
053        }
054        
055        public Document parse(InputStream input, boolean validating) throws Exception {
056            if ( documentBuilderFactory==null )
057                documentBuilderFactory = DocumentBuilderFactory.newInstance();
058            
059            documentBuilderFactory.setValidating( validating );
060            
061            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
062            
063            if (validating)
064                documentBuilder.setErrorHandler(new SaxParserErrorHandler());
065            
066            return parse( input, documentBuilder );
067        }
068        
069        
070        private static Document parse( String fileLocation, DocumentBuilder documentBuilder  ) throws Exception {                
071    
072            InputStream is = new URLInputStream(fileLocation).getInputStream();
073            return documentBuilder.parse(is);        
074        }
075        
076        private static Document parse( InputStream input, DocumentBuilder documentBuilder  ) throws Exception {                              
077    
078            return documentBuilder.parse(input);        
079        }    
080        
081    
082        
083        private static class SaxParserErrorHandler implements ErrorHandler{
084            
085            public void error(SAXParseException e) throws SAXException {
086                System.err.println("error: "+ e.getMessage());
087            }
088            
089            public void fatalError(SAXParseException e) throws SAXException {
090                System.err.println("fatal error: "+ e.getMessage());
091            }
092            
093            public void warning(SAXParseException e) throws SAXException {
094                System.err.println("warning: "+ e.getMessage());
095            }
096            
097        }
098        
099    }