forrest-svn mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thors...@apache.org
Subject svn commit: r702467 - in /forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper: NSAllocator.java StAX.java
Date Tue, 07 Oct 2008 13:04:44 GMT
Author: thorsten
Date: Tue Oct  7 06:04:43 2008
New Revision: 702467

URL: http://svn.apache.org/viewvc?rev=702467&view=rev
Log:
FOR-1118
Extracting helper methods from the allocator to the stax helper. Making this methods static.
Updating stax helper to use as well the map interface. Further updating to the new changes
regarding support for old contracts (shrink). This way there is no real need to touch the
current contracts right away.

Modified:
    forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/NSAllocator.java
    forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/StAX.java

Modified: forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/NSAllocator.java
URL: http://svn.apache.org/viewvc/forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/NSAllocator.java?rev=702467&r1=702466&r2=702467&view=diff
==============================================================================
--- forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/NSAllocator.java
(original)
+++ forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/NSAllocator.java
Tue Oct  7 06:04:43 2008
@@ -16,18 +16,9 @@
  */
 package org.apache.forrest.dispatcher.impl.helper;
 
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Set;
-
-import javax.xml.namespace.NamespaceContext;
-import javax.xml.namespace.QName;
 import javax.xml.stream.XMLEventFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
-import javax.xml.stream.events.Attribute;
-import javax.xml.stream.events.Namespace;
 import javax.xml.stream.events.XMLEvent;
 import javax.xml.stream.util.XMLEventAllocator;
 import javax.xml.stream.util.XMLEventConsumer;
@@ -45,11 +36,8 @@
 public class NSAllocator extends DefaultEventAllocator implements
     XMLEventAllocator {
 
-  private XMLEventFactory eventFactory;
-
-  protected NSAllocator(boolean accurateLocation) {
+  public NSAllocator(boolean accurateLocation) {
     super(accurateLocation);
-    eventFactory = XMLEventFactory.newInstance();
   }
 
   /*
@@ -62,11 +50,7 @@
     XMLEvent event = allocate(reader);
     if (event != null) {
       if (event.isStartElement()) {
-        QName name = event.asStartElement().getName();
-        Iterator attributes = allocateAttributes(reader);
-        Iterator namespaces = allocateNamespaces(name, reader);
-        XMLEvent startElement = eventFactory.createStartElement(name,
-            attributes, namespaces);
+        XMLEvent startElement = StAX.createStartElementNS(reader, event);
         consumer.add(startElement);
       } else {
         if (event.isEndDocument()) {
@@ -76,48 +60,4 @@
     }
   }
 
-  /**
-   * Here we calculate the namespaces. If we do not find a declaration for a
-   * prefixed node we will see if it is in the NameSpaceContext and create it.
-   * 
-   * @param name qname of the node that we are working with
-   * @param reader the underlying stream reader.
-   * @return iterator of the calculated namespaces. 
-   */
-  private Iterator<Namespace> allocateNamespaces(QName name,
-      XMLStreamReader reader) {
-    ArrayList<Namespace> namespaces = new ArrayList<Namespace>();
-    Set<String> set = new HashSet<String>();
-    Namespace namespace;
-    for (int i = 0, n = reader.getNamespaceCount(); i < n; ++i) {
-      String prefix = reader.getNamespacePrefix(i);
-      String uri = reader.getNamespaceURI(i);
-      namespace = eventFactory.createNamespace(prefix, uri);
-      namespaces.add(namespace);
-      set.add(prefix);
-    }
-    String prefix = name.getPrefix();
-    if (null != prefix && !prefix.equals("") && !set.contains(prefix)) {
-      NamespaceContext context = reader.getNamespaceContext();
-      String uri = context.getNamespaceURI(prefix);
-      namespace = eventFactory.createNamespace(prefix, uri);
-      namespaces.add(namespace);
-    }
-    return namespaces.iterator();
-  }
-
-  /**
-   * Handy method to create an attribute iterator of the current event.
-   * @param reader the underlying XMLStreamReader.
-   * @return an attribute iterator.
-   */
-  private Iterator<Attribute> allocateAttributes(XMLStreamReader reader) {
-    ArrayList<Attribute> attributes = new ArrayList<Attribute>();
-    for (int i = 0, n = reader.getAttributeCount(); i < n; ++i) {
-      attributes.add(eventFactory.createAttribute(reader.getAttributeName(i),
-          reader.getAttributeValue(i)));
-    }
-    return attributes.iterator();
-  }
-
 }

Modified: forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/StAX.java
URL: http://svn.apache.org/viewvc/forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/StAX.java?rev=702467&r1=702466&r2=702467&view=diff
==============================================================================
--- forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/StAX.java
(original)
+++ forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/StAX.java
Tue Oct  7 06:04:43 2008
@@ -20,8 +20,14 @@
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.HashMap;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
 
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
 import javax.xml.stream.XMLEventFactory;
 import javax.xml.stream.XMLEventReader;
 import javax.xml.stream.XMLEventWriter;
@@ -31,6 +37,8 @@
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.Namespace;
 import javax.xml.stream.events.XMLEvent;
 import javax.xml.stream.util.XMLEventAllocator;
 
@@ -47,10 +55,12 @@
 public class StAX extends Loggable {
   private XMLOutputFactory outputFactory = null;
 
-  private XMLEventFactory eventFactory = null;
+  private static XMLEventFactory eventFactory;
 
   private XMLInputFactory inputFactory = null;
 
+  private XMLInputFactory nsInputFactory= null;
+
   /**
    * Easy helper to get StAX based reader and writer.
    */
@@ -117,6 +127,26 @@
   }
 
   /**
+   * Get a stream reader based on the incoming stream (ns enhanced)
+   * 
+   * @param in
+   *                the stream we want to read from
+   * @return ready to use stream reader
+   * @throws XMLStreamException
+   */
+  public XMLStreamReader getNSReader(InputStream in) throws XMLStreamException {
+    initNs();
+    XMLStreamReader reader = nsInputFactory.createXMLStreamReader(in);
+    return reader;
+  }
+  
+  private void initNs() {
+    if(null==nsInputFactory)
+      nsInputFactory = XMLInputFactory.newInstance();
+      nsInputFactory.setEventAllocator(new NSAllocator(true));
+  }
+
+  /**
    * Get the ready to use EventFactory
    * 
    * @return ready to use EventFactory
@@ -132,18 +162,81 @@
   public XMLEventAllocator getEventAllocator() {
     return  inputFactory.getEventAllocator();
   }
+  
+  /**
+   *  Get the ready to use NS enhanced Event Allocator
+   * @return ready to use Event Allocator
+   */
+  public XMLEventAllocator getNSEventAllocator() {
+    initNs();
+    return  nsInputFactory.getEventAllocator();
+  }
+  
+  public static XMLEvent createStartElement(final QName name, final Iterator attributes,
final Iterator namespaces){
+    XMLEvent startElement = eventFactory.createStartElement(name,
+        attributes, namespaces);
+    return startElement;
+  }
 
   /**
+   * Here we calculate the namespaces. If we do not find a declaration for a
+   * prefixed node we will see if it is in the NameSpaceContext and create it.
+   * 
+   * @param name qname of the node that we are working with
+   * @param reader the underlying stream reader.
+   * @return iterator of the calculated namespaces. 
+   */
+  public static Iterator<Namespace> allocateNamespacesForced(QName name,
+      XMLStreamReader reader) {
+    ArrayList<Namespace> namespaces = new ArrayList<Namespace>();
+    Set<String> set = new HashSet<String>();
+    Namespace namespace;
+    for (int i = 0, n = reader.getNamespaceCount(); i < n; ++i) {
+      String prefix = reader.getNamespacePrefix(i);
+      String uri = reader.getNamespaceURI(i);
+      namespace = eventFactory.createNamespace(prefix, uri);
+      namespaces.add(namespace);
+      set.add(prefix);
+    }
+    String prefix = name.getPrefix();
+    if (null != prefix && !prefix.equals("") && !set.contains(prefix)) {
+      NamespaceContext context = reader.getNamespaceContext();
+      String uri = context.getNamespaceURI(prefix);
+      namespace = eventFactory.createNamespace(prefix, uri);
+      namespaces.add(namespace);
+    }
+    return namespaces.iterator();
+  }
+
+  /**
+   * Handy method to create an attribute iterator of the current event.
+   * @param reader the underlying XMLStreamReader.
+   * @return an attribute iterator.
+   */
+  public static Iterator<Attribute> allocateAttributes(final XMLStreamReader reader)
{
+    ArrayList<Attribute> attributes = new ArrayList<Attribute>();
+    for (int i = 0, n = reader.getAttributeCount(); i < n; ++i) {
+      attributes.add(eventFactory.createAttribute(reader.getAttributeName(i),
+          reader.getAttributeValue(i)));
+    }
+    return attributes.iterator();
+  }
+  /**
    * @param reader
+   * @param shrink 
    * @return
    * @throws XMLStreamException
    */
-  protected InputSource recordXmlProperies(XMLStreamReader reader) throws XMLStreamException
{
+  protected byte[] recordXmlProperies(XMLStreamReader reader, boolean shrink) throws XMLStreamException
{
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     XMLEventWriter writerProperty = getWriter(out);
     XMLEventAllocator allocator = getEventAllocator();
+    writerProperty.add(eventFactory.createStartDocument());
     XMLEvent currentEvent = allocator.allocate(reader);
-    writerProperty.add(currentEvent);
+    if (currentEvent.isStartElement()){
+      XMLEvent startElement = StAX.createStartElementNS(reader, currentEvent);
+      writerProperty.add(startElement);
+    }
     boolean process = true;
     boolean hadChilds = false;
     while (process) {
@@ -153,13 +246,17 @@
       case XMLStreamConstants.START_ELEMENT:
         if (!reader.getLocalName().equals(Captions.PROPERTY_ELEMENT)){
           hadChilds=true;
+          writerProperty.add(currentEvent);
+        }else{
+          XMLEvent startElement = StAX.createStartElementNS(reader, currentEvent);
+          writerProperty.add(startElement);
         }
-        writerProperty.add(currentEvent);
         break;
       
       case XMLStreamConstants.END_ELEMENT:
         if (reader.getLocalName().equals(Captions.PROPERTY_ELEMENT)) {
           writerProperty.add(currentEvent);
+          writerProperty.add(eventFactory.createEndDocument());
           writerProperty.flush();
           writerProperty.close();
           process = false;
@@ -173,9 +270,11 @@
         break;
       }
     }
-    InputSource value = null;
-    if(hadChilds){
-      value = new InputSource(new ByteArrayInputStream(out.toByteArray()));
+    log.debug(out.toString());
+
+    byte[] value = null;
+    if(!shrink || hadChilds){
+      value = out.toByteArray();
     }
     return value;
   }
@@ -185,17 +284,18 @@
    * @param param
    * @param propertyName
    * @param propertyValue
+   * @param shrink 
    * @throws XMLStreamException
    */
-  protected void addProperties(XMLStreamReader reader, HashMap<String, Object> param,
-      String propertyName, String propertyValue,boolean allowXmlProperties) throws XMLStreamException
{
+  protected void addProperties(XMLStreamReader reader, Map<String, Object> param,
+      String propertyName, String propertyValue,boolean allowXmlProperties, boolean shrink)
throws XMLStreamException {
     /*
      *  if we are in allowXmlProperties mode then
      *  we need to see whether we have an input stream meaning child
      *  elements of the property element.
      */
     if (allowXmlProperties ) {
-      InputSource recordXmlProperies = recordXmlProperies(reader);
+      byte[] recordXmlProperies = recordXmlProperies(reader, shrink);
       if(null != recordXmlProperies){
         param.put(propertyName, recordXmlProperies);
         return;
@@ -211,4 +311,16 @@
       param.put(propertyName, new String(propertyValue));    
     }
   }
+
+  /**
+   * @param reader
+   * @param currentEvent
+   * @return
+   */
+  public static XMLEvent createStartElementNS(final XMLStreamReader reader, final XMLEvent
currentEvent) {
+    QName name = currentEvent.asStartElement().getName();
+    Iterator attributes = allocateAttributes(reader);
+    Iterator namespaces = allocateNamespacesForced(name, reader);
+    return createStartElement(name, attributes, namespaces);
+  }
 }



Mime
View raw message