/** * Configure schema validation as recommended by the JAXP 1.2 spec. The <code>properties</code> * object may contains information about the schema local and language. * * @param properties parser optional info */ private static void configureOldXerces(SAXParser parser, Properties properties) throws ParserConfigurationException, SAXNotSupportedException { String schemaLocation = (String) properties.get("schemaLocation"); String schemaLanguage = (String) properties.get("schemaLanguage"); try { if (schemaLocation != null) { parser.setProperty(JAXP_SCHEMA_LANGUAGE, schemaLanguage); parser.setProperty(JAXP_SCHEMA_SOURCE, schemaLocation); } } catch (SAXNotRecognizedException e) { log.info(parser.getClass().getName() + ": " + e.getMessage() + " not supported."); } }
public void parse(InputSource input) throws SAXException, IOException { try { SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setFeature("http://xml.org/sax/features/namespaces", true); if (parser.getClass().getName().equals("org.apache.xerces.jaxp.SAXParserImpl")) { // disable DTD validate String feature = "http://apache.org/xml/features/nonvalidating/load-external-dtd"; reader.setFeature(feature, false); } reader.setContentHandler(this); reader.setErrorHandler(this); reader.setDTDHandler(this); reader.setEntityResolver(this); reader.parse(input); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } }
public synchronized void returnParser(SAXParser parser) { try { if (!resetChecked) { try { parser.getClass().getDeclaredMethod("reset", (Class[]) null); resetAvailable = true; } catch (NoSuchMethodException e) { resetAvailable = false; } resetChecked = true; } if (resetAvailable) { parser.reset(); parsers.push(parser); } else if (USE_INTERNAL_PARSER_IMPL) { ((XmlRpcSaxParser) parser).reset(); parsers.push(parser); } } catch (Exception e) { e.printStackTrace(); // TODO log properly } }
private final Document parseTemplateUsingPool( final Configuration configuration, final String documentName, final Reader reader, final ResourcePool<SAXParser> poolToBeUsed) { final SAXParser saxParser = poolToBeUsed.allocate(); final TemplatePreprocessingReader templateReader = (reader instanceof TemplatePreprocessingReader ? (TemplatePreprocessingReader) reader : new TemplatePreprocessingReader(reader, 8192)); try { /* * Parse the document */ final Document document = doParse(configuration, documentName, templateReader, saxParser); if (this.canResetParsers) { try { /* * Reset the parser so that it can be used again. */ saxParser.reset(); } catch (final UnsupportedOperationException e) { if (this.logger.isWarnEnabled()) { this.logger.warn( "[THYMELEAF] The SAX Parser implementation being used (\"{}\") does not implement " + "the \"reset\" operation. This will force Thymeleaf to re-create parser instances " + "each time they are needed for parsing templates, which is more costly. Enabling template " + "cache is recommended, and also using a parser library which implements \"reset\" such as " + "xerces version 2.9.1 or newer.", saxParser.getClass().getName()); } this.canResetParsers = false; } } return document; } catch (final IOException e) { throw new TemplateInputException("Exception parsing document", e); } catch (final TemplateProcessingException e) { throw e; } catch (final SAXParseException e) { final String message = String.format( "Exception parsing document: template=\"%s\", line %d - column %d", documentName, Integer.valueOf(e.getLineNumber()), Integer.valueOf(e.getColumnNumber())); throw new TemplateInputException(message, e); } catch (final SAXException e) { throw new TemplateInputException("Exception parsing document", e); } finally { if (this.canResetParsers) { poolToBeUsed.release(saxParser); } else { poolToBeUsed.discardAndReplace(saxParser); } } }