users@jaxb.java.net

RE: Re: Ignore DOCTYPE dtd Declaration

From: Douglas Seifert <dseifert_at_SeeBeyond.com>
Date: Fri, 14 Nov 2003 09:44:09 -0800

Here is the below mentioned code using the load-external-dtd feature of
xerces for anybody that needs it ...

 

import java.io.FileInputStream;

 

import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;

import javax.xml.bind.Marshaller;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import javax.xml.transform.sax.SAXSource;

 

import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

import org.xml.sax.XMLReader;

 

public class NoDtdJaxbParser {

      private static final String EXTERNAL_DTD_LOADING_FEATURE =

 
"http://apache.org/xml/features/nonvalidating/load-external-dtd";

 

      private SAXParserFactory mParserFactory = null;

      private XMLReader mParser = null;

 

      public NoDtdJaxbParser() {

            mParserFactory = SAXParserFactory.newInstance();

            mParserFactory.setNamespaceAware(true);

            mParserFactory.setValidating(false);

      }

 

      public Object parseXmlFile(File aXmlFile, String aPackage) throws
JAXBException {

            XMLReader lReader = null;

            try {

                  lReader = getXMLReader();

            } catch (Exception e) {

                  JAXBException lEx = new JAXBException("Error creating
SAX Parser");

                  lEx.setLinkedException(e);

                  throw lEx;

            }

            JAXBContext lContext = JAXBContext.newInstance(aPackage);

            Unmarshaller lUnmarshaller = lContext.createUnmarshaller();

            FileInputStream lXmlStream = null;

            try {

                  lXmlStream = new FileInputStream(aXmlFile);

                  SAXSource lSource = new SAXSource(lReader, new
InputSource(lXmlStream));

                  return lUnmarshaller.unmarshal(lSource);

            } finally {

                  if (lXmlStream != null) {

                        try {

                              lXmlStream.close();

                        } catch (Exception e) {

                              // ignore

                        }

                  }

            }

      }

 

      private synchronized XMLReader getXmlReader() throws
ParserConfigurationException, SAXException {

            if (mParser == null) {

                  SAXParser lSaxParser = mParserFactory.newSAXParser();

                  XMLReader lReader = lSaxParser.getXMLReader();

                  // Implementation note: The following method call will
raise an exception if

                  // xerxes is not the SAX Parser. You may choose
instead to catch the raised

                  // exception and ignore it.

                  lReader.setFeature(EXTERNAL_DTD_LOADING_FEATURE,
false);

                  mParser = lReader;

            }

            return mParser;

      }

}

 

-----Original Message-----
From: Douglas Seifert
Sent: Thursday, November 13, 2003 7:59 AM
To: users_at_jaxb.dev.java.net
Subject: RE: Re: Ignore DOCTYPE dtd Declaration

 

Andrew,

 

If you are using xerces, there is this "feature" available (note it is
not a "property"):

 

http://apache.org/xml/features/nonvalidating/load-external-dtd

 

This feature is referenced here:
http://xml.apache.org/xerces-j/features.html

 

I believe you would call the setFeature() method on the SAXParser once
you created it. If you are using j2sdk rather than xerces, I don't know
what the equivalent feature, if any, would be. But I haven't tried
looking for it.

 

I am using xerces, but I haven't tried setting this feature yet. If it
works, I will let the list know and provide a code sample.

 

JAXB team, it might make things easier to provide a way to set both
features and properties for the underlying SAX parser through the
JAXBContext and/or Marshaller and Unmarshaller interfaces. But on the
other hand, providing this might make things easier for the XML impaired
to mess things up by not forcing them to learn the dense XML apis.

 

-Doug Seifert