use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.
the class RESTServiceTest method parseResponse.
private int parseResponse(final String data) throws IOException, SAXException, ParserConfigurationException {
final SAXParserFactory factory = ExistSAXParserFactory.getSAXParserFactory();
factory.setNamespaceAware(true);
final InputSource src = new InputSource(new StringReader(data));
final SAXParser parser = factory.newSAXParser();
final XMLReader reader = parser.getXMLReader();
final SAXAdapter adapter = new SAXAdapter();
reader.setContentHandler(adapter);
reader.parse(src);
final Document doc = adapter.getDocument();
final Element root = doc.getDocumentElement();
final String hits = root.getAttributeNS(Namespaces.EXIST_NS, "hits");
return Integer.parseInt(hits);
}
use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.
the class DecodeExiFunction method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
try {
BinaryValue exiBinary = ((BinaryValue) args[0].itemAt(0));
context.pushDocumentContext();
try {
MemTreeBuilder builder = context.getDocumentBuilder();
// create default factory and EXI grammar for schema
EXIFactory exiFactory = DefaultEXIFactory.newInstance();
if (args.length > 1) {
if (!args[1].isEmpty()) {
Item xsdItem = args[1].itemAt(0);
try (InputStream xsdInputStream = EXIUtils.getInputStream(xsdItem, context)) {
GrammarFactory grammarFactory = GrammarFactory.newInstance();
Grammars grammar = grammarFactory.createGrammars(xsdInputStream);
exiFactory.setGrammars(grammar);
}
}
}
SAXDecoder decoder = new SAXDecoder(exiFactory);
SAXAdapter adapter = new AppendingSAXAdapter(builder);
decoder.setContentHandler(adapter);
try (InputStream inputStream = exiBinary.getInputStream()) {
decoder.parse(new InputSource(inputStream));
}
return (NodeValue) builder.getDocument().getDocumentElement();
} finally {
context.popDocumentContext();
}
} catch (EXIException | SAXException | IOException exie) {
throw new XPathException(this, new JavaErrorCode(exie.getCause()), exie.getMessage());
}
}
use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.
the class Configurator method parse.
public static Configuration parse(final InputStream is) throws ConfigurationException {
try {
final SAXParserFactory factory = ExistSAXParserFactory.getSAXParserFactory();
factory.setNamespaceAware(true);
final InputSource src = new InputSource(is);
final SAXParser parser = factory.newSAXParser();
final XMLReader reader = parser.getXMLReader();
reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
reader.setFeature(FEATURE_SECURE_PROCESSING, true);
final SAXAdapter adapter = new SAXAdapter();
reader.setContentHandler(adapter);
reader.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
reader.parse(src);
return new ConfigurationImpl(adapter.getDocument().getDocumentElement());
} catch (final ParserConfigurationException | SAXException | IOException e) {
throw new ConfigurationException(e);
}
}
use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.
the class CollectionConfigurationManager method testConfiguration.
/**
* Check the passed collection configuration. Throws an exception if errors
* are detected in the configuration document. Note: some configuration
* settings depend on the current environment, in particular the
* availability of trigger or index classes.
*
* @param broker DBBroker
* @param config the configuration to test
* @throws CollectionConfigurationException if errors were detected
*/
public void testConfiguration(DBBroker broker, String config) throws CollectionConfigurationException {
try {
final SAXAdapter adapter = new SAXAdapter();
final InputSource src = new InputSource(new StringReader(config));
final XMLReaderPool parserPool = broker.getBrokerPool().getParserPool();
XMLReader reader = null;
try {
reader = parserPool.borrowXMLReader();
reader.setContentHandler(adapter);
reader.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
reader.parse(src);
} finally {
if (reader != null) {
parserPool.returnXMLReader(reader);
}
}
final Document doc = adapter.getDocument();
final CollectionConfiguration conf = new CollectionConfiguration(broker.getBrokerPool());
conf.read(broker, doc, true, null, null);
} catch (final Exception e) {
throw new CollectionConfigurationException(e);
}
}
use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.
the class ConfigurableTest method subelement.
@Test
public void subelement() throws Exception {
InputStream is = new UnsynchronizedByteArrayInputStream(config2.getBytes(UTF_8));
// initialize xml parser
// we use eXist's in-memory DOM implementation to work
// around a bug in Xerces
SAXParserFactory factory = ExistSAXParserFactory.getSAXParserFactory();
factory.setNamespaceAware(true);
InputSource src = new InputSource(is);
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
SAXAdapter adapter = new SAXAdapter();
reader.setContentHandler(adapter);
reader.parse(src);
ConfigurationImpl config = new ConfigurationImpl(adapter.getDocument().getDocumentElement());
ConfigurableObject object = new ConfigurableObject(config);
assertEquals("a", object.some);
assertEquals(Integer.valueOf(5), object.someInteger);
}
Aggregations