Search in sources :

Example 11 with SAXAdapter

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);
}
Also used : InputSource(org.xml.sax.InputSource) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) SAXAdapter(org.exist.dom.memtree.SAXAdapter) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Document(org.w3c.dom.Document) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) ExistSAXParserFactory(org.exist.util.ExistSAXParserFactory)

Example 12 with SAXAdapter

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());
    }
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) InputSource(org.xml.sax.InputSource) SAXDecoder(com.siemens.ct.exi.api.sax.SAXDecoder) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) BinaryValue(org.exist.xquery.value.BinaryValue) GrammarFactory(com.siemens.ct.exi.GrammarFactory) EXIException(com.siemens.ct.exi.exceptions.EXIException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) JavaErrorCode(org.exist.xquery.ErrorCodes.JavaErrorCode) Item(org.exist.xquery.value.Item) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) AppendingSAXAdapter(org.exist.dom.memtree.AppendingSAXAdapter) SAXAdapter(org.exist.dom.memtree.SAXAdapter) AppendingSAXAdapter(org.exist.dom.memtree.AppendingSAXAdapter) Grammars(com.siemens.ct.exi.grammars.Grammars) DefaultEXIFactory(com.siemens.ct.exi.helpers.DefaultEXIFactory) EXIFactory(com.siemens.ct.exi.EXIFactory)

Example 13 with SAXAdapter

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);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) InputSource(org.xml.sax.InputSource) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParser(javax.xml.parsers.SAXParser) SAXAdapter(org.exist.dom.memtree.SAXAdapter) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) ExistSAXParserFactory(org.exist.util.ExistSAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 14 with SAXAdapter

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);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) SAXAdapter(org.exist.dom.memtree.SAXAdapter) Document(org.w3c.dom.Document) BinaryDocument(org.exist.dom.persistent.BinaryDocument) XMLReaderPool(org.exist.util.XMLReaderPool) XMLReader(org.xml.sax.XMLReader) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) EXistException(org.exist.EXistException) IOException(java.io.IOException) TriggerException(org.exist.collections.triggers.TriggerException)

Example 15 with SAXAdapter

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);
}
Also used : InputSource(org.xml.sax.InputSource) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) SAXParser(javax.xml.parsers.SAXParser) SAXAdapter(org.exist.dom.memtree.SAXAdapter) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) ExistSAXParserFactory(org.exist.util.ExistSAXParserFactory) Test(org.junit.Test)

Aggregations

SAXAdapter (org.exist.dom.memtree.SAXAdapter)22 InputSource (org.xml.sax.InputSource)18 XMLReader (org.xml.sax.XMLReader)17 IOException (java.io.IOException)13 SAXException (org.xml.sax.SAXException)12 StringReader (java.io.StringReader)10 SAXParser (javax.xml.parsers.SAXParser)10 SAXParserFactory (javax.xml.parsers.SAXParserFactory)7 NodeImpl (org.exist.dom.memtree.NodeImpl)6 ExistSAXParserFactory (org.exist.util.ExistSAXParserFactory)6 XMLReaderPool (org.exist.util.XMLReaderPool)6 XPathException (org.exist.xquery.XPathException)6 InputStream (java.io.InputStream)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 Document (org.w3c.dom.Document)4 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)3 Either (com.evolvedbinary.j8fu.Either)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 DocumentImpl (org.exist.dom.memtree.DocumentImpl)2