You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/11/11 22:05:36 UTC

svn commit: r835068 - in /incubator/pivot/trunk: core/src/org/apache/pivot/xml/Element.java core/src/org/apache/pivot/xml/XMLSerializer.java tools/src/org/apache/pivot/tools/xml/XMLViewer.java tools/src/org/apache/pivot/tools/xml/xml_viewer.wtkx

Author: gbrown
Date: Wed Nov 11 21:05:36 2009
New Revision: 835068

URL: http://svn.apache.org/viewvc?rev=835068&view=rev
Log:
Add namespace support to XMLSerializer; move element attributes into an AttributeDictionary.

Modified:
    incubator/pivot/trunk/core/src/org/apache/pivot/xml/Element.java
    incubator/pivot/trunk/core/src/org/apache/pivot/xml/XMLSerializer.java
    incubator/pivot/trunk/tools/src/org/apache/pivot/tools/xml/XMLViewer.java
    incubator/pivot/trunk/tools/src/org/apache/pivot/tools/xml/xml_viewer.wtkx

Modified: incubator/pivot/trunk/core/src/org/apache/pivot/xml/Element.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/xml/Element.java?rev=835068&r1=835067&r2=835068&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/xml/Element.java (original)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/xml/Element.java Wed Nov 11 21:05:36 2009
@@ -31,11 +31,11 @@
 /**
  * Node class representing an XML element.
  */
-public class Element extends Node implements List<Node>, Dictionary<String, String> {
+public class Element extends Node implements List<Node> {
     /**
      * Dictionary representing the namespaces declared by this element.
      */
-    public class NamespaceDictionary implements Dictionary<String, String> {
+    public class NamespaceDictionary implements Dictionary<String, String>, Iterable<String> {
         private NamespaceDictionary() {
         }
 
@@ -129,6 +129,111 @@
         public boolean isEmpty() {
             return namespaces.isEmpty();
         }
+
+        /**
+         * Returns an iterator over the element's namespace prefixes.
+         */
+        @Override
+        public Iterator<String> iterator() {
+            return new ImmutableIterator<String>(namespaces.iterator());
+        }
+    }
+
+    /**
+     * Dictionary representing the attributes declared by this element.
+     */
+    public class AttributeDictionary implements Dictionary<String, String>, Iterable<String> {
+        private AttributeDictionary() {
+        }
+
+        /**
+         * Returns an attribute value.
+         */
+        @Override
+        public String get(String attribute) {
+            return attributes.get(attribute);
+        }
+
+        /**
+         * Sets an attribute value.
+         *
+         * @param attribute
+         * @param value
+         *
+         * @return
+         * The value previously associated with the given attribute.
+         */
+        @Override
+        public String put(String attribute, String value) {
+            if (value == null) {
+                throw new IllegalArgumentException("value is null.");
+            }
+
+            boolean update = containsKey(attribute);
+            String previousValue = attributes.put(attribute, value);
+
+            if (update) {
+                elementListeners.attributeUpdated(Element.this, attribute, previousValue);
+            } else {
+                elementListeners.attributeAdded(Element.this, attribute);
+            }
+
+            return previousValue;
+        }
+
+        /**
+         * Removes an attribute value.
+         *
+         * @param attribute
+         *
+         * @return
+         * The value previously associated with the given attribute.
+         */
+        @Override
+        public String remove(String attribute) {
+            String value = null;
+
+            if (containsKey(attribute)) {
+                value = attributes.remove(attribute);
+                elementListeners.namespaceRemoved(Element.this, attribute, value);
+            }
+
+            return value;
+        }
+
+        /**
+         * Tests for the existence of an attribute.
+         *
+         * @param attribute
+         *
+         * @return
+         * <tt>true</tt> if this element defines the given attribute; <tt>false<tt>,
+         * otherwise.
+         */
+        @Override
+        public boolean containsKey(String attribute) {
+            return attributes.containsKey(attribute);
+        }
+
+        /**
+         * Determines if this element defines any attributes.
+         *
+         * @return
+         * <tt>true</tt> if this element does not define any attributes;
+         * <tt>false</tt>, otherwise.
+         */
+        @Override
+        public boolean isEmpty() {
+            return attributes.isEmpty();
+        }
+
+        /**
+         * Returns an iterator over the element's attributes.
+         */
+        @Override
+        public Iterator<String> iterator() {
+            return new ImmutableIterator<String>(attributes.iterator());
+        }
     }
 
     private static class ElementListenerList extends ListenerList<ElementListener>
@@ -205,6 +310,8 @@
     private String defaultNamespaceURI = null;
 
     private HashMap<String, String> attributes = new HashMap<String, String>();
+    private AttributeDictionary attributeDictionary = new AttributeDictionary();
+
     private ArrayList<Node> nodes = new ArrayList<Node>();
 
     private ListListenerList<Node> listListeners = new ListListenerList<Node>();
@@ -401,91 +508,10 @@
     }
 
     /**
-     * Returns an attribute value.
-     */
-    @Override
-    public String get(String attribute) {
-        return attributes.get(attribute);
-    }
-
-    /**
-     * Sets an attribute value.
-     *
-     * @param attribute
-     * @param value
-     *
-     * @return
-     * The value previously associated with the given attribute.
-     */
-    @Override
-    public String put(String attribute, String value) {
-        if (value == null) {
-            throw new IllegalArgumentException("value is null.");
-        }
-
-        boolean update = containsKey(attribute);
-        String previousValue = attributes.put(attribute, value);
-
-        if (update) {
-            elementListeners.attributeUpdated(Element.this, attribute, previousValue);
-        } else {
-            elementListeners.attributeAdded(Element.this, attribute);
-        }
-
-        return previousValue;
-    }
-
-    /**
-     * Removes an attribute value.
-     *
-     * @param attribute
-     *
-     * @return
-     * The value previously associated with the given attribute.
-     */
-    @Override
-    public String remove(String attribute) {
-        String value = null;
-
-        if (containsKey(attribute)) {
-            value = attributes.remove(attribute);
-            elementListeners.namespaceRemoved(Element.this, attribute, value);
-        }
-
-        return value;
-    }
-
-    /**
-     * Tests for the existence of an attribute.
-     *
-     * @param attribute
-     *
-     * @return
-     * <tt>true</tt> if this element defines the given attribute; <tt>false<tt>,
-     * otherwise.
-     */
-    @Override
-    public boolean containsKey(String attribute) {
-        return attributes.containsKey(attribute);
-    }
-
-    /**
-     * Determines if this element defines any attributes.
-     *
-     * @return
-     * <tt>true</tt> if this element does not define any attributes;
-     * <tt>false</tt>, otherwise.
-     */
-    @Override
-    public boolean isEmpty() {
-        return attributes.isEmpty();
-    }
-
-    /**
-     * Returns an iterator over this element's attributes.
+     * Returns the element's attribute dictionary.
      */
-    public Iterator<String> getAttributes() {
-        return new ImmutableIterator<String>(attributes.iterator());
+    public AttributeDictionary getAttributes() {
+        return attributeDictionary;
     }
 
     /**

Modified: incubator/pivot/trunk/core/src/org/apache/pivot/xml/XMLSerializer.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/xml/XMLSerializer.java?rev=835068&r1=835067&r2=835068&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/xml/XMLSerializer.java (original)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/xml/XMLSerializer.java Wed Nov 11 21:05:36 2009
@@ -66,7 +66,7 @@
         this.charset = charset;
 
         xmlInputFactory = XMLInputFactory.newInstance();
-        xmlInputFactory.setProperty("javax.xml.stream.isCoalescing", true);
+        xmlInputFactory.setProperty("javax.xml.stream.isCoalescing", false);
     }
 
     public Charset getCharset() {
@@ -100,17 +100,9 @@
                 int event = xmlStreamReader.next();
 
                 switch (event) {
-                    case XMLStreamConstants.NAMESPACE: {
-                        // TODO
-                        break;
-                    }
-
                     case XMLStreamConstants.CHARACTERS: {
                         String text = xmlStreamReader.getText();
-                        text = text.trim();
-                        if (text.length() > 0) {
-                            element.add(new TextNode(text));
-                        }
+                        element.add(new TextNode(text));
 
                         break;
                     }
@@ -126,6 +118,19 @@
 
                         Element element = new Element(prefix, localName);
 
+                        // Get the element's namespaces
+                        for (int i = 0, n = xmlStreamReader.getNamespaceCount(); i < n; i++) {
+                            String namespacePrefix = xmlStreamReader.getNamespacePrefix(i);
+                            String namespaceURI = xmlStreamReader.getNamespaceURI(i);
+
+                            if (namespacePrefix == null) {
+                                element.setDefaultNamespaceURI(namespaceURI);
+                            } else {
+                                element.getNamespaces().put(namespacePrefix, namespaceURI);
+                            }
+                        }
+
+                        // Get the element's attributes
                         for (int i = 0, n = xmlStreamReader.getAttributeCount(); i < n; i++) {
                             String attributePrefix = xmlStreamReader.getAttributePrefix(i);
                             String attributeLocalName = xmlStreamReader.getAttributeLocalName(i);
@@ -138,7 +143,7 @@
                                 attribute = attributePrefix + ":" + attributeLocalName;
                             }
 
-                            element.put(attribute, attributeValue);
+                            element.getAttributes().put(attribute, attributeValue);
                         }
 
                         if (this.element != null) {

Modified: incubator/pivot/trunk/tools/src/org/apache/pivot/tools/xml/XMLViewer.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tools/src/org/apache/pivot/tools/xml/XMLViewer.java?rev=835068&r1=835067&r2=835068&view=diff
==============================================================================
--- incubator/pivot/trunk/tools/src/org/apache/pivot/tools/xml/XMLViewer.java (original)
+++ incubator/pivot/trunk/tools/src/org/apache/pivot/tools/xml/XMLViewer.java Wed Nov 11 21:05:36 2009
@@ -20,7 +20,6 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.StringReader;
-import java.util.Iterator;
 
 import org.apache.pivot.collections.ArrayList;
 import org.apache.pivot.collections.HashMap;
@@ -55,6 +54,7 @@
 
     @WTKX private TreeView treeView = null;
     @WTKX private CardPane propertiesCardPane = null;
+    @WTKX private TableView namespacesTableView = null;
     @WTKX private TableView attributesTableView = null;
     @WTKX private TextArea textArea = null;
 
@@ -164,15 +164,29 @@
         } else if (node instanceof Element) {
             Element element = (Element)node;
 
+            // Populate the namespaces table
+            ArrayList<HashMap<String, String>> namespacesTableData =
+                new ArrayList<HashMap<String, String>>();
+
+            Element.NamespaceDictionary namespaceDictionary = element.getNamespaces();
+            for (String prefix : namespaceDictionary) {
+                HashMap<String, String> row = new HashMap<String, String>();
+                row.put("prefix", prefix);
+                row.put("uri", namespaceDictionary.get(prefix));
+                namespacesTableData.add(row);
+            }
+
+            namespacesTableView.setTableData(namespacesTableData);
+
+            // Populate the attributes table
             ArrayList<HashMap<String, String>> attributesTableData =
                 new ArrayList<HashMap<String, String>>();
-            Iterator<String> attributes = element.getAttributes();
 
-            while (attributes.hasNext()) {
-                String attribute = attributes.next();
+            Element.AttributeDictionary attributeDictionary = element.getAttributes();
+            for (String attribute : attributeDictionary) {
                 HashMap<String, String> row = new HashMap<String, String>();
                 row.put("attribute", attribute);
-                row.put("value", element.get(attribute));
+                row.put("value", attributeDictionary.get(attribute));
                 attributesTableData.add(row);
             }
 
@@ -180,7 +194,7 @@
 
             propertiesCardPane.setSelectedIndex(0);
         } else {
-            propertiesCardPane.setSelectedIndex(-1);
+            throw new IllegalStateException();
         }
     }
 

Modified: incubator/pivot/trunk/tools/src/org/apache/pivot/tools/xml/xml_viewer.wtkx
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tools/src/org/apache/pivot/tools/xml/xml_viewer.wtkx?rev=835068&r1=835067&r2=835068&view=diff
==============================================================================
--- incubator/pivot/trunk/tools/src/org/apache/pivot/tools/xml/xml_viewer.wtkx (original)
+++ incubator/pivot/trunk/tools/src/org/apache/pivot/tools/xml/xml_viewer.wtkx Wed Nov 11 21:05:36 2009
@@ -64,34 +64,63 @@
                         </Border>
                     </left>
                     <right>
-                        <Border styles="{color:10}">
-                            <content>
-                                <CardPane wtkx:id="propertiesCardPane">
-                                    <ScrollPane horizontalScrollBarPolicy="fill_to_capacity"
-                                        verticalScrollBarPolicy="fill_to_capacity">
-                                        <view>
-                                            <TableView wtkx:id="attributesTableView" selectMode="none">
-                                                <columns>
-                                                    <TableView.Column name="attribute" width="1*" headerData="Attribute"/>
-                                                    <TableView.Column name="value" width="1*" headerData="Value"/>
-                                                </columns>
-                                            </TableView>
-                                        </view>
-                                        <columnHeader>
-                                            <TableViewHeader tableView="$attributesTableView"/>
-                                        </columnHeader>
-                                    </ScrollPane>
+                        <CardPane wtkx:id="propertiesCardPane">
+                            <SplitPane orientation="vertical" splitRatio="0.2">
+                                <top>
+                                    <Border styles="{color:10}">
+                                        <content>
+                                            <ScrollPane horizontalScrollBarPolicy="fill"
+                                                verticalScrollBarPolicy="fill_to_capacity">
+                                                <view>
+                                                    <TableView wtkx:id="namespacesTableView" selectMode="none">
+                                                        <columns>
+                                                            <TableView.Column name="prefix" width="1*" headerData="Prefix"/>
+                                                            <TableView.Column name="uri" width="2*" headerData="URI"/>
+                                                        </columns>
+                                                    </TableView>
+                                                </view>
+                                                <columnHeader>
+                                                    <TableViewHeader tableView="$namespacesTableView"
+                                                        styles="{headersPressable:false}"/>
+                                                </columnHeader>
+                                            </ScrollPane>
+                                        </content>
+                                    </Border>
+                                </top>
+                                <bottom>
+                                    <Border styles="{color:10}">
+                                        <content>
+                                            <ScrollPane horizontalScrollBarPolicy="fill"
+                                                verticalScrollBarPolicy="fill_to_capacity">
+                                                <view>
+                                                    <TableView wtkx:id="attributesTableView" selectMode="none">
+                                                        <columns>
+                                                            <TableView.Column name="attribute" width="1*" headerData="Attribute"/>
+                                                            <TableView.Column name="value" width="1*" headerData="Value"/>
+                                                        </columns>
+                                                    </TableView>
+                                                </view>
+                                                <columnHeader>
+                                                    <TableViewHeader tableView="$attributesTableView"
+                                                        styles="{headersPressable:false}"/>
+                                                </columnHeader>
+                                            </ScrollPane>
+                                        </content>
+                                    </Border>
+                                </bottom>
+                            </SplitPane>
 
+                            <Border styles="{color:10}">
+                                <content>
                                     <ScrollPane horizontalScrollBarPolicy="fill"
                                         verticalScrollBarPolicy="fill_to_capacity">
                                         <view>
-                                            <TextArea wtkx:id="textArea"/>
+                                            <TextArea wtkx:id="textArea" editable="false"/>
                                         </view>
                                     </ScrollPane>
-                                </CardPane>
-
-                            </content>
-                        </Border>
+                                </content>
+                            </Border>
+                        </CardPane>
                     </right>
                 </SplitPane>
             </content>