Search in sources :

Example 31 with ContentHandler

use of org.xml.sax.ContentHandler in project geode by apache.

the class LuceneIndexXmlGenerator method generate.

@Override
public void generate(CacheXmlGenerator cacheXmlGenerator) throws SAXException {
    final ContentHandler handler = cacheXmlGenerator.getContentHandler();
    handler.startPrefixMapping(PREFIX, NAMESPACE);
    AttributesImpl attr = new AttributesImpl();
    // TODO - should the type be xs:string ?
    XmlGeneratorUtils.addAttribute(attr, NAME, index.getName());
    XmlGeneratorUtils.startElement(handler, PREFIX, INDEX, attr);
    for (String field : index.getFieldNames()) {
        AttributesImpl fieldAttr = new AttributesImpl();
        XmlGeneratorUtils.addAttribute(fieldAttr, NAME, field);
        Analyzer analyzer = index.getFieldAnalyzers().get(field);
        if (analyzer != null) {
            XmlGeneratorUtils.addAttribute(fieldAttr, ANALYZER, analyzer.getClass().getName());
        }
        XmlGeneratorUtils.emptyElement(handler, PREFIX, FIELD, fieldAttr);
    }
    XmlGeneratorUtils.endElement(handler, PREFIX, INDEX);
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) Analyzer(org.apache.lucene.analysis.Analyzer) ContentHandler(org.xml.sax.ContentHandler)

Example 32 with ContentHandler

use of org.xml.sax.ContentHandler in project geode by apache.

the class LuceneIndexXmlGeneratorJUnitTest method generateWithFields.

/**
   * Test of generating and reading cache configuration back in.
   */
@Test
public void generateWithFields() throws Exception {
    LuceneIndex index = mock(LuceneIndex.class);
    when(index.getName()).thenReturn("index");
    String[] fields = new String[] { "field1", "field2" };
    when(index.getFieldNames()).thenReturn(fields);
    LuceneIndexXmlGenerator generator = new LuceneIndexXmlGenerator(index);
    CacheXmlGenerator cacheXmlGenerator = mock(CacheXmlGenerator.class);
    ContentHandler handler = mock(ContentHandler.class);
    when(cacheXmlGenerator.getContentHandler()).thenReturn(handler);
    generator.generate(cacheXmlGenerator);
    ArgumentCaptor<Attributes> captor = ArgumentCaptor.forClass(Attributes.class);
    verify(handler).startElement(eq(""), eq("index"), eq("lucene:index"), captor.capture());
    Attributes value = captor.getValue();
    assertEquals("index", value.getValue(LuceneXmlConstants.NAME));
    captor = ArgumentCaptor.forClass(Attributes.class);
    verify(handler, times(2)).startElement(eq(""), eq("field"), eq("lucene:field"), captor.capture());
    Set<String> foundFields = new HashSet<String>();
    for (Attributes fieldAttr : captor.getAllValues()) {
        foundFields.add(fieldAttr.getValue(LuceneXmlConstants.NAME));
    }
    HashSet<String> expected = new HashSet<String>(Arrays.asList(fields));
    assertEquals(expected, foundFields);
    verify(handler, times(2)).endElement(eq(""), eq("field"), eq("lucene:field"));
    verify(handler).endElement(eq(""), eq("index"), eq("lucene:index"));
}
Also used : LuceneIndex(org.apache.geode.cache.lucene.LuceneIndex) CacheXmlGenerator(org.apache.geode.internal.cache.xmlcache.CacheXmlGenerator) Attributes(org.xml.sax.Attributes) ContentHandler(org.xml.sax.ContentHandler) HashSet(java.util.HashSet) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 33 with ContentHandler

use of org.xml.sax.ContentHandler in project geode by apache.

the class MockCacheExtensionXmlGenerator method generate.

@Override
public void generate(CacheXmlGenerator cacheXmlGenerator) throws SAXException {
    final ContentHandler handler = cacheXmlGenerator.getContentHandler();
    try {
        handler.startPrefixMapping(PREFIX, NAMESPACE);
        final AttributesImpl atts = new AttributesImpl();
        addAttribute(atts, ATTRIBUTE_VALUE, extension.getValue());
        emptyElement(handler, PREFIX, ELEMENT_CACHE, atts);
    } finally {
        handler.endPrefixMapping("mock");
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) ContentHandler(org.xml.sax.ContentHandler)

Example 34 with ContentHandler

use of org.xml.sax.ContentHandler in project jackrabbit by apache.

the class AbstractWorkspace method importXML.

/**
     * Parses the given input stream as an XML document and processes the
     * SAX events using the {@link ContentHandler} returned by
     * {@link Workspace#getImportContentHandler(String, int)}.
     *
     * @param parentAbsPath passed through
     * @param in input stream to be parsed as XML and imported
     * @param uuidBehavior passed through
     * @throws IOException if an I/O error occurs
     * @throws InvalidSerializedDataException if an XML parsing error occurs
     * @throws RepositoryException if a repository error occurs
     */
public void importXML(String parentAbsPath, InputStream in, int uuidBehavior) throws IOException, InvalidSerializedDataException, RepositoryException {
    try {
        ContentHandler handler = getImportContentHandler(parentAbsPath, uuidBehavior);
        new ParsingContentHandler(handler).parse(in);
    } catch (SAXException e) {
        Throwable exception = e.getException();
        if (exception instanceof RepositoryException) {
            throw (RepositoryException) exception;
        } else if (exception instanceof IOException) {
            throw (IOException) exception;
        } else {
            throw new InvalidSerializedDataException("XML parse error", e);
        }
    } finally {
        // JCR-2903
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignore) {
            }
        }
    }
}
Also used : ParsingContentHandler(org.apache.jackrabbit.commons.xml.ParsingContentHandler) InvalidSerializedDataException(javax.jcr.InvalidSerializedDataException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) ParsingContentHandler(org.apache.jackrabbit.commons.xml.ParsingContentHandler) ContentHandler(org.xml.sax.ContentHandler) SAXException(org.xml.sax.SAXException)

Example 35 with ContentHandler

use of org.xml.sax.ContentHandler in project jackrabbit-oak by apache.

the class WorkspaceImpl method importXML.

@Override
public void importXML(String parentAbsPath, InputStream in, int uuidBehavior) throws IOException, RepositoryException {
    ensureIsAlive();
    try {
        ContentHandler handler = getImportContentHandler(parentAbsPath, uuidBehavior);
        new ParsingContentHandler(handler).parse(in);
    } catch (SAXException e) {
        Throwable exception = e.getException();
        if (exception instanceof RepositoryException) {
            throw (RepositoryException) exception;
        } else if (exception instanceof IOException) {
            throw (IOException) exception;
        } else if (exception instanceof CommitFailedException) {
            throw ((CommitFailedException) exception).asRepositoryException();
        } else {
            throw new InvalidSerializedDataException("XML parse error", e);
        }
    } finally {
        // JCR-2903
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignore) {
            }
        }
    }
}
Also used : ParsingContentHandler(org.apache.jackrabbit.commons.xml.ParsingContentHandler) InvalidSerializedDataException(javax.jcr.InvalidSerializedDataException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) ContentHandler(org.xml.sax.ContentHandler) ParsingContentHandler(org.apache.jackrabbit.commons.xml.ParsingContentHandler) SAXException(org.xml.sax.SAXException)

Aggregations

ContentHandler (org.xml.sax.ContentHandler)354 Metadata (org.apache.tika.metadata.Metadata)229 BodyContentHandler (org.apache.tika.sax.BodyContentHandler)229 InputStream (java.io.InputStream)210 Test (org.junit.Test)208 ParseContext (org.apache.tika.parser.ParseContext)164 Parser (org.apache.tika.parser.Parser)106 TikaTest (org.apache.tika.TikaTest)103 AutoDetectParser (org.apache.tika.parser.AutoDetectParser)102 TikaInputStream (org.apache.tika.io.TikaInputStream)75 ByteArrayInputStream (java.io.ByteArrayInputStream)64 SAXException (org.xml.sax.SAXException)40 IOException (java.io.IOException)34 TeeContentHandler (org.apache.tika.sax.TeeContentHandler)28 TikaException (org.apache.tika.exception.TikaException)24 ExcelParserTest (org.apache.tika.parser.microsoft.ExcelParserTest)24 WordParserTest (org.apache.tika.parser.microsoft.WordParserTest)24 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)21 AttributesImpl (org.xml.sax.helpers.AttributesImpl)21 InputSource (org.xml.sax.InputSource)20