Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 1&1 Internet AG, http://www.1and1.org
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as published by
   * the Free Software Foundation; either version 2 of the License,
   * or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package net.sf.beezle.sushi.xml;
 
 
Create SaxParser- and DocumentBuilder Factories. Kind of a FactoryFactory to encapsulates xml parser selection. I have to use jaxp 1.2 (in particular: I cannot use setSchema from jaxp 1.3) because old Jaxp and Xerces Parsers from JBoss and Tariff/Castor pollute our classpath. I wasn't able to force Jdk's built-in xerces by directly instantiating the respective Factory because JBoss didn't find the getSchema method in its endorsed xpi-apis.jar.
 
 public class Factories {
     private static final Logger LOG = Logger.getLogger(Factories.class.getName());
     
     public static SAXParser saxParser(Node schemathrows IOException {
         SAXParserFactory factory;
         SAXParser parser;
         
         factory = sax();
         factory.setValidating(true);
         factory.setNamespaceAware(false);
         try {
             parser = factory.newSAXParser();
             parser.setProperty();
             parser.setProperty(new ByteArrayInputStream(schema.readBytes()));
             return parser;
         } catch (ParserConfigurationException e) {
             throw new RuntimeException(e);
         } catch (SAXException e) {
             throw new RuntimeException(e);
         }
     }
 
     public static SAXParser saxParser() {
         SAXParserFactory factory;
         
         factory = sax();
         factory.setValidating(false);
         factory.setNamespaceAware(false);
         try {
             return factory.newSAXParser();
         } catch (ParserConfigurationException e) {
             throw new RuntimeException(e);
         } catch (SAXException e) {
             throw new RuntimeException(e);
         }
     }
 
     public static SAXParserFactory sax() {
         return SAXParserFactory.newInstance();
     }
 
     
     public static DocumentBuilderFactory document(Node schemathrows IOExceptionSAXException {
         DocumentBuilderFactory factory;
         Throwable throwable;
         
         factory = document();
         try {
             setJaxp13Validating(factoryschema);
             throwable = null;
         } catch (UnsupportedOperationException e) {
             throwable = e;
         } catch (Error e) {
             throwable = e;
         }
        if (throwable != null) {
            .log(.factory.getClass().getName() + ": JAXP 1.3 validation failed, using 1.2"throwable);
            setJaxp12Validating(factoryschema);
        }
        return factory;
    }
    private static void setJaxp13Validating(DocumentBuilderFactory factoryNode schemathrows IOExceptionSAXException {
        SchemaFactory sf;
        Source src;
        
        sf = SchemaFactory.newInstance(.);
        src = new StreamSource(schema.createInputStream());
        factory.setSchema(sf.newSchema(src));
    }
    private static void setJaxp12Validating(DocumentBuilderFactory factoryNode schemathrows IOException {
        factory.setNamespaceAware(true);
        factory.setValidating(true);
        try {
            factory.setAttribute();
        } catch (IllegalArgumentException x) {
            throw new RuntimeException(factory.getClass().getName() + ": parser does not support JAXP 1.2"x);
        }
        factory.setAttribute(new ByteArrayInputStream(schema.readBytes()));
        
    }
    public static DocumentBuilderFactory document() {
        return DocumentBuilderFactory.newInstance(); 
    }
    //--
    
    private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
    private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
    private static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
New to GrepCode? Check out our FAQ X