abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From veit...@apache.org
Subject svn commit: r1374702 - in /abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax: FOMBuilder.java FOMStAXFilter.java
Date Sun, 19 Aug 2012 07:49:20 GMT
Author: veithen
Date: Sun Aug 19 07:49:20 2012
New Revision: 1374702

URL: http://svn.apache.org/viewvc?rev=1374702&view=rev
Log:
Also move attribute filtering to FOMStAXFilter, so that FOMBuilder no longer needs to override
StAXOMBuilder#processAttributes. Now, all filtering and transformation is done in FOMStAXFilter.

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

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=1374702&r1=1374701&r2=1374702&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 Sun
Aug 19 07:49:20 2012
@@ -20,7 +20,6 @@ package org.apache.abdera.parser.stax;
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamReader;
 
-import org.apache.abdera.filter.ParseFilter;
 import org.apache.abdera.model.Content;
 import org.apache.abdera.model.Document;
 import org.apache.abdera.model.Element;
@@ -32,12 +31,10 @@ import org.apache.axiom.om.OMContainer;
 import org.apache.axiom.om.OMDocument;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMException;
-import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.impl.OMContainerEx;
 import org.apache.axiom.om.impl.OMNodeEx;
 import org.apache.axiom.om.impl.builder.StAXOMBuilder;
-import org.apache.axiom.om.impl.util.OMSerializerUtil;
 
 @SuppressWarnings( {"unchecked", "deprecation"})
 public class FOMBuilder extends StAXOMBuilder implements Constants {
@@ -105,14 +102,6 @@ public class FOMBuilder extends StAXOMBu
         return ctype;
     }
 
-    private boolean isAcceptableToParse(QName qname, boolean attribute) {
-        if (parserOptions == null)
-            return true;
-        ParseFilter filter = parserOptions.getParseFilter();
-        return (filter != null) ? (!attribute) ? filter.acceptable(qname) : filter.acceptable(parser.getName(),
qname)
-            : true;
-    }
-
     /**
      * Method next.
      * 
@@ -142,30 +131,6 @@ public class FOMBuilder extends StAXOMBu
         return element;
     }
 
-    @Override
-    protected void processAttributes(OMElement node) {
-        int attribCount = parser.getAttributeCount();
-        for (int i = 0; i < attribCount; i++) {
-            QName attr = parser.getAttributeName(i);
-            if (isAcceptableToParse(attr, true)) {
-                String uri = parser.getAttributeNamespace(i);
-                String prefix = parser.getAttributePrefix(i);
-                OMNamespace namespace = null;
-                if (uri != null && uri.length() > 0) {
-                    namespace = node.findNamespace(uri, prefix);
-                    if (namespace == null) {
-                        if (prefix == null || "".equals(prefix)) {
-                            prefix = OMSerializerUtil.getNextNSPrefix();
-                        }
-                        namespace = node.declareNamespace(uri, prefix);
-                    }
-                }
-                String value = parser.getAttributeValue(i);
-                node.addAttribute(parser.getAttributeLocalName(i), value, namespace);
-            }
-        }
-    }
-
     public <T extends Element> Document<T> getFomDocument() {
         while (!indoc && !done) {
             next();

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=1374702&r1=1374701&r2=1374702&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
Sun Aug 19 07:49:20 2012
@@ -51,6 +51,8 @@ class FOMStAXFilter extends XMLStreamRea
     private int altEventType;
     private QName altQName;
     private String altText;
+    private int[] attributeMap;
+    private int attributeCount;
     
     FOMStAXFilter(XMLStreamReader parent, ParserOptions parserOptions) {
         super(parent);
@@ -61,6 +63,7 @@ class FOMStAXFilter extends XMLStreamRea
                 ignoreWhitespace = parseFilter.getIgnoreWhitespace();
                 ignoreComments = parseFilter.getIgnoreComments();
                 ignorePI = parseFilter.getIgnoreProcessingInstructions();
+                attributeMap = new int[8];
             }
         }
         resetEvent();
@@ -70,16 +73,9 @@ class FOMStAXFilter extends XMLStreamRea
         altEventType = -1;
         altQName = null;
         altText = null;
+        attributeCount = -1;
     }
     
-    private boolean isAcceptableToParse(QName qname, boolean attribute) {
-        if (parserOptions == null)
-            return true;
-        ParseFilter filter = parserOptions.getParseFilter();
-        return (filter != null) ? (!attribute) ? filter.acceptable(qname) : filter.acceptable(getName(),
qname)
-            : true;
-    }
-
     private void translateQName() {
         if (parserOptions.isQNameAliasMappingEnabled()) {
             Map<QName,QName> map = parserOptions.getQNameAliasMap();
@@ -89,6 +85,25 @@ class FOMStAXFilter extends XMLStreamRea
         }
     }
     
+    private void mapAttributes() {
+        attributeCount = 0;
+        int orgAttCount = super.getAttributeCount();
+        if (orgAttCount > 0) {
+            QName elementQName = super.getName();
+            ParseFilter filter = parserOptions.getParseFilter();
+            for (int i=0; i<orgAttCount; i++) {
+                if (filter.acceptable(elementQName, super.getAttributeName(i))) {
+                    if (attributeCount == attributeMap.length) {
+                        int[] newAttributeMap = new int[attributeMap.length*2];
+                        System.arraycopy(attributeMap, 0, newAttributeMap, 0, attributeMap.length);
+                        attributeMap = newAttributeMap;
+                    }
+                    attributeMap[attributeCount++] = i;
+                }
+            }
+        }
+    }
+    
     @Override
     public int next() throws XMLStreamException {
         resetEvent();
@@ -120,11 +135,15 @@ class FOMStAXFilter extends XMLStreamRea
                         // shouldn't be that big of a problem
                         continue;
                     case START_ELEMENT:
-                        if (!isAcceptableToParse(getName(), false)) {
+                        ParseFilter filter = parserOptions.getParseFilter();
+                        if (filter != null && !filter.acceptable(super.getName()))
{
                             depthInSkipElement = 1;
                             continue;
                         }
                         translateQName();
+                        if (attributeMap != null) {
+                            mapAttributes();
+                        }
                         break;
                     case END_ELEMENT:
                         translateQName();
@@ -189,4 +208,39 @@ class FOMStAXFilter extends XMLStreamRea
     public QName getName() {
         return altQName != null ? altQName : super.getName();
     }
+
+    @Override
+    public int getAttributeCount() {
+        return attributeCount != -1 ? attributeCount : super.getAttributeCount();
+    }
+
+    @Override
+    public String getAttributeNamespace(int index) {
+        return attributeCount != -1 ? super.getAttributeNamespace(attributeMap[index]) :
super.getAttributeNamespace(index);
+    }
+
+    @Override
+    public String getAttributeLocalName(int index) {
+        return attributeCount != -1 ? super.getAttributeLocalName(attributeMap[index]) :
super.getAttributeLocalName(index);
+    }
+
+    @Override
+    public String getAttributePrefix(int index) {
+        return attributeCount != -1 ? super.getAttributePrefix(attributeMap[index]) : super.getAttributePrefix(index);
+    }
+
+    @Override
+    public QName getAttributeName(int index) {
+        return attributeCount != -1 ? super.getAttributeName(attributeMap[index]) : super.getAttributeName(index);
+    }
+
+    @Override
+    public String getAttributeType(int index) {
+        return attributeCount != -1 ? super.getAttributeType(attributeMap[index]) : super.getAttributeType(index);
+    }
+
+    @Override
+    public String getAttributeValue(int index) {
+        return attributeCount != -1 ? super.getAttributeValue(attributeMap[index]) : super.getAttributeValue(index);
+    }
 }



Mime
View raw message