abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r611875 - in /incubator/abdera/java/trunk: examples/src/main/java/org/apache/abdera/examples/simple/ examples/src/main/java/org/apache/abdera/examples/xsltxpath/ parser/src/main/java/org/apache/abdera/parser/stax/ parser/src/main/java/org/a...
Date Mon, 14 Jan 2008 18:27:10 GMT
Author: jmsnell
Date: Mon Jan 14 10:27:08 2008
New Revision: 611875

URL: http://svn.apache.org/viewvc?rev=611875&view=rev
Log:
A few cleanups, new methods for setting the default functions/variables for the xpath impl,
and a new example showing the use of custom functions in xpath

Added:
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XPathFunctionsExample.java
Modified:
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/LangTagExample.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/XsltExample.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMXPath.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/ResolveFunction.java

Modified: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/LangTagExample.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/LangTagExample.java?rev=611875&r1=611874&r2=611875&view=diff
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/LangTagExample.java
(original)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/LangTagExample.java
Mon Jan 14 10:27:08 2008
@@ -26,6 +26,7 @@
 /**
  * Example using the Lang tag implementation
  */
+@SuppressWarnings("unused") 
 public class LangTagExample {
 
   public static void main(String... args) throws Exception {

Modified: 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=611875&r1=611874&r2=611875&view=diff
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/UnacceptableElementsExample.java
(original)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/simple/UnacceptableElementsExample.java
Mon Jan 14 10:27:08 2008
@@ -39,6 +39,7 @@
      * when the parsed XML contains any content we don't want
      */
     ListParseFilter exceptionFilter = new BlackListParseFilter() {
+      private static final long serialVersionUID = 7564587859561916928L;
       @Override
       public boolean acceptable(QName qname) {
         boolean answer = super.acceptable(qname);

Added: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XPathFunctionsExample.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XPathFunctionsExample.java?rev=611875&view=auto
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XPathFunctionsExample.java
(added)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XPathFunctionsExample.java
Mon Jan 14 10:27:08 2008
@@ -0,0 +1,109 @@
+/*
+* 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.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.model.Feed;
+import org.apache.abdera.model.Link;
+import org.apache.abdera.model.Source;
+import org.apache.abdera.parser.stax.FOMXPath;
+import org.apache.abdera.xpath.XPath;
+import org.apache.axiom.om.OMNode;
+import org.jaxen.Context;
+import org.jaxen.Function;
+import org.jaxen.FunctionCallException;
+
+public class XPathFunctionsExample {
+
+  @SuppressWarnings("unchecked") 
+  public static void main(String... args) throws Exception {
+    
+    Abdera abdera = Abdera.getInstance();
+    Feed feed = abdera.newFeed();
+    feed.setBaseUri("http://example.org/");
+    // add additional feed metadata
+    Entry entry = feed.addEntry();
+    // add additional feed metadata
+    entry.addLink("alternate.xml");  // relative URI
+    
+    XPath xpath = abdera.getXPath();
+    System.out.println(xpath.valueOf("abdera:resolve(/a:feed/a:entry/a:link/@href)",feed));
+    
+    
+    // You can add your own xpath functions. 
+    FOMXPath fxpath = (FOMXPath) xpath;
+    Map<QName,Function> functions = fxpath.getDefaultFunctions();
+    functions.put(AlternateLinkFunction.QNAME, new AlternateLinkFunction());
+    fxpath.setDefaultFunctions(functions);
+    List<Link> links = 
+      fxpath.selectNodes(
+        "abdera:altlinks(/a:feed/a:entry)", 
+        feed);
+    System.out.println(links);
+  }
+  
+  
+  public static class AlternateLinkFunction 
+    implements Function {
+
+    public static final QName QNAME = 
+      new QName(
+        "http://incubator.apache.org/abdera", 
+        "altlinks");
+    
+    @SuppressWarnings("unchecked") 
+    public Object call(
+      Context context, 
+      List args) 
+        throws FunctionCallException {
+      List<Link> results = new ArrayList<Link>();
+      if (args.isEmpty()) return null;
+      for(Object obj : args) {
+        if (obj instanceof List) {
+          for (Object o : (List)obj) {
+            try {
+              if (o instanceof OMNode) {
+                OMNode node = (OMNode) o;
+                List<Link> links = null;
+                if (node instanceof Source) {
+                  Source source = (Source) node;
+                  links = source.getLinks("alternate");
+                } else if (node instanceof Entry) {
+                  Entry entry = (Entry) node;
+                  links = entry.getLinks("alternate");
+                }
+                if (links != null) results.addAll(links);
+              }
+            } catch (Exception e) {}
+          }
+        } else {
+          // nothing to do
+        }
+      }
+      return results;
+    }
+    
+  }
+}

Modified: 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=611875&r1=611874&r2=611875&view=diff
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XsltExample.java
(original)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/xsltxpath/XsltExample.java
Mon Jan 14 10:27:08 2008
@@ -27,6 +27,7 @@
 import org.apache.abdera.Abdera;
 import org.apache.abdera.model.Content;
 import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Element;
 import org.apache.abdera.model.Entry;
 import org.apache.abdera.model.Feed;
 import org.apache.abdera.parser.Parser;
@@ -44,7 +45,7 @@
       TransformerFactory factory = TransformerFactory.newInstance();
       
       // Abdera is capable of parsing any well-formed XML document, even XSLT
-      Document xslt = parser.parse(XsltExample.class.getResourceAsStream("/test.xslt"));
+      Document<Element> xslt = parser.parse(XsltExample.class.getResourceAsStream("/test.xslt"));
       AbderaSource xsltSource = new AbderaSource(xslt);
       Transformer transformer = factory.newTransformer(xsltSource);
       

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMXPath.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMXPath.java?rev=611875&r1=611874&r2=611875&view=diff
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMXPath.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMXPath.java
Mon Jan 14 10:27:08 2008
@@ -297,7 +297,17 @@
     return new HashMap<QName,Function>(functions);
   }
   
+  public synchronized void setDefaultFunctions(Map<QName,Function> functions) {
+    this.functions.clear();
+    this.functions.putAll(functions);
+  }
+  
   public Map<QName,Object> getDefaultVariables() {
     return new HashMap<QName,Object>(variables);
+  }
+  
+  public synchronized void setDefaultVariables(Map<QName,Object> variables) {
+    this.variables.clear();
+    this.variables.putAll(variables);
   }
 }

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/ResolveFunction.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/ResolveFunction.java?rev=611875&r1=611874&r2=611875&view=diff
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/ResolveFunction.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/util/ResolveFunction.java
Mon Jan 14 10:27:08 2008
@@ -22,9 +22,9 @@
 
 import javax.xml.namespace.QName;
 
+import org.apache.abdera.i18n.iri.IRI;
 import org.apache.abdera.model.Document;
 import org.apache.abdera.model.Element;
-import org.apache.abdera.i18n.iri.IRI;
 import org.apache.axiom.om.OMAttribute;
 import org.apache.axiom.om.OMContainer;
 import org.apache.axiom.om.OMNode;
@@ -38,8 +38,9 @@
   
   public static final QName QNAME = new QName("http://incubator.apache.org/abdera", "resolve");
   
+  @SuppressWarnings("unchecked") 
   public Object call(Context context, List args) throws FunctionCallException {
-    List<IRI> results = new ArrayList<IRI>();
+    List<String> results = new ArrayList<String>();
     if (args.isEmpty()) return null;
     Navigator navigator = context.getNavigator();
     for(Object obj : args) {
@@ -53,7 +54,7 @@
               OMNode node = (OMNode) o;
               OMContainer el = node.getParent();
               if (el instanceof Document) {
-                Document doc = (Document) el;
+                Document<Element> doc = (Document<Element>) el;
                 baseUri = doc.getBaseUri();
               } else if (el instanceof Element) {
                 Element element = (Element) el;
@@ -66,7 +67,7 @@
             }
             if (baseUri != null) {
               resolved = baseUri.resolve(value);
-              results.add(resolved);
+              results.add(resolved.toString());
             }
           } catch (Exception e) {}
         }



Mime
View raw message