001 /* 002 // $Id: //open/util/resgen/src/org/eigenbase/xom/wrappers/XercesDOMParser.java#4 $ 003 // Package org.eigenbase.xom is an XML Object Mapper. 004 // Copyright (C) 2005-2005 The Eigenbase Project 005 // Copyright (C) 2005-2005 Disruptive Tech 006 // Copyright (C) 2005-2005 LucidEra, Inc. 007 // Portions Copyright (C) 2001-2005 Kana Software, Inc. and others. 008 // 009 // This library is free software; you can redistribute it and/or modify it 010 // under the terms of the GNU Lesser General Public License as published by the 011 // Free Software Foundation; either version 2 of the License, or (at your 012 // option) any later version approved by The Eigenbase Project. 013 // 014 // This library is distributed in the hope that it will be useful, 015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 // GNU Lesser General Public License for more details. 018 // 019 // You should have received a copy of the GNU Lesser General Public License 020 // along with this library; if not, write to the Free Software 021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 022 // 023 // klo, 1 August, 2001 024 */ 025 026 package org.eigenbase.xom.wrappers; 027 028 import org.eigenbase.xom.DOMWrapper; 029 import org.eigenbase.xom.XOMException; 030 import org.apache.xerces.dom.DocumentImpl; 031 import org.apache.xerces.parsers.DOMParser; 032 import org.w3c.dom.Document; 033 import org.w3c.dom.Node; 034 import org.xml.sax.InputSource; 035 import org.xml.sax.SAXException; 036 037 import java.io.IOException; 038 039 /** 040 * This private helper class presents a GenericDOMParser using Xerces, with 041 * simple error handling appropriate for a testing environment. 042 */ 043 044 public class XercesDOMParser extends GenericDOMParser { 045 private DOMParser parser; 046 047 /** 048 * Constructs a non-validating Xerces DOM Parser. 049 */ 050 public XercesDOMParser() throws XOMException { 051 this(false); 052 } 053 054 /** 055 * Constructs a Xerces DOM Parser. 056 * @param validate whether to enable validation 057 */ 058 public XercesDOMParser(boolean validate) throws XOMException { 059 parser = new DOMParser(); 060 try { 061 if (!validate) { 062 parser.setFeature(VALIDATION_FEATURE, false); 063 parser.setFeature(LOAD_EXTERNAL_DTD_FEATURE, false); 064 } 065 } catch (SAXException e) { 066 throw new XOMException(e, "Error setting up validation"); 067 } 068 069 parser.setErrorHandler(this); 070 document = new DocumentImpl(); 071 } 072 073 // implement GenericDOMParser 074 protected Document parseInputSource(InputSource in) throws XOMException { 075 prepareParse(); 076 try { 077 parser.parse(in); 078 } catch (SAXException ex) { 079 // Display any pending errors 080 handleErrors(); 081 throw new XOMException(ex, "Document parse failed"); 082 } catch (IOException ex) { 083 // Display any pending errors 084 handleErrors(); 085 throw new XOMException(ex, "Document parse failed"); 086 } 087 088 handleErrors(); 089 return parser.getDocument(); 090 } 091 092 // implement Parser 093 public DOMWrapper create(String tagName) { 094 Node node = document.createElement(tagName); 095 return new W3CDOMWrapper(node, this); 096 } 097 } 098 099 // End XercesDOMParser.java