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