Red Hat Application Migration Toolkit
package org.exolab.castor.xml.util; import java.util.StringTokenizer; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.castor.core.util.AbstractProperties; import org.castor.core.util.Messages; import org.exolab.castor.xml.OutputFormat; import org.exolab.castor.xml.Serializer; import org.exolab.castor.xml.XMLSerializerFactory; import org.xml.sax.Parser; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; import org.xml.sax.XMLReader; public class XMLParserUtils { static final Log LOG = LogFactory.getFactory().getInstance(XMLParserUtils.class); private static final String VALIDATION = "http://xml.org/sax/features/validation"; private static final String NAMESPACES = "http://xml.org/sax/features/namespaces"; public static void setFeaturesOnXmlReader(String parserFeatures, String parserFeaturesToDisable, boolean validation, boolean namespaces, XMLReader xmlReader) { try { xmlReader.setFeature("http://xml.org/sax/features/validation", validation); xmlReader.setFeature("http://xml.org/sax/features/namespaces", namespaces); enableFeatures(parserFeatures, xmlReader); disableFeatures(parserFeaturesToDisable, xmlReader); } catch (SAXException var6) { LOG.error(Messages.format("conf.configurationError", var6)); } } private static void enableFeatures(String features, XMLReader xmlReader) throws SAXNotRecognizedException, SAXNotSupportedException { if(features != null) { StringTokenizer token = new StringTokenizer(features, ", "); while(token.hasMoreTokens()) { xmlReader.setFeature(token.nextToken(), true); } } } private static void disableFeatures(String features, XMLReader xmlReader) throws SAXNotRecognizedException, SAXNotSupportedException { if(features != null) { StringTokenizer token = new StringTokenizer(features, ", "); while(token.hasMoreTokens()) { xmlReader.setFeature(token.nextToken(), false); } } } public static SAXParser getSAXParser(boolean validation, boolean namespaces) { SAXParser saxParser = null; SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(namespaces); factory.setValidating(validation); try { saxParser = factory.newSAXParser(); if(LOG.isDebugEnabled()) { LOG.debug("Successfully instantiated a JAXP SAXParser instance."); } } catch (ParserConfigurationException var5) { LOG.error(Messages.format("conf.configurationError", var5)); } catch (SAXException var6) { LOG.error(Messages.format("conf.configurationError", var6)); } return saxParser; } public static XMLReader instantiateXMLReader(String className) { try { Class except = Class.forName(className); XMLReader xmlReader = (XMLReader)except.newInstance(); if(LOG.isDebugEnabled()) { LOG.debug("Successfully instantiated " + className); } return xmlReader; } catch (Exception var3) { throw new RuntimeException(Messages.format("conf.failedInstantiateParser", className, var3)); } } public static Parser instantiateParser(String className) { try { Class except = Class.forName(className); Parser parser = (Parser)except.newInstance(); if(LOG.isDebugEnabled()) { LOG.debug("Successfully instantiated " + className); } return parser; } catch (Exception var3) { throw new RuntimeException(Messages.format("conf.failedInstantiateParser", className, var3)); } } public static Parser getParser(AbstractProperties properties, String features) { Parser parser = null; Boolean validation = properties.getBoolean("org.exolab.castor.parser.validation"); Boolean namespaces = properties.getBoolean("org.exolab.castor.parser.namespaces"); String parserClassName = properties.getString("org.exolab.castor.parser"); if(parserClassName == null || parserClassName.length() == 0) { SAXParser xmlReader = getSAXParser(validation.booleanValue(), namespaces.booleanValue()); if(xmlReader != null) { try { parser = xmlReader.getParser(); } catch (SAXException var8) { LOG.error(Messages.format("conf.configurationError", var8)); } } } if(parser == null) { if(parserClassName == null || parserClassName.length() == 0 || parserClassName.equalsIgnoreCase("xerces")) { parserClassName = "org.apache.xerces.parsers.SAXParser"; } parser = instantiateParser(parserClassName); if(parser instanceof XMLReader) { XMLReader xmlReader1 = (XMLReader)parser; setFeaturesOnXmlReader(properties.getString("org.exolab.castor.sax.features", features), properties.getString("org.exolab.castor.sax.features-to-disable", ""), validation.booleanValue(), namespaces.booleanValue(), xmlReader1); } } return parser; } public static Serializer getSerializer(AbstractProperties properties) { Serializer serializer = getSerializerFactory(properties.getString("org.exolab.castor.xml.serializer.factory")).getSerializer(); serializer.setOutputFormat(getOutputFormat(properties)); return serializer; } public static OutputFormat getOutputFormat(AbstractProperties properties) { boolean indent = properties.getBoolean("org.exolab.castor.indent", false); OutputFormat format = getSerializerFactory(properties.getString("org.exolab.castor.xml.serializer.factory")).getOutputFormat(); format.setMethod("xml"); format.setIndenting(indent); if(!indent) { format.setPreserveSpace(true); } return format; } public static XMLSerializerFactory getSerializerFactory(String serializerFactoryName) { try { XMLSerializerFactory serializerFactory = (XMLSerializerFactory)Class.forName(serializerFactoryName).newInstance(); return serializerFactory; } catch (Exception var3) { throw new RuntimeException(Messages.format("conf.failedInstantiateSerializerFactory", serializerFactoryName, var3)); } } }