/** * Creates new empty element on <code>contentHandler</code> with given <code>prefix</code>, * <code>localName</code> and <code>attributes</code>. * * @param contentHandler to create empty element on. * @param prefix of element * @param localName of element * @param attributes of element * @throws SAXException if {@link ContentHandler#startElement(String, String, String, Attributes)} * or {@link ContentHandler#endElement(String, String, String)} throws * {@link SAXException}. * @since GemFire 8.1 */ public static void emptyElement(final ContentHandler contentHandler, final String prefix, final String localName, final AttributesImpl attributes) throws SAXException { startElement(contentHandler, prefix, localName, attributes); endElement(contentHandler, prefix, localName); }
@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); } LuceneSerializer serializer = index.getLuceneSerializer(); if (serializer != null) { XmlGeneratorUtils.startElement(handler, PREFIX, SERIALIZER, EMPTY); XmlGeneratorUtils.addDeclarable(handler, serializer); XmlGeneratorUtils.endElement(handler, PREFIX, SERIALIZER); } XmlGeneratorUtils.endElement(handler, PREFIX, INDEX); } }
/** * Test method for * {@link XmlGeneratorUtils#startElement(ContentHandler, String, String, AttributesImpl)}. */ @Test public void testStartElementContentHandlerStringStringAttributesImpl() throws SAXException { final AtomicReference<String> uriRef = new AtomicReference<String>(); final AtomicReference<String> localnameRef = new AtomicReference<String>(); final AtomicReference<String> qNameRef = new AtomicReference<String>(); final AtomicReference<Attributes> attributesRef = new AtomicReference<Attributes>(); final ContentHandler contentHandler = new MockContentHandler() { @Override public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { uriRef.set(uri); localnameRef.set(localName); qNameRef.set(qName); attributesRef.set(atts); } }; XmlGeneratorUtils.startElement(contentHandler, "prefix", "localname", null); assertEquals("localname", localnameRef.get()); assertEquals("prefix:localname", qNameRef.get()); assertEquals(NULL_NS_URI, uriRef.get()); assertNull(attributesRef.get()); }