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);
}
}
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;
}
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");
}
}
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);
}
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"));
}
Aggregations