Search in sources :

Example 26 with ContentHandler

use of org.xml.sax.ContentHandler in project intellij-community by JetBrains.

the class XmlInstanceValidator method doValidation.

public static void doValidation(@NotNull final XmlDocument doc, final Validator.ValidationHost host, final XmlFile descriptorFile) {
    try {
        final Schema schema = RngParser.getCachedSchema(descriptorFile);
        if (schema == null) {
            // did not manage to get a compiled schema. no validation...
            return;
        }
        final ErrorHandler eh = MyErrorHandler.create(doc, host);
        if (eh == null) {
            return;
        }
        final PropertyMapBuilder builder = new PropertyMapBuilder();
        builder.put(ValidateProperty.ERROR_HANDLER, eh);
        final ContentHandler handler = schema.createValidator(builder.toPropertyMap()).getContentHandler();
        doc.accept(new Psi2SaxAdapter(handler));
    } catch (ProcessCanceledException e) {
        throw e;
    } catch (RuntimeException e) {
        LOG.error(e);
    } catch (Exception e) {
        LOG.info(e);
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) Schema(com.thaiopensource.validate.Schema) PropertyMapBuilder(com.thaiopensource.util.PropertyMapBuilder) ContentHandler(org.xml.sax.ContentHandler) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 27 with ContentHandler

use of org.xml.sax.ContentHandler in project enclojure by EricThorsen.

the class Processor method process.

public int process() throws TransformerException, IOException, SAXException {
    ZipInputStream zis = new ZipInputStream(input);
    final ZipOutputStream zos = new ZipOutputStream(output);
    final OutputStreamWriter osw = new OutputStreamWriter(zos);
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    TransformerFactory tf = TransformerFactory.newInstance();
    if (!tf.getFeature(SAXSource.FEATURE) || !tf.getFeature(SAXResult.FEATURE)) {
        return 0;
    }
    SAXTransformerFactory saxtf = (SAXTransformerFactory) tf;
    Templates templates = null;
    if (xslt != null) {
        templates = saxtf.newTemplates(xslt);
    }
    // configuring outHandlerFactory
    // ///////////////////////////////////////////////////////
    EntryElement entryElement = getEntryElement(zos);
    ContentHandler outDocHandler = null;
    switch(outRepresentation) {
        case BYTECODE:
            outDocHandler = new OutputSlicingHandler(new ASMContentHandlerFactory(zos, computeMax), entryElement, false);
            break;
        case MULTI_XML:
            outDocHandler = new OutputSlicingHandler(new SAXWriterFactory(osw, true), entryElement, true);
            break;
        case SINGLE_XML:
            ZipEntry outputEntry = new ZipEntry(SINGLE_XML_NAME);
            zos.putNextEntry(outputEntry);
            outDocHandler = new SAXWriter(osw, false);
            break;
    }
    // configuring inputDocHandlerFactory
    // /////////////////////////////////////////////////
    ContentHandler inDocHandler;
    if (templates == null) {
        inDocHandler = outDocHandler;
    } else {
        inDocHandler = new InputSlicingHandler("class", outDocHandler, new TransformerHandlerFactory(saxtf, templates, outDocHandler));
    }
    ContentHandlerFactory inDocHandlerFactory = new SubdocumentHandlerFactory(inDocHandler);
    if (inDocHandler != null && inRepresentation != SINGLE_XML) {
        inDocHandler.startDocument();
        inDocHandler.startElement("", "classes", "classes", new AttributesImpl());
    }
    int i = 0;
    ZipEntry ze;
    while ((ze = zis.getNextEntry()) != null) {
        update(ze.getName(), n++);
        if (isClassEntry(ze)) {
            processEntry(zis, ze, inDocHandlerFactory);
        } else {
            OutputStream os = entryElement.openEntry(getName(ze));
            copyEntry(zis, os);
            entryElement.closeEntry();
        }
        i++;
    }
    if (inDocHandler != null && inRepresentation != SINGLE_XML) {
        inDocHandler.endElement("", "classes", "classes");
        inDocHandler.endDocument();
    }
    if (outRepresentation == SINGLE_XML) {
        zos.closeEntry();
    }
    zos.flush();
    zos.close();
    return i;
}
Also used : SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) TransformerFactory(javax.xml.transform.TransformerFactory) ZipEntry(java.util.zip.ZipEntry) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) Templates(javax.xml.transform.Templates) ContentHandler(org.xml.sax.ContentHandler) ZipInputStream(java.util.zip.ZipInputStream) AttributesImpl(org.xml.sax.helpers.AttributesImpl) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStreamWriter(java.io.OutputStreamWriter)

Example 28 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 29 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 30 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)

Aggregations

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