Search in sources :

Example 61 with Attributes

use of org.xml.sax.Attributes in project sirix by sirixdb.

the class TestNodeWrapperS9ApiXQuerySAXHandler method testWhereBooks.

@Test
public void testWhereBooks() throws Exception {
    final StringBuilder strBuilder = new StringBuilder();
    final ContentHandler contHandler = new XMLFilterImpl() {

        @Override
        public void startElement(final String uri, final String localName, final String qName, final Attributes atts) throws SAXException {
            strBuilder.append("<" + localName);
            for (int i = 0; i < atts.getLength(); i++) {
                strBuilder.append(" " + atts.getQName(i));
                strBuilder.append("=\"" + atts.getValue(i) + "\"");
            }
            strBuilder.append(">");
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            strBuilder.append("</" + localName + ">");
        }

        @Override
        public void characters(final char[] ch, final int start, final int length) throws SAXException {
            for (int i = start; i < start + length; i++) {
                strBuilder.append(ch[i]);
            }
        }
    };
    new XQueryEvaluatorSAXHandler("for $x in /bookstore/book where $x/price>30 return $x/title", mHolder.getSession(), contHandler).call();
    assertEquals(strBuilder.toString(), "<title lang=\"en\">XQuery Kick Start</title><title lang=\"en\">Learning XML</title>");
}
Also used : XQueryEvaluatorSAXHandler(org.sirix.saxon.evaluator.XQueryEvaluatorSAXHandler) XMLFilterImpl(org.xml.sax.helpers.XMLFilterImpl) Attributes(org.xml.sax.Attributes) ContentHandler(org.xml.sax.ContentHandler) Test(org.junit.Test)

Example 62 with Attributes

use of org.xml.sax.Attributes in project acs-aem-commons by Adobe-Consulting-Services.

the class StaticReferenceRewriteTransformerFactoryTest method test_with_prefix_and_matching_pattern_and_single_host.

@Test
public void test_with_prefix_and_matching_pattern_and_single_host() throws Exception {
    MockBundle bundle = new MockBundle(-1);
    MockComponentContext ctx = new MockComponentContext(bundle);
    ctx.setProperty("prefixes", new String[] { "/content/dam" });
    ctx.setProperty("attributes", new String[] { "img:srcset,src" });
    ctx.setProperty("host.pattern", "static.host.com");
    ctx.setProperty("matchingPatterns", "img:srcset;(\\/content\\/dam\\/.+?\\.(png|jpg))");
    StaticReferenceRewriteTransformerFactory factory = new StaticReferenceRewriteTransformerFactory();
    factory.activate(ctx);
    Transformer transformer = factory.createTransformer();
    transformer.setContentHandler(handler);
    AttributesImpl imageWithSrcSet = new AttributesImpl();
    imageWithSrcSet.addAttribute(null, "srcset", null, "CDATA", "/content/dam/flower.jpg 1280w,/content/dam/house.png 480w");
    transformer.startElement(null, "img", null, imageWithSrcSet);
    AttributesImpl imageWithJustSrc = new AttributesImpl();
    imageWithJustSrc.addAttribute(null, "src", null, "CDATA", "/content/dam/flower.jpg");
    transformer.startElement(null, "img", null, imageWithJustSrc);
    verify(handler, times(2)).startElement(isNull(String.class), eq("img"), isNull(String.class), attributesCaptor.capture());
    List<Attributes> values = attributesCaptor.getAllValues();
    assertEquals("//static.host.com/content/dam/flower.jpg 1280w,//static.host.com/content/dam/house.png 480w", values.get(0).getValue(0));
    assertEquals("//static.host.com/content/dam/flower.jpg", values.get(1).getValue(0));
}
Also used : MockComponentContext(org.apache.sling.commons.testing.osgi.MockComponentContext) Transformer(org.apache.sling.rewriter.Transformer) AttributesImpl(org.xml.sax.helpers.AttributesImpl) Attributes(org.xml.sax.Attributes) MockBundle(org.apache.sling.commons.testing.osgi.MockBundle) Test(org.junit.Test)

Example 63 with Attributes

use of org.xml.sax.Attributes in project acs-aem-commons by Adobe-Consulting-Services.

the class StaticReferenceRewriteTransformerFactoryTest method test_with_prefix_and_multiple_numbered_hosts.

@Test
public void test_with_prefix_and_multiple_numbered_hosts() throws Exception {
    MockBundle bundle = new MockBundle(-1);
    MockComponentContext ctx = new MockComponentContext(bundle);
    ctx.setProperty("prefixes", new String[] { "/etc/clientlib" });
    ctx.setProperty("host.pattern", "static{}.host.com");
    ctx.setProperty("host.count", 2);
    StaticReferenceRewriteTransformerFactory factory = new StaticReferenceRewriteTransformerFactory();
    factory.activate(ctx);
    Transformer transformer = factory.createTransformer();
    transformer.setContentHandler(handler);
    AttributesImpl in = new AttributesImpl();
    in.addAttribute(null, "href", null, "CDATA", "/etc/clientlib/testA.css");
    transformer.startElement(null, "link", null, in);
    verify(handler, only()).startElement(isNull(String.class), eq("link"), isNull(String.class), attributesCaptor.capture());
    Attributes out = attributesCaptor.getValue();
    assertEquals("//static2.host.com/etc/clientlib/testA.css", out.getValue(0));
}
Also used : MockComponentContext(org.apache.sling.commons.testing.osgi.MockComponentContext) Transformer(org.apache.sling.rewriter.Transformer) AttributesImpl(org.xml.sax.helpers.AttributesImpl) Attributes(org.xml.sax.Attributes) MockBundle(org.apache.sling.commons.testing.osgi.MockBundle) Test(org.junit.Test)

Example 64 with Attributes

use of org.xml.sax.Attributes in project acs-aem-commons by Adobe-Consulting-Services.

the class StaticReferenceRewriteTransformerFactoryTest method test_with_prefix_and_single_host.

@Test
public void test_with_prefix_and_single_host() throws Exception {
    MockBundle bundle = new MockBundle(-1);
    MockComponentContext ctx = new MockComponentContext(bundle);
    ctx.setProperty("prefixes", new String[] { "/etc/clientlib" });
    ctx.setProperty("host.pattern", "static.host.com");
    StaticReferenceRewriteTransformerFactory factory = new StaticReferenceRewriteTransformerFactory();
    factory.activate(ctx);
    Transformer transformer = factory.createTransformer();
    transformer.setContentHandler(handler);
    AttributesImpl in = new AttributesImpl();
    in.addAttribute(null, "href", null, "CDATA", "/etc/clientlib/test.css");
    transformer.startElement(null, "link", null, in);
    verify(handler, only()).startElement(isNull(String.class), eq("link"), isNull(String.class), attributesCaptor.capture());
    Attributes out = attributesCaptor.getValue();
    assertEquals("//static.host.com/etc/clientlib/test.css", out.getValue(0));
}
Also used : MockComponentContext(org.apache.sling.commons.testing.osgi.MockComponentContext) Transformer(org.apache.sling.rewriter.Transformer) AttributesImpl(org.xml.sax.helpers.AttributesImpl) Attributes(org.xml.sax.Attributes) MockBundle(org.apache.sling.commons.testing.osgi.MockBundle) Test(org.junit.Test)

Example 65 with Attributes

use of org.xml.sax.Attributes in project acs-aem-commons by Adobe-Consulting-Services.

the class StaticReferenceRewriteTransformerFactoryTest method test_with_nonrewritten_element.

@Test
public void test_with_nonrewritten_element() throws Exception {
    MockBundle bundle = new MockBundle(-1);
    MockComponentContext ctx = new MockComponentContext(bundle);
    ctx.setProperty("prefixes", new String[] { "/etc/clientlib" });
    ctx.setProperty("host.pattern", "static{}.host.com");
    ctx.setProperty("host.count", 2);
    StaticReferenceRewriteTransformerFactory factory = new StaticReferenceRewriteTransformerFactory();
    factory.activate(ctx);
    Transformer transformer = factory.createTransformer();
    transformer.setContentHandler(handler);
    AttributesImpl in = new AttributesImpl();
    in.addAttribute(null, "src", null, "CDATA", "/etc/clientlib/testABC.css");
    transformer.startElement(null, "iframe", null, in);
    verify(handler, only()).startElement(isNull(String.class), eq("iframe"), isNull(String.class), attributesCaptor.capture());
    Attributes out = attributesCaptor.getValue();
    assertEquals("/etc/clientlib/testABC.css", out.getValue(0));
}
Also used : MockComponentContext(org.apache.sling.commons.testing.osgi.MockComponentContext) Transformer(org.apache.sling.rewriter.Transformer) AttributesImpl(org.xml.sax.helpers.AttributesImpl) Attributes(org.xml.sax.Attributes) MockBundle(org.apache.sling.commons.testing.osgi.MockBundle) Test(org.junit.Test)

Aggregations

Attributes (org.xml.sax.Attributes)279 DefaultHandler (org.xml.sax.helpers.DefaultHandler)74 SAXException (org.xml.sax.SAXException)66 AttributesImpl (org.xml.sax.helpers.AttributesImpl)50 SAXParser (javax.xml.parsers.SAXParser)48 Test (org.junit.Test)46 POSaveFailedException (org.adempiere.pipo.exception.POSaveFailedException)37 InputSource (org.xml.sax.InputSource)33 SAXParserFactory (javax.xml.parsers.SAXParserFactory)30 IOException (java.io.IOException)29 File (java.io.File)22 ByteArrayInputStream (java.io.ByteArrayInputStream)19 InputStream (java.io.InputStream)17 ArrayList (java.util.ArrayList)17 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)17 Test (org.junit.jupiter.api.Test)17 XMLReader (org.xml.sax.XMLReader)17 ContentHandler (org.xml.sax.ContentHandler)15 StringReader (java.io.StringReader)12 Transformer (org.apache.sling.rewriter.Transformer)10