Search in sources :

Example 66 with AttributesImpl

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());
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) ContentHandler(org.xml.sax.ContentHandler)

Example 67 with AttributesImpl

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);
        }
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) RepositoryException(javax.jcr.RepositoryException) ContentHandler(org.xml.sax.ContentHandler) RepositoryException(javax.jcr.RepositoryException) SAXException(org.xml.sax.SAXException) InvalidItemStateException(javax.jcr.InvalidItemStateException) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Session(javax.jcr.Session) SAXException(org.xml.sax.SAXException)

Example 68 with AttributesImpl

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");
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) AttributesImpl(org.xml.sax.helpers.AttributesImpl) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXException(org.xml.sax.SAXException)

Example 69 with AttributesImpl

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);
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) Analyzer(org.apache.lucene.analysis.Analyzer) ContentHandler(org.xml.sax.ContentHandler)

Example 70 with AttributesImpl

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) {
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) SAXException(org.xml.sax.SAXException) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Aggregations

AttributesImpl (org.xml.sax.helpers.AttributesImpl)310 SAXException (org.xml.sax.SAXException)53 Test (org.junit.Test)34 DiskWriteAttributesImpl (org.apache.geode.internal.cache.DiskWriteAttributesImpl)23 PartitionAttributesImpl (org.apache.geode.internal.cache.PartitionAttributesImpl)23 ContentHandler (org.xml.sax.ContentHandler)21 Attributes (org.xml.sax.Attributes)17 PreparedStatement (java.sql.PreparedStatement)16 ResultSet (java.sql.ResultSet)16 Map (java.util.Map)16 PackOut (org.adempiere.pipo.PackOut)16 IOException (java.io.IOException)15 POSaveFailedException (org.adempiere.pipo.exception.POSaveFailedException)12 Iterator (java.util.Iterator)11 TransformerHandler (javax.xml.transform.sax.TransformerHandler)11 StreamResult (javax.xml.transform.stream.StreamResult)11 Metadata (org.apache.tika.metadata.Metadata)11 File (java.io.File)9 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)9 DatabaseAccessException (org.adempiere.pipo.exception.DatabaseAccessException)9