abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r592221 - in /incubator/abdera/java/trunk: core/src/main/java/org/apache/abdera/parser/ParserOptions.java core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java parser/src/main/java/org/apache/abdera/parser/stax/FOMBuilder.java
Date Tue, 06 Nov 2007 01:24:01 GMT
Author: jmsnell
Date: Mon Nov  5 17:23:59 2007
New Revision: 592221

URL: http://svn.apache.org/viewvc?rev=592221&view=rev
Log:
I'm not sure if this is something we should keep or not, but... as has been recently discussed

on the user mailing list, there are still examples of Atom feeds in the wild that use the
old
work-in-progress atom namespace ("http://purl.org/atom/ns#").  Those feeds really should be
updated,
but until they are, there are developers who want to be able to do interesting things with
those feeds.

Rather than go through and implement full support for the old namespace, this change provides
a way for
developers to optionally enable a QName-Alias mapping.  It is turned off by default.

Example use: 

    String s = "<entry xmlns='http://purl.org/atom/ns#'/>";
    StringReader sr = new StringReader(s);
    Abdera abdera = new Abdera();
    ParserOptions options = abdera.getParser().getDefaultParserOptions();
    options.setQNameAliasMappingEnabled(true);
    
    Map<QName,QName> map = new HashMap<QName,QName>();
    map.put(new QName("http://purl.org/atom/ns#","entry"),Constants.ENTRY);
    options.setQNameAliasMap(map);
    
    Document doc = abdera.getParser().parse(sr,options);
    System.out.println(doc.getRoot().getClass());
    
Ordinarily, without the QName Alias Map, the parser would parse the element as an extension
element, meaning the Entry interface could not be used. With the QName alias map in place,
it is parsed as if the QName was the proper Atom qname.  In the resulting object model, and
in the reserialized result, it will come out with the proper Atom namespace.  

There are several drawbacks to this approach, however... specifically, once a QName has been
aliased in this way, there is no going back. The reserialized entry will use the Atom namespace.
 

Like I said, I'm not sure if this is a solution we'll want to keep long term.  Please review
it; if you think it's unnecessary or if there is a better way, let me know

Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/parser/ParserOptions.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMBuilder.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/parser/ParserOptions.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/parser/ParserOptions.java?rev=592221&r1=592220&r2=592221&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/parser/ParserOptions.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/parser/ParserOptions.java
Mon Nov  5 17:23:59 2007
@@ -17,6 +17,10 @@
 */
 package org.apache.abdera.parser;
 
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
 import org.apache.abdera.factory.Factory;
 import org.apache.abdera.filter.ParseFilter;
 import org.apache.abdera.util.CompressionUtil.CompressionCodec;
@@ -150,4 +154,25 @@
    * True if undeclared named entities should be resolved.
    */
   boolean getResolveEntities();
+  
+  /**
+   * True if QName-Alias mapping is enabled
+   */
+  void setQNameAliasMappingEnabled(boolean enabled);
+  
+  /**
+   * True if QName-Alias mapping is enabled (default is false)
+   */
+  boolean isQNameAliasMappingEnabled();
+
+  /**
+   * Get the QName-Alias Mapping (default null)
+   */
+  Map<QName,QName> getQNameAliasMap();
+  
+  /**
+   * Set the QName-Alias Mapping
+   */
+  void setQNameAliasMap(Map<QName,QName> map);
+  
 }

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java?rev=592221&r1=592220&r2=592221&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java
Mon Nov  5 17:23:59 2007
@@ -20,6 +20,8 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.xml.namespace.QName;
+
 import org.apache.abdera.factory.Factory;
 import org.apache.abdera.filter.ParseFilter;
 import org.apache.abdera.parser.ParserOptions;
@@ -41,6 +43,9 @@
   protected CompressionCodec[] codecs = null;
   protected boolean resolveentities = true;
   protected Map<String,String> entities = new HashMap<String,String>();
+  
+  protected boolean qnamealiasing = false;
+  protected Map<QName,QName> aliases = null;
 
   protected abstract void initFactory();
   protected abstract void checkFactory(Factory factory);
@@ -396,5 +401,21 @@
   
   public boolean getResolveEntities() {
     return this.resolveentities;
+  }
+  
+  public Map<QName,QName> getQNameAliasMap() {
+    return aliases;
+  }
+  
+  public void setQNameAliasMap(Map<QName,QName> map) {
+    this.aliases = map;
+  }
+  
+  public boolean isQNameAliasMappingEnabled() {
+    return qnamealiasing;
+  }
+  
+  public void setQNameAliasMappingEnabled(boolean enabled) {
+    this.qnamealiasing = enabled;
   }
 }

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMBuilder.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMBuilder.java?rev=592221&r1=592220&r2=592221&view=diff
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMBuilder.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMBuilder.java
Mon Nov  5 17:23:59 2007
@@ -17,6 +17,8 @@
 */
 package org.apache.abdera.parser.stax;
 
+import java.util.Map;
+
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
@@ -258,6 +260,13 @@
     }
   }
   
+  private QName getAlias(QName qname) {
+    Map<QName,QName> map = parserOptions.getQNameAliasMap();
+    if (map == null) return qname;
+    QName alias = map.get(qname);
+    return alias != null ? alias : qname;
+  }
+  
   protected OMElement constructNode(OMContainer parent, String name) {
     OMElement element = null;
     if (fomDocument == null) {
@@ -265,6 +274,9 @@
       parent = (OMContainer) fomDocument;
     }
     QName qname = parser.getName();
+    if (parserOptions.isQNameAliasMappingEnabled()) {
+      qname = getAlias(qname);
+    }
     element = fomfactory.createElement(qname, parent, this);
     if (element == null) {
       element = new FOMElement(qname, parent, fomfactory, this);



Mime
View raw message