abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r415842 - in /incubator/abdera/java/trunk: examples/src/main/java/org/apache/abdera/examples/simple/ examples/src/main/java/org/apache/abdera/examples/xsltxpath/ examples/src/main/resources/ parser/src/test/java/org/apache/abdera/test/parse...
Date Tue, 20 Jun 2006 23:34:47 GMT
Author: jmsnell
Date: Tue Jun 20 16:34:46 2006
New Revision: 415842

URL: http://svn.apache.org/viewvc?rev=415842&view=rev
Log:
Expanded test cases and examples

* Test cases for alternatives, abderasource/abderaresult 
* Examples for xpath, xslt, parsefilter, textfilter

Added:
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/TextFilterExample.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/UnacceptableElementsExample.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XPathExample.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XsltExample.java
    incubator/abdera/java/trunk/examples/src/main/resources/content.xslt
    incubator/abdera/java/trunk/examples/src/main/resources/test.xslt
    incubator/abdera/java/trunk/examples/src/main/resources/xmlcontent.xml
    incubator/abdera/java/trunk/parser/src/test/resources/content.xslt
    incubator/abdera/java/trunk/parser/src/test/resources/test.xslt
    incubator/abdera/java/trunk/parser/src/test/resources/xmlcontent.xml
Modified:
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/Create.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/Parse.java
    incubator/abdera/java/trunk/examples/src/main/resources/simple.xml
    incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/FOMTest.java

Modified: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/Create.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/Create.java?rev=415842&r1=415841&r2=415842&view=diff
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/Create.java
(original)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/Create.java
Tue Jun 20 16:34:46 2006
@@ -17,6 +17,7 @@
 */
 package org.apache.abdera.examples.simple;
 
+import java.util.Calendar;
 import java.util.Date;
 
 import org.apache.abdera.factory.Factory;
@@ -36,24 +37,32 @@
     feed.addLink("http://example.org/");
     feed.setUpdated(new Date());
     feed.addAuthor("John Doe");
-    feed.setId("urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6", false);
+    feed.setId("urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6");
     feed.addContributor("Bob Jones");
     feed.addCategory("example");
     
     Entry entry = feed.insertEntry();
     entry.setTitleAsText("Atom-Powered Robots Run Amok");
     entry.addLink("http://example.org/2003/12/13/atom03");
-    entry.setId("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a", false);
+    entry.setId("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a");
     entry.setUpdated(new Date());
     entry.setSummaryAsText("Some text.");
     
     Entry entry2 = feed.insertEntry();
     entry2.setTitleAsText("re: Atom-Powered Robots Run Amok");
     entry2.addLink("/2003/12/13/atom03/1");
-    entry2.setId("urn:uuid:1225c695-cfb8-4ebb-aaaa-80cb323feb5b", false);
+    entry2.setId("urn:uuid:1225c695-cfb8-4ebb-aaaa-80cb323feb5b");
     entry2.setUpdated(new Date());
     entry2.setSummaryAsText("A response");
     entry2.addInReplyTo(entry);
+    
+    Entry entry3 = feed.addEntry();
+    entry3.setTitleAsXhtml("<div xmlns=\"http://www.w3.org/1999/xhtml\"><p>Test</p></div>");
+    entry3.addLink("/2003/12/13/atom03/2");
+    entry3.setId("HTTP://www.Example.org/foo/../bar", true); // normalizes the id to the
value http://www.example.org/bar
+    entry3.setUpdated(Calendar.getInstance());
+    entry3.setSummaryAsHtml("<p><a href=\"foo\">Test</a></p>", "http://example.org/site/");
+    entry3.setSource(feed.getAsSource());
     
     feed.getDocument().writeTo(System.out);
   }

Modified: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/Parse.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/Parse.java?rev=415842&r1=415841&r2=415842&view=diff
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/Parse.java
(original)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/Parse.java
Tue Jun 20 16:34:46 2006
@@ -44,7 +44,8 @@
 
     System.out.println(entry.getTitle());
     System.out.println(entry.getTitleType());
-    System.out.println(entry.getAlternateLink().getResolvedHref());
+    System.out.println(entry.getAlternateLink().getHref());            // relative URI
+    System.out.println(entry.getAlternateLink().getResolvedHref());    // absolute URI resolved
against Base URI
     System.out.println(entry.getId());
     System.out.println(entry.getUpdated());
     System.out.println(entry.getSummary());

Added: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/TextFilterExample.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/TextFilterExample.java?rev=415842&view=auto
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/TextFilterExample.java
(added)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/TextFilterExample.java
Tue Jun 20 16:34:46 2006
@@ -0,0 +1,70 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.examples.simple;
+
+import java.io.InputStream;
+import java.net.URL;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.filter.TextFilter;
+import org.apache.abdera.model.Base;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.model.ExtensionElement;
+import org.apache.abdera.model.Feed;
+import org.apache.abdera.parser.Parser;
+import org.apache.abdera.parser.ParserOptions;
+import org.apache.abdera.util.Constants;
+
+public class TextFilterExample {
+
+  public static void main(String[] args) throws Exception {
+    
+    // First create the text filter
+    TextFilter filter = new TextFilter() {
+      @Override
+      public String filterText(String text, Element element) {
+        ExtensionElement ee = (ExtensionElement) element;
+        QName qname = ee.getQName();
+        Base elparent = element.getParentElement();
+        if (Constants.NAME.equals(qname)) {
+          text = "Jane Doe";
+        } else if (Constants.TITLE.equals(qname) && elparent instanceof Entry) {
+          text = text.replaceAll("Amok", "Crazy");
+        }
+        return text;
+      }
+    };
+    
+    // Set the filter using the ParserOptions
+    ParserOptions options = Parser.INSTANCE.getDefaultParserOptions();
+    options.setTextFilter(filter);
+    
+    // Parse!
+    URL url = TextFilterExample.class.getResource("/simple.xml");
+    InputStream in = url.openStream();
+    Document<Feed> doc = Parser.INSTANCE.parse(in, url.toURI(), options);
+    Feed feed = doc.getRoot();
+    System.out.println(feed.getAuthor().getName());          // Jane Doe
+    System.out.println(feed.getEntries().get(0).getTitle()); // Atom-Powered Robots Run Crazy
+    
+  }
+
+}

Added: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/UnacceptableElementsExample.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/UnacceptableElementsExample.java?rev=415842&view=auto
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/UnacceptableElementsExample.java
(added)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/UnacceptableElementsExample.java
Tue Jun 20 16:34:46 2006
@@ -0,0 +1,68 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.examples.simple;
+
+import java.net.URI;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.filter.ParseFilter;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Feed;
+import org.apache.abdera.parser.Parser;
+import org.apache.abdera.parser.ParserOptions;
+import org.apache.abdera.parser.stax.FOMException;
+import org.apache.abdera.util.BlackListParseFilter;
+
+public class UnacceptableElementsExample {
+
+  public static void main(String[] args) throws Exception {
+    
+    /**
+     * By subclassing BlackListParseFilter, we can throw an error 
+     * when the parsed XML contains any content we don't want
+     */
+    ParseFilter exceptionFilter = new BlackListParseFilter() {
+      @Override
+      public boolean acceptable(QName qname) {
+        boolean answer = super.acceptable(qname);
+        if (!(answer)) {
+          throw new FOMException("Unacceptable element ::" + qname);
+        }
+        return answer;
+      }
+
+      @Override
+      public boolean acceptableAttribute(QName qname, QName attribute) {
+        return true;
+      }
+    };
+    exceptionFilter.add(new QName("http://example.org", "a"));
+    
+    ParserOptions options = Parser.INSTANCE.getDefaultParserOptions();
+    options.setParseFilter(exceptionFilter);
+    Document<Feed> doc = Parser.INSTANCE.parse(
+      UnacceptableElementsExample.class.getResourceAsStream("/xmlcontent.xml"), 
+      (URI)null, options);
+    
+    // this will throw a FOMException
+    doc.writeTo(System.out);
+    
+  }
+
+}

Added: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XPathExample.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XPathExample.java?rev=415842&view=auto
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XPathExample.java
(added)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XPathExample.java
Tue Jun 20 16:34:46 2006
@@ -0,0 +1,48 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.examples.xsltxpath;
+
+import java.io.InputStream;
+
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Feed;
+import org.apache.abdera.parser.Parser;
+import org.apache.abdera.xpath.XPath;
+
+public class XPathExample {
+
+  public static void main(String[] args) throws Exception {
+    
+    InputStream in = XPathExample.class.getResourceAsStream("/simple.xml");
+    Document<Feed> doc = Parser.INSTANCE.parse(in);
+    Feed feed = doc.getRoot();
+    XPath xpath = XPath.INSTANCE;
+    
+    System.out.println(xpath.evaluate("count(/a:feed)", feed));         // 1.0
+    System.out.println(xpath.numericValueOf("count(/a:feed)", feed));   // 1.0
+    System.out.println(xpath.isTrue("/a:feed/a:entry", feed));          // true (the feed
has an entry)
+    System.out.println(xpath.valueOf("/a:feed/a:entry/a:title", feed)); // Atom-Powered Robots
Run Amok
+    System.out.println(xpath.selectNodes("/a:feed/a:entry", feed));     // every entry
+    System.out.println(xpath.selectSingleNode("/a:feed", feed));
+    System.out.println(xpath.selectSingleNode("..", feed.getTitleElement()));
+    System.out.println(xpath.selectSingleNode("ancestor::*", feed.getEntries().get(0)));
+    System.out.println(xpath.valueOf("concat('The feed is is ',/a:feed/a:id)", feed)); //
"The feed is is urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6"
+    
+  }
+
+}

Added: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XsltExample.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XsltExample.java?rev=415842&view=auto
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XsltExample.java
(added)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XsltExample.java
Tue Jun 20 16:34:46 2006
@@ -0,0 +1,81 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.examples.xsltxpath;
+
+import java.io.ByteArrayOutputStream;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.abdera.model.Content;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.model.Feed;
+import org.apache.abdera.parser.Parser;
+import org.apache.abdera.util.AbderaSource;
+
+public class XsltExample {
+
+  public static void main(String[] args) {
+    
+    try {
+      
+      // Apply an XSLT transform to the entire Feed
+      TransformerFactory factory = TransformerFactory.newInstance();
+      
+      // Abdera is capable of parsing any well-formed XML document, even XSLT
+      Document xslt = Parser.INSTANCE.parse(XsltExample.class.getResourceAsStream("/test.xslt"));
+      AbderaSource xsltSource = new AbderaSource(xslt);
+      Transformer transformer = factory.newTransformer(xsltSource);
+      
+      // Now let's get the feed we're going to transform
+      Document<Feed> feed = Parser.INSTANCE.parse(XsltExample.class.getResourceAsStream("/simple.xml"));
+      AbderaSource feedSource = new AbderaSource(feed);
+      
+      // Prepare the output
+      ByteArrayOutputStream out = new ByteArrayOutputStream();
+      Result result = new StreamResult(out);
+      transformer.transform(feedSource, result);
+      System.out.println(out); // "This is a test urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6"
+      
+      // Apply an XSLT transform to XML in the content element
+      xslt = Parser.INSTANCE.parse(XsltExample.class.getResourceAsStream("/content.xslt"));
+      xsltSource = new AbderaSource(xslt);
+      transformer = factory.newTransformer(xsltSource);
+      
+      feed = Parser.INSTANCE.parse(XsltExample.class.getResourceAsStream("/xmlcontent.xml"));
+      Entry entry = feed.getRoot().getEntries().get(0);
+      Content content = entry.getContentElement();
+      AbderaSource contentSource = new AbderaSource(content.getValueElement());
+      
+      // Note that the AbderaSource is set to the value element of atom:content!!
+      
+      out = new ByteArrayOutputStream();
+      result = new StreamResult(out);
+      transformer.transform(contentSource, result);
+      System.out.println(out); // "This is a test test"
+      
+    } catch (Exception exception) {
+      // TrAX is likely not configured, skip the test
+    }
+    
+  }
+
+}

Added: incubator/abdera/java/trunk/examples/src/main/resources/content.xslt
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/resources/content.xslt?rev=415842&view=auto
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/resources/content.xslt (added)
+++ incubator/abdera/java/trunk/examples/src/main/resources/content.xslt Tue Jun 20 16:34:46
2006
@@ -0,0 +1,7 @@
+<xsl:stylesheet 
+  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
+  xmlns:a="http://example.org"
+  version="1.0" >
+  <xsl:output method="text" />
+  <xsl:template match = "/" >This is a test <xsl:value-of select="a:a/a:b/a:c" /></xsl:template>
+</xsl:stylesheet>
\ No newline at end of file

Modified: incubator/abdera/java/trunk/examples/src/main/resources/simple.xml
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/resources/simple.xml?rev=415842&r1=415841&r2=415842&view=diff
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/resources/simple.xml (original)
+++ incubator/abdera/java/trunk/examples/src/main/resources/simple.xml Tue Jun 20 16:34:46
2006
@@ -1,8 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
-<feed xmlns="http://www.w3.org/2005/Atom">
+<feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://example.org/">
   
   <title>Example Feed</title>
-  <link href="http://example.org/"/>
+  <link href=""/>
   <updated>2003-12-13T18:30:02Z</updated>
   <author>
     <name>John Doe</name>
@@ -11,7 +11,7 @@
 
   <entry>
     <title>Atom-Powered Robots Run Amok</title>
-    <link href="http://example.org/2003/12/13/atom03"/>
+    <link href="/2003/12/13/atom03"/>
     <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
     <updated>2003-12-13T18:30:02Z</updated>
     <summary>Some text.</summary>

Added: incubator/abdera/java/trunk/examples/src/main/resources/test.xslt
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/resources/test.xslt?rev=415842&view=auto
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/resources/test.xslt (added)
+++ incubator/abdera/java/trunk/examples/src/main/resources/test.xslt Tue Jun 20 16:34:46
2006
@@ -0,0 +1,7 @@
+<xsl:stylesheet 
+  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
+  xmlns:a="http://www.w3.org/2005/Atom"
+  version="1.0" >
+  <xsl:output method="text" />
+  <xsl:template match = "/" >This is a test <xsl:value-of select="a:feed/a:id" /></xsl:template>
+</xsl:stylesheet>
\ No newline at end of file

Added: incubator/abdera/java/trunk/examples/src/main/resources/xmlcontent.xml
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/resources/xmlcontent.xml?rev=415842&view=auto
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/resources/xmlcontent.xml (added)
+++ incubator/abdera/java/trunk/examples/src/main/resources/xmlcontent.xml Tue Jun 20 16:34:46
2006
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+  
+  <title>Example Feed</title>
+  <link href="http://example.org/"/>
+  <updated>2003-12-13T18:30:02Z</updated>
+  <author>
+    <name>John Doe</name>
+  </author>
+  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
+
+  <entry>
+    <title>Atom-Powered Robots Run Amok</title>
+    <link href="http://example.org/2003/12/13/atom03"/>
+    <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
+    <updated>2003-12-13T18:30:02Z</updated>
+    <content type="application/xml"><a xmlns="http://example.org"><b><c>test</c></b></a></content>
+  </entry>
+
+</feed>
\ No newline at end of file

Modified: incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/FOMTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/FOMTest.java?rev=415842&r1=415841&r2=415842&view=diff
==============================================================================
--- incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/FOMTest.java
(original)
+++ incubator/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/FOMTest.java
Tue Jun 20 16:34:46 2006
@@ -28,6 +28,10 @@
 import javax.activation.DataHandler;
 import javax.activation.MimeType;
 import javax.xml.namespace.QName;
+import javax.xml.transform.Result;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
 
 import org.apache.abdera.factory.Factory;
 import org.apache.abdera.filter.ParseFilter;
@@ -60,6 +64,7 @@
 import org.apache.abdera.parser.ParserOptions;
 import org.apache.abdera.parser.stax.FOMEntry;
 import org.apache.abdera.parser.stax.FOMFactory;
+import org.apache.abdera.util.AbderaSource;
 import org.apache.abdera.util.BlackListParseFilter;
 import org.apache.abdera.util.Constants;
 import org.apache.abdera.util.URIHelper;
@@ -671,6 +676,37 @@
      Feed feed = doc.getRoot();
      entry = feed.getEntries().get(0);
      assertTrue(entry instanceof MyEntry);
+  }
+  
+  public void testSourceResult() throws Exception {
+    try {
+      // Apply an XSLT transform to the entire Feed
+      TransformerFactory factory = TransformerFactory.newInstance();
+      Document xslt = Parser.INSTANCE.parse(FOMTest.class.getResourceAsStream("/test.xslt"));
+      AbderaSource xsltSource = new AbderaSource(xslt);
+      Transformer transformer = factory.newTransformer(xsltSource);
+      Document<Feed> feed = Parser.INSTANCE.parse(FOMTest.class.getResourceAsStream("/simple.xml"));
+      AbderaSource feedSource = new AbderaSource(feed);
+      ByteArrayOutputStream out = new ByteArrayOutputStream();
+      Result result = new StreamResult(out);
+      transformer.transform(feedSource, result);
+      assertEquals(out.toString(), "This is a test urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6");
+      
+      // Apply an XSLT transform to XML in the content element
+      xslt = Parser.INSTANCE.parse(FOMTest.class.getResourceAsStream("/content.xslt"));
+      xsltSource = new AbderaSource(xslt);
+      transformer = factory.newTransformer(xsltSource);
+      feed = Parser.INSTANCE.parse(FOMTest.class.getResourceAsStream("/xmlcontent.xml"));
+      Entry entry = feed.getRoot().getEntries().get(0);
+      Content content = entry.getContentElement();
+      AbderaSource contentSource = new AbderaSource(content.getValueElement());
+      out = new ByteArrayOutputStream();
+      result = new StreamResult(out);
+      transformer.transform(contentSource, result);
+      assertEquals(out.toString(), "This is a test test");
+    } catch (Exception exception) {
+      // TrAX is likely not configured, skip the test
+    }
   }
   
   public static class MyFactory extends FOMFactory {

Added: incubator/abdera/java/trunk/parser/src/test/resources/content.xslt
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/test/resources/content.xslt?rev=415842&view=auto
==============================================================================
--- incubator/abdera/java/trunk/parser/src/test/resources/content.xslt (added)
+++ incubator/abdera/java/trunk/parser/src/test/resources/content.xslt Tue Jun 20 16:34:46
2006
@@ -0,0 +1,7 @@
+<xsl:stylesheet 
+  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
+  xmlns:a="http://example.org"
+  version="1.0" >
+  <xsl:output method="text" />
+  <xsl:template match = "/" >This is a test <xsl:value-of select="a:a/a:b/a:c" /></xsl:template>
+</xsl:stylesheet>
\ No newline at end of file

Added: incubator/abdera/java/trunk/parser/src/test/resources/test.xslt
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/test/resources/test.xslt?rev=415842&view=auto
==============================================================================
--- incubator/abdera/java/trunk/parser/src/test/resources/test.xslt (added)
+++ incubator/abdera/java/trunk/parser/src/test/resources/test.xslt Tue Jun 20 16:34:46 2006
@@ -0,0 +1,7 @@
+<xsl:stylesheet 
+  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
+  xmlns:a="http://www.w3.org/2005/Atom"
+  version="1.0" >
+  <xsl:output method="text" />
+  <xsl:template match = "/" >This is a test <xsl:value-of select="a:feed/a:id" /></xsl:template>
+</xsl:stylesheet>
\ No newline at end of file

Added: incubator/abdera/java/trunk/parser/src/test/resources/xmlcontent.xml
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/test/resources/xmlcontent.xml?rev=415842&view=auto
==============================================================================
--- incubator/abdera/java/trunk/parser/src/test/resources/xmlcontent.xml (added)
+++ incubator/abdera/java/trunk/parser/src/test/resources/xmlcontent.xml Tue Jun 20 16:34:46
2006
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+  
+  <title>Example Feed</title>
+  <link href="http://example.org/"/>
+  <updated>2003-12-13T18:30:02Z</updated>
+  <author>
+    <name>John Doe</name>
+  </author>
+  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
+
+  <entry>
+    <title>Atom-Powered Robots Run Amok</title>
+    <link href="http://example.org/2003/12/13/atom03"/>
+    <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
+    <updated>2003-12-13T18:30:02Z</updated>
+    <content type="application/xml"><a xmlns="http://example.org"><b><c>test</c></b></a></content>
+  </entry>
+
+</feed>
\ No newline at end of file



Mime
View raw message