Code example for SAXParserFactory

Methods: newSAXParsersetFeature

0
   { 
       try 
       { 
          SAXParserFactory spf = SAXParserFactory.newInstance();
          configureParserFactory(spf);
          SAXParser sp = spf.newSAXParser();
          XMLReader xmlReader = sp.getXMLReader();
          SAXSource saxSource = new SAXSource(xmlReader, source);
          return delegate.unmarshal(saxSource);
      } 
      catch (SAXException e)
      { 
         throw new JAXBException(e);
      } 
      catch (ParserConfigurationException e)
      { 
         throw new JAXBException(e);
      } 
   } 
 
   public Object unmarshal(Node node) throws JAXBException {
      return delegate.unmarshal(node);
   } 
 
   public Object unmarshal(Source source) throws JAXBException {
      if(source instanceof SAXSource)
      { 
         try 
         { 
            SAXParserFactory spf = SAXParserFactory.newInstance();
            configureParserFactory(spf);
            SAXParser sp = spf.newSAXParser();
            XMLReader xmlReader = sp.getXMLReader();
            ((SAXSource) source).setXMLReader(xmlReader);
            return delegate.unmarshal(source);
         } 
         catch (SAXException e)
         { 
            throw new JAXBException(e);
         } 
         catch (ParserConfigurationException e)
         { 
            throw new JAXBException(e);
         } 
      } 
       
      throw new UnsupportedOperationException(errorMessage("Source, Class<T>"));
   } 
 
   public Object unmarshal(XMLStreamReader reader) throws JAXBException {
      throw new UnsupportedOperationException(errorMessage("XMLStreamReader"));
   } 
 
   public Object unmarshal(XMLEventReader reader) throws JAXBException {
      throw new UnsupportedOperationException(errorMessage("XMLEventReader"));
   } 
 
   public <T> JAXBElement<T> unmarshal(Node node, Class<T> declaredType) throws JAXBException {
      throw new UnsupportedOperationException(errorMessage("Node, Class<T>"));
   } 
 
   public <T> JAXBElement<T> unmarshal(Source source, Class<T> declaredType) throws JAXBException
   { 
      if(source instanceof SAXSource)
      { 
         try 
         { 
            SAXParserFactory spf = SAXParserFactory.newInstance();
            configureParserFactory(spf);
            SAXParser sp = spf.newSAXParser();
            XMLReader xmlReader = sp.getXMLReader();
            ((SAXSource) source).setXMLReader(xmlReader);
            return delegate.unmarshal(source, declaredType);
         } 
         catch (SAXException e)
         { 
            throw new JAXBException(e);
         } 
         catch (ParserConfigurationException e)
         { 
            throw new JAXBException(e);
         } 
      } 
       
      throw new UnsupportedOperationException(errorMessage("Source, Class<T>"));
   } 
 
   public <T> JAXBElement<T> unmarshal(XMLStreamReader reader, Class<T> declaredType) throws JAXBException {
      throw new UnsupportedOperationException(errorMessage("XMLStreamReader, Class<T>"));
   } 
 
   public <T> JAXBElement<T> unmarshal(XMLEventReader reader, Class<T> declaredType) throws JAXBException {
      throw new UnsupportedOperationException(errorMessage("XMLEventReader, Class<T>"));
   } 
 
   public Unmarshaller getDelegate()
   { 
      return delegate;
   } 
 
   public void setDelegate(Unmarshaller delegate)
   { 
      this.delegate = delegate;
   } 
    
   protected void configureParserFactory(SAXParserFactory factory) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedExceptionfactory.setFeature("http://xml.org/sax/features/validation", false);
      factory.setFeature("http://xml.org/sax/features/namespaces", true);
      factory.setFeature("http://xml.org/sax/features/external-general-entities", !disableExternalEntities);
      factory.setFeature("http://xml.org/sax/features/external-parameter-entities", !disableExternalEntities);
      factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, enableSecureProcessingFeature); 
      factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", disableDTDs); 
   } 
    
   private String errorMessage(String s)
   { 
      return "ExternalEntityUnmarshallerWrapper: unexpected use of unmarshal(" + s + ")";