abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From veit...@apache.org
Subject svn commit: r1374550 - in /abdera/java/trunk/parser/src: main/java/org/apache/abdera/parser/stax/ test/java/org/apache/abdera/test/parser/stax/ test/resources/
Date Sat, 18 Aug 2012 10:35:25 GMT
Author: veithen
Date: Sat Aug 18 10:35:25 2012
New Revision: 1374550

URL: http://svn.apache.org/viewvc?rev=1374550&view=rev
Log:
Fixed the qnameAliasMap stuff. It was not exactly working as expected because Axiom resets
the namespace information after an element has been created. Moved the logic to FOMStAXFilter
so that QName aliasing is entirely transparent to Axiom. Also added a test case.

Modified:
    abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMBuilder.java
    abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMStAXFilter.java
    abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/ParserOptionsTest.java
    abdera/java/trunk/parser/src/test/resources/parseroptionstest.xml

Modified: abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMBuilder.java
URL: http://svn.apache.org/viewvc/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMBuilder.java?rev=1374550&r1=1374549&r2=1374550&view=diff
==============================================================================
--- abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMBuilder.java (original)
+++ abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMBuilder.java Sat
Aug 18 10:35:25 2012
@@ -17,8 +17,6 @@
  */
 package org.apache.abdera.parser.stax;
 
-import java.util.Map;
-
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamReader;
 
@@ -130,14 +128,6 @@ public class FOMBuilder extends StAXOMBu
         }
     }
 
-    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 (!indoc) {
@@ -145,9 +135,6 @@ public class FOMBuilder extends StAXOMBu
             indoc = true;
         }
         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);

Modified: abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMStAXFilter.java
URL: http://svn.apache.org/viewvc/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMStAXFilter.java?rev=1374550&r1=1374549&r2=1374550&view=diff
==============================================================================
--- abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMStAXFilter.java
(original)
+++ abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMStAXFilter.java
Sat Aug 18 10:35:25 2012
@@ -17,6 +17,8 @@
  */
 package org.apache.abdera.parser.stax;
 
+import java.util.Map;
+
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
@@ -47,6 +49,7 @@ class FOMStAXFilter extends XMLStreamRea
     private boolean ignorePI = false;
     private int depthInSkipElement;
     private int altEventType;
+    private QName altQName;
     private String altText;
     
     FOMStAXFilter(XMLStreamReader parent, ParserOptions parserOptions) {
@@ -65,6 +68,7 @@ class FOMStAXFilter extends XMLStreamRea
 
     private void resetEvent() {
         altEventType = -1;
+        altQName = null;
         altText = null;
     }
     
@@ -76,6 +80,15 @@ class FOMStAXFilter extends XMLStreamRea
             : true;
     }
 
+    private void translateQName() {
+        if (parserOptions.isQNameAliasMappingEnabled()) {
+            Map<QName,QName> map = parserOptions.getQNameAliasMap();
+            if (map != null) {
+                altQName = map.get(super.getName());
+            }
+        }
+    }
+    
     @Override
     public int next() throws XMLStreamException {
         resetEvent();
@@ -111,6 +124,10 @@ class FOMStAXFilter extends XMLStreamRea
                             depthInSkipElement = 1;
                             continue;
                         }
+                        translateQName();
+                        break;
+                    case END_ELEMENT:
+                        translateQName();
                         break;
                     case SPACE:
                         if (ignoreWhitespace) {
@@ -157,4 +174,19 @@ class FOMStAXFilter extends XMLStreamRea
     public String getText() {
         return altText != null ? altText : super.getText();
     }
+
+    @Override
+    public String getNamespaceURI() {
+        return altQName != null ? altQName.getNamespaceURI() : super.getNamespaceURI();
+    }
+
+    @Override
+    public String getLocalName() {
+        return altQName != null ? altQName.getLocalPart() : super.getLocalName();
+    }
+
+    @Override
+    public QName getName() {
+        return altQName != null ? altQName : super.getName();
+    }
 }

Modified: abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/ParserOptionsTest.java
URL: http://svn.apache.org/viewvc/abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/ParserOptionsTest.java?rev=1374550&r1=1374549&r2=1374550&view=diff
==============================================================================
--- abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/ParserOptionsTest.java
(original)
+++ abdera/java/trunk/parser/src/test/java/org/apache/abdera/test/parser/stax/ParserOptionsTest.java
Sat Aug 18 10:35:25 2012
@@ -18,8 +18,14 @@
 package org.apache.abdera.test.parser.stax;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
 import org.apache.abdera.Abdera;
 import org.apache.abdera.filter.ParseFilter;
 import org.apache.abdera.model.Document;
@@ -66,4 +72,17 @@ public class ParserOptionsTest {
                 "/parseroptionstest.xml"), options);
         assertEquals("", doc.getRoot().getEntries().get(0).getSummary());
     }
+    
+    @Test
+    public void test() {
+        Parser parser = abdera.getParser();
+        ParserOptions options = parser.getDefaultParserOptions();
+        Map<QName,QName> qnameAliases = new HashMap<QName,QName>();
+        qnameAliases.put(new QName("urn:test", "mylink"), new QName("http://www.w3.org/2005/Atom",
"link"));
+        options.setQNameAliasMap(qnameAliases);
+        options.setQNameAliasMappingEnabled(true);
+        Document<Feed> doc = parser.parse(ParserOptionsTest.class.getResourceAsStream(
+                "/parseroptionstest.xml"), options);
+        assertFalse(doc.getRoot().getEntries().get(0).getLinks().isEmpty());
+    }
 }

Modified: abdera/java/trunk/parser/src/test/resources/parseroptionstest.xml
URL: http://svn.apache.org/viewvc/abdera/java/trunk/parser/src/test/resources/parseroptionstest.xml?rev=1374550&r1=1374549&r2=1374550&view=diff
==============================================================================
--- abdera/java/trunk/parser/src/test/resources/parseroptionstest.xml (original)
+++ abdera/java/trunk/parser/src/test/resources/parseroptionstest.xml Sat Aug 18 10:35:25
2012
@@ -4,7 +4,7 @@
     <?pi processing instruction?>
     <entry>
         <title>Atom-Powered Robots Run Amok</title>
-        <link href="http://example.org/2003/12/13/atom03"/>
+        <ns:mylink href="http://example.org/2003/12/13/atom03" xmlns:ns="urn:test"/>
         <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
         <updated>2003-12-13T18:30:02Z</updated>
         <summary>      </summary>



Mime
View raw message