1 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 39 public Document parse( String fileLocation, boolean validating ) throws Exception { 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 { 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 fileLocation, DocumentBuilder documentBuilder ) throws Exception { 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 { 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 |