You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2016/10/11 20:18:18 UTC

svn commit: r1764342 - in /webservices/axiom/trunk/implementations/fom-impl/src/main/java/org/apache/abdera/parser/stax: ElementUnwrapperFilterHandler.java FOMDiv.java

Author: veithen
Date: Tue Oct 11 20:18:18 2016
New Revision: 1764342

URL: http://svn.apache.org/viewvc?rev=1764342&view=rev
Log:
Use the Serializer (instead of StAX) to serialize the content of a FOMDiv.

Added:
    webservices/axiom/trunk/implementations/fom-impl/src/main/java/org/apache/abdera/parser/stax/ElementUnwrapperFilterHandler.java   (with props)
Modified:
    webservices/axiom/trunk/implementations/fom-impl/src/main/java/org/apache/abdera/parser/stax/FOMDiv.java

Added: webservices/axiom/trunk/implementations/fom-impl/src/main/java/org/apache/abdera/parser/stax/ElementUnwrapperFilterHandler.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/implementations/fom-impl/src/main/java/org/apache/abdera/parser/stax/ElementUnwrapperFilterHandler.java?rev=1764342&view=auto
==============================================================================
--- webservices/axiom/trunk/implementations/fom-impl/src/main/java/org/apache/abdera/parser/stax/ElementUnwrapperFilterHandler.java (added)
+++ webservices/axiom/trunk/implementations/fom-impl/src/main/java/org/apache/abdera/parser/stax/ElementUnwrapperFilterHandler.java Tue Oct 11 20:18:18 2016
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.abdera.parser.stax;
+
+import org.apache.axiom.core.stream.StreamException;
+import org.apache.axiom.core.stream.XmlHandler;
+import org.apache.axiom.core.stream.XmlHandlerWrapper;
+
+/**
+ * Transforms a document into a fragment by unwrapping the root element.
+ */
+final class ElementUnwrapperFilterHandler extends XmlHandlerWrapper {
+    private int depth;
+
+    public ElementUnwrapperFilterHandler(XmlHandler parent) {
+        super(parent);
+    }
+
+    @Override
+    public void startDocument(String inputEncoding, String xmlVersion, String xmlEncoding,
+            Boolean standalone) throws StreamException {
+        super.startFragment();
+    }
+
+    @Override
+    public void startElement(String namespaceURI, String localName, String prefix)
+            throws StreamException {
+        if (depth > 0) {
+            super.startElement(namespaceURI, localName, prefix);
+        }
+    }
+
+    @Override
+    public void processAttribute(String namespaceURI, String localName, String prefix, String value,
+            String type, boolean specified) throws StreamException {
+        if (depth > 0) {
+            super.processAttribute(namespaceURI, localName, prefix, value, type, specified);
+        }
+    }
+
+    @Override
+    public void processAttribute(String name, String value, String type, boolean specified)
+            throws StreamException {
+        if (depth > 0) {
+            super.processAttribute(name, value, type, specified);
+        }
+    }
+
+    @Override
+    public void processNamespaceDeclaration(String prefix, String namespaceURI)
+            throws StreamException {
+        if (depth > 0) {
+            super.processNamespaceDeclaration(prefix, namespaceURI);
+        }
+    }
+
+    @Override
+    public void attributesCompleted() throws StreamException {
+        if (depth > 0) {
+            super.attributesCompleted();
+        }
+        depth++;
+    }
+
+    @Override
+    public void endElement() throws StreamException {
+        if (--depth > 0) {
+            super.endElement();
+        }
+    }
+}

Propchange: webservices/axiom/trunk/implementations/fom-impl/src/main/java/org/apache/abdera/parser/stax/ElementUnwrapperFilterHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/axiom/trunk/implementations/fom-impl/src/main/java/org/apache/abdera/parser/stax/FOMDiv.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/implementations/fom-impl/src/main/java/org/apache/abdera/parser/stax/FOMDiv.java?rev=1764342&r1=1764341&r2=1764342&view=diff
==============================================================================
--- webservices/axiom/trunk/implementations/fom-impl/src/main/java/org/apache/abdera/parser/stax/FOMDiv.java (original)
+++ webservices/axiom/trunk/implementations/fom-impl/src/main/java/org/apache/abdera/parser/stax/FOMDiv.java Tue Oct 11 20:18:18 2016
@@ -25,12 +25,13 @@ import static org.apache.abdera.util.Con
 import java.io.StringWriter;
 import java.util.Iterator;
 
-import javax.xml.stream.XMLOutputFactory;
-import javax.xml.stream.XMLStreamWriter;
-
 import org.apache.abdera.i18n.iri.IRI;
 import org.apache.abdera.model.Div;
 import org.apache.axiom.core.CoreModelException;
+import org.apache.axiom.core.stream.NamespaceRepairingFilterHandler;
+import org.apache.axiom.core.stream.StreamException;
+import org.apache.axiom.core.stream.XmlReader;
+import org.apache.axiom.core.stream.serializer.Serializer;
 import org.apache.axiom.fom.AbderaDiv;
 import org.apache.axiom.fom.FOMSemantics;
 import org.apache.axiom.om.OMElement;
@@ -104,19 +105,18 @@ public class FOMDiv extends FOMExtensibl
     }
 
     protected String getInternalValue() {
+        StringWriter sw = new StringWriter();
         try {
-            StringWriter out = new StringWriter();
-            XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(out);
-            writer.writeStartElement(""); 
-            for (Iterator<?> nodes = this.getChildren(); nodes.hasNext();) {
-                OMNode node = (OMNode)nodes.next();
-                node.serialize(writer);
+            XmlReader reader = coreGetReader(
+                    new ElementUnwrapperFilterHandler(new NamespaceRepairingFilterHandler(new Serializer(sw), null, true)),
+                    true, false);
+            while (!reader.proceed()) {
+                // Just loop
             }
-            writer.flush(); 
-            return out.getBuffer().toString().substring(2);
-        } catch (Exception e) {
+        } catch (StreamException ex) {
+            throw new FOMException(ex);
         }
-        return "";
+        return sw.toString();
     }
 
 }