use of org.xml.sax.helpers.AttributesImpl in project jackrabbit by apache.
the class ToXmlContentHandlerTest method testAttributeOrder.
public void testAttributeOrder() throws SAXException {
ContentHandler handler;
AttributesImpl attributes;
handler = new ToXmlContentHandler();
handler.startDocument();
attributes = new AttributesImpl();
attributes.addAttribute("", "foo", "foo", "CDATA", "A");
attributes.addAttribute("", "bar", "bar", "CDATA", "B");
handler.startElement("", "test", "test", attributes);
handler.endElement("", "test", "test");
handler.endDocument();
assertEquals("<?xml version=\"1.0\"?><test foo=\"A\" bar=\"B\"/>", handler.toString());
handler = new ToXmlContentHandler();
handler.startDocument();
attributes = new AttributesImpl();
attributes.addAttribute("", "bar", "bar", "CDATA", "B");
attributes.addAttribute("", "foo", "foo", "CDATA", "A");
handler.startElement("", "test", "test", attributes);
handler.endElement("", "test", "test");
handler.endDocument();
assertEquals("<?xml version=\"1.0\"?><test bar=\"B\" foo=\"A\"/>", handler.toString());
}
use of org.xml.sax.helpers.AttributesImpl in project jackrabbit by apache.
the class ConcurrentImportTest method addNode.
/**
* Adds a new node with the given type and uuid. If a node with the same
* uuid already exists, an exception is thrown.
*
* @param parent parent node
* @param name node name
* @param type primary node type
* @param uuid uuid or <code>null</code>
* @param mixins mixins
* @return the new node.
* @throws RepositoryException if an error occurs
*/
public static Node addNode(Node parent, String name, String type, String uuid, String[] mixins) throws RepositoryException {
try {
final Session session = parent.getSession();
final ContentHandler handler = session.getImportContentHandler(parent.getPath(), ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
// first define the current namespaces
String[] prefixes = session.getNamespacePrefixes();
handler.startDocument();
for (String prefix : prefixes) {
handler.startPrefixMapping(prefix, session.getNamespaceURI(prefix));
}
AttributesImpl attrs = new AttributesImpl();
attrs.addAttribute(Name.NS_SV_URI, "name", "sv:name", "CDATA", name);
handler.startElement(Name.NS_SV_URI, "node", "sv:node", attrs);
// add the jcr:primaryTye
attrs = new AttributesImpl();
attrs.addAttribute(Name.NS_SV_URI, "name", "sv:name", "CDATA", JcrConstants.JCR_PRIMARYTYPE);
attrs.addAttribute(Name.NS_SV_URI, "type", "sv:type", "CDATA", "Name");
handler.startElement(Name.NS_SV_URI, "property", "sv:property", attrs);
handler.startElement(Name.NS_SV_URI, "value", "sv:value", EMPTY_ATTRS);
handler.characters(type.toCharArray(), 0, type.length());
handler.endElement(Name.NS_SV_URI, "value", "sv:value");
handler.endElement(Name.NS_SV_URI, "property", "sv:property");
if (mixins.length > 0) {
// add the jcr:mixinTypes
attrs = new AttributesImpl();
attrs.addAttribute(Name.NS_SV_URI, "name", "sv:name", "CDATA", JcrConstants.JCR_MIXINTYPES);
attrs.addAttribute(Name.NS_SV_URI, "type", "sv:type", "CDATA", "Name");
handler.startElement(Name.NS_SV_URI, "property", "sv:property", attrs);
for (String mix : mixins) {
handler.startElement(Name.NS_SV_URI, "value", "sv:value", EMPTY_ATTRS);
handler.characters(mix.toCharArray(), 0, mix.length());
handler.endElement(Name.NS_SV_URI, "value", "sv:value");
}
handler.endElement(Name.NS_SV_URI, "property", "sv:property");
}
// add the jcr:uuid
if (uuid != null) {
attrs = new AttributesImpl();
attrs.addAttribute(Name.NS_SV_URI, "name", "sv:name", "CDATA", JcrConstants.JCR_UUID);
attrs.addAttribute(Name.NS_SV_URI, "type", "sv:type", "CDATA", "String");
handler.startElement(Name.NS_SV_URI, "property", "sv:property", attrs);
handler.startElement(Name.NS_SV_URI, "value", "sv:value", EMPTY_ATTRS);
handler.characters(uuid.toCharArray(), 0, uuid.length());
handler.endElement(Name.NS_SV_URI, "value", "sv:value");
handler.endElement(Name.NS_SV_URI, "property", "sv:property");
}
handler.endElement(Name.NS_SV_URI, "node", "sv:node");
handler.endDocument();
return parent.getNode(name);
} catch (SAXException e) {
Exception root = e.getException();
if (root instanceof RepositoryException) {
throw (RepositoryException) root;
} else if (root instanceof RuntimeException) {
throw (RuntimeException) root;
} else {
throw new RepositoryException("Error while creating node", root);
}
}
}
use of org.xml.sax.helpers.AttributesImpl in project jackrabbit by apache.
the class SerializingContentHandler method needsXmlnsAttributes.
/**
* Probes the available XML serializer for xmlns support. Used to set
* the value of the {@link #NEEDS_XMLNS_ATTRIBUTES} flag.
*
* @return whether the XML serializer needs explicit xmlns attributes
*/
private static boolean needsXmlnsAttributes() {
try {
StringWriter writer = new StringWriter();
TransformerHandler probe = FACTORY.newTransformerHandler();
probe.setResult(new StreamResult(writer));
probe.startDocument();
probe.startPrefixMapping("p", "uri");
probe.startElement("uri", "e", "p:e", new AttributesImpl());
probe.endElement("uri", "e", "p:e");
probe.endPrefixMapping("p");
probe.endDocument();
return writer.toString().indexOf("xmlns") == -1;
} catch (Exception e) {
throw new UnsupportedOperationException("XML serialization fails");
}
}
use of org.xml.sax.helpers.AttributesImpl 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.helpers.AttributesImpl in project geode by apache.
the class LuceneIndexXmlParserJUnitTest method attemptInvalidAnalyzerClass.
@Test
public void attemptInvalidAnalyzerClass() throws SAXException {
AttributesImpl attrs = new AttributesImpl();
XmlGeneratorUtils.addAttribute(attrs, LuceneXmlConstants.NAME, "index");
this.parser.startElement(LuceneXmlConstants.NAMESPACE, LuceneXmlConstants.INDEX, null, attrs);
try {
addField("field", "some.invalid.class");
fail("Should not have been able to add a field with an invalid analyzer class name");
} catch (Exception e) {
}
}
Aggregations