use of org.exist.validation.GrammarPool in project exist by eXist-db.
the class GrammarTooling method eval.
/**
* @see org.exist.xquery.BasicFunction#eval(Sequence[], Sequence)
*/
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
final GrammarPool grammarpool = (GrammarPool) config.getProperty(XMLReaderObjectFactory.GRAMMAR_POOL);
if (isCalledAs("clear-grammar-cache")) {
final Sequence result = new ValueSequence();
final int before = countTotalNumberOfGrammar(grammarpool);
LOG.debug("Clearing {} grammars", before);
clearGrammarPool(grammarpool);
final int after = countTotalNumberOfGrammar(grammarpool);
LOG.debug("Remained {} grammars", after);
final int delta = before - after;
result.add(new IntegerValue(delta));
return result;
} else if (isCalledAs("show-grammar-cache")) {
context.pushDocumentContext();
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
final NodeImpl result = writeReport(grammarpool, builder);
return result;
} finally {
context.popDocumentContext();
}
} else if (isCalledAs("pre-parse-grammar")) {
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
// Setup for XML schema support only
final XMLGrammarPreparser parser = new XMLGrammarPreparser();
parser.registerPreparser(TYPE_XSD, null);
final List<Grammar> allGrammars = new ArrayList<>();
// iterate through the argument sequence and parse url
for (final SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
String url = i.nextItem().getStringValue();
// Fix database urls
if (url.startsWith("/")) {
url = "xmldb:exist://" + url;
}
LOG.debug("Parsing {}", url);
// parse XSD grammar
try {
if (url.endsWith(".xsd")) {
final InputStream is = new URL(url).openStream();
final XMLInputSource xis = new XMLInputSource(null, url, url, is, null);
final Grammar schema = parser.preparseGrammar(TYPE_XSD, xis);
is.close();
allGrammars.add(schema);
} else {
throw new XPathException(this, "Only XMLSchemas can be preparsed.");
}
} catch (final Exception ex) {
LOG.debug(ex);
throw new XPathException(this, ex);
}
}
LOG.debug("Successfully parsed {} grammars.", allGrammars.size());
// Send all XSD grammars to grammarpool
Grammar[] grammars = new Grammar[allGrammars.size()];
grammars = allGrammars.toArray(grammars);
grammarpool.cacheGrammars(TYPE_XSD, grammars);
// Construct result to end user
final ValueSequence result = new ValueSequence();
for (final Grammar one : grammars) {
result.add(new StringValue(one.getGrammarDescription().getNamespace()));
}
return result;
} else {
// oh oh
LOG.error("function not found error");
throw new XPathException(this, "function not found");
}
}
use of org.exist.validation.GrammarPool in project exist by eXist-db.
the class Jaxp method eval.
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
XMLEntityResolver entityResolver = null;
GrammarPool grammarPool = null;
final ValidationReport report = new ValidationReport();
ContentHandler contenthandler = null;
MemTreeBuilder instanceBuilder = null;
InputSource instance = null;
if (isCalledAs("jaxp-parse")) {
instanceBuilder = context.getDocumentBuilder();
// (namespace?)
contenthandler = new DocumentBuilderReceiver(instanceBuilder, true);
} else {
contenthandler = new ValidationContentHandler();
}
try {
report.start();
// Get initialized parser
final XMLReader xmlReader = getXMLReader();
// Setup validation reporting
xmlReader.setContentHandler(contenthandler);
xmlReader.setErrorHandler(report);
// Get inputstream for instance document
instance = Shared.getInputSource(args[0].itemAt(0), context);
// Handle catalog
if (args.length == 2) {
LOG.debug("No Catalog specified");
} else if (args[2].isEmpty()) {
// Use system catalog
LOG.debug("Using system catalog.");
final Configuration config = brokerPool.getConfiguration();
entityResolver = (eXistXMLCatalogResolver) config.getProperty(XMLReaderObjectFactory.CATALOG_RESOLVER);
setXmlReaderEnitityResolver(xmlReader, entityResolver);
} else {
// Get URL for catalog
final String[] catalogUrls = Shared.getUrls(args[2]);
final String singleUrl = catalogUrls[0];
if (singleUrl.endsWith("/")) {
// Search grammar in collection specified by URL. Just one collection is used.
LOG.debug("Search for grammar in {}", singleUrl);
entityResolver = new SearchResourceResolver(catalogUrls[0], brokerPool);
setXmlReaderEnitityResolver(xmlReader, entityResolver);
} else if (singleUrl.endsWith(".xml")) {
LOG.debug("Using catalogs {}", getStrings(catalogUrls));
entityResolver = new eXistXMLCatalogResolver();
((eXistXMLCatalogResolver) entityResolver).setCatalogList(catalogUrls);
setXmlReaderEnitityResolver(xmlReader, entityResolver);
} else {
LOG.error("Catalog URLs should end on / or .xml");
}
}
// Use grammarpool
final boolean useCache = ((BooleanValue) args[1].itemAt(0)).getValue();
if (useCache) {
LOG.debug("Grammar caching enabled.");
final Configuration config = brokerPool.getConfiguration();
grammarPool = (GrammarPool) config.getProperty(XMLReaderObjectFactory.GRAMMAR_POOL);
xmlReader.setProperty(XMLReaderObjectFactory.APACHE_PROPERTIES_INTERNAL_GRAMMARPOOL, grammarPool);
}
// Jaxp document
LOG.debug("Start parsing document");
xmlReader.parse(instance);
LOG.debug("Stopped parsing document");
// Distill namespace from document
if (contenthandler instanceof ValidationContentHandler) {
report.setNamespaceUri(((ValidationContentHandler) contenthandler).getNamespaceUri());
}
} catch (final MalformedURLException ex) {
LOG.error(ex.getMessage());
report.setException(ex);
} catch (final IOException ex) {
LOG.error(ex.getCause());
report.setException(ex);
} catch (final Throwable ex) {
LOG.error(ex);
report.setException(ex);
} finally {
report.stop();
Shared.closeInputSource(instance);
}
// Create response
if (isCalledAs("jaxp")) {
final Sequence result = new ValueSequence();
result.add(new BooleanValue(report.isValid()));
return result;
} else /* isCalledAs("jaxp-report or jaxp-parse ") */
{
if (report.getThrowable() != null) {
throw new XPathException(report.getThrowable().getMessage(), report.getThrowable());
}
if (contenthandler instanceof DocumentBuilderReceiver) {
// DocumentBuilderReceiver dbr = (DocumentBuilderReceiver) contenthandler;
return instanceBuilder.getDocument().getNode(0);
} else {
context.pushDocumentContext();
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
return Shared.writeReport(report, builder);
} finally {
context.popDocumentContext();
}
}
}
}
use of org.exist.validation.GrammarPool in project exist by eXist-db.
the class XMLReaderPoolTest method reusedXmlReaderStillHasGrammarPool.
@Test
public void reusedXmlReaderStillHasGrammarPool() throws SAXNotSupportedException, SAXNotRecognizedException {
final GrammarPool mockGrammarPool = createMock(GrammarPool.class);
final Configuration mockConfiguration = createMock(Configuration.class);
expect(mockConfiguration.getProperty(XMLReaderObjectFactory.GRAMMAR_POOL)).andReturn(mockGrammarPool);
expect(mockConfiguration.getProperty(XMLReaderObjectFactory.CATALOG_RESOLVER)).andReturn(null);
expect(mockConfiguration.getProperty(XMLReaderObjectFactory.PROPERTY_VALIDATION_MODE)).andReturn("auto");
expect(mockConfiguration.getProperty(XMLReaderPool.XmlParser.XML_PARSER_FEATURES_PROPERTY)).andReturn(Collections.emptyMap());
replay(mockConfiguration);
final XMLReaderObjectFactory xmlReaderObjectFactory = new XMLReaderObjectFactory();
xmlReaderObjectFactory.configure(mockConfiguration);
final XMLReaderPool xmlReaderPool = new XMLReaderPool(xmlReaderObjectFactory, 1, 0);
xmlReaderPool.configure(mockConfiguration);
XMLReader xmlreader = xmlReaderPool.borrowXMLReader();
assertNotNull(xmlreader);
try {
// explicitly disable the grammar pool
xmlreader.setProperty(XMLReaderObjectFactory.APACHE_PROPERTIES_INTERNAL_GRAMMARPOOL, null);
} finally {
xmlReaderPool.returnXMLReader(xmlreader);
}
// borrow again after return...
xmlreader = xmlReaderPool.borrowXMLReader();
assertNotNull(xmlreader);
try {
assertSame(mockGrammarPool, xmlreader.getProperty(XMLReaderObjectFactory.APACHE_PROPERTIES_INTERNAL_GRAMMARPOOL));
} finally {
xmlReaderPool.returnXMLReader(xmlreader);
}
verify(mockConfiguration);
}
use of org.exist.validation.GrammarPool in project exist by eXist-db.
the class XMLReaderPoolTest method reusedXmlReaderStillHasNoGrammarPool.
@Test
public void reusedXmlReaderStillHasNoGrammarPool() throws SAXNotSupportedException, SAXNotRecognizedException {
final Configuration mockConfiguration = createMock(Configuration.class);
expect(mockConfiguration.getProperty(XMLReaderObjectFactory.GRAMMAR_POOL)).andReturn(null);
expect(mockConfiguration.getProperty(XMLReaderObjectFactory.CATALOG_RESOLVER)).andReturn(null);
expect(mockConfiguration.getProperty(XMLReaderObjectFactory.PROPERTY_VALIDATION_MODE)).andReturn("auto");
expect(mockConfiguration.getProperty(XMLReaderPool.XmlParser.XML_PARSER_FEATURES_PROPERTY)).andReturn(Collections.emptyMap());
replay(mockConfiguration);
final XMLReaderObjectFactory xmlReaderObjectFactory = new XMLReaderObjectFactory();
xmlReaderObjectFactory.configure(mockConfiguration);
final XMLReaderPool xmlReaderPool = new XMLReaderPool(xmlReaderObjectFactory, 1, 0);
xmlReaderPool.configure(mockConfiguration);
XMLReader xmlreader = xmlReaderPool.borrowXMLReader();
assertNotNull(xmlreader);
try {
// explicitly enable the grammar pool
final GrammarPool mockGrammarPool = createMock(GrammarPool.class);
xmlreader.setProperty(XMLReaderObjectFactory.APACHE_PROPERTIES_INTERNAL_GRAMMARPOOL, mockGrammarPool);
} finally {
xmlReaderPool.returnXMLReader(xmlreader);
}
// borrow again after return...
xmlreader = xmlReaderPool.borrowXMLReader();
assertNotNull(xmlreader);
try {
assertNull(xmlreader.getProperty(XMLReaderObjectFactory.APACHE_PROPERTIES_INTERNAL_GRAMMARPOOL));
} finally {
xmlReaderPool.returnXMLReader(xmlreader);
}
verify(mockConfiguration);
}
use of org.exist.validation.GrammarPool in project exist by eXist-db.
the class XMLReaderPoolTest method xmlReaderWithGrammarPool.
@Test
public void xmlReaderWithGrammarPool() throws SAXNotSupportedException, SAXNotRecognizedException {
final GrammarPool mockGrammarPool = createMock(GrammarPool.class);
final Configuration mockConfiguration = createMock(Configuration.class);
expect(mockConfiguration.getProperty(XMLReaderObjectFactory.GRAMMAR_POOL)).andReturn(mockGrammarPool);
expect(mockConfiguration.getProperty(XMLReaderObjectFactory.CATALOG_RESOLVER)).andReturn(null);
expect(mockConfiguration.getProperty(XMLReaderObjectFactory.PROPERTY_VALIDATION_MODE)).andReturn("auto");
expect(mockConfiguration.getProperty(XMLReaderPool.XmlParser.XML_PARSER_FEATURES_PROPERTY)).andReturn(Collections.emptyMap());
replay(mockConfiguration);
final XMLReaderObjectFactory xmlReaderObjectFactory = new XMLReaderObjectFactory();
xmlReaderObjectFactory.configure(mockConfiguration);
final XMLReaderPool xmlReaderPool = new XMLReaderPool(xmlReaderObjectFactory, 1, 0);
xmlReaderPool.configure(mockConfiguration);
final XMLReader xmlreader = xmlReaderPool.borrowXMLReader();
assertNotNull(xmlreader);
try {
assertSame(mockGrammarPool, xmlreader.getProperty(XMLReaderObjectFactory.APACHE_PROPERTIES_INTERNAL_GRAMMARPOOL));
} finally {
xmlReaderPool.returnXMLReader(xmlreader);
}
verify(mockConfiguration);
}
Aggregations